From 00032a20256039f579050d801e4ecbf87b99ea28 Mon Sep 17 00:00:00 2001 From: Pablo Moleri Date: Sun, 08 Nov 2009 12:07:24 +0000 Subject: Simple IA added --- diff --git a/Quinteti.activity/gui/board.py b/Quinteti.activity/gui/board.py index ebbb338..a0d9deb 100644 --- a/Quinteti.activity/gui/board.py +++ b/Quinteti.activity/gui/board.py @@ -127,6 +127,8 @@ class Board: for n in self.numbers: self.items.add(n) + + self.arrange_gui() # Arranges the gui according to the game state. def new_game(self): self.game = GameState("", "") @@ -211,8 +213,30 @@ class Board: image = pygame.image.load(file_dir + instructions_image) rect = image.get_rect() rect.center = self.screen.get_rect().center - self.screen.blit(image, rect) + self.screen.blit(image, rect) + def arrange_gui(self): + '''Arranges the numbers according to the game state.''' + + # First moves all the numbers to its original positions + i = 0 + for number in self.numbers: + number.rect.center = self.number_locations[i] + i += 1 + + # Then for each occupied cell, moves the number to that cell + coords = [ (row,col) for row in range(1,4) for col in range(1,4) ] + i = 0 + for row, col in coords: + cell = self.game.get_cell(row, col) + if cell: + number, player = cell + if number > 0: + gui_number = self.numbers[number-1] + gui_cell = self.cells[i] + gui_number.rect.center = gui_cell.rect.center + i += 1 + def processXY(self, x, y): # If is showing instructions, it disables them if self.showing_instructions: @@ -232,8 +256,9 @@ class Board: row, col = c.get_pos() player = self.game.get_enabled_player() ok, hits = self.game.make_move(row, col, self.lastSelectedNumberCell.id_cell, player) - if ok: - self.lastSelectedNumberCell.rect = self.lastSelectedBoardCell.rect # Moves the number to the board + if ok: + # Moves the number to the board + self.lastSelectedNumberCell.rect.center = self.lastSelectedBoardCell.rect.center self.lastSelectedNumberCell.set_selected(False) self.lastSelectedNumberCell = None @@ -245,7 +270,9 @@ class Board: if number.id_cell in hits: number.set_selected(True) - + player = self.game.get_enabled_player() + self.game.auto_play(player) + self.arrange_gui() break # Checks if the selected coordinate is a number @@ -268,7 +295,11 @@ class Board: # Deselect all numbers for number in self.numbers: number.set_selected(False) - + + #player = self.game.get_enabled_player() + #self.game.auto_play(player) + #self.arrange_gui() + def _show_instructions(self): self.showing_instructions = True diff --git a/Quinteti.activity/logic/game.py b/Quinteti.activity/logic/game.py index 247de6c..877c27a 100644 --- a/Quinteti.activity/logic/game.py +++ b/Quinteti.activity/logic/game.py @@ -19,6 +19,8 @@ """GamesState, keeps the state of a game, and encloses game logic.""" +import random + class GameState: def __init__(self, player_1, player_2, matrix_size=3, target_score=15): @@ -86,6 +88,15 @@ class GameState: Returns a boolean if the move is valid and the score difference. """ + ok, hits, score = self._make_move(row1, col1, number, player, True) + return ok, hits + + def _make_move(self, row1, col1, number, player, real): + """Makes a move with the given number in the given cell. + + Returns a boolean if the move is valid and the score difference. + """ + row, col = (row1-1, col1-1) if (self.state[row][col] == None): if (self.turn == player): @@ -109,18 +120,26 @@ class GameState: if row_score: hits.extend(row_list) - self.state[row][col] = self.turn - self.matrix[row][col] = number - self.numbers.remove(number) - - if self.turn == 1: - self.player_1_score += score - self.turn = 2 - else: - self.player_2_score += score - self.turn = 1 - return True, hits - return False, None + if real: + self.state[row][col] = self.turn + self.matrix[row][col] = number + self.numbers.remove(number) + + if self.turn == 1: + self.player_1_score += score + self.turn = 2 + else: + self.player_2_score += score + self.turn = 1 + return True, hits, score +# else: +# print "invalid number" +# else: +# print "invalid player" +# else: +# print "invalid cell state" + + return False, None, 0 def _check_action(self, list, pos, number): """Tests if a move in a row (or column) scores.""" @@ -166,6 +185,46 @@ class GameState: 'target_score': self.target_score} return str(dic) + + def auto_play(self, player): + '''Returns an automatic play from computer. + + The strategy is: + - Try to make 2 points. + - Try to make 1 point. + - Try to make a move that enables two different points (make tha game more interesting). + - Random move that doesn't enable the other player to make a point. + - Random move. + ''' + + options = [(row, col) + for row in range(1, len(self.matrix) + 1) + for col in range(1, len(self.matrix) + 1) + if self.matrix[row-1][col-1] == 0] + print options + + # Try two points + for row, col in options: + for number in self.numbers: + ok, hits, score = self._make_move(row, col, number, player, False) + if score >= 2: + self.make_move(row, col, number, player) + return (row, col) + + # Try one point + for row, col in options: + for number in self.numbers: + ok, hits, score = self._make_move(row, col, number, player, False) + if score >= 1: + self.make_move(row, col, number, player) + return (row, col) + + # Random + row, col = random.choice(options) + number = random.choice(self.numbers) + self.make_move(row, col, number, player) + return (row, col) + if __name__ == "__main__": """Module test function.""" -- cgit v0.9.1