def testValidMoves(self): start = CheckerBoardLocation(3, 5) end = CheckerBoardLocation(2, 4) piece = self.game.board.get_piece(start) self.game.move_piece((start, end)) self.game.board.get_piece(end) self.assertIs(piece, self.game.board.get_piece(end))
def testInvalidMoves(self): start = CheckerBoardLocation(3, 5) # Must have a start and end square. self.assertRaises(InvalidCheckerMoveException, self.game.move_piece, (start, )) # Black piece can only move one space end = CheckerBoardLocation(1, 3) self.assertRaises(InvalidCheckerMoveException, self.game.move_piece, (start, end))
def get_board_location(self, xy): loc = CheckerBoardLocation(0, 0) for col in range(self.BOARD_SIZE): current_square_start = PyGameCheckerBoard.BORDER_SIZE_PX + (PyGameCheckerBoard.SQUARE_SIZE_PX * col) next_square_start = PyGameCheckerBoard.BORDER_SIZE_PX + (PyGameCheckerBoard.SQUARE_SIZE_PX * (col + 1)) if current_square_start <= xy[0] < next_square_start: loc.col = col break for row in range(self.BOARD_SIZE): current_square_start = PyGameCheckerBoard.BORDER_SIZE_PX + (PyGameCheckerBoard.SQUARE_SIZE_PX * row) next_square_start = PyGameCheckerBoard.BORDER_SIZE_PX + (PyGameCheckerBoard.SQUARE_SIZE_PX * (row + 1)) if current_square_start <= xy[1] < next_square_start: loc.row = row break return loc
def testCheckerBoardSetup(self): # Should have a usable 8x8 checkerboard. board = self.board board_size = CheckerBoard.BOARD_SIZE rows_of_pieces = CheckerBoard.ROWS_OF_PIECES self.assertEqual(board_size, 8, "Checkerboard is not 8x8!") self.assertEqual(rows_of_pieces, 3, "Checkerboard is not 8x8!") red_rows = range(board_size)[:rows_of_pieces] black_rows = range(board_size)[-rows_of_pieces:] for col in xrange(board_size): # Red pieces should be at the top for row in red_rows + black_rows: if (col + row) % 2 == 0: board_location = CheckerBoardLocation(col, row) piece = board.get_piece(board_location) self.assertEqual( piece.rank, CheckerPiece.Rank.REGULAR, "Not a regular piece at location: %s" % board_location) self.assertNotEqual(piece, CheckerBoard.EmptySpace) if row in red_rows: self.assertEqual( piece.player, CheckerPiece.Player.RED, "Red piece missing from location: %s" % board_location) elif row in black_rows: self.assertEqual( piece.player, CheckerPiece.Player.BLACK, "Black piece missing from location: %s" % board_location)
def get_board_location(self, xy): loc = CheckerBoardLocation(0, 0) for col in range(self.BOARD_SIZE): current_square_start = PyGameCheckerBoard.BORDER_SIZE_PX + ( PyGameCheckerBoard.SQUARE_SIZE_PX * col) next_square_start = PyGameCheckerBoard.BORDER_SIZE_PX + ( PyGameCheckerBoard.SQUARE_SIZE_PX * (col + 1)) if current_square_start <= xy[0] < next_square_start: loc.col = col break for row in range(self.BOARD_SIZE): current_square_start = PyGameCheckerBoard.BORDER_SIZE_PX + ( PyGameCheckerBoard.SQUARE_SIZE_PX * row) next_square_start = PyGameCheckerBoard.BORDER_SIZE_PX + ( PyGameCheckerBoard.SQUARE_SIZE_PX * (row + 1)) if current_square_start <= xy[1] < next_square_start: loc.row = row break return loc
def display(self): print " ".join([" "] + [str(item) for item in range(self.BOARD_SIZE)]) ctr = 0 output = [] for row in range(self.BOARD_SIZE): l = [str(ctr)] + [str(self.get_piece(CheckerBoardLocation(col, row))) for col in range(self.BOARD_SIZE)] output.append(" ".join(l)) ctr += 1 print "\n".join(output)
def get_move(self): move_list = [] moves = raw_input("What is your move Player %s? " % self.current_player.name) unparsed = moves.split('-') for m in unparsed: move = m.split('.') move_list.append(CheckerBoardLocation(int(move[0]), int(move[1]))) try: self.move_piece(move_list) except Exception as ex: print ex.message
def display(self): for row in range(self.BOARD_SIZE): for col in range(self.BOARD_SIZE): loc = CheckerBoardLocation(col, row) if self.is_square_playable(loc): px = PyGameCheckerBoard.get_pixel_coords(loc) pygame.draw.rect(self.DISPLAYSURF, PyGameCheckerBoard.BOARD_COLOR, (px, (PyGameCheckerBoard.SQUARE_SIZE_PX, PyGameCheckerBoard.SQUARE_SIZE_PX))) piece = self.get_piece(loc) if piece != self.EmptySpace: piece.display(px) pygame.display.update()