コード例 #1
0
    def move(self, n=1):
        n = int(n)

        self.controller.moveSnake(n)

        playerBoard = self.controller.getPlayer().getGameBoard()
        BoardUtils.printBoardV4(playerBoard.getBoard())
コード例 #2
0
    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)
コード例 #3
0
 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
コード例 #4
0
 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
コード例 #5
0
 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
コード例 #6
0
 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)