-
Notifications
You must be signed in to change notification settings - Fork 3
/
tictactoe.py
178 lines (149 loc) · 5.74 KB
/
tictactoe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from pyfiglet import Figlet
from tabulate import tabulate
def main():
# MAIN FUNCTION
POINT = [0, 0]
figlet = Figlet()
fonts = figlet.getFonts()
figlet.setFont(font="slant")
print(figlet.renderText("Tic-Tac-Toe"))
try:
while True:
boardgame = [" "]*10
# ASK THE PLAYERS TO CHOOSE THEIR MARKER
player1,player2 = player_input()
# CHOOSE WHO GOES IN FIRST
turn = choose_first(player1)
print("\n"+turn+" goes first")
play_game = input('\nLets play the game? (Y or N): ')
game_on = play_game.upper() == 'Y'
while game_on:
# CHECK WHOSE TURN
table = [["Player", "Point"]]
if turn == "Player 1":
# MOVES FOR PLAYER ONE
display_board(boardgame)
# ASK THE PLAYER TO PLACE THEIR MARKER,
position = player_choice(boardgame)
place_marker(boardgame,player1,position)
# CHECK FOR WIN OR TIE
if win_check(boardgame, player1):
display_board(boardgame)
POINT[0] += 1
print('Player 1 has WON the game!\n')
game_on = False
else:
if full_board_check(boardgame):
display_board(boardgame)
print('Well played, ended up as a tie!\n')
game_on = False
else:
turn = 'Player 2'
else:
# MOVES FOR PLAYER TWO
display_board(boardgame)
# ASK THE PLAYER TO PLACE THEIR MARKER,
position = player_choice(boardgame)
place_marker(boardgame,player2,position)
# CHECK FOR WIN OR TIE
if win_check(boardgame, player2):
display_board(boardgame)
POINT[1] += 1
print('Player 2 has WON the game!\n')
game_on = False
else:
if full_board_check(boardgame):
display_board(boardgame)
print('The game has been TIED!\n')
game_on = False
else:
turn = 'Player 1'
if not game_on:
table.append(["P1", POINT[0]])
table.append(["P2", POINT[1]])
print(tabulate(table, headers="firstrow",
tablefmt="grid"))
print()
# Check whether the player wants to play again
if not replay():
break
except (EOFError, KeyboardInterrupt):
print("\n\nAww! We are sad you left early🥲.")
else:
print("\n", tabulate([["Thank you for playing Tic-Tac-Toe!"]],
tablefmt="grid"), "\n")
def display_board(board):
# display the game as a board
print('\n'*100)
table = [[board[1], board[2], board[3]], [board[4], board[5],
board[6]], [board[7], board[8], board[9]]]
print(tabulate(table, tablefmt="grid"))
print("\n\n")
def player_input():
# take users input on which marker to choose
pmark = input('Choose your marker (X or O): ').upper()
table = [["Player", "Marker"]]
while pmark.upper() not in ('X','O'):
print('Sorry! Invalid marker chosen.')
pmark = input('Choose your marker (X or O): ')
table.append(["P1", pmark])
if pmark.upper() == 'X':
table.append(["P2", "O"])
player1 = 'X'
player2 = 'O'
else:
table.append(["P2", "X"])
player1 = 'O'
player2 = 'X'
print(tabulate(table, headers="firstrow", tablefmt="grid"))
return player1, player2
def place_marker(board, marker, position):
# place a marker at a position by the user
board[position] = marker
def win_check(board, marker):
# win_check() checks if any user wins during the game
return any(
board[1] == board[2] == board[3] == marker,
board[4] == board[5] == board[6] == marker,
board[7] == board[4] == board[1] == marker,
board[8] == board[5] == board[2] == marker,
board[9] == board[6] == board[3] == marker,
board[1] == board[5] == board[9] == marker,
board[7] == board[8] == board[9] == marker,
board[7] == board[5] == board[3] == marker
)
def choose_first(player1):
# selects the player that chooses X as the first player
return f"Player {1 if player1 == 'X' else 2}"
def space_check(board, position):
# check if there is a space in the given position
return board[position] == " "
def full_board_check(board):
# checks if the board is full to find if its a draw
countn=0
for i in range(1,10):
if board[i] == " ":
return False
countn += 1
if countn == 9:
return True
def marker_position(a):
return a in range(1, 10)
def player_choice(board):
# enter the next user choice
a = int(input('Enter your next position: '))
while not marker_position(a):
print("Invalid position entered\n")
a = int(input('Enter your next position: '))
while a in range(1, 10):
if space_check(board,a):
return a
else:
print('Please change the position since it is full')
a = int(input('Enter your next position: '))
def replay():
# ask user if they want to play again
replay=input('Would you like to play again by changing the markers? (Y or N): ')
return replay.upper() == 'Y'
if __name__ == "__main__":
main()