Skip to content

Commit

Permalink
- Removed magic numbers for allowed moves highlighting
Browse files Browse the repository at this point in the history
- Reworked checking for game over
- Fixed restart_game() method. (was missing clearing of clicked_cells)
- Added ability for random play
- Added side to move indicator
  • Loading branch information
AngelVI13 committed May 2, 2019
1 parent 5c9279e commit 05da26b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
7 changes: 7 additions & 0 deletions board/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@
LOSS = 0.0
DRAW = 0.5
WIN = 1.0

# From the viewpoint of player X
RESULT_TEXT = {
WIN: 'X won!',
DRAW: 'Tie!',
LOSS: 'O won!'
}
3 changes: 3 additions & 0 deletions gui/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
BLUE_HIGHLIGHT = (164, 217, 221)
GREY = (170, 191, 193)
YELLOW = (0, 250, 250)
HIGHLIGHT_LOW = 140
HIGHLIGHT_HIGH = 200
HIGHLIGHT_STEP = 2

BRIGHT_RED = (255, 0, 0)
BRIGHT_GREEN = (0, 255, 0)
35 changes: 18 additions & 17 deletions gui/gui_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def restart_game(self):
self.board = UltimateBoard()
self.allowed_cells = set()
self.grids_with_result = set()
self.clicked_cells = set()

def subcell_clicked(self, cell):
# do not provide player just yet, only do so when we are sure we can click the cell
Expand Down Expand Up @@ -66,9 +67,21 @@ def draw_results(self): # todo this is the same as draw_all moves? parameterize
pygame.draw.rect(self.gameDisplay, GRID_RESULT_COLORS[grid.player],
(grid.pos_x, grid.pos_y, grid.width, grid.height))

def check_for_game_over(self):
result = self.board.get_result(player_jm=PLAYER_X)
if result is not None:
self.message_display(text=RESULT_TEXT[result])
time.sleep(PAUSE_BEFORE_GAME_RESTART)
self.restart_game()

def click_random_cell(self):
random_cell = random.choice(list(self.allowed_cells))
self.subcell_clicked(random_cell)
time.sleep(0.2) # sleep 1s so output can be checked

def game_loop(self):
# set up an endless cycle of B values (rgB) for highlighting moves
highlight_range = [i for i in range(140, 201, 2)]
highlight_range = [i for i in range(HIGHLIGHT_LOW, HIGHLIGHT_HIGH+1, HIGHLIGHT_STEP)]
brightness_iter = cycle(chain(highlight_range, reversed(highlight_range)))

while True:
Expand All @@ -86,31 +99,19 @@ def game_loop(self):
self.gameDisplay.fill(WHITE)
self.draw_main_grid()

# todo check for game end here as well. Might not have allowed cells because game is over
if not self.allowed_cells:
self.allowed_cells = self.find_allowed_cells()

self.click_random_cell()

self.draw_clicked_cells()
self.draw_results()
self.draw_allowed_moves(highlight)
self.draw_side_to_move(-self.board.playerJustMoved)
pygame.display.update()
self.clock.tick(FRAMES_PER_SECOND)

# todo factor out repetitive code below
if self.board.get_result(player_jm=PLAYER_X) == WIN:
self.message_display(text='X won!')
time.sleep(PAUSE_BEFORE_GAME_RESTART)
self.restart_game()
elif self.board.get_result(player_jm=PLAYER_O) == WIN:
self.message_display(text='O won!')
time.sleep(PAUSE_BEFORE_GAME_RESTART)
self.restart_game()
elif self.board.get_result(player_jm=PLAYER_O) == DRAW:
self.message_display(text='Tie!')
time.sleep(PAUSE_BEFORE_GAME_RESTART)
self.restart_game()

# todo add indication whose turn it is to play
self.check_for_game_over()


if __name__ == '__main__':
Expand Down
7 changes: 7 additions & 0 deletions gui/gui_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,10 @@ def draw_clicked_cells(self):
for cell in self.clicked_cells:
pygame.draw.rect(self.gameDisplay, self.colors[cell.player],
(cell.pos_x, cell.pos_y, cell.width, cell.height))

def draw_side_to_move(self, player_to_move):
"""Draws side to move in top left corner of screen (game screen)"""
# todo remove magic numbers
pygame.draw.rect(self.gameDisplay, BLACK, (OFFSET_X+8, OFFSET_Y-22, 24, 14))
pygame.draw.rect(self.gameDisplay, self.colors[player_to_move], (OFFSET_X+10, OFFSET_Y-20, 20, 10))
self.message_display(text=' to move', pos=(OFFSET_X+60, OFFSET_Y-15), size=14)

0 comments on commit 05da26b

Please sign in to comment.