How to make tic tac toe game using python || Tic tac toe game using python

Tic Tac Toe Game Using Python:

Description:

1. Board Representation:The game board is represented using a list called board, with each element corresponding to a position on the board.

2. Players and Marks: Players are identified as Player 1 (X) and Player 2 (O). The variable Mark keeps track of the current player's symbol.

3. Win/Drawing Conditions: The game checks for win/draw conditions after each move. The CheckWin() function determines if a player has won or if the game is a draw.

4. Displaying the Board: The DrawBoard() function displays the current state of the board.

5. Game Loop: The main game loop continues until there's a winner or a draw. Players take turns entering their moves, and the board is updated accordingly.

6. *Clearing the Console:* The os.system('cls') command is used to clear the console, providing a cleaner display during the game.

7. User Input: Players input their moves by entering a position number between 1 and 9.

8. Player Names: The players' names are taken as input before the game starts.

9. Sleep Function: There are time delays using time.sleep() for a more interactive experience.

10. Output Messages: The game outputs messages indicating which player's turn it is, the result of the game (draw or win), and congratulations to the winning player.


Code:

import os
import time


board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
player = 1

# Win Flags
Win = 1
Draw = -1
Running = 0
Stop = 1

Game = Running
Mark = 'X'


def DrawBoard():
print(" %c | %c | %c " % (board[1], board[2], board[3]))
print("___|___|___")
print(" %c | %c | %c " % (board[4], board[5], board[6]))
print("___|___|___")
print(" %c | %c | %c " % (board[7], board[8], board[9]))
print(" | | ")


def CheckPosition(x):
if board[x] == ' ':
return True
else:
return False


def CheckWin():
global Game

if board[1] == board[2] and board[2] == board[3] and board[1] != ' ':
Game = Win
elif board[4] == board[5] and board[5] == board[6] and board[4] != ' ':
Game = Win
elif board[7] == board[8] and board[8] == board[9] and board[7] != ' ':
Game = Win
elif board[1] == board[4] and board[4] == board[7] and board[1] != ' ':
Game = Win
elif board[2] == board[5] and board[5] == board[8] and board[2] != ' ':
Game = Win
elif board[3] == board[6] and board[6] == board[9] and board[3] != ' ':
Game = Win
elif board[1] == board[5] and board[5] == board[9] and board[5] != ' ':
Game = Win
elif board[3] == board[5] and board[5] == board[7] and board[5] != ' ':
Game = Win
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] != ' ':
Game = Draw
else:
Game = Running

print("Tic-Tac-Toe Game Designed By Nikesh patel")
print("Player 1 [X] --- Player 2 [O]\n")
print()
print()
print("Please Wait for starting the game...")
time.sleep(3)

print()

player1=str(input("Enter the name of player 1: "))
player2=str(input("Enter the name of player 2: "))
time.sleep(2)

while Game == Running:
os.system('cls')
DrawBoard()
if player % 2 != 0:
print(player1,"chance")
Mark = 'X'
else:
print(player2,"chance")
Mark = 'O'

choice = int(input("Enter the position between [1-9] where you want to mark: "))
if CheckPosition(choice):
board[choice] = Mark
player += 1
CheckWin()

os.system('cls')
DrawBoard()

if Game == Draw:
print("Game Draw")
elif Game == Win:
player -= 1
if player % 2 != 0:
print(player1,"congratulations! you are Won the match")
else:
print(player2,"congratulations! you are Won the match")

Output:









Comments

Popular posts from this blog

How to make personal ai using python || Jarvish ai using python

How to make travel website using html and css? ||How to make Tourism website using html and css? || How to make travel website ||How to make tourism website

How to make car racing game using python languge