Skip to content

Commit 2d6e0a3

Browse files
author
Ayush Varshney
committed
AI_Tic_Tac_toe_Game
1 parent f05226e commit 2d6e0a3

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

AI TicTacToe/src/TicTacToe.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import random
2+
3+
def display_board(board):
4+
print('-------------')
5+
print('| ' + board[7] + ' | ' + board[8] + ' | ' + board[9] + ' |')
6+
print('-------------')
7+
print('| ' + board[4] + ' | ' + board[5] + ' | ' + board[6] + ' |')
8+
print('-------------')
9+
print('| ' + board[1] + ' | ' + board[2] + ' | ' + board[3] + ' |')
10+
print('-------------')
11+
12+
13+
def player_input():
14+
marker = ''
15+
while marker != 'X' and marker != 'O':
16+
marker = input('Player 1, choose X or O: ').upper()
17+
player1 = marker
18+
player2 = 'O' if player1 == 'X' else 'X'
19+
return player1, player2
20+
21+
22+
def place_marker(board, marker, position):
23+
board[position] = marker
24+
25+
26+
def win_check(board, mark):
27+
winning_combinations = [
28+
[1, 2, 3], [4, 5, 6], [7, 8, 9], # rows
29+
[1, 4, 7], [2, 5, 8], [3, 6, 9], # columns
30+
[1, 5, 9], [3, 5, 7] # diagonals
31+
]
32+
return any(all(board[i] == mark for i in combination) for combination in winning_combinations)
33+
34+
35+
def choose_first():
36+
return random.choice(['Player 1', 'Player 2'])
37+
38+
39+
def space_check(board, position):
40+
return board[position] == ' '
41+
42+
43+
def full_board_check(board):
44+
return all(board[i] != ' ' for i in range(1, 10))
45+
46+
47+
def player_choice(board):
48+
position = 0
49+
while position not in range(1, 10) or not space_check(board, position):
50+
position = int(input('Choose a position (1-9): '))
51+
return position
52+
53+
54+
def replay():
55+
choice = input('Do you want to play again? Enter Yes or No: ')
56+
return choice.lower() == 'yes'
57+
58+
59+
def play_tic_tac_toe():
60+
print('Welcome to Tic Tac Toe!')
61+
while True:
62+
the_board = [' '] * 10
63+
player1_marker, player2_marker = player_input()
64+
turn = choose_first()
65+
print(turn + ' will go first.')
66+
play_game = input('Are you ready to play? Enter y or n: ')
67+
if play_game.lower() == 'y':
68+
game_on = True
69+
else:
70+
game_on = False
71+
72+
while game_on:
73+
if turn == 'Player 1':
74+
display_board(the_board)
75+
position = player_choice(the_board)
76+
place_marker(the_board, player1_marker, position)
77+
78+
if win_check(the_board, player1_marker):
79+
display_board(the_board)
80+
print('Player 1 has won!')
81+
game_on = False
82+
else:
83+
if full_board_check(the_board):
84+
display_board(the_board)
85+
print('TIE GAME!')
86+
game_on = False
87+
else:
88+
turn = 'Player 2'
89+
else:
90+
display_board(the_board)
91+
position = player_choice(the_board)
92+
place_marker(the_board, player2_marker, position)
93+
94+
if win_check(the_board, player2_marker):
95+
display_board(the_board)
96+
print('Player 2 has won!')
97+
game_on = False
98+
else:
99+
if full_board_check(the_board):
100+
display_board(the_board)
101+
print('TIE GAME!')
102+
game_on = False
103+
else:
104+
turn = 'Player 1'
105+
106+
if not replay():
107+
if play_game.lower() == 'n':
108+
print('BYE! Have a good day.')
109+
else:
110+
print('Thank you for playing.')
111+
break
112+
113+
# Start the game
114+
play_tic_tac_toe()

0 commit comments

Comments
 (0)