예제 #1
0
    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)
예제 #7
0
    def up(self):
        self.controller.moveUp()

        playerBoard = self.controller.getPlayer().getGameBoard()
        BoardUtils.printBoardV4(playerBoard.getBoard())
예제 #8
0
    def drawSnakeAndApples(self):
        self.controller.placeSnake()
        self.controller.placeApples()

        playerBoard = self.controller.getPlayer().getGameBoard()
        BoardUtils.printBoardV4(playerBoard.getBoard())
예제 #9
0
 def printBoard(board):
     BoardUtils.printBoard(board)
예제 #10
0
 def createBoard():
     board = BoardUtils.createBoard(Constants.EMPTY, Constants.ROW_COUNT, Constants.COLUMN_COUNT)
     return board
예제 #11
0
 def __init__(self):
     self.board = BoardUtils.createBoard(Constants.EMPTY, Constants.ROW_COUNT, Constants.COLUMN_COUNT)