0% found this document useful (0 votes)
46 views

Ai ML Lab - 3: Write A Python Program For Tic Tac Toe

The document contains the code for a tic tac toe game written in Python. It defines functions for determining if a player has won, getting player input, checking for a win, displaying the board, and running the main game loop. The main_game function handles the core logic, alternating turns between a human player and CPU player and checking for a win or draw after each turn before continuing the game.

Uploaded by

test
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Ai ML Lab - 3: Write A Python Program For Tic Tac Toe

The document contains the code for a tic tac toe game written in Python. It defines functions for determining if a player has won, getting player input, checking for a win, displaying the board, and running the main game loop. The main_game function handles the core logic, alternating turns between a human player and CPU player and checking for a win or draw after each turn before continuing the game.

Uploaded by

test
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

AI ML Lab - 3

report
Shreyash hire
20190802114

Aim: Write a python program for tic tac toe

Code:
def win(i, board, mark):
temp_board = list(board)
temp_board[i] = mark
if winning(mark, temp_board):
return True
else:
return False

def playerinput(mark):
while True:
inp = input(f" '{mark}' Enter your choice:")
if inp.isdigit() and int(inp) < 10 and int(inp) > 0:
inp = int(inp)
if board[inp] == " ":
return inp
else:
print(f" '{mark}' place taken.")
else:
print(f" '{mark}' Enter valid option (1 - 9).")

def winning(mark, board):


winning_place = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9],
[1, 5, 9], [3, 5, 7]]
for win_place in winning_place:
if board[win_place[0]] == board[win_place[1]] == board[win_place[2]] == mark:
return True

def cpu_input(cpu, human, board):


for i in range(1, 10):
if board[i] == ' ' and win(i, board, cpu):
return i
for i in range(1, 10):
if board[i] == ' ' and win(i, board, human):
return i
for i in [5, 1, 7, 3, 2, 9, 8, 6, 4]:
if board[i] == ' ':
return i
def new_game():
while True:
nxt = input(' Do you want to play again?(y/n):')
if nxt in ['y', 'Y']:
again = True
break
elif nxt in ['n', 'N']:
print('Have a great day')
again = False
break
else:
print('Enter correct input')
if again:
print('__________NEW GAME__________')
main_game()
else:
return False

def win_check(human, cpu):


winning_place = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9],
[1, 5, 9], [3, 5, 7]]
for win_place in winning_place:
if board[win_place[0]] == board[win_place[1]] == board[win_place[2]] == human:
print(' wins the match!')
if not new_game():
return False
elif board[win_place[0]] == board[win_place[1]] == board[win_place[2]] == cpu:
print('Computer wins the match!')
if not new_game():
return False
if ' ' not in board:
print('MATCH DRAW')
if not new_game():
return False
return True

def display_board():
print()
print(' reference:')
print(' | | ', 10 * ' ', ' | | ', )
print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3] + ' ', 10 * ' ', '
1 | 2 | 3 ')
print('-----+----+-----', 10 * ' ', "-----+----+-----")
print(' | | ', 10 * ' ', " | | ")
print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6] + ' ', 10 * ' ', "
4 | 5 | 6 ")
print('-----+----+-----', 10 * ' ', "-----+----+-----")
print(' | | ', 10 * ' ', " | | ")
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9] + ' ', 10 * ' ', "
7 | 8 | 9 \n\n")
def main_game():
global board
play = True
board = ['', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
human, cpu = 'x','o'
display_board()
while play:
if human == 'x':
x = playerinput(human)
board[x] = human
display_board()
play = win_check(human, cpu)
if play:
o = cpu_input(cpu, human, board)
print(f'Computer Entered:{o}')
board[o] = cpu
display_board()
play = win_check(human, cpu)
else:
x = cpu_input(cpu, human, board)
print(f'Computer Entered:{x}')
board[x] = cpu
display_board()
play = win_check(human, cpu)
if play:
o = playerinput(human)
board[o] = human
display_board()
play = win_check(human, cpu)

if __name__ == '__main__':
main_game()
Output:

Conclusion: Thus, using Python, we were able to create the code for tic tac toe game.

You might also like