Project 2: Tic-Tac-Toe Game

Introduction

 
In this chapter, we will learn to implement a Tic-Tac-Toe game in Python.
 

Tic-Tac-Toe 

 
Tic-Tac-Toe is a very simple two-player game. So only two players can play at a time. This game is also known as Noughts and Crosses or Xs and Os game. One player plays with X and the other player plays with O. In this game we have a board consisting of a 3X3 grid. The number of grids may be increased.
 
The Tic-Tac-Toe board looks like the following:
 
 

Game Rules 

  1. Traditionally the first player plays with "X". So you can decide who wants to go with "X" and who wants to go with "O".
  2. Only one player can play at a time.
  3. If any of the players have filled a square then the other player and the same player cannot override that square.
  4. There are only two conditions that may match will be a draw or may win.
  5. The player that succeeds in placing three respective marks (X or O) in a horizontal, vertical, or diagonal row wins the game.

Winning condition

 
Whoever places three respective marks (X or O) horizontally, vertically, or diagonally will be the winner.
 
The code for the game is as follows,
  1. import os      
  2. import time      
  3.       
  4. board = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']      
  5. player = 1      
  6.      
  7. ########win Flags##########      
  8. Win = 1      
  9. Draw = -1      
  10. Running = 0      
  11. Stop = 1      
  12. ###########################      
  13. Game = Running      
  14. Mark = 'X'      
  15.      
  16. #This Function Draws Game Board      
  17. def DrawBoard():      
  18.     print(" %c | %c | %c " % (board[1],board[2],board[3]))      
  19.     print("___|___|___")      
  20.     print(" %c | %c | %c " % (board[4],board[5],board[6]))      
  21.     print("___|___|___")      
  22.     print(" %c | %c | %c " % (board[7],board[8],board[9]))      
  23.     print("   |   |   ")      
  24.      
  25. #This Function Checks position is empty or not      
  26. def CheckPosition(x):      
  27.     if(board[x] == ' '):      
  28.         return True      
  29.     else:      
  30.         return False      
  31.      
  32. #This Function Checks player has won or not      
  33. def CheckWin():      
  34.     global Game      
  35.     #Horizontal winning condition      
  36.     if(board[1] == board[2and board[2] == board[3and board[1] != ' '):      
  37.         Game = Win      
  38.     elif(board[4] == board[5and board[5] == board[6and board[4] != ' '):      
  39.         Game = Win      
  40.     elif(board[7] == board[8and board[8] == board[9and board[7] != ' '):      
  41.         Game = Win      
  42.     #Vertical Winning Condition      
  43.     elif(board[1] == board[4and board[4] == board[7and board[1] != ' '):      
  44.         Game = Win      
  45.     elif(board[2] == board[5and board[5] == board[8and board[2] != ' '):      
  46.         Game = Win      
  47.     elif(board[3] == board[6and board[6] == board[9and board[3] != ' '):      
  48.         Game=Win      
  49.     #Diagonal Winning Condition      
  50.     elif(board[1] == board[5and board[5] == board[9and board[5] != ' '):      
  51.         Game = Win      
  52.     elif(board[3] == board[5and board[5] == board[7and board[5] != ' '):      
  53.         Game=Win      
  54.     #Match Tie or Draw Condition      
  55.     elif(board[1]!=' ' and board[2]!=' ' and board[3]!=' ' and board[4]!=' ' and board[5]!=' ' and board[6]!=' ' and board[7]!=' ' and board[8]!=' ' and board[9]!=' '):      
  56.         Game=Draw      
  57.     else:              
  58.         Game=Running      
  59.       
  60. print("Tic-Tac-Toe Game Designed By Sourabh Somani")      
  61. print("Player 1 [X] --- Player 2 [O]\n")      
  62. print()      
  63. print()      
  64. print("Please Wait...")      
  65. time.sleep(3)      
  66. while(Game == Running):      
  67.     os.system('cls')      
  68.     DrawBoard()      
  69.     if(player % 2 != 0):      
  70.         print("Player 1's chance")      
  71.         Mark = 'X'      
  72.     else:      
  73.         print("Player 2's chance")      
  74.         Mark = 'O'      
  75.     choice = int(input("Enter the position between [1-9] where you want to mark : "))      
  76.     if(CheckPosition(choice)):      
  77.         board[choice] = Mark      
  78.         player+=1      
  79.         CheckWin()      
  80.       
  81. os.system('cls')      
  82. DrawBoard()      
  83. if(Game==Draw):      
  84.     print("Game Draw")      
  85. elif(Game==Win):      
  86.     player-=1      
  87.     if(player%2!=0):      
  88.         print("Player 1 Won")      
  89.     else:      
  90.         print("Player 2 Won")       

Output

 
 
 

Conclusion 

 
In the next chapter, we will learn how to make a music instrument using Python.
Author
Sourabh Somani
Tech Writter 47.6k 10.8m
Next » Project 3: Playing Sargam In Python