Exemple #1
0
def brief():
    """Non-interactive version for batch processing (one move per line
    with a . to end)."""
    game = chess.Game(None, None)
    lines = sys.stdin.readlines()
    while game.ok() and lines:
        command = lines.pop(0)
        command = command[:-1]
        words = string.split(command)
        try:
            if command == '':
                pass
            elif command[0] == '#':
                pass
            elif command == '.':
                break
            else:
                move = game.move(command)
                print "%d %s %s %s" % (game.round + 1, \
                                       chess.COLORS[game.turn], command, \
                                       move.longAlgebraic())
                game.advance()
        except chess.Error, e:
            print str(e) + ": " + command
            break
Exemple #2
0
    def open(cls, path):
        pgn_file = PgnFile()
        current_game = None
        in_tags = False

        for line in open(path, 'r'):
            # Decode and strip the line.
            line = line.decode('latin-1').strip()

            # Skip empty lines and comments.
            if not line or line.startswith("%"):
                continue

            # Check for tag lines.
            tag_match = tag_regex.match(line)
            if tag_match:
                tag_name = tag_match.group(1)
                tag_value = tag_match.group(2).replace("\\\\", "\\").replace(
                    "\\[", "]").replace("\\\"", "\"")
                if current_game:
                    if in_tags:
                        current_game.headers[tag_name] = tag_value
                    else:
                        cls.__parse_movetext(current_game, movetext)
                        pgn_file.add_game(current_game)
                        current_game = None
                if not current_game:
                    current_game = chess.Game()
                    current_game.headers[tag_name] = tag_value
                    movetext = ""
                in_tags = True
            # Parse movetext lines.
            else:
                if current_game:
                    movetext += "\n" + line
                    pass
                else:
                    raise chess.PgnError(
                        "Invalid PGN. Expected header before movetext: %s",
                        repr(line))
                in_tags = False

        if current_game:
            cls.__parse_movetext(current_game, movetext)
            pgn_file.add_game(current_game)

        return pgn_file
Exemple #3
0
                        resize_screen(int(event.w / 8.0))
                    print_board(game.board, color)
    except:
        print(format_exc(), file=stderr)
        bug_file = open('bug_report.txt', 'a')
        bug_file.write('----- ' + strftime('%x %X') + ' -----\n')
        bug_file.write(format_exc())
        bug_file.write('\nPlaying as WHITE:\n\t' if color ==
                       chess.WHITE else '\nPlaying as BLACK:\n\t')
        bug_file.write(game.get_move_list() + '\n\t')
        bug_file.write('\n\t'.join(game.position_history))
        bug_file.write('\n-----------------------------\n\n')
        bug_file.close()


game = chess.Game()
arg = parser.parse_args()
if arg.color == 'white':
    play_as(game, chess.WHITE)
else:
    play_as(game, chess.BLACK)
'''
def play_as_white(game=chess.Game()):
    return play_as(game, chess.WHITE)

def play_as_black(game=chess.Game()):
    return play_as(game, chess.BLACK)



def play_random_color(game=chess.Game()):
def play_random_color(game=chess.Game()):
    color = choice([chess.WHITE, chess.BLACK])
    play_as(game, color)
def play_as_black(game=chess.Game()):
    return play_as(game, chess.BLACK)
def play_as_white(game=chess.Game()):
    return play_as(game, chess.WHITE)
Exemple #7
0
def main():
    """Interactive mode.  Type ? for help."""
    game = chess.Game(None, None)
    board = game.board
    for line in game.show():
        print line
    while game.ok():
        print '%s> ' % chess.COLORS[game.turn],
        command = raw_input()
        words = string.split(command)
        print
        try:
            if len(words) == 1 and \
               (command == '?' or command == 'help'):
                print \
"""\
show             Show the current board.
turn             Indicate who's turn it is.
query (move)     Show the results of disambiguating move.
move (move)      Make move.
test (loc)       Show legal moves for loc with *.
info (loc)       Show internal info for loc.
threat (loc)     Show pieces currently threatening loc with #.
dump             Dump current gamestate to stdout.
read             Read current gamestate from stdin.\
"""
            elif command == '':
                pass
            elif command[0] == '#':
                pass  # comment
            elif len(words) == 1 and \
                 (words[0] == 'show' or words[0] == 'board'):
                for line in game.show():
                    print line
            elif len(words) == 1 and words[0] == 'turn':
                print "%s's turn" % chess.COLORS[game.turn]
            elif len(words) == 2 and words[0] == 'query':
                print game.disambiguate(words[1])
            elif len(words) == 2 and words[0] == 'move':
                move = game.move(words[1])
                print "%s moves %s" % \
                      (chess.COLORS[game.turn], move.longAlgebraic())
                if move.capture:
                    print "%s takes %s" % (move.piece.name, move.capture.name)
                for line in game.show():
                    print line
                game.advance()
            elif len(words) == 2 and words[0] == 'test':
                loc = chess.AlgebraicLoc(words[1])
                piece = board.get(loc)
                rep = board.asArray()
                for loc in piece.possible(board):
                    rep[loc.x][loc.y] = '*'
                for line in board.pretty(rep):
                    print line
            elif len(words) == 2 and words[0] == 'info':
                loc = chess.AlgebraicLoc(words[1])
                piece = board.get(loc)
                print "piece %s (%s)" % (piece.name, piece.symbol[piece.color])
                print "location %s" % ` piece.loc `
                print "color %s" % chess.COLORS[piece.color]
                print "moves %d" % piece.moves
                print "idle %d" % piece.idle
            elif len(words) == 2 and words[0] == 'threat':
                loc = chess.AlgebraicLoc(words[1])
                rep = board.asArray()
                color = game.turn
                there = board.at(loc)
                if there:
                    color = there.color
                for piece in board.threat(loc, color):
                    rep[piece.loc.x][piece.loc.y] = '#'
                for line in board.pretty(rep):
                    print line
            elif len(words) == 1 and words[0] == 'possible':
                # List all possible moves.
                pieces = board.findAll(game.turn)
                for piece in pieces:
                    possible = piece.possible(board)
                    if possible:
                        print '%s%s' % (piece, piece.loc), possible
            elif len(words) == 1 and words[0] == 'dump':
                game.save(sys.stdout)
            elif len(words) == 1 and words[0] == 'read':
                game.load(sys.stdin)
            else:
                print "i don't understand"
            check = game.checked()
            if check is not None:
                print "%s in check" % chess.COLORS[check]
        except chess.Error, e:
            print str(e)
Exemple #8
0
 def __init__(self):
     self.game = chess.Game()
     self.move_list = []
     self.possible_moves = []  # Move objects
     self.agent = evalagent.EvalAgent()
     self.reset()
Exemple #9
0
 def load_from_fen(self, fen):
     self.game = chess.Game(fen=fen)
Exemple #10
0
 def reset(self):
     self.game = chess.Game()
     self.get_possible_moves()
Exemple #11
0
 def __init__(self):
     self.white = None
     self.black = None
     self.game = chess.Game()