-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
114 lines (98 loc) · 3.74 KB
/
Game.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
theBoard = {1: ' ' , 2: ' ' , 3: ' ' ,
4: ' ' , 5: ' ' , 6: ' ' ,
7: ' ' , 8: ' ' , 9: ' ' }
WIN_MESSAGE = " won. ***"
FINISH = "\nGame Over\n"
''' We will have to print the updated board after every move in the game and
thus we will make a function in which we'll define the printBoard function
so that we can easily print the board everytime by calling this function. '''
def printBoard(board):
print(board[1] + '|' + board[2] + '|' + board[3])
print('-+-+-')
print(board[4] + '|' + board[5] + '|' + board[6])
print('-+-+-')
print(board[7] + '|' + board[8] + '|' + board[9])
board_keys = []
for key in theBoard:
board_keys.append(key)
# Now we'll write the main function which has all the gameplay functionality.
def game():
turn = 'X'
count = 0
# print("Board Layout:\n")
# print('1' + '|' + '2' + '|' + '3')
# print('-+-+-')
# print('4' + '|' + '5' + '|' + '6')
# print('-+-+-')
# print('7' + '|' + '8' + '|' + '9')
for i in range(10):
printBoard(theBoard)
print("It's "+ turn + "s turn. Which place to mark ?!")
move = input()
if theBoard[move] == ' ':
theBoard[move] = turn
count += 1
else:
print("That place is already marked. Try Another.\n")
continue
# Now we will check if player X or O has won,for every move after 5 moves.
if count >= 5:
if theBoard[1] == theBoard[2] == theBoard[3] != ' ':
printBoard(theBoard)
print(FINISH)
print("***" + turn + WIN_MESSAGE)
break
elif theBoard[4] == theBoard[5] == theBoard[6] != ' ':
printBoard(theBoard)
print(FINISH)
print("***" + turn + WIN_MESSAGE)
break
elif theBoard[7] == theBoard[8] == theBoard[9] != ' ':
printBoard(theBoard)
print(FINISH)
print("***" + turn + WIN_MESSAGE)
break
elif theBoard[1] == theBoard[4] == theBoard[7] != ' ':
printBoard(theBoard)
print(FINISH)
print("***" + turn + WIN_MESSAGE)
break
elif theBoard[2] == theBoard[5] == theBoard[8] != ' ':
printBoard(theBoard)
print(FINISH)
print("***" + turn + WIN_MESSAGE)
break
elif theBoard[3] == theBoard[6] == theBoard[9] != ' ':
printBoard(theBoard)
print(FINISH)
print("***" + turn + WIN_MESSAGE)
break
elif theBoard[7] == theBoard[5] == theBoard[3] != ' ':
printBoard(theBoard)
print(FINISH)
print("***" + turn + WIN_MESSAGE)
break
elif theBoard[1] == theBoard[5] == theBoard[9] != ' ':
printBoard(theBoard)
print(FINISH)
print("***" + turn + WIN_MESSAGE)
break
# If neither X nor O wins and the board is full, we'll declare the result as 'tie'.
if count == 9:
print(FINISH)
print("It's a Tie!\nDon't Worry... It's quite common")
# Now we have to change the player after every move.
if turn =='X':
turn = 'O'
else:
turn = 'X'
# Now we will ask if player wants to restart the game or not.
restart = input("Wanna give it another go ?! (y/n)")
if restart == 'y' or restart == 'Y':
for key in board_keys:
theBoard[key] = ' '
game()
else:
print("Cools! Have a Great Day!\n")
if __name__ == "__main__":
game()