Ejemplo n.º 1
0
 def is_text_command(user_in):
     global game, valid_filename_re
     cmd_parts = user_in.split()
     if not cmd_parts:
         return False
     cmd = cmd_parts[0].lower()
     if not cmd in 'ai castle game gui help license load promote reset save saves sk'.split(
     ):
         return False  # fast fail
     modifier = cmd_parts[1] if len(
         cmd_parts) > 1 else ''  # some commands require a modifier
     if cmd == 'ai':
         print(str(ai_command(modifier.lower())))
     elif cmd == 'castle':
         if modifier in 'KQkq':
             game.castle(modifier)
         else:
             print('"castle" must be followed by "K", "Q", "k", or "q".')
     elif cmd == 'game':
         print(game)
     elif cmd == 'gui':
         game.scene_gui()
     elif cmd == 'help':
         print(help_str)
     elif cmd == 'license':
         print(license())
     elif cmd == 'load':
         filename = ' '.join(cmd_parts[1:])
         if is_valid_filename(filename):
             from Phantom.core.game_class import load_game
             game = load_game(filename)
             print(game)
         else:
             print(filename + ' is not a valid filename.')
     elif cmd == 'promote':
         game.promote(cmd_parts[1], cmd_parts[2])
     elif cmd == 'reset':
         game = ChessGame()
         print(game)
     elif cmd == 'save':
         filename = ' '.join(cmd_parts[1:])
         if is_valid_filename(filename):
             game.board.save(filename)
         else:
             print(filename + ' is not a valid filename.')
     elif cmd == 'saves':
         from Phantom.boardio.load import list_games
         print('\n'.join(
             '{:>3} {}'.format(i + 1, game_name)
             for i, game_name in enumerate(sorted(list_games()))))
     elif cmd == 'sk':
         game.sk_gui()
     else:
         assert False, 'This should never happen.'
     return True
Ejemplo n.º 2
0
 def is_text_command(user_in):
     global game, valid_filename_re
     cmd_parts = user_in.split()
     if not cmd_parts:
         return False
     cmd = cmd_parts[0].lower()
     if not cmd in 'ai castle game gui help license load promote reset save saves sk'.split():
         return False  # fast fail
     modifier = cmd_parts[1] if len(cmd_parts) > 1 else ''  # some commands require a modifier
     if cmd == 'ai':
         print(str(ai_command(modifier.lower())))
     elif cmd == 'castle':
         if modifier in 'KQkq':
             game.castle(modifier)
         else:
             print('"castle" must be followed by "K", "Q", "k", or "q".')
     elif cmd == 'game':
         print(game)
     elif cmd == 'gui':
         game.scene_gui()
     elif cmd == 'help':
         print(help_str)
     elif cmd == 'license':
         print(license())
     elif cmd == 'load':
         filename = ' '.join(cmd_parts[1:])
         if is_valid_filename(filename):
             from Phantom.core.game_class import load_game
             game = load_game(filename)
             print(game)
         else:
             print(filename + ' is not a valid filename.')
     elif cmd == 'promote':
         game.promote(cmd_parts[1], cmd_parts[2])
     elif cmd == 'reset':
         game = ChessGame()
         print(game)
     elif cmd == 'save':
         filename = ' '.join(cmd_parts[1:])
         if is_valid_filename(filename):
             game.board.save(filename)
         else:
             print(filename + ' is not a valid filename.')
     elif cmd == 'saves':
         from Phantom.boardio.load import list_games
         print('\n'.join('{:>3} {}'.format(i+1, game_name)
                    for i, game_name in enumerate(sorted(list_games()))))
     elif cmd == 'sk':
         game.sk_gui()
     else:
         assert False, 'This should never happen.'
     return True
Ejemplo n.º 3
0
 def __init__(self, game=None, in_scene=None):
     self.game = game or ChessGame()
     in_scene = in_scene or ChessLoadingScreen
     self.present('full_screen',
                  orientations=['landscape'],
                  hide_title_bar=True)
     self.scene_view = scene.SceneView(frame=self.frame)
     self.scene_view.scene = in_scene()
     self.add_subview(self.scene_view)
     self.add_subview(self.close_button())
     self.switch_scene()  # switch from loading to main
Ejemplo n.º 4
0
def main(clear=True):
    if clear: clear_log()
    log_msg('Testing Phantom.core.board.Board.__init__() method', 0)
    g = None
    try:
        g = ChessGame()
    except Exception as e:
        log_msg('ChessGame instantiation test failed:\n{}'.format(e), 0, err=True)
    finally:
        log_msg('Test complete', 0)
    return g
Ejemplo n.º 5
0
def main(clear=True):
    if clear: clear_log()
    log_msg('Testing Phantom.ai.tree.generate.spawn_tree()', 0)
    tree = None
    try:
        g = ChessGame()
        tree = spawn_tree(g.board)
    except Exception as e:
        log_msg(
            'Phantom.ai.tree.generate.spawn_tree() test failed:\n{}'.format(e),
            0,
            err=True)
    finally:
        log_msg('Test complete', 0)
    return tree
Ejemplo n.º 6
0
 def __init__(self, game=None):
     self.game = game or ChessGame()
     self.width, self.height = ui.get_screen_size()
     assert self.width > self.height, 'This app only works in landscape mode!!'
     if photos.is_authorized():  # add a photo as the background image
         self.add_subview(self.make_image_view())
     center_frame, left_frame, right_frame, status_frame = screen_frames()
     self.add_subview(self.make_left_side_view(left_frame))
     self.make_buttons(left_frame)
     self.add_subview(self.make_board_scene(center_frame))
     self.info_view = self.make_right_side_view(right_frame)
     self.add_subview(self.info_view)
     self.status_view = self.make_status_view(status_frame)
     self.add_subview(self.status_view)
     self.present(orientations=['landscape'], hide_title_bar=True)
Ejemplo n.º 7
0
def main(clear=True):
    from Phantom.core.game_class import ChessGame
    board = ChessGame().board
    board.pprint()
    for i in xrange(105):
        print('make_smart_move {}: {}'.format(i + 1, make_smart_move(board)))
        board.pprint()
        winner = False # board.game.is_won()
        if winner:
            print('{} won this game in {} smart moves.'.format(winner, (i + 1) / 2))
            break
    return(board.game.ai_rateing)
Ejemplo n.º 8
0
def main(clear=True):
    if clear: clear_log()
    log_msg('Testing Phantom.core.board.Board.move() method', 0)
    #b = Board(None)  # white to move, opening layout
    b = ChessGame().board
    try:
        b.move('e2e4')
        b.move('g8f6')
        b.move('g2g3')
    except Exception as e:
        log_msg('Phantom.core.board.Board.move() method test failed:\n{}'.format(e), 0, err=True)
    finally:
        log_msg('Test complete', 0)
Ejemplo n.º 9
0
def main(clear=True):
    print('=' * 50)
    from Phantom.core.game_class import ChessGame
    from Phantom.utils.debug import log_msg, clear_log

    if clear: clear_log()
    log_msg('Testing Phantom.ai.tree.generate.spawn_tree()', 0)
    tree = None
    try:
        g = ChessGame()
        tree = spawn_tree(g.board)
    except ImportError:  #Exception as e:
        log_msg(
            'Phantom.ai.tree.generate.spawn_tree() test failed:\n{}'.format(e),
            0,
            err=True)
    finally:
        log_msg('Test complete', 0)
    return tree
Ejemplo n.º 10
0
def main(clear=True):
    if clear: clear_log()
    log_msg(
        'Testing Phantom.ai.pos_eval.basic pos_eval_basic() & pos_material()',
        0)
    game = ChessGame()
    score = material = None
    try:
        score = pos_eval_basic(game.board)
    except Exception as e:
        log_msg('AI basic position evaluation failed: \n{}'.format(e),
                0,
                err=True)
    try:
        material = pos_material(game.board)
    except Exception as e:
        log_msg('AI material assesment failed: \n{}'.format(e), 0, err=True)
    log_msg('Test complete', 0)
    return score, material
Ejemplo n.º 11
0
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
# GNU General Public License for more details.                          #
#                                                                       #
# You should have received a copy of the GNU General Public License     #
# along with PhantomChess.  If not, see <http://www.gnu.org/licenses/>. #
#########################################################################

"""To start a new game, simply run this file."""

from Phantom.core.game_class import ChessGame
from Phantom.docs.documentation import program_use
from Phantom.utils.lic import license
from Phantom.core.coord.point import Coord
from Phantom.boardio.epd_read import load_test

game = ChessGame()
print """PhantomChess Copyright (C) 2015 671620616
This program comes with ABSOLUTELY NO WARRANTY; for details enter 'license'
This is free software, and you are welcome to redistribute it
under certain conditions; type 'license' for details.

Your game can be displayed by typing 'game'
To move, type 'e2e4' or similar.
To execute a function, simply type the function as you normally would.
To exit, type 'quit' or close the program.
For a full list of commands, type 'help'."""

print game

if __name__ == '__main__':
    import re
Ejemplo n.º 12
0
        elif touch.location in self.rrect:
            self.selected = self.rook
        elif touch.location in self.brect:
            self.selected = self.bishop
        elif touch.location in self.krect:
            self.selected = self.knight
        
        if self.selected:
            self.promote()
            self.parent.switch_scene(self.game.data['screen_main'])
        
    def promote(self):
        self.game.board.promote(self.promoter.coord, self.selected.fen_char.upper())

    def draw(self):
        scene.background(0, 0, 0)
        for i, piece in enumerate(self.pieces):
            img = self.img_names[piece.pythonista_gui_imgname]
            pos = piece.coord.as_screen()
            scene.image(img, pos.x, pos.y, scale_factor, scale_factor)
        tpos = Coord(screen_width/2, screen_height/2 + 2*scale_factor)
        scene.text('Select a piece to promote to', x=tpos.x, y=tpos.y)

