Exemple #1
0
    def test_get_set(self):
        """Tests the get and set methods."""
        pos = chess.Position()
        self.assertEqual(pos["b1"], chess.Piece("N"))

        del pos["e2"]
        self.assertEqual(pos[chess.Square("e2")], None)

        pos[chess.Square("e4")] = chess.Piece("r")
        self.assertEqual(pos["e4"], chess.Piece("r"))
Exemple #2
0
	def test_creation(self):
		"""Tests creation of Square instances."""
		self.assertEqual(chess.Square.from_x_and_y(3, 5), chess.Square("d6"))

		# endian issue
		#self.assertEqual(chess.Square.from_x88(2), chess.Square("c8"))

		self.assertEqual(chess.Square.from_x88(2), chess.Square("c1"))

		self.assertEqual(chess.Square.from_rank_and_file(rank=2, file="g"), chess.Square("g2"))
Exemple #3
0
	def test_equality(self):
		"""Tests the overriden equality behaviour of the Square class."""
		a = chess.Square("b4")
		b = chess.Square("b4")
		c = chess.Square("b3")
		d = chess.Square("f3")

		self.assertEqual(a, b)
		self.assertEqual(b, a)

		self.assertNotEqual(a, c)
		self.assertNotEqual(a, d)
		self.assertNotEqual(c, d)
Exemple #4
0
 def test_default_position(self):
     """Tests the default position."""
     pos = chess.Position()
     self.assertEqual(pos[chess.Square('b1')], chess.Piece('N'))
     self.assertEqual(
         pos.fen,
         "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
     self.assertEqual(pos.turn, "w")
Exemple #5
0
 def move(self, move: str):
     if not self.game_over:
         if not (3 < len(move) < 6):
             print("Invalid format")
         pos = chess.string_to_coords(move[:2])
         start = chess.Square(pos[0], pos[1])
         new_pos = chess.string_to_coords(move[2:4])
         end = chess.Square(new_pos[0], new_pos[1])
         prom = None
         if len(move) == 5:
             prom = move[4]
         move_o = chess.Move(start, end, prom=prom)
         if move_o in self.possible_moves:
             self.game = self.game.move(move_o)
             self.move_list.append(move)
             self.after_move()
             return True
     return False
Exemple #6
0
	def test_simple_properties(self):
		"""Tests simple properties of Square objects."""
		f7 = chess.Square("f7")
		self.assertFalse(f7.is_dark())
		self.assertTrue(f7.is_light())

		#XXX rank changed to return string
		self.assertEqual(f7.rank, '7')

		self.assertEqual(f7.file, 'f')
		self.assertEqual(f7.name, 'f7')

		
		# XXX joy joy I have enidaness has changed
		#self.assertEqual(f7.x88, 21)

		self.assertEqual(f7.x, 5)
		self.assertEqual(f7.y, 6)
		self.assertFalse(f7.is_backrank())
Exemple #7
0
def piece_safety(board):
    """
    Heuristic trying to assess the safety of pieces. Looks at each piece on the board, gets a list
    of attacking and defending pieces, decides whether or not the piece is safe. Quite costly and
    slow, but could be interesting to see whether or not this feature (that is usually worked out by
    searching deeper) can be of any use done oftly.

    :param board: the current state, oh wonder
    :return: A value summerizing the safety of pieces
        Note: Pieces near the center of the board are considered more important
    """
    result = 0
    for state, occupation in board.piece_map().items():
        square = chess.Square(state)
        white_watchers = [
            SIMPLE_MATERIAL_VALUES[board.piece_at(sq).piece_type]
            for sq in board.attackers(color=chess.WHITE, square=square)
        ]
        black_watchers = [
            SIMPLE_MATERIAL_VALUES[board.piece_at(sq).piece_type]
            for sq in board.attackers(color=chess.BLACK, square=square)
        ]
        try:
            decision = score_single_square(chess.WHITE,
                                           SIMPLE_MATERIAL_VALUES[occupation.piece_type],
                                           attackers=black_watchers, defenders=white_watchers)\
                if occupation.color else score_single_square(chess.BLACK,
                                                             SIMPLE_MATERIAL_VALUES[
                                                                 occupation.piece_type],
                                                             attackers=white_watchers,
                                                             defenders=black_watchers)
            factor = 1 if decision == chess.WHITE else -1
            result += factor * SQUARE_VALUES[square]
        except AttributeError:
            pass
    return result
Exemple #8
0
	def test_iteration(self):
		"""Tests iteration over all squares."""
		self.assertTrue(chess.Square("h8") in chess.Square.get_all())
		self.assertTrue(chess.Square("b1") in chess.Square.get_all())
Exemple #9
0
    'n': -781,
    'b': -825,
    'r': -1276,
    'q': -2538,
    'k': -32000,
    'P': 126,
    'N': 781,
    'B': 825,
    'R': 1276,
    'Q': 2538,
    'K': 32000
}

SIDE_TO_PLAY = {chess.WHITE: 50000, chess.BLACK: -50000}

SQUARES = [chess.Square(s) for s in range(64)]

# go through the file passed on the cmd line
if len(sys.argv) < 2:
    print("Must pass file to parse on the command line")
    exit(-1)

file = sys.argv[1]

seen_fens = set()

with open(file, 'r') as fp:
    for line in fp.readlines():
        score, fen = line.split(":")

        if fen in seen_fens: