Exemple #1
0
    def test_pawn_taking(self):
        fen = "rnbqkbnr/pppppppp/8/8/8/Pr6/PPPPPPPP/RNBQKBNR w KQkq - 0 1"

        game = Game(fen)

        self.assertSetEqual(game.valid_ends('a2'), self._squarify(['b3']))
        self.assertSetEqual(game.valid_ends('c2'), self._squarify(['b3', 'c3', 'c4']))
Exemple #2
0
    def test_castling_out_of_check(self):
        game = Game('4rk2/8/8/8/8/8/8/R3K2R w KQ - 0 1')

        self.assertSetEqual(game.board.check_status(), set("w"))
        self.assertNotIn(BoardSquare('c1'), game.valid_ends('e1'), 'Attempting to castle out of check')
        self.assertNotIn(BoardSquare('g1'), game.valid_ends('e1'), 'Attempting to castle out of check')
        self.assertSetEqual(game.valid_ends('e1'), self._squarify(['d2', 'f2', 'd1', 'f1']))
Exemple #3
0
    def test_en_passant_taking(self):
        game = Game('rnbqkbnr/pppppppp/8/8/p7/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')

        game = game.move(BasicMove('b2', 'b4'))
        self.assertEqual(game.en_passant, BoardSquare('b3'))
        game = game.move(BasicMove('a4', 'b3'))
        self.assertEqual(game.en_passant, None)
        self.assertEqual(game.board.piece_at_board_square(BoardSquare('b4')), None)
Exemple #4
0
    def test_moving_no_piece(self):
        game = Game()

        try:
            empty_square = 'c5'
            game.valid_ends(empty_square)
            self.fail('valid_ends expected to have raised NoPieceAtSquareException for moves from %s' % start)
        except NoPieceAtSquareException:
            pass
Exemple #5
0
    def test_castling_state(self):
        game = Game(self.PAWNLESS_FEN)

        self.assertEqual(game.castling.fen(), 'KQkq')
        game = game.move(BasicMove('a1', 'a2'))
        self.assertEqual(game.castling.fen(), 'Kkq')
        game = game.move(BasicMove('e8', 'e7'))
        self.assertEqual(game.castling.fen(), 'K')
        game = game.move(BasicMove('e1', 'e2'))
        self.assertEqual(game.castling.fen(), '-')
Exemple #6
0
    def test_castling(self):
        game = Game('r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w KQkq - 0 1')

        self.assertSetEqual(game.valid_ends('e1'), self._squarify({'c1', 'd1', 'f1', 'g1'}))

        new_game = game.move(BasicMove('e1', 'c1'))
        self.assertEqual(new_game.fen(), 'r3k2r/pppppppp/8/8/8/8/PPPPPPPP/2KR3R b kq - 1 1')

        new_game = game.move(BasicMove('e1', 'g1'))
        self.assertEqual(new_game.fen(), 'r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R4RK1 b kq - 1 1')
Exemple #7
0
    def test_must_leave_check(self):
        game = Game('r6r/k7/8/8/8/8/8/R3K2R b KQ - 1 1')

        self.assertSetEqual(game.board.check_status(), set(['b']))

        #Can't move out of check with either rook
        self.assertSetEqual(game.valid_ends('a8'), set())
        self.assertSetEqual(game.valid_ends('a8'), set())
        #Only 3 of 5 king moves possible
        self.assertSetEqual(game.valid_ends('a7'), self._squarify({'b8', 'b7', 'b6'}))
Exemple #8
0
async def on_reaction_add(reaction, user):
    if user == client.user:
        return
    elif "challenge" in reaction.message.content and user in reaction.message.mentions:
        if "🏳️" == str(reaction):
            await reaction.message.delete()
        elif "🏴" == str(reaction):
            if reaction.message.author in reaction.message.mentions:
                await reaction.message.channel.send(
                    f"Sorry {reaction.message.author.name}! you can't challenge yourself"
                )
                await reaction.message.delete()
                return
            challengee_count = len(reaction.message.mentions)
            async for user in reaction.users():
                if user in reaction.message.mentions:
                    challengee_count -= 1
            if 0 == challengee_count:
                with TemporaryFile(mode="w+") as match_file:
                    try:
                        players = reaction.message.mentions + [
                            reaction.message.author
                        ]
                        contestants = map(lambda x: claimed[x.id], players)
                        random.shuffle(players)
                        match = Game(
                            4, **{
                                name: pos
                                for name, pos in zip(
                                    contestants,
                                    choose_positions(len(players), 4))
                            })
                        winner = match.play(training=False,
                                            reporting=True,
                                            file=match_file)
                        match_file.seek(0)
                        byte_stream = io.BytesIO(
                            match_file.read().encode("utf-8"))
                        await reaction.message.channel.send(
                            f"Congratulations `{winner.name}`! Here's a full breakdown of the match",
                            file=discord.File(
                                byte_stream,
                                f"{datetime.now().strftime('%d/%m/%y_%H/%M/%S')}.txt"
                            ))
                        await reaction.message.delete()
                    except KeyError:
                        await reaction.message.channel.send(
                            f"Sorry! looks like someone doesen't have a Bmiibo yet"
                        )