if __name__ == '__main__':
    import Phantom as P
    g = ChessGame('Long Endgame 1')
    # add a piece to test the promotion mechanism
    g.board.pieces.add(P.Pawn(P.Coord(0, 1), 'black', g.board.player2))
    g.gui()

Ejemplo n.º 13
0
def gui_sk(game=None):
    SkChessView(game or ChessGame())
Ejemplo n.º 14
0
 def rst_game(self):
     sc_data = self.data
     new_game = ChessGame()
     new_game.board.cfg = sc_data
     self.game = new_game
Ejemplo n.º 15
0
            selected = None

        if selected:
            self.promote(selected.fen_char.upper())
            print('b del')
            del self  # ???
            print('a del')
            #self.parent.switch_scene(self.game.data['screen_main'])

    def promote(self, fen_char):
        print('promoting', fen_char)
        self.game_view.game.board.promote(self.promoter.coord, fen_char)
        print('promoted ', fen_char)

    def draw(self):
        scene.background(0, 0, 0)
        for piece in self.pieces:
            img = self.img_names[piece.pythonista_gui_img_name]
            pos = piece.coord.as_screen
            scene.image(img, pos.x, pos.y, scale_factor, scale_factor)
        tpos = Coord(screen_width / 2, screen_height / 2 + 2 * scale_factor)
        scene.text('Select a piece to promote to', x=tpos.x, y=tpos.y)


if __name__ == '__main__':
    import Phantom as P
    g = ChessGame('Long Endgame 1')
    # add a piece to test the promotion mechanism
    g.board.pieces.add(P.Pawn(P.Coord(0, 1), 'black', g.board.player2))
    g.gui()
Ejemplo n.º 16
0
            #freeze:self.freeze()
            self.pieces.append(new)
            #freeze:self.unfreeze()
            self.kill(pawn)
        self.switch_turn()

    def set_checkmate_validation(self, val):
        self.cfg.do_checkmate = val

    # TODO: make this work
    def is_checkmate(self):
        if not self.cfg.do_checkmate:
            return False
        return False

    # TODO: make this work
    def will_checkmate(self, p1, p2):
        if not self.cfg.do_checkmate:
            return False
        test = Board(fen=self.fen_str())
        test.move(p1, p2)
        test.cfg.do_checkmate = self.cfg.do_checkmate
        return test.is_checkmate()
__all__.append('Board')

if __name__ == '__main__':
    from Phantom.core.game_class import ChessGame
    b = ChessGame().board
    b.set_name('Chess')
    b.pprint()
Ejemplo n.º 17
0
        
    def touch_moved(self, touch):
        self.active_scene.touch_moved(touch)
        
    def touch_ended(self, touch):
        self.active_scene.touch_ended(touch)
    
    def set_main_scene(self, s):
        self.main_scene = s
    
    def set_load_scene(self, s):
        self.load_scene = s
    
    def set_dbg_scene(self, s):
        self.dbg_scene = s
    
    def set_options_scene(self, s):
        self.options_scene = s
    
    def set_promote_scene(self, s):
        self.promote_scene = s
    
    def did_begin(self):
        self.switch_scene(self.main_scene)

if __name__ == '__main__':
    from Phantom.core.game_class import ChessGame
    game = ChessGame()
    game.gui()

Ejemplo n.º 18
0
# PhantomChess is distributed in the hope that it will be useful,       #
# but WITHOUT ANY WARRANTY; without even the implied warranty of        #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
# GNU General Public License for more details.                          #
#                                                                       #
# You should have received a copy of the GNU General Public License     #
# along with PhantomChess.  If not, see <http://www.gnu.org/licenses/>. #
#########################################################################
"""To start a new game, simply run this file."""

from Phantom.core.game_class import ChessGame
from Phantom.docs.documentation import program_use
from Phantom.utils.lic import license
from Phantom.boardio.epd_read import load_test

game = ChessGame()
print("""PhantomChess Copyright (C) 2015 671620616
This program comes with ABSOLUTELY NO WARRANTY; for details enter 'license'
This is free software, and you are welcome to redistribute it
under certain conditions; type 'license' for details.

Your game can be displayed by typing 'game'
To move, type 'e2e4' or similar.
To execute a function, simply type the function as you normally would.
To exit, type 'quit' or close the program.
For a full list of commands, type 'help'.""")

print(game)

if __name__ == '__main__':
    import re