def move(self, n=1): n = int(n) self.controller.moveSnake(n) playerBoard = self.controller.getPlayer().getGameBoard() BoardUtils.printBoardV4(playerBoard.getBoard())
def placeSnake(self): playerBoard = self.currentPlayer.getGameBoard() board = BoardUtils.clone(playerBoard.getBoard()) row, column = BoardUtils.rowCount(board) // 2 - 1, BoardUtils.columnCount(board) // 2 board, snake_coordinates = GameService.insertSnake(board, row, column) playerBoard.setBoard(board) self.currentPlayer.snake.addCoordinates(snake_coordinates)
def insertApple(board): appleInserted = False while not appleInserted: try: rowIndex, columnIndex = BoardUtils.randomPosition(board) if BoardUtils.surroundingContains(board, rowIndex, columnIndex, "."): print("Hmm") raise ServiceException() board[rowIndex][columnIndex] = Constants.APPLE appleInserted = True except ServiceException: continue
def placeApples(self): applesInserted = False while not applesInserted: board = self.currentPlayer.getGameBoard().getBoard() try: for appleCount in range(Constants.APPLE_COUNT): rowIndex, columnIndex = \ BoardUtils.randomCustomPosition(board, lambda row, column: not BoardUtils.neighboursContains(board, row, column, ".")) board[rowIndex][columnIndex] = Constants.APPLE applesInserted = True except (ServiceException, ValueError): continue
def createMatrix(): """ Creates the snake matrix for the up direction What the matrix has to look like * + + """ snake_matrix = BoardUtils.createBoard(Constants.EMPTY, 3, 1) snake_matrix[0][0] = '*' snake_matrix[1][0] = '+' snake_matrix[2][0] = '+' return snake_matrix
def moveSnake(self, n): gameBoard = self.currentPlayer.getGameBoard() direction = self.currentPlayer.snake.getDirection().lower() directionRowIndex, directionColumnIndex = BoardUtils.DIRECTIONS.get(direction) for i in range(n): board = BoardUtils.clone(gameBoard.getBoard()) row, column = self.currentPlayer.snake.getHead() GameService.validateMove(board, row + directionRowIndex, column + directionColumnIndex) eatApple = False if board[row + directionRowIndex][column + directionColumnIndex] == Constants.APPLE: eatApple = True self.moveHead(board, row + directionRowIndex, column + directionColumnIndex, eatApple) gameBoard.setBoard(board)
def up(self): self.controller.moveUp() playerBoard = self.controller.getPlayer().getGameBoard() BoardUtils.printBoardV4(playerBoard.getBoard())
def drawSnakeAndApples(self): self.controller.placeSnake() self.controller.placeApples() playerBoard = self.controller.getPlayer().getGameBoard() BoardUtils.printBoardV4(playerBoard.getBoard())
def printBoard(board): BoardUtils.printBoard(board)
def createBoard(): board = BoardUtils.createBoard(Constants.EMPTY, Constants.ROW_COUNT, Constants.COLUMN_COUNT) return board
def __init__(self): self.board = BoardUtils.createBoard(Constants.EMPTY, Constants.ROW_COUNT, Constants.COLUMN_COUNT)