-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.py
136 lines (112 loc) · 5.1 KB
/
tic_tac_toe.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
# PORTFOLIO PROJECT: TIC-TAC-TOE GAME
board_position = {1: [0, 0], 2: [0, 1], 3: [0, 2], 4: [1, 0], 5: [1, 1], 6: [1, 2], 7: [2, 0], 8: [2, 1], 9: [2, 2]}
class Player:
def __init__(self, player_number):
self._set_name(player_number)
if player_number == 1:
self._set_marker()
self.score = 0
def __repr__(self):
return f"{self.name} has a total score of {self.score} wins"
def _set_name(self, player_number):
self.name = input(f"Please enter name for player #{player_number}: ").title()
while not self.name.strip(): # checks if name empty or whitespace
print("Invalid Input Error: Name cannot by empty.")
self.name = input(f"Please enter name for player #{player_number}: ").title()
def _set_marker(self):
self.marker = input("Please enter which marker you want to use (X or O): ").upper()
if self.marker in ["X", "O"]:
pass
else:
print("Invalid Input Error: Marker should be either X or O")
self._set_marker()
def add_score(self):
self.score += 1
def game_rules():
print("\nWelcome to the Tic-Tac-Toe Game!\n")
print("This is a 2 player game, with each player taking turns to place their alloted marker (either X or O) on the board.")
print("When taking a turn, enter a position from 1 to 9 to place your marker.")
print("The first person to get 3 of their markers in a row (either horizontally, vertically or diagonally) wins the game.")
print(f"\n 1 \u2502 2 \u2502 3")
print("\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500")
print(f" 4 \u2502 5 \u2502 6")
print("\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500")
print(f" 7 \u2502 8 \u2502 9\n")
def init_player():
player1 = Player(1)
player2 = Player(2)
player2.marker = "O" if player1.marker == "X" else "X"
return player1, player2
def init_game():
won_game = False
board_full = False
counter = 1
game_board = [[" ", " ", " "], [" ", " ", " "],[" ", " ", " "]] # Game_board is effectively written as [row0, row1, row2]
return won_game, board_full, counter, game_board
def input_marker(counter, game_board, player1, player2):
while True:
try:
if counter % 2 != 0:
input_message = f"Which empty space would you like to place your {player1.marker}, {player1.name}? "
else:
input_message = f"Which empty space would you like to place your {player2.marker}, {player2.name}? "
player_turn = int(input(input_message))
if player_turn in board_position.keys():
x_position = board_position[player_turn][0]
y_position = board_position[player_turn][1]
if game_board[x_position][y_position] == " ":
game_board[x_position][y_position] = player1.marker if counter % 2 != 0 else player2.marker
break
else:
print("Position Error: Position already filled")
else:
print("Position Error: Number needs to be between 1 to 9.")
except ValueError:
print("Invalid Input Error: Please enter a number between 1 to 9.")
def check_won(game_board):
# row or column win
for i in range(3):
if game_board[i][0] == game_board[i][1] == game_board[i][2] != " ":
return True, game_board[i][0]
elif game_board[0][i] == game_board[1][i] == game_board[2][i] != " ":
return True, game_board[0][i]
# diagonals win
if game_board[0][0] == game_board[1][1] == game_board[2][2] != " " or \
game_board[0][2] == game_board[1][1] == game_board[2][0]!= " ":
return True, game_board[1][1]
# no win
return False, None
def print_game_board(game_board):
line = "\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500"
print(f"\n {game_board[0][0]} \u2502 {game_board[0][1]} \u2502 {game_board[0][2]}")
print(line)
print(f" {game_board[1][0]} \u2502 {game_board[1][1]} \u2502 {game_board[1][2]}")
print(line)
print(f" {game_board[2][0]} \u2502 {game_board[2][1]} \u2502 {game_board[2][2]}\n")
def play_game(player1, player2):
won_game, board_full, counter, game_board = init_game()
while not won_game and not board_full:
input_marker(counter, game_board, player1, player2)
if counter >= 5:
won_game, winner = check_won(game_board)
print_game_board(game_board)
counter += 1
if counter > 9:
board_full = True
if won_game:
if winner == player1.marker:
player1.add_score()
print(f"{player1.name} won!")
else:
player2.add_score()
print(f"{player2.name} won!")
elif board_full:
print("It's a draw!")
print(player1)
print(player2)
play_again = input("\nWould you like to play another round of Tic-Tac-Toe? (Y/N): ")
if play_again.upper() == "Y":
play_game(player1, player2)
game_rules()
player1, player2 = init_player()
play_game(player1, player2)