-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEngine.py
More file actions
175 lines (155 loc) · 6.35 KB
/
GameEngine.py
File metadata and controls
175 lines (155 loc) · 6.35 KB
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
# import modules
import random
import sys
import copy
class Game:
"Tic-Tac-Toe class. This class holds the user interaction, and game logic"
def __init__(self):
self.board = [' '] * 9
self.player_marker = ''
self.bot_marker = ''
self.winning_combos = (
[6, 7, 8], [3, 4, 5], [0, 1, 2], [0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6],
)
self.corners = [0,2,6,8]
self.sides = [1,3,5,7]
self.middle = 4
self.GameFinished=False
self.form = '''
\t| %s | %s | %s |
\t-------------
\t| %s | %s | %s |
\t-------------
\t| %s | %s | %s |
'''
def print_board(self,board = None):
"Display board on screen"
if board is None:
return self.form % tuple(self.board[6:9] + self.board[3:6] + self.board[0:3])
else:
# when the game starts, display numbers on all the grids
return self.form % tuple(board[6:9] + board[3:6] + board[0:3])
def help(self):
return '''
\n\t The game board has 9 sqaures(3X3).
\n\t Two players take turns in marking the spots/grids on the board.
\n\t The first player to have 3 pieces in a horizontal, vertical or diagonal row wins the game.
\n\t To place your mark in the desired square, simply type the number corresponding with the square on the grid
\n\t Press Ctrl + C to quit
'''
def quit_game(self):
"exits game"
self.print_board
return "\n\t Thanks for playing :-) \n\t Come play again soon!\n"
def is_winner(self, board, marker):
"check if this marker will win the game"
# order of checks:
# 1. across the horizontal top
# 2. across the horizontal middle
# 3. across the horizontal bottom
# 4. across the vertical left
# 5. across the vertical middle
# 6. across the vertical right
# 7. across first diagonal
# 8. across second diagonal
for combo in self.winning_combos:
if (board[combo[0]] == board[combo[1]] == board[combo[2]] == marker):
return True
return False
def get_bot_move(self):
# check if bot can win in the next move
for i in range(0,len(self.board)):
board_copy = copy.deepcopy(self.board)
if self.is_space_free(board_copy, i):
self.make_move(board_copy,i,self.bot_marker)
if self.is_winner(board_copy, self.bot_marker):
return i
# check if player could win on his next move
for i in range(0,len(self.board)):
board_copy = copy.deepcopy(self.board)
if self.is_space_free(board_copy, i):
self.make_move(board_copy,i,self.player_marker)
if self.is_winner(board_copy, self.player_marker):
return i
# check for space in the corners, and take it
move = self.choose_random_move(self.corners)
if move != None:
return move
# If the middle is free, take it
if self.is_space_free(self.board,self.middle):
return self.middle
# else, take one free space on the sides
return self.choose_random_move(self.sides)
def is_space_free(self, board, index):
"checks for free space of the board"
# print "SPACE %s is taken" % index
return board[index] == ' '
def is_board_full(self):
"checks if the board is full"
for i in range(1,9):
if self.is_space_free(self.board, i):
return False
return True
def make_move(self,board,index,move):
board[index] = move
def choose_random_move(self, move_list):
possible_winning_moves = []
for index in move_list:
if self.is_space_free(self.board, index):
possible_winning_moves.append(index)
if len(possible_winning_moves) != 0:
return random.choice(possible_winning_moves)
else:
return None
def start_game(self):
"welcomes user, prints help message and hands over to the main game loop"
welcomeScreen = '''\n\t-----------------------------------
\n\t TIC-TAC-TOE Game
\n\t------------------------------------
'''
self.player_marker, self.bot_marker = "X","Y"
if random.randint(0,1) == 0:
bot_move =self.get_bot_move()
self.make_move(self.board, bot_move, self.bot_marker)
welcomeScreen+=self.help()
welcomeScreen+=self.print_board()
welcomeScreen+= "I moved first now its your turn \n \t"
else:
welcomeScreen+=self.help()
welcomeScreen+=self.print_board(range(1,10))
welcomeScreen+= "You will go first \n \t"
return welcomeScreen
def check_valid_move(self,move):
if(move in [1,2,3,4,5,6,7,8,9] and self.is_space_free(self.board,move-1)):
return True
else:
return False
def make_moves(self,move):
message=""
move = int(move)
#Player Movement
self.make_move(self.board,(move - 1), self.player_marker)
if(self.is_winner(self.board, self.player_marker)):
self.GameFinished=True
message+=self.print_board()
message= (message+ "\n\tCONGRATULATIONS , YOU HAVE WON THE GAME!!! \\tn")
elif(self.is_board_full()):
self.GameFinished=True
message+=self.print_board()
message= (message+ "\n\t-- Match Draw --\t\n")
#Bot Movement
else:
bot_move = self.get_bot_move()
self.make_move(self.board, bot_move, self.bot_marker)
if (self.is_winner(self.board, self.bot_marker)):
self.GameFinished=True
message+=self.print_board()
message= (message+ "\n\t TBot HAS WON!!!!\t\n")
elif(self.is_board_full()):
self.GameFinished=True
message+=self.print_board()
message= (message+ "\n\t-- Match Draw --\t\n")
else:
message+=self.print_board()
return {'message':message, 'resultBoolean':self.GameFinished}