import test_helper import unittest, piece, board class TestCase (unittest.TestCase): def test_bishop (self): self.assertEquals ("b", piece.Bishop(None,piece.BLACK).code()) self.assertEquals ("B", piece.Bishop(None,piece.WHITE).code()) b = board.GameState() bw = piece.Bishop(b, piece.WHITE) b.putPieceAt(bw, (3,3)) pb = piece.Pawn(b, piece.BLACK) b.putPieceAt(pb, (5,5)) pb = piece.Pawn(b, piece.BLACK) b.putPieceAt(pb, (4,1)) pw = piece.Pawn(b, piece.WHITE) b.putPieceAt(pw, (1,1)) print board.dumpBoard(b) print "B moves=", bw.moves() def test_king (self): self.assertEquals ("k", piece.King(None,piece.BLACK).code()) self.assertEquals ("K", piece.King(None,piece.WHITE).code()) b = board.GameState() kw = piece.King(b, piece.WHITE) b.putPieceAt(kw, (0,0)) kb = piece.King(b, piece.BLACK) b.putPieceAt(kb, (3,3)) pb = piece.Pawn(b, piece.BLACK) b.putPieceAt(pb, (4,4)) pb = piece.Pawn(b, piece.BLACK) b.putPieceAt(pb, (1,1)) pw = piece.Pawn(b, piece.WHITE) b.putPieceAt(pw, (0,1)) print board.dumpBoard(b) print "K moves=", kw.moves() print "k moves=", kb.moves() def test_knight (self): self.assertEquals ("n", piece.Knight(None,piece.BLACK).code()) self.assertEquals ("N", piece.Knight(None,piece.WHITE).code()) b = board.GameState() kw = piece.Knight(b, piece.WHITE) b.putPieceAt(kw, (3,3)) kb = piece.Knight(b, piece.BLACK) b.putPieceAt(kb, (1,0)) pb = piece.Pawn(b, piece.BLACK) b.putPieceAt(pb, (4,5)) pb = piece.Pawn(b, piece.BLACK) b.putPieceAt(pb, (4,1)) pw = piece.Pawn(b, piece.WHITE) b.putPieceAt(pw, (2,1)) print board.dumpBoard(b) print "N moves=", kw.moves() print "n moves=", kb.moves() def test_pawn (self): self.assertEquals ("p", piece.Pawn(None,piece.BLACK).code()) self.assertEquals ("P", piece.Pawn(None,piece.WHITE).code()) b = board.GameState() # simple pawn move pw = piece.Pawn(b, piece.WHITE) b.putPieceAt(pw, (2,1)) # pawn capture pb = piece.Pawn(b, piece.BLACK) b.putPieceAt(pb, (3,2)) print board.dumpBoard(b) print "P moves=", pw.moves() print "p moves=", pb.moves() self.assert_ ((3,2) in pw.moves()) self.assert_ ((2,2) in pw.moves()) self.assert_ ((2,1) in pb.moves()) # pawn promotion def test_pawn_initial_move_pos_white(self): b = board.GameState() pw = piece.Pawn(b, piece.WHITE) b.putPieceAt(pw, board.position('a2')) self.assert_(board.position('a4') in pw.moves(), "pos is %s, moves is %s" \ % (board.position('a4'), pw.moves(),)) def test_pawn_initial_move_pos_black(self): b = board.GameState() pw = piece.Pawn(b, piece.BLACK) b.putPieceAt(pw, board.position('a7')) self.assert_(board.position('a5') in pw.moves()) def test_pawn_initial_move_neg_offsides_white(self): b = board.GameState() pw = piece.Pawn(b, piece.WHITE) b.putPieceAt(pw, board.position('a7')) self.assert_(board.position('a5') not in pw.moves()) def test_pawn_initial_move_neg_offsides_black(self): b = board.GameState() pw = piece.Pawn(b, piece.BLACK) b.putPieceAt(pw, board.position('a2')) self.assert_(board.position('a3') not in pw.moves()) def test_pawn_initial_move_neg_middle_white(self): b = board.GameState() pw = piece.Pawn(b, piece.WHITE) b.putPieceAt(pw, board.position('a3')) self.assert_(board.position('a5') not in pw.moves()) def test_pawn_initial_move_neg_middle_black(self): b = board.GameState() pw = piece.Pawn(b, piece.BLACK) b.putPieceAt(pw, board.position('a5')) self.assert_(board.position('a3') not in pw.moves()) def test_enPassant(self): b = board.GameState() blackPawn = piece.Pawn(b, piece.BLACK) whitePawn = piece.Pawn(b, piece.WHITE) b.putPieceAt(blackPawn, board.position('e4')) b.putPieceAt(whitePawn, board.position('f2')) b.movePiece(b.whereIs(whitePawn), board.position('f4')) self.assert_(board.position('f3') in blackPawn.moves()) def test_enPassant_neg_timelapse(self): b = board.GameState() pos = board.position blackPawn = piece.Pawn(b, piece.BLACK) whitePawn = piece.Pawn(b, piece.WHITE) blackRook = piece.Rook(b, piece.BLACK) whiteRook = piece.Rook(b, piece.WHITE) b.putPieceAt(blackRook, pos('e7')) b.putPieceAt(whiteRook, pos('b5')) b.putPieceAt(blackPawn, pos('e4')) b.putPieceAt(whitePawn, pos('f2')) b.movePiece(b.whereIs(whitePawn), pos('f4')) b.movePiece(b.whereIs(blackRook), pos('e8')) b.movePiece(b.whereIs(whiteRook), pos('b4')) self.assert_(pos('f3') not in blackPawn.moves()) def test_enPassant_neg_shortmove(self): b = board.GameState() blackPawn = piece.Pawn(b, piece.BLACK) whitePawn = piece.Pawn(b, piece.WHITE) b.putPieceAt(blackPawn, board.position('e4')) b.putPieceAt(whitePawn, board.position('f3')) b.movePiece(b.whereIs(whitePawn), board.position('f4')) self.assert_(board.position('f3') not in blackPawn.moves()) def test_queen (self): self.assertEquals ("q", piece.Queen(None,piece.BLACK).code()) self.assertEquals ("Q", piece.Queen(None,piece.WHITE).code()) b = board.GameState() qw = piece.Queen(b, piece.WHITE) b.putPieceAt(qw, (3,3)) qb = piece.Queen(b, piece.BLACK) b.putPieceAt(qb, (0,1)) pb = piece.Pawn(b, piece.BLACK) b.putPieceAt(pb, (5,3)) pb = piece.Pawn(b, piece.BLACK) b.putPieceAt(pb, (4,1)) pb = piece.Pawn(b, piece.BLACK) b.putPieceAt(pb, (0,3)) pw = piece.Pawn(b, piece.WHITE) b.putPieceAt(pw, (1,1)) pw = piece.Pawn(b, piece.WHITE) b.putPieceAt(pw, (3,4)) pw = piece.Pawn(b, piece.WHITE) b.putPieceAt(pw, (3,1)) print board.dumpBoard(b) print "Q moves=", qw.moves() print "q moves=", qb.moves() def test_rook (self): self.assertEquals ("r", piece.Rook(None,piece.BLACK).code()) self.assertEquals ("R", piece.Rook(None,piece.WHITE).code()) b = board.GameState() rw = piece.Rook(b, piece.WHITE) b.putPieceAt(rw, (3,3)) pb = piece.Pawn(b, piece.BLACK) b.putPieceAt(pb, (5,3)) pb = piece.Pawn(b, piece.BLACK) b.putPieceAt(pb, (4,1)) pw = piece.Pawn(b, piece.WHITE) b.putPieceAt(pw, (1,3)) print board.dumpBoard(b) print "R moves=", rw.moves() def test_attacks (self): pos = board.position b = board.GameState() pr = piece.Rook(b,piece.WHITE) b.putPieceAt(pr, (3,3)) self.assertEquals (pr.attacks(), pr.moves()) # pawns attack differently than they move p = piece.Pawn(b,piece.WHITE) b.putPieceAt(p, pos('e2')) self.assert_ (pos('d3') in p.attacks()) self.assert_ (pos('f3') in p.attacks()) self.assert_ (pos('e3') in p.moves()) self.assert_ (pos('e4') in p.moves()) self.assert_ (pos('d3') not in p.moves()) self.assert_ (pos('f3') not in p.moves()) self.assert_ (pos('e3') not in p.attacks()) self.assert_ (pos('e4') not in p.attacks()) def test_ray(self): b = board.GameState() pm = piece.MoveablePiece(b,piece.WHITE) print "Ray: ", for p in pm.ray((3,3),(-1,1)): print p, print self.assertEquals(list(pm.ray((3,3),(-1,1))),[(2,4),(1,5),(0,6)]) if __name__=="__main__": unittest.main ()