Exemple #9
0
    def test_square_fetch(self):
        game = Game()

        def f(file_rank):
            sq = BoardSquare(file_rank)
            return game.board.piece_at_board_square(sq)

        self.assertEqual(f('d4'), None)
        self.assertEqual(f('a1'), "R")
        self.assertEqual(f('f7'), "p")
Exemple #10
0
    def test_en_passant_updating(self):
        game = Game(self.STARTING_FEN)

        self.assertEqual(game.en_passant, None)
        game = game.move(BasicMove('e2', 'e4'))
        self.assertEqual(game.en_passant, BoardSquare('e3'))
        game = game.move(BasicMove('c7', 'c5'))
        self.assertEqual(game.en_passant, BoardSquare('c6'))
        game = game.move(BasicMove('g1', 'f3'))
        self.assertEqual(game.en_passant, None)
        game = game.move(BasicMove('a7', 'a5'))
        self.assertEqual(game.en_passant, BoardSquare('a6'))
Exemple #11
0
    def test_basic_check(self):
        #Game in check
        game = Game('rnbqkbnr/8/8/1B6/8/8/8/RNBQK1NR b KQkq - 1 1')

        self.assertSetEqual(game.board.check_status(), set(['b']))
        #Can't use Rook to get out of check
        self.assertSetEqual(game.valid_ends('a8'), set())
        #Only one place to move the Queen - in the way
        self.assertSetEqual(game.valid_ends('d8'), self._squarify(['d7']))

        #Make that move to be out of check
        game = game.move(BasicMove('d8', 'd7'))
        #Dummy move by white to get the turn back to black
        game = game.move(BasicMove('a1', 'a2'))
        #Now we can only move that Queen to things still in the way...
        self.assertSetEqual(game.valid_ends('d7'), self._squarify(['b5', 'c6']))
Exemple #12
0
    def test_promotion(self):
        game = Game('4k3/P7/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')

        try:
            game = game.move(BasicMove('a7', 'a8'))
            self.fail('Should not accept promotion move without promotion data')
        except MoveMissingPromotionException:
            pass

        try:
            game.move(BasicMove('a7', 'a8', 'LLAMA'))
            self.fail('Should not accept non-promotion move with promotion data')
        except InvalidPromotionDataException:
            pass

        game = game.move(BasicMove('a7', 'a8', 'Q'))
        self.assertEqual(game.fen(), 'Q3k3/8/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1')

        try:
            game.move(BasicMove('e8', 'e7', 'Q'))
            self.fail('Should not accept non-promotion move with promotion data')
        except InvalidPromotionDataException:
            pass
Exemple #13
0
    def __init__(self):
        self.chess = Game.Chess()
        self.root = Tk()
        self.root.protocol("WM_DELETE_WINDOW", self.close)
        self.root.geometry("920x740")
        self.root.iconbitmap('icon.ico')
        self.root.resizable(0, 0)
        self.root.title("Classic Chess")
        self.timer = None

        self.left = Frame(self.root)
        self.bottom = Frame(self.root)
        self.right = Frame(self.root)
        self.game = Frame(self.root)
        self.lastmovements = []
        self.piecesposition = {}

        self.table()
        mainloop()
Exemple #14
0
    def test_valid_ends(self):
        game = Game()

        self.assertSetEqual(game.valid_ends('a1'), set())
        self.assertSetEqual(game.valid_ends('a2'), self._squarify({'a3', 'a4'}))
        self.assertSetEqual(game.valid_ends('b2'), self._squarify({'b3', 'b4'}))
        self.assertSetEqual(game.valid_ends('b1'), self._squarify({'a3', 'c3'}))
        self.assertSetEqual(game.valid_ends('c1'), set())
        self.assertSetEqual(Game(self.SIMPLE_BISHOP_FEN).valid_ends('d1'),
                {BoardSquare('c2'),
                 BoardSquare('b3'),
                 BoardSquare('a4'),
                 BoardSquare('e2'),
                 BoardSquare('f3'),
                 BoardSquare('g4'),
                 BoardSquare('h5')})
Exemple #15
0
    def test_move_counters(self):
        game = Game(self.STARTING_FEN)

        self.assertEqual(game.halfmove, 0)
        self.assertEqual(game.fullmove, 1)
        game = game.move(BasicMove('e2', 'e4'))
        self.assertEqual(game.halfmove, 0)
        self.assertEqual(game.fullmove, 1)
        game = game.move(BasicMove('c7', 'c5'))
        self.assertEqual(game.halfmove, 0)
        self.assertEqual(game.fullmove, 2)
        game = game.move(BasicMove('g1', 'f3'))
        self.assertEqual(game.halfmove, 1)
        self.assertEqual(game.fullmove, 2)
        game = game.move(BasicMove('a7', 'a5'))
        self.assertEqual(game.halfmove, 0)
        self.assertEqual(game.fullmove, 3)
Exemple #16
0
#!/usr/bin/env python3

"""
Chess: tests
running_game.py
desc: tests the integrity of a game that has started and is running
vers: 0.0.1
"""

import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from chess import Game

game = Game()
print(game)
Exemple #17
0
#!/usr/bin/env python3
from chess import Game

if __name__ == "__main__":
    chess = Game()
#!/usr/bin/env python3

"""
Chess: tests
print_board.py
desc: print the board
vers: 0.0.1
"""

import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from chess import Game

game = Game()
game.print_help()
Exemple #19
0
    def test_move(self):
        game = Game()

        new_game = game.move(BasicMove('a2', 'a4'))

        self.assertEqual(new_game.fen(), "rnbqkbnr/pppppppp/8/8/P7/8/1PPPPPPP/RNBQKBNR b KQkq a3 0 1")
from tkinter import *
import time

Board = [[' ', ' ', ' ', ' ', 'k', ' ', ' ', ' '],
         [' ', ' ', ' ', 'p', ' ', 'p', ' ', ' '],
         [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
         [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
         [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
         [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
         [' ', ' ', ' ', ' ', 'P', ' ', ' ', ' '],
         [' ', ' ', ' ', ' ', 'K', ' ', ' ', ' ']]

Board = INITIAL_POSITION
# Board = KING_AND_PAWS_POSITION
game = Game(Board, True)

originClick = None
destClick = None
ComputerIsMoving = False
IsGameEnd = False

canvas_width = 500
square_width = canvas_width / 8

root = tk.Tk()
images = []

canvas = Canvas(root, width=square_width * 8, height=(200 + square_width * 8))

Exemple #21
0
        type=str,
        nargs="+",
        action="extend",
        help="the Bmiibos that will take part in these matches")
    args = parser.parse_args()

    if args.basic_training:
        wins = {"basic_one": 0, "basic_two": 0}
        if args.match_file:
            output = open(args.match_file, "w")
        else:
            output = None
        for i in range(args.matches):
            print("match", i + 1)
            game = Game(args.board_size,
                        basic_one=(0, 0),
                        basic_two=(args.board_size - 1, args.board_size - 1))
            winner = game.play(reporting=args.reporting, file=output)
            wins[winner.name] += 1
            for player in game.players + game.dead:
                player.brain.save()
        if output:
            output.close()
        print("basic_one winrate:", (wins["basic_one"] / args.matches) * 100)
        print("basic_two winrate:", (wins["basic_two"] / args.matches) * 100)
    elif args.train_discord_bmiibos:
        if args.match_file:
            output = open(args.match_file, "w")
        else:
            output = None
        with open("training.json", "r") as training_file:
Exemple #22
0
 def test(fen):
     game = Game(fen)
     self.assertEqual(game.fen(), fen)
Exemple #23
0
from chess import Game, GameOver
from pgn_parser import parse

games = parse()

#print(games)
i = 1
for game in games[5:6]:
    test_game = Game()
    print(f"================Game #{i}====================")
    for move in game:
        try:
            print(move)
            test_game.move(move)
            test_game.board.print_board()
        except GameOver:
            test_game.board.print_board()
            print(f"================Game #{i} OVER====================")
            print(len(games))
            i += 1
            break
Exemple #24
0
    def test_pawn_starting_move(self):
        fen = "rnbqkbnr/pppppppp/8/8/8/Pr6/PPPPPPPP/RNBQKBNR w KQkq - 0 1"

        game = Game(fen)
        self.assertEqual(game.valid_ends('b2'), set())
Exemple #25
0
    def test_not_moving_into_check(self):
        game = Game('1k6/8/8/8/8/8/8/R1RK4 b - - 1 1')

        self.assertSetEqual(game.board.check_status(), set())

        self.assertSetEqual(game.valid_ends('b8'), self._squarify(['b7']))
Exemple #26
0
    def test_castling_through_check(self):
        game = Game('3rkr2/8/8/8/8/8/8/R3K2R w KQ - 0 1')

        self.assertSetEqual(game.board.check_status(), set())
        self.assertSetEqual(game.valid_ends('e1'), self._squarify(['e2']))
Exemple #27
0
    def test_starting(self):
        game = Game()

        self.assertEqual(game.fen(), self.STARTING_FEN)
Exemple #28
0
#!/usr/bin/env python3

"""
Chess: tests
print_board.py
desc: print the board
vers: 0.0.1
"""

import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from chess import Game

game = Game()
print(game.board)
game.move(1, "D2", "D4")
print(game.board)
game.move(0, "E7", "E5")
print(game.board)
game.move(1, "D4", "E5")
print(game.board)