💥Build "Tic Tac Toe" Game using Python
Introduction of this game :-
- Tic Tac Toe game it is paper and pencil game for two players , 'X' and 'O' who take turns marking the spaces in 3x3 grid.
- A game in which two players alternately put crosses and circles in one of the compartments of a square grid of nine spaces.
- The player who succeeds in placing three of their same marks in a horizontal , vertical or diagonal row is the winner.
🤔Consider there are two players - User and Computer , 'X' is assigned to user and 'O' is assigned to computer . If they play as shown below , the winner will be user .
💻Let's Code :-
- In this code we need to use many functions, because this code may have many conditions, so by using functions we can easily understand the code.
- In this tutorial we will learn the new concept that is "Python Function".
⍟Python Function:-
- A function is a block of code, in function we can pass the values also known as parameters.
- You can add as many arguments as you want,just separate them with a comma.
- In Python, function can defined using the def keyword.
- We can used the return keyword , to exit a function and return a value.
- In function , the most important part is indentation(Space),python follows a particular style of indentation to define the code.
- Python functions don't have any curly braces to indicate the start and stop for the function, they have to rely on this indentation.
- Here we take a simple example with "print" command,when we write print function right below the def my_function(): it will show an "Syntax error: expected an indented block".
Creating a function:-
Calling a function:-
Here we will implement the game through this code,
Step 1:-
- This insertletter() used to pass the letter and position of this letter.
- This spaceIsFree() used to check the space is available or not.
- This printBoard() shows the how board is printed on the screen this is 3x3 grid.
- This isBoardFull() used to check the board is full or not.
- This IsWinner() represents the possibilty to win .
Step 3:-
- This playerMove() explain the playerMove side part,first take a input,this while loop is used to take a input again and again till the condition gets fails.
- Then run = true,it represent now user's turn.
- In try block,first convert the string into int, move represents the position of this letter.
- When user take a input between 0 and 10 then check a condition,if condition is true then only it comes in a if block,and go further to check a spaceIsfree or not, if spceIsfree then only user can insert that letter on particular position,now the computer turns after this which is represented through the run = False.
- In this computerMove() the computer works like what we have provide the instruction,so in this function computer can have list of possibleMove which can consist of spaces, all the numerical value from that they can take move.Then move=0, represents the initial value of move is zero.
- If we want to computer wins every time for that there are three posiblities like corners,middle and edges position,the position will explain in this function,acoording to computer can work,but when we provide the possiblities from that computer can select the value randomly by using random function.
- This selectRandom function used in computerMove function to select a value randomly.
Output 1.2 |
Output 1.3 |
Output 1.4 |
Yes , we successfully completed another project in this tutorial !! 👍🙌👍
Hopes you understand this project and enjoyed it !!😇😇
Tags:
Python