def get(self, layer, row, column): # regarde si on sait mettre une bille sur le plateau si elle correspond au dimension etc ... if layer < 0 or row < 0 or column < 0: raise game.InvalidMoveException('The position ({}) is outside of the board'.format([layer, row, column])) try: return self._state['visible']['board'][layer][row][column] except: raise game.InvalidMoveException('The position ({}) is outside of the board'.format([layer, row, column]))
def update(self, move, player): state = self._state['visible'] if move['move'] == 'place': if state['reserve'][player] < 1: raise game.InvalidMoveException('no more sphere') self.set(move['to'], player) state['reserve'][player] -= 1 elif move['move'] == 'move': if move['to'][0] <= move['from'][0]: raise game.InvalidMoveException('you can only move to upper layer') sphere = self.remove(move['from'], player) try: self.set(move['to'], player) except game.InvalidMoveException as e: self.set(move['from'], player) raise e else: raise game.InvalidMoveException('Invalid Move:\n{}'.format(move)) if 'remove' in move: if not self.createSquare(move['to']): raise game.InvalidMoveException('You cannot remove spheres') if len(move['remove']) > 2: raise game.InvalidMoveException('Can\'t remove more than 2 spheres') for coord in move['remove']: sphere = self.remove(coord, player) state['reserve'][player] += 1 state['turn'] = (state['turn'] + 1) % 2
def get(self, layer, row, column): if layer < 0 or row < 0 or column < 0: raise game.InvalidMoveException('The position ({}) is outside of the board'.format([layer, row, column])) try: return self._state['visible']['board'][layer][row][column] except: raise game.InvalidMoveException('The position ({}) is outside of the board'.format([layer, row, column]))
def applymove(self, move): #{pos: 8, quarto: true, nextPiece: 2} stateBackup = copy.deepcopy(self._state) try: state = self._state['visible'] if state['pieceToPlay'] is not None: try: if state['board'][move['pos']] is not None: raise game.InvalidMoveException('The position is not free') state['board'][move['pos']] = state['remainingPieces'][state['pieceToPlay']] del(state['remainingPieces'][state['pieceToPlay']]) except game.InvalidMoveException as e: raise e except: raise game.InvalidMoveException("Your move should contain a \"pos\" key in range(16)") if len(state['remainingPieces']) > 0: try: state['pieceToPlay'] = move['nextPiece'] except: raise game.InvalidMoveException("You must specify the next piece to play") else: state['pieceToPlay'] = None if 'quarto' in move: state['quartoAnnounced'] = move['quarto'] winner = self.winner() if winner is None or winner == -1: raise game.InvalidMoveException("There is no Quarto !") else: state['quartoAnnounced'] = False except game.InvalidMoveException as e: self._state = stateBackup raise e
def update(self, coord, player): state = self._state['visible'] line, column = coord index = 3 * line + column if not (0 <= line <= 2 and 0 <= column <= 2): raise game.InvalidMoveException('The move is outside of the board') if state[index] is not None: raise game.InvalidMoveException('The specified cell is not empty') state[index] = player
def applymove(self, move): try: index = int(move) move = (index // 3, index % 3) if not (0 <= move[0] <= 2 and 0 <= move[1] <= 2): raise game.InvalidMoveException('The move is outside of the board') if self.__state[move[0]][move[1]] is not None: raise game.InvalidMoveException('The specified cell is not empty') self.__state[move[0]][move[1]] = self.currentplayer except: raise game.InvalidMoveException('A valid move must be a two-integer tuple')
def canMove(self, layer, row, column): # regarde si la bille peut bouger if self.get(layer, row, column) == None: # impossible car none donc il y a pas de bille raise game.InvalidMoveException('The position ({}) is empty'.format([layer, row, column])) if layer < 3: # regarde si il y a quelque chose au dessus if ( self.safeGet(layer + 1, row, column) != None or self.safeGet(layer + 1, row - 1, column) != None or self.safeGet(layer + 1, row - 1, column - 1) != None or self.safeGet(layer + 1, row, column - 1) != None ): raise game.InvalidMoveException('The position ({}) is not movable'.format([layer, row, column]))
def validPosition(self, layer, row, column): if self.get(layer, row, column) != None: # il regarde dans la liste si la valeur est different de none ce qui signifierait qu'il y a deja une bille sur la position raise game.InvalidMoveException('The position ({}) is not free'.format([layer, row, column])) if layer > 0: # si il y a une bille qui manque donc qu'on essaie de monter une bille sur 3 billes il dit que c'est pas stable et provoque une erreur if ( self.get(layer - 1, row, column) == None or self.get(layer - 1, row + 1, column) == None or self.get(layer - 1, row + 1, column + 1) == None or self.get(layer - 1, row, column + 1) == None ): raise game.InvalidMoveException('The position ({}) is not stable'.format([layer, row, column]))
def canMove(self, layer, row, column): if self.get(layer, row, column) is None: raise game.InvalidMoveException('The position ({}) is empty'.format([layer, row, column])) if layer < 3: if ( self.safeGet(layer + 1, row, column) is not None or self.safeGet(layer + 1, row - 1, column) is not None or self.safeGet(layer + 1, row - 1, column - 1) is not None or self.safeGet(layer + 1, row, column - 1) is not None ): raise game.InvalidMoveException('The position ({}) is not movable'.format([layer, row, column]))
def validPosition(self, layer, row, column): if self.get(layer, row, column) is not None: raise game.InvalidMoveException('The position ({}) is not free'.format([layer, row, column])) if layer > 0: if ( self.get(layer - 1, row, column) is None or self.get(layer - 1, row + 1, column) is None or self.get(layer - 1, row + 1, column + 1) is None or self.get(layer - 1, row, column + 1) is None ): raise game.InvalidMoveException('The position ({}) is not stable'.format([layer, row, column]))
def _setassassins(self, move): state = self._state if 'assassins' not in move: raise game.InvalidMoveException('The dictionary must contain an "assassins" key') if not isinstance(move['assassins'], list): raise game.InvalidMoveException('The value of the "assassins" key must be a list') for assassin in move['assassins']: if not isinstance(assassin, str): raise game.InvalidMoveException('The "assassins" must be identified by their name') if not assassin in POPULATION: raise game.InvalidMoveException('Unknown villager: {}'.format(assassin)) state.setassassins(move['assassins']) state.update([], 0)
def applymove(self, move): try: move = int(move) if not (0 <= move < 7): raise game.InvalidMoveException( 'The move is outside of the board') if self.__state[move][-1] is not None: raise game.InvalidMoveException( 'The specified columns is full') self.__state[move][self.__state[move].index( None)] = self.currentplayer except: raise game.InvalidMoveException( 'A valid move must be an integer between 0 and 6')
def update(self, move, player): state = self._state['visible'] try: move = int(move) except: raise game.InvalidMoveException( 'A valid move must be an integer between 0 and 6') else: if not (0 <= move < 7): raise game.InvalidMoveException( 'The move is outside of the board') if state[move][-1] is not None: raise game.InvalidMoveException( 'The specified columns is full') state[move][state[move].index(None)] = player
def remove(self, coord, player): layer, row, column = tuple(coord) self.canMove(layer, row, column) sphere = self.get(layer, row, column) if sphere != player: raise game.InvalidMoveException('not your sphere') self._state['visible']['board'][layer][row][column] = None
def applymove(self, move): try: index = int(move) self._state.update((index // 3, index % 3), self.currentplayer) except: raise game.InvalidMoveException( 'A valid move must be a two-integer tuple')
def applymove(self, move): try: index = int(move) except: raise game.InvalidMoveException('A valid move must be a stringified integer') else: self._state.update((index // 3, index % 3), self.currentplayer)
def applymove(self, move): try: move = json.loads(move) except: raise game.InvalidMoveException('A valid move must be a valid JSON string') else: self._state.applymove(move)
def applymove(self, move): try: state = self._state move = json.loads(move) if state.isinitial(): self._setassassins(move) else: self._state.update(move['actions'], self.currentplayer) except game.InvalidMoveException as e: raise e except Exception as e: print(e) raise game.InvalidMoveException('A valid move must be a dictionary')
def applymove(self, move): try: self._state.update(json.loads(move), self.currentplayer) except json.JSONDecodeError: raise game.InvalidMoveException('move must be valid JSON string: {}'.format(move))
def update(self, moves, player): visible = self._state['visible'] hidden = self._state['hidden'] people = visible['people'] for move in moves: print(move) # ('move', x, y, dir): moves person at position (x,y) of one cell in direction dir if move[0] == 'move': x, y, d = int(move[1]), int(move[2]), move[3] p = people[x][y] if p is None: raise game.InvalidMoveException( '{}: there is no one to move'.format(move)) nx, ny = self._getcoord((x, y, d)) new = people[nx][ny] # King, assassins, villagers can only move on a free cell if p != 'knight' and new is not None: raise game.InvalidMoveException( '{}: cannot move on a cell that is not free'.format( move)) if p == 'king' and BOARD[nx][ny] == 'R': raise game.InvalidMoveException( '{}: the king cannot move on a roof'.format(move)) if p in {'assassin'} + POPULATION and player != 0: raise game.InvalidMoveException( '{}: villagers and assassins can only be moved by player 0' .format(move)) if p in {'king', 'knight'} and player != 1: raise game.InvalidMoveException( '{}: the king and knights can only be moved by player 1' .format(move)) # Move granted if cell is free if new is None: people[x][y], people[nx][ny] = people[nx][ny], people[x][y] # If cell is not free, check if the knight can push villagers else: nf = self._nextfree((x, y, d)) if nf is None: raise game.InvalidMoveException( '{}: cannot move-and-push in the given direction'. format(move)) nfx, nfy = nf while (nfx, nfy) != (x, y): px, py = self._getcoord((nfx, nfx, { 'E': 'W', 'W': 'E', 'S': 'N', 'N': 'S' }[d])) people[nfx][nfy] = people[px][py] nfx, nfy = px, py # ('arrest', x, y, dir): arrests the villager in direction dir with knight at position (x, y) elif move[0] == 'arrest': if player != 1: raise game.InvalidMoveException( 'arrest action only possible for player 1') x, y, d = int(move[1]), int(move[2]), move[3] arrester = people[x][y] if arrester != 'knight': raise game.InvalidMoveException( '{}: the attacker is not a knight'.format(move)) tx, ty = self._getcoord((x, y, d)) target = people[tx][ty] if target not in POPULATION: raise game.InvalidMoveException( '{}: only villagers can be arrested'.format(move)) visible['arrested'].append(people[tx][ty]) people[tx][ty] = None # ('kill', x, y, dir): kills the assassin/knight in direction dir with knight/assassin at position (x, y) elif move[0] == 'kill': x, y, d = int(move[1]), int(move[2]), move[3] killer = people[x][y] if killer == 'assassin' and player != 0: raise game.InvalidMoveException( '{}: kill action for assassin only possible for player 0' .format(move)) if killer == 'knight' and player != 1: raise game.InvalidMoveException( '{}: kill action for knight only possible for player 1' .format(move)) tx, ty = self._getcoord((x, y, d)) target = people[tx][ty] if target is None: raise game.InvalidMoveException( '{}: there is no one to kill'.format(move)) if killer == 'assassin' and target == 'knight': visible['killed']['knights'] += 1 people[tx][tx] = None elif killer == 'knight' and target == 'assassin': visible['killed']['assassins'] += 1 people[tx][tx] = None else: raise game.InvalidMoveException( '{}: forbidden kill'.format(move)) # ('attack', x, y, dir): attacks the king in direction dir with assassin at position (x, y) elif move[0] == 'attack': if player != 0: raise game.InvalidMoveException( 'attack action only possible for player 0') x, y, d = int(move[1]), int(move[2]), move[3] attacker = people[x][y] if attacker != 'assassin': raise game.InvalidMoveException( '{}: the attacker is not an assassin'.format(move)) tx, ty = self._getcoord((x, y, d)) target = people[tx][ty] if target != 'king': raise game.InvalidMoveException( '{}: only the king can be attacked'.format(move)) visible['king'] = 'injured' if visible[ 'king'] == 'healthy' else 'dead' # ('reveal', x, y): reveals villager at position (x,y) as an assassin elif move[0] == 'reveal': if player != 0: raise game.InvalidMoveException( 'raise action only possible for player 0') x, y = int(move[1]), int(move[2]) p = people[x][y] if p not in hidden['assassins']: raise game.InvalidMoveException( '{}: the specified villager is not an assassin'.format( move)) people[x][y] = 'assassin' # If assassins' team just played, draw a new card if player == 0: visible['card'] = hidden['cards'].pop()