Build Tic Tac Toe Game using Python

💥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 .             

          
Fig no 1. Rough Diagram

💻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:-
  1. A function is a block of code, in function we can pass the values also known as parameters.
  2. You can add as many arguments as you want,just separate them with a comma.
  3. In Python, function can defined using the def  keyword.
  4. We can used the return keyword , to exit a function and return a value.
  5. In function , the most important part is indentation(Space),python follows a particular style of indentation to define the code. 
  6. Python functions don't have any curly braces to indicate the start and stop for the function, they have to rely on this indentation.
  7. 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".
          
         Now, when you add the indent(space) in front of "print" function,it will give the expected output.

        Creating a function:-
        



         Calling a function:-





Output 

         Arguments or Parameters:-

Output

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.



Step 2:-
  • 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.              


Step 4:-
  • 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.
 




Step 5:-
  • This selectRandom function used in computerMove function to select a value randomly.
Step 6:- 

Step 7:-




Output:-


Output 1

Output 1.1

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 !!😇😇




Post a Comment

Comments :

Previous Post Next Post