Esempio n. 1
0
def verify_move():
    content = request.json
    boardArray = [int(str(element)) for element in content['data']]

    # try:
    #     current_board = pickle.load(open("current_board.pkl", "rb"))
    # except:
    current_board = Board()
    pickle.dump(current_board, open("current_board.pkl", "wb"))

    verified = current_board.verifyMove(RED, next_board = Board(new_array = boardArray))
    if(verified):
        pickle.dump(Board(new_array = boardArray), open("current_board.pkl", "wb"))

    return json.dumps(verified)
Esempio n. 2
0
class BoardTestCase(ut.TestCase):
	def setUp(self):
		self.board = Board()

	def tearDown(self):
		self.board = None

	def testGetMovesList(self):

		def testWithGrid(grid = None, red_c = 7, black_c = 7):
			self.board = Board(new_grid = grid)
			self.board.getMoveList(RED)
			self.board.getMoveList(BLACK)

			# self.board.printBoard()
			# print "red moves:", len(self.board.getMoveList(RED))
			# print "black moves:", len(self.board.getMoveList(BLACK))

			self.assertEqual(len(self.board.getMoveList(RED)), red_c, \
				'incorrect number of RED moves available')

			self.assertEqual(len(self.board.getMoveList(BLACK)), black_c, \
				'incorrect number of BLACK moves available')

			# for board, move in self.board.getMoveList(BLACK) + self.board.getMoveList(RED):
				# print
				# move.printMove()
				# board.printBoard()


		# ---------- My Testing -----------------
		# self.board = Board(new_grid = RED_EASY_LOOKAHEAD)
		# for bd in self.board.getMoveList(RED):
		# 	bd[0].printBoard()


		testWithGrid()
		testWithGrid(KINGS, 6, 1)
		testWithGrid(CORNER, 6, 2)

		self.board = Board(new_grid = NEW_KING)
		test_board = Board(new_grid = NEW_KING_RESULT)
		
		self.assertTrue(any(bd[0] == test_board for bd in self.board.getMoveList(RED)))


	def testApplyMove(self):
		for board, move in self.board.getMoveList(BLACK) + self.board.getMoveList(RED):
			self.assertEqual(board, self.board.applyMove(move), \
				'move_board does not match move applied to self.board')

		self.board = Board(new_grid = NEW_KING)
		for board, move in self.board.getMoveList(BLACK) + self.board.getMoveList(RED):
			self.assertEqual(board, self.board.applyMove(move), \
				'move_board does not match move applied to self.board')

		self.board = Board(new_grid = BLK_DOUBLE)
		for board, move in self.board.getMoveList(BLACK) + self.board.getMoveList(RED):
			self.assertEqual(board, self.board.applyMove(move), \
				'move_board does not match move applied to self.board')

	def testVerifyMove(self):
		'''
		For board verify that a move is valid and in the set of moves
		'''
		# self.board = Board(new_grid = START_MOVE_B_9_13).getInverse()
		self.board.getMoveList(RED)
		self.board.getMoveList(BLACK)

		self.assertTrue(self.board.verifyMove(BLACK, next_board = Board(new_grid = START_MOVE_B_9_13)))
		self.assertTrue(self.board.verifyMove(RED, next_board = Board(new_grid = START_MOVE_R_21_17)))

	def testGetInverse(self):
		self.assertEqual(self.board.getInverse().getInverse(), self.board, \
			'inverse of inverse of board is not original board')