-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
182 lines (145 loc) · 8.85 KB
/
main.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
179
180
181
182
# ===========================================================================
# Getting players' choices
# ===========================================================================
def getPlayerChoice(P1_NAME):
PLAYER_1 = input(f"{P1_NAME}, give your symbol 'X' or 'O': ").upper()
while not (PLAYER_1 == "X" or PLAYER_1 == "O"):
print("Please enter 'X' or 'O'")
PLAYER_1 = input(f"{P1_NAME}, give your symbol 'X' or 'O': ").upper()
if PLAYER_1 == "X":
PLAYER_2 = "O"
else:
PLAYER_2 = "X"
return (PLAYER_1, PLAYER_2)
# ===========================================================================
# ===========================================================================
# Set up who plays first
# ===========================================================================
from random import randint
def getPlayerTurn():
if randint(1, 2) == 1:
return 'Player 1'
else:
return 'Player 2'
# ===========================================================================
# ===========================================================================
# Getting players' names
# ===========================================================================
def getPlayerNames():
PLAYER_1_NAME = input("Player 1, give your name: ")
PLAYER_2_NAME = input("Player 2, give your name: ")
return (PLAYER_1_NAME, PLAYER_2_NAME)
# ===========================================================================
# ===========================================================================
# Display the board
# ===========================================================================
from IPython.display import clear_output
def displayBoard(lista):
clear_output() # Remember! This only works in jupyter notebook!!
LINE = '---|---|---'
# Print everything from the list along with a space at the start
# to make it look better.
print(f' {lista[7]} | {lista[8]} | {lista[9]}')
print(LINE)
print(f' {lista[4]} | {lista[5]} | {lista[6]}')
print(LINE)
print(f' {lista[1]} | {lista[2]} | {lista[3]}')
# ===========================================================================
# =============================================================================================
# Get the current player's position choice
# =============================================================================================
def getPlayerPosition(lista, currentPlayer):
POSITION = int(input(f"{currentPlayer}, give your place on the board. Select (1 - 9): "))
while (1 < POSITION > 9) or (lista[POSITION] != " "): # position not in [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(f"{currentPlayer}, the place you gave is taken or the place you entered is not between 1 and 9.")
POSITION = int(input(f"{currentPlayer}, give your place on the board. Select (1 - 9): "))
return POSITION
# =============================================================================================
# =================================================================================================
# Check if player 1 or player 2 won
# =================================================================================================
def checkWin(lista, mark):
return (
(lista[7] == mark and lista[8] == mark and lista[9] == mark) or # Top Horizontal (---)
(lista[7] == mark and lista[5] == mark and lista[3] == mark) or # Diagonal (\)
(lista[4] == mark and lista[5] == mark and lista[6] == mark) or # Middle Horizontal (---)
(lista[9] == mark and lista[5] == mark and lista[1] == mark) or # Diagonal (/)
(lista[1] == mark and lista[2] == mark and lista[3] == mark) or # Bottom Horizontal (---)
(lista[7] == mark and lista[4] == mark and lista[1] == mark) or # Left Vertical (|)
(lista[9] == mark and lista[6] == mark and lista[3] == mark) or # Right Vertical (|)
(lista[8] == mark and lista[5] == mark and lista[2] == mark) # Center Vertical (|)
)
# =================================================================================================
# =================================================================================================
# Check if the board is full, if it is, it's a TIE.
# =================================================================================================
def checkFullBoard(lista):
for item in lista:
if item == " ":
return False
return True
# =================================================================================================
# =================================================================================================
# Question to play again or no
# =================================================================================================
def playAgain():
play = input("Do you want to play again? (Y/N): ")
ch = "yYnN"
while play not in ch:
play = input("Wrong choice. Do you want to play again? (Y/N): ")
return play.lower() == 'y'
# =================================================================================================
# ===========================================================================================
# MAIN PROGRAM
# ===========================================================================================
print("Welcome to Tic-Tac-Toe!")
# ΟÏίζω την gameOn True και το παιχνίδι ξεκινάει.
gameOn = True
while gameOn:
# ΠάÏε τα ονόματα των παιχτ΄΄ων
PLAYER_NAME_1, PLAYER_NAME_2 = getPlayerNames()
# ΑÏχικοποίηση του board.
board = ["#"] + [' '] * 9 # board = ['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
# ΟÏίζω τα σÏμβολα των δÏο παιχτών.
PLAYER_1_MARK, PLAYER_2_MARK = getPlayerChoice(PLAYER_NAME_1)
# ΚαθοÏίζω τον παίχτη που πα΄ίζει Ï€Ïώτος.
PLAYER_TURN = getPlayerTurn()
Win = False
Tie = False
# Το παιχνίδι συνεχίζεται μÎχÏι κάποιος να κεÏδίσει ή να ÎÏθει ισοπαλία
while not (Win or Tie):
# Εμφανίζω το board.
displayBoard(board)
if PLAYER_TURN == "Player 1": # ΕλÎγχω ποιος παίχτης παίζει (Player 1)
print(f"\n{PLAYER_NAME_1} PLAYS!")
# Ο Player 1 επιλÎγει τη θÎση και γεμίζει το board με το σÏμβολο που Îχει διαλÎξει.
board[getPlayerPosition(board, PLAYER_NAME_1)] = PLAYER_1_MARK
# Win Check
if checkWin(board, PLAYER_1_MARK):
displayBoard(board)
print(f"Congratulations! {PLAYER_NAME_1}, you won the game!")
Win = True
# Tie Check
elif checkFullBoard(board):
displayBoard(board)
print('Congratulations to both players! It\'s a TIE!')
Tie = True
PLAYER_TURN = 'Player 2'
else: # ΕλÎγχω ποιος παίχτης παίζει (Player 2)
print(f"\n{PLAYER_NAME_2} PLAYS!")
# Ο Player 2 επιλÎγει τη θÎση και γεμίζει το board με το σÏμβολο που Îχει διαλÎξει.
board[getPlayerPosition(board, PLAYER_NAME_2)] = PLAYER_2_MARK
# Win Check
if checkWin(board, PLAYER_2_MARK):
displayBoard(board)
print(f"Congratulations! {PLAYER_NAME_2}, you won the game!")
Win = True
# Tie Check
elif checkFullBoard(board):
displayBoard(board)
print('Congratulations to both players! It\'s a TIE!')
Tie = True
PLAYER_TURN = 'Player 1'
gameOn = playAgain()
print("Goodbye! Thank you for playing!")
# ===========================================================================================