Skip to main content

5 Unique and Useful Python modules.

Motive           There are different types of modules in python which not everyone knows so we thought of introducing them to you guys. Let's start! Note: This is not a tutorial of how to use these but is to show that these modules also exist and if you want to know more you can visit there own page and if you want us to explain leave a comment below. Links to there page are also given below. Hope you found something useful. 1.Wikipedia          Yes, there is a module called Wikipedia in python, you can easily install Wikipedia by pip install wikipedia . We cannot rely on this module for advanced use. It was designed for ease of use and simplicity.  Learn More Here .  Here is a simple example: import wikipedia wikiPython = wikipedia.summary("Python", sentences=2) print(wikiPython)           One big disadvantage is that it works only if the internet is connected to your device. You can even control the number of sentences that you

Never Lose a Game of Chess | Python Chess Hack

Motive 

         I lost every game that I played chess with my friends. So why not cheat, even though it was not the best option, I had to somehow prove that I was not dumb and can even win against them. Since they play online with me, I had a better chance to cheat so I thought of creating an ai to play against them but it was not that easy so I started surfing through the internet to find a better way. Then I found stockfish it was a chess engine that generates the best move for us. So I used it as my AI. Again, luck was with me I was able to find a module called chess in python that helped me a lot and now my work was really very easy. At last, I created the program and won the games against them but they had figured out that I was cheating and my friends started mocking at me. Then I understood that I was wrong and I shouldn't have cheated and started working hard for it and now I literally can win matches with them even though I lost many of them. This is just of educational purpose! I don't promote cheating. LOL. 

The Project

        If you are only interested in the code, you can scroll down to get the full code. So as I told above use stockfish as our engine and need to install it first before anything else so go to https://stockfishchess.org/ and install stockfish according to your system. Then extract the zip file and move it to a new folder and create a new python file in that same folder. Now let's install the libraries that we need.



        Chess is the only module that we need for this project so, open the terminal and type pip install python-chess and hit enter and wait for the installation to completeYou can learn more at https://pypi.org/project/python-chess/.  


        Now that you have installed chess we can move on with our project by importing chess and chess.engine for our project.


        Now create a variable called engine and the set the engine as per your system and create another variable called board and set it to chess.Board().



        Let's create a new function called newGame to ask the user for the side they want to play. So for that, we need to take the input from the user so let's create a variable called colour and then take the input like this Black(b)/While(w): then make the input into lower case and if the input is b, we should run the function called playAsBlack which we will make later and if the input is w, we should run the function called, you guessed it, playAsBlack so let's code it up.


        Now let's create another function called printBoard which basically prints the variable that we created at the beginning called board and some other information to make your move a little bit easier.


        Another thing that I noticed was if we accidentally made a wrong move and could even mess the whole game up. So, we need another function to undo the move, for that we need to call the function named board.pop() and then call the function playerMove().


        Let's create the playAsBlack function and the playAsWhite function now. For the playAsBlack function, we will first check if the game is over and if it's not we will let the function called ai_move which we will make later and then the playerMove we will also be made later. And for the playAsWhite function we will do the opposite i.e playerMove first and then ai_move.


        So here comes the main part of the code ai_move function for that we need to get the best move possible that's were our stockfish engine comes in use. Then we print the board and the move that our engine made. Then push the move that the engine makes.


        Now let's make the last and final function for our project, playerMove. First, we print the board the print and the list of the legal moves possible then takes an input from the user that says "Undo(u)/New Game(n)/Enter Your Move:" and some if-else statements for the undo and newGame functions to work.


Now just call the newGame Function. 

The Full Code       


        If you have any doubts or errors in the program feel free to comment down below and I will respond as early as possible. And if I have made any mistake please feel free to correct me in the comment box below. Don't forget to follow us for such python related content.

Comments

Post a Comment

Popular posts from this blog

Digital Clock | Python | Pygame

  Motive            Just to keep this the way I make other projects on my website. Just ignore this and jump right into the project. Oh and here's a preview of what we are going to make. The Project          We will be using pygame to create our Digital Clock, I believe all of you have installed pygame, if not just open the command prompt and type pip install pygame and hit enter. Learn more about pygame here  https://www.pygame.org/docs/ . Feel free to skip to the end to get the full code.           Okay back to the project, first, let's make a basic pygame window. So import pygame, initialize pygame, set an icon, make a screen like a rectangle then set its caption or whatever you call it, make a while loop to make it run till the exit button is pressed, and update the pygame display. Looks like a lot but these are necessary to even make a basic pygame project. Everyone who has at least worked with pyga

Chrome Dino Game Automation with Python.

Motive           I saw a video on YouTube that made a python program to automate the chrome dino game at the end he shows a clip of the program playing the game. But there was a problem with the code I do not work when it's dark so I thought of turning the dark mode in the laptop so that it stays dark all the time but that did not work after reaching a score of 700 it becomes day again so I have to think of an alternative see how I did it. The Project  First Import the necessary modules in your IDE import pyautogui from PIL import Image, ImageGrab import time PyAutoGUI   lets Python control the mouse and keyboard, and other GUI automation tasks.                    First, open your terminal and run pip                          learn more on   Pyautogui