Pythontictactoe
Pythontictactoe
TIC-TAC-TOE GAME
ALGORITHM:
1. Initialization:
o Import the random and os modules.
o Define helper functions:
choose_first(): Randomly selects which
player will go first.
cls(): Clears the console screen.
display_board(board): Displays the current
state of the game board.
space_check(board, position): Checks if a
position on the board is empty.
place_marker(board, marker, position):
Places the player's marker (X or O) on the
board if the position is empty.
full_board_check(board): Checks if the
board is completely filled.
win_check(board, mark): Checks if a
player has won by forming a line of their
markers.
replay(): Asks if the players want to replay
the game.
FirstDisplay(): Displays the Tic-Tac-Toe
board layout based on numeric keypad
numbering.
2. Game Loop:
o Initialize the find variable as False to control
the overall game loop.
o While find is False:
Reset the game board (the_board) to a list
of empty spaces.
Prompt both players to enter their names
(player_1 and player_2).
Perform a toss using choose_first() to
decide which player goes first:
Assign player 1 to "X" and player 2 to
"O".
Print the toss results and assign
markers (m1 and m2).
Display the default board layout using
FirstDisplay().
3. Turn-Based Gameplay:
o Enter an infinite loop for the game:
Player 1's Turn:
Prompt player 1 to enter a position
for their marker.
Validate the position using
place_marker():
If valid, place the marker and
display the updated board.
Check for a win condition using
win_check():
If player 1 wins, print the
result and break out of the
loop.
Check for a draw using
full_board_check():
If the board is full, declare
the match as a draw and
break out of the loop.
Player 2's Turn:
Prompt player 2 to enter a position
for their marker.
Validate the position using
place_marker():
If valid, place the marker and
display the updated board.
Check for a win condition using
win_check():
If player 2 wins, print the
result and break out of the
loop.
Check for a draw using
full_board_check():
If the board is full, declare
the match as a draw and
break out of the loop.
4. Replay Option:
o After a match ends, call the replay() function:
If the players want to play again, reset the
game.
Otherwise, set find to True to exit the
outer game loop and end the program.
SOURCE CODE:
import random
import os
def choose_first():
return (random.randint(0,1))
def cls():
os.system('cls')
def display_board(board):
print (' | | ')
print (' '+board[7]+' | '+board[8]+' | '+board[9])
print (' | | ')
print ('-----------')
print (' | | ')
print (' '+board[4]+' | '+board[5]+' | '+board[6])
print (' | | ')
print ('-----------')
print (' | | ')
print (' '+board[1]+' | '+board[2]+' | '+board[3])
print (' | | ')
def win_check(board,mark):
return (board[7]==board[8]==board[9]==mark or \
board[4]==board[5]==board[6]==mark or \
board[1]==board[2]==board[3]==mark or \
board[7]==board[4]==board[1]==mark or \
board[8]==board[5]==board[2]==mark or \
board[9]==board[6]==board[3]==mark or \
board[7]==board[5]==board[3]==mark or \
board[9]==board[5]==board[1]==mark)
def replay():
yn =input('\nDo you want to play again (yes/no)')
if (yn.lower()=='yes'):
return True
else:
print ('----- welcome to tic tac toe -----')
return False
def FirstDisplay():
print ('\nTic-Tac-Toe Board Numbering')
print ('as per your keyboard numeric pad')
print (' 7 | 8 | 9')
print ('-----------')
print (' 4 | 5 | 6')
print ('-----------')
print (' 1 | 2 | 3')
find=False
while not(find):
inner_loop=''
the_board =[' ']*10
the_board[0]='$'
player_1=input('\nEnter First player name ')
player_2=input('Enter Second player name ')
if (choose_first()):
p1=player_1
m1='X'
p2=player_2
m2='O'
else:
p1=player_2
m1='X'
p2=player_1
m2='O'
print ('\nAfter Toss {} won the toss and come first
with X '.format(p1))
print ('other one {} come seccond with
O'.format(p2))
FirstDisplay()
while(True):
ok=False
while not(ok):
print ('Enter player {} position'.format(p1))
pos=int(input('number : '))
cls()
if (place_marker(the_board, m1, pos)):
display_board(the_board)
ok=True
if (win_check(the_board,m1)):
print (p1+' won the match')
inner_loop='break'
break
elif not(full_board_check(the_board)):
print ('Match is Draw')
inner_loop='break'
break
if (inner_loop=='break'):
break
ok=False
while not(ok):
print ('Enter player {} position'.format(p2))
pos=int(input('number : '))
cls()
if (place_marker(the_board, m2, pos)):
display_board(the_board)
ok=True
if (win_check(the_board,m2)):
print (p2+' won the match')
inner_loop='break'
break
elif not (full_board_check(the_board)):
print ('Match is Draw')
inner_loop='break'
break
if (inner_loop=='break'):
break
if not(replay()):
find=True
OUTPUT:
Enter First player name RONALDO
Enter Second player name MESSI
After Toss RONALDO won the toss and come first with X
other one MESSI come second with O
Tic-Tac-Toe Board | |
Numbering |X|
as per your keyboard | |
numeric pad
-----------
7|8|9
| |
-----------
| |
4|5|6
| |
-----------
Enter player MESSI
1|2|3 position
Enter player RONALDO number : 1
position
| |
number : 5
| |
| |
| |
| |
-----------
| |
| |
-----------
|X| | |
| | -----------
----------- | |
| | |X|
O| | | |
| | -----------
Enter player RONALDO | |
position O| |O
number : 9 | |
| | Enter player RONALDO
| |X position
| | number : 7
----------- | |
| | X| |X
|X| | |
| | -----------
----------- | |
| | |X|
O| | | |
| | -----------
Enter player MESSI | |
position O| |O
number : 3 | |
| | Enter player MESSI
| |X position
number : 4 | |
| | X|X|X
X| |X | |
| | -----------
----------- | |
| | O|X|
O|X| | |
| | -----------
----------- | |
| | O| |O
O| |O | |
| | RONALDO won the match
Enter player RONALDO
position Do you want to play
number : 8 again (yes/no)N