def checkNeighbours(self, row, col): conn=[] # check the rows above and below the supplied col for r in [row-1, row+1]: if self.isValidPos(r, col) and self.checkOccupied(r, col): conn.append(Tile.toAlpha((r, col))) # check the columns left and right of the supplied row for c in [col-1, col+1]: if self.isValidPos(row, c) and self.checkOccupied(row, c): conn.append(Tile.toAlpha((row, c))) return conn
def getOccupied(self): occupied=[] for row in range(0,self.rows): for col in range(0,self.cols): if self.tiles[row][col]: occupied.append(Tile.toAlpha((row,col))) return occupied
def playTile(self, playerId, tile=None, alpha=None): if tile is None: if alpha is not None: tile = Tile.newTileFromAlpha(alpha) else: return False, "Invalid argument to playTile" return self.on_event(self._getPlayer(playerId), 'PlaceTile', tile=tile)
def __init__(self, rows, cols, tiles=[], initialState=None): self.rows=rows self.cols=cols self.tiles=[[False for col in range(0,self.cols)] for row in range(0,self.rows)] if initialState: for alpha in initialState: self.placeTile(Tile.newTileFromAlpha(alpha)) for tile in tiles: self.placeTile(tile)
def findConnected(self, conn, r, c, skip=[]): if conn is None: conn=[] # Get alpha representation of tile position, and confirm it's valid curralpha=Tile.toAlpha((r,c)) if self.alphaIsValid(curralpha): # if it's in the list already, no need to continue if curralpha in conn: return conn # it's not in the list already, so check if the space is occupied if not self.checkOccupied(r, c): return conn # okay, so it's occupied and not in the list, add it and check neighbours conn.append(curralpha) nb = self.checkNeighbours(r, c) for n in nb: if n not in conn and n not in [str(t) for t in skip]: rnew, cnew = Tile.fromAlpha(n) conn = self.findConnected(conn, rnew, cnew, skip) return conn;
def loadFromSavedData(sd): p = TileBagPlayer(sd['id'], name=sd['name']) pd = sd['playerdata'] # restore the tiles for tile in pd['tiles']: t = Tile.newTileFromAlpha(tile) p.receiveTile(t) # restore the money p.money = pd['money'] # restore the stocks for key, value in pd['stocks'].items(): [p.receiveStock(Stock(key)) for i in range(0, int(value))] return p
def on_event(self, event, **kwargs): if event == 'StartGame': # Initialize Game Components self._game._log.recordGameMessage("TileBagGame Started!") self._game.board = Board(9, 12) self._game.tilebag = TileBag(9, 12) # Determine Start Order: draw a single tile for each player self._game._currplayer = None lowestTile = Tile(10, 8) for player in self._game._players: t = self._game.tilebag.takeTile() self._game._log.recordTileAction(player.name, str(t)) if t <= lowestTile: lowestTile = t self._game._currplayer = player self._game.board.placeTile(t) self._game._log.recordGameMessage( "{} is the first player".format( self._game._currplayer.name)) # Now set the game order to the above self._game._rotation = islice( cycle(self._game._players), self._game._players.index(self._game._currplayer) + 1, None) # give each player seven tiles to start for player in self._game._players: for i in range(0, 7): player.receiveTile(self._game.tilebag.takeTile()) # good to go, flip into the main game state cycle return True, "", TileBagGame.PlaceTile(self._game) return False, "ILLEGAL EVENT - StartGame when not in StartGame State", self
def alphaIsValid(self, alpha): row, col = Tile.fromAlpha(alpha) return self.isValidPos(row, col)
'ncols' : self.cols, 'occupied' : self.getOccupied(), } def __repr__(self): return str(self.tiles) if __name__ == "__main__": board = Board(8,5) print("---- Testing board helper functions ----") print("getsize: {}".format(board.getBoardSize())) print("checkTile(2,2): {}".format(not board.checkTile(2,2))) print("---- Testing Placing a tile ----") t=Tile(2,2) print("placed Tile" + str(t)) board.placeTile(t) print(board) print("Occupied: {}".format(board.getOccupied())) print("---- Testing alphaIsValid ----") print("alphaIsValid: {}".format(board.alphaIsValid("1A"))) print("!alphaIsValid: {}".format(not board.alphaIsValid("A1"))) print("!alphaIsValid: {}".format(not board.alphaIsValid("9I"))) print("isValidPos: {}".format(board.isValidPos(0, 1))) print("!isValidPos: {}".format(not board.isValidPos(110, 1))) print("!isValidPos: {}".format(not board.isValidPos(0, 110))) print("!isValidPos: {}".format(not board.isValidPos(0, -1))) print("!isValidPos: {}".format(not board.isValidPos(-1, 1)))