def test_ai_vs_ai(self): #print some_game = chesslib.game(wplayer='ai',bplayer='ai',logfile='d:\\temp\\aiaigametest.txt') some_game.zboard.piecefy({'h8': ' ', 'h2': ' ', 'h3': 'wp', 'h1': 'wr', 'h6': ' ', 'h7': 'bp', 'h4': ' ', 'h5': ' ', 'd8': 'bk', 'a8': 'br', 'd6': 'bp', 'd7': ' ', 'd4': ' ', 'd5': ' ', 'd2': ' ', 'd3': ' ', 'd1': 'wr', 'g7': 'bq', 'g6': ' ', 'g5': ' ', 'g4': ' ', 'g3': 'wp', 'g2': 'wp', 'g1': ' ', 'g8': ' ', 'c8': 'bb', 'c3': 'wn', 'c2': 'wp', 'c1': 'wk', 'c7': ' ', 'c6': 'bp', 'c5': ' ', 'c4': ' ', 'f1': ' ', 'f2': ' ', 'f3': 'wq', 'f4': ' ', 'f5': ' ', 'f6': ' ', 'f7': 'wb', 'f8': ' ', 'b4': ' ', 'b5': ' ', 'b6': ' ', 'b7': 'bp', 'b1': ' ', 'b2': 'wp', 'b3': ' ', 'b8': 'bn', 'a1': ' ', 'a3': ' ', 'a2': 'wp', 'a5': ' ', 'e8': ' ', 'a7': 'bp', 'a6': ' ', 'e5': 'wp', 'e4': ' ', 'e7': 'bp', 'e6': ' ', 'e1': ' ', 'e3': ' ', 'e2': ' ', 'a4': ' '}) zgame = chesslib.game(wplayer='ai',bplayer='ai',logfile='d:\\temp\\aiaigametest.txt') zgame.zboard = copy.deepcopy(some_game.zboard) zgame.cycle(aidepth=2,verbose=0)
def test_game_cycle_stalemate(self): zgame = chesslib.game() # Stalemate zgame.zboard.piecefy({'h8': ' ', 'h2': ' ', 'h3': ' ', 'h1': 'wr', 'h6': ' ', 'h7': ' ', 'h4': ' ', 'h5': ' ', 'd8': ' ', 'a8': 'br', 'd6': ' ', 'd7': 'bb', 'd4': ' ', 'd5': ' ', 'd2': ' ', 'd3': ' ', 'd1': ' ', 'g7': ' ', 'g6': ' ', 'g5': ' ', 'g4': ' ', 'g3': ' ', 'g2': ' ', 'g1': ' ', 'g8': ' ', 'c8': ' ', 'c3': 'bn', 'c2': ' ', 'c1': ' ', 'c7': ' ', 'c6': ' ', 'c5': ' ', 'c4': ' ', 'f1': ' ', 'f2': ' ', 'f3': ' ', 'f4': ' ', 'f5': ' ', 'f6': ' ', 'f7': ' ', 'f8': ' ', 'b4': ' ', 'b5': 'wb', 'b6': ' ', 'b7': 'wr', 'b1': ' ', 'b2': ' ', 'b3': ' ', 'b8': ' ', 'a1': 'wr', 'a3': ' ', 'a2': ' ', 'a5': ' ', 'e8': 'bk', 'a7': ' ', 'a6': ' ', 'e5': 'bq', 'e4': 'wn', 'e7': ' ', 'e6': ' ', 'e1': 'wk', 'e3': ' ', 'e2': ' ', 'a4': ' '}) self.assertEqual('1/2-1/2',zgame.cycle(['Bxd7','Kd8','Rh7','Ra2','Rxa2','Qa5','Rxa5','Ne2','Kxe2','exit'],0,verbose=0)) #stalemate # Draws zgame = chesslib.game() zgame.zboard.piecefy({'h8': ' ', 'h2': ' ', 'h3': ' ', 'h1': ' ', 'h6': ' ', 'h7': ' ', 'h4': ' ', 'h5': ' ', 'd8': ' ', 'a8': ' ', 'd6': ' ', 'd7': ' ', 'd4': ' ', 'd5': ' ', 'd2': ' ', 'd3': ' ', 'd1': ' ', 'g7': ' ', 'g6': ' ', 'g5': ' ', 'g4': ' ', 'g3': ' ', 'g2': ' ', 'g1': ' ', 'g8': ' ', 'c8': ' ', 'c3': ' ', 'c2': ' ', 'c1': ' ', 'c7': ' ', 'c6': ' ', 'c5': ' ', 'c4': ' ', 'f1': ' ', 'f2': ' ', 'f3': ' ', 'f4': ' ', 'f5': ' ', 'f6': ' ', 'f7': ' ', 'f8': ' ', 'b4': ' ', 'b5': ' ', 'b6': ' ', 'b7': ' ', 'b1': ' ', 'b2': ' ', 'b3': ' ', 'b8': ' ', 'a1': ' ', 'a3': ' ', 'a2': ' ', 'a5': ' ', 'e8': 'bk', 'a7': ' ', 'a6': ' ', 'e5': 'bq', 'e4': ' ', 'e7': ' ', 'e6': ' ', 'e1': 'wk', 'e3': ' ', 'e2': 'wq', 'a4': ' '}) self.assertEqual('1/2-1/2',zgame.cycle(['Kd1','Qxe2','Kxe2','Ke7'],0,verbose=0))# draw -- should trigger on Kxe2 ### insert here test for King + light piece vs King draws ### # Repetition Draw zgame = chesslib.game() zgame.zboard.piecefy({'h8': 'bk', 'h2': ' ', 'h3': ' ', 'h1': ' ', 'h6': ' ', 'h7': ' ', 'h4': ' ', 'h5': ' ', 'd8': ' ', 'a8': ' ', 'd6': ' ', 'd7': ' ', 'd4': ' ', 'd5': ' ', 'd2': ' ', 'd3': ' ', 'd1': ' ', 'g7': ' ', 'g6': ' ', 'g5': ' ', 'g4': ' ', 'g3': ' ', 'g2': ' ', 'g1': ' ', 'g8': ' ', 'c8': ' ', 'c3': ' ', 'c2': ' ', 'c1': ' ', 'c7': ' ', 'c6': ' ', 'c5': ' ', 'c4': ' ', 'f1': ' ', 'f2': ' ', 'f3': ' ', 'f4': ' ', 'f5': ' ', 'f6': ' ', 'f7': ' ', 'f8': ' ', 'b4': ' ', 'b5': ' ', 'b6': ' ', 'b7': ' ', 'b1': ' ', 'b2': ' ', 'b3': ' ', 'b8': ' ', 'a1': ' ', 'a3': ' ', 'a2': ' ', 'a5': ' ', 'e8': ' ', 'a7': ' ', 'a6': ' ', 'e5': 'bq', 'e4': ' ', 'e7': ' ', 'e6': ' ', 'e1': 'wk', 'e3': ' ', 'e2': 'wq', 'a4': ' '}) self.assertEqual('1/2-1/2',zgame.cycle(['Kd1','Qd5+','2.Qd2','Qh1+','Qe1','Qd5','4.Qd2','Qh1+','5.Qe1','Qh5+','Qe2','Qh1','Qe1','Qd5','exit'],0,verbose=0))# draw by repetition
def test_game_undo(self): zgame = chesslib.game() zgame.zboard.piecefy({'h8': ' ', 'h2': ' ', 'h3': ' ', 'h1': 'wr', 'h6': ' ', 'h7': ' ', 'h4': ' ', 'h5': ' ', 'd8': ' ', 'a8': 'br', 'd6': ' ', 'd7': 'bb', 'd4': ' ', 'd5': ' ', 'd2': ' ', 'd3': ' ', 'd1': ' ', 'g7': ' ', 'g6': ' ', 'g5': ' ', 'g4': ' ', 'g3': ' ', 'g2': ' ', 'g1': ' ', 'g8': ' ', 'c8': ' ', 'c3': 'bn', 'c2': ' ', 'c1': ' ', 'c7': ' ', 'c6': ' ', 'c5': ' ', 'c4': ' ', 'f1': ' ', 'f2': ' ', 'f3': ' ', 'f4': ' ', 'f5': ' ', 'f6': ' ', 'f7': ' ', 'f8': ' ', 'b4': ' ', 'b5': 'wb', 'b6': ' ', 'b7': 'wr', 'b1': ' ', 'b2': ' ', 'b3': ' ', 'b8': ' ', 'a1': 'wr', 'a3': ' ', 'a2': ' ', 'a5': ' ', 'e8': 'bk', 'a7': ' ', 'a6': ' ', 'e5': 'bq', 'e4': 'wn', 'e7': ' ', 'e6': ' ', 'e1': 'wk', 'e3': ' ', 'e2': ' ', 'a4': ' '}) #print(zgame.show()) zgame.cycle(['Bxd7','Kd8','Rh7','Ra2','Rxa2','Qa5','undo','exit'],0,verbose=0) #zgame.turnundo() #print(zgame.show()) self.assertEqual("""| | | |bk| | | | | | |wr| |wb| | | |wr| | | | | | | | | | | | | | |bq| | | | | | | | |wn| | | | | | |bn| | | | | | |br| | | | | | | | |wr| | | |wk| | | | """,zgame.zboard.show()) #before 'Rxa2' #by black
def test_decode_move(self): zgame = chesslib.game() zgame.zboard.piecefy({'h8': ' ', 'h2': ' ', 'h3': ' ', 'h1': 'wr', 'h6': ' ', 'h7': ' ', 'h4': ' ', 'h5': ' ', 'd8': ' ', 'a8': 'br', 'd6': ' ', 'd7': 'bb', 'd4': ' ', 'd5': ' ', 'd2': ' ', 'd3': ' ', 'd1': ' ', 'g7': ' ', 'g6': ' ', 'g5': ' ', 'g4': ' ', 'g3': ' ', 'g2': ' ', 'g1': ' ', 'g8': ' ', 'c8': ' ', 'c3': 'bn', 'c2': ' ', 'c1': ' ', 'c7': ' ', 'c6': ' ', 'c5': ' ', 'c4': ' ', 'f1': ' ', 'f2': ' ', 'f3': ' ', 'f4': ' ', 'f5': ' ', 'f6': ' ', 'f7': ' ', 'f8': ' ', 'b4': ' ', 'b5': 'wb', 'b6': ' ', 'b7': 'wr', 'b1': ' ', 'b2': ' ', 'b3': ' ', 'b8': ' ', 'a1': 'wr', 'a3': ' ', 'a2': ' ', 'a5': ' ', 'e8': 'bk', 'a7': ' ', 'a6': ' ', 'e5': 'bq', 'e4': 'wn', 'e7': ' ', 'e6': ' ', 'e1': 'wk', 'e3': ' ', 'e2': ' ', 'a4': ' '}) #print('\n'+zgame.zboard.show()) """ |br| | | |bk| | | | | |wr| |bb| | | | | | | | | | | | | | | |wb| | |bq| | | | | | | | |wn| | | | | | |bn| | | | | | | | | | | | | | | |wr| | | |wk| | |wr| """ self.assertEqual((zgame.zboard.piece_by_sq('e1'),'e1','m','f2','Kf2'),zgame.decode_move('13. Kf2',zgame.turnset())) self.assertEqual((zgame.zboard.piece_by_sq('e1'),'e1','c','g1','O-O'),zgame.decode_move('O-O',zgame.turnset())) # O-O-O because it only decodes against the set, without validation self.assertEqual((zgame.zboard.piece_by_sq('e1'),'e1','c','c1','O-O-O'),zgame.decode_move('O-O-O',zgame.turnset())) #print(zgame.show()) self.assertEqual((zgame.zboard.piece_by_sq('b5'),'b5','t','d7','Bxd7'),zgame.decode_move('Bxd7+',zgame.turnset())) zgame.zboard.piecefy({'h8': ' ', 'h2': ' ', 'h3': ' ', 'h1': 'wr', 'h6': ' ', 'h7': 'wp', 'h4': ' ', 'h5': ' ', 'd8': 'bq', 'a8': 'br', 'd6': ' ', 'd7': 'bp', 'd4': ' ', 'd5': ' ', 'd2': 'wp', 'd3': ' ', 'd1': 'wq', 'g7': 'bp', 'g6': ' ', 'g5': 'wp', 'g4': ' ', 'g3': ' ', 'g2': ' ', 'g1': ' ', 'g8': 'bn', 'c8': 'bb', 'c3': 'bn', 'c2': 'wp', 'c1': 'wb', 'c7': 'bp', 'c6': ' ', 'c5': ' ', 'c4': ' ', 'f1': 'wb', 'f2': 'wp', 'f3': ' ', 'f4': ' ', 'f5': 'bp', 'f6': ' ', 'f7': ' ', 'f8': 'bb', 'b4': ' ', 'b5': ' ', 'b6': ' ', 'b7': 'bp', 'b1': 'wn', 'b2': 'wp', 'b3': ' ', 'b8': ' ', 'a1': 'wr', 'a3': ' ', 'a2': 'wp', 'a5': ' ', 'e8': 'bk', 'a7': 'bp', 'a6': ' ', 'e5': ' ', 'e4': 'wn', 'e7': 'bp', 'e6': ' ', 'e1': 'wk', 'e3': ' ', 'e2': 'wp', 'a4': ' '}) """ |br| |bb|bq|bk|bb|bn| | |bp|bp|bp|bp|bp| |bp|wp| | | | | | | | | | | | | | | |bp|wp| | | | | | |wn| | | | | | |bn| | | | | | |wp|wp|wp|wp|wp|wp| | | |wr|wn|wb|wq|wk|wb| |wr| """ #print(zgame.show()) self.assertEqual((zgame.zboard.piece_by_sq('h7'),'h7','p','h8','h8Q'),zgame.decode_move('h8Q',zgame.turnset())) self.assertEqual((zgame.zboard.piece_by_sq('h7'),'h7','+','g8','hxg8N'),zgame.decode_move('hxg8N',zgame.turnset())) #print(zgame.decode_move('Rxh7',zgame.turnset())) # cant promote to King self.assertRaises(chesslib.MoveException, zgame.decode_move, '999. b8',zgame.turnset()) # no pawn to reach b8 self.assertRaises(chesslib.MoveException, zgame.decode_move, '2 fxgdfgdfgsdfg',zgame.turnset()) # ##cannot take own self.assertRaises(chesslib.MoveException, zgame.decode_move, 'Ra3',zgame.turnset()) # invalid #e.p. self.assertEqual((zgame.zboard.piece_by_sq('g5'),'g5','e','f6','gxf6'),zgame.decode_move('gxf6',zgame.turnset()))
def test_game_cycle_ai(self): zgame = chesslib.game(bplayer='ai',logfile='d:\\temp\\aigametest.txt') # using log different from the defailt, so that it doesn't get overwrittent by subsequent test
def test_game_cycle_n_mate(self): zgame = chesslib.game() self.assertEqual('1-0',zgame.cycle(['1. e4','e5','2. Bc4','a6','3. Qf3','b5','Qxf7#'],0,verbose=0))