예제 #1
0
    def get(self, request, *args, **kwargs):
        game_id = self.kwargs['pk']
        action = kwargs['action']
        line_k = kwargs['line']
        column_k = kwargs['column']
        print 'PieceAction.get : game_id : %s, action : %s, line : %s, column : %s' % (
            game_id, action, line_k, column_k)

        #  Rotem - ChessGame gets the game id or initializes a new game
        game_logic = ChessLogic.ChessGame(user_id=self.request.user.id,
                                          game_id=game_id)
        if not game_logic:
            print('PieceActionView.get ERROR : game not found : %s' % game_id)
            return HttpResponseRedirect(reverse('home'))
        if action == 'select':  # From
            game_logic.move_piece_select_source(request.user, column_k, line_k)
            return HttpResponseRedirect(
                reverse('chess-game', kwargs={'pk': game_id}))
        elif action == 'move':  # To
            game_logic.move_piece_select_target(request.user, column_k, line_k)
            return HttpResponseRedirect(
                reverse('chess-game', kwargs={'pk': game_id}))
        elif action == 'aibotmove':
            game_logic.rage_against_the_machine()
            return HttpResponseRedirect(
                reverse('chess-game', kwargs={'pk': game_id}))
        else:
            print 'PieceAction.get ERROR: unknown action : %s' % action
        return HttpResponseRedirect(reverse('home'))
예제 #2
0
    def get(self, request, *args, **kwargs):
        game_id = self.kwargs['pk']

        game_logic = ChessLogic.ChessGame(user_id=self.request.user.id,
                                          game_id=game_id)
        if not game_logic:
            print('PieceActionView.get ERROR : game not found : %s' % game_id)
            return HttpResponseRedirect(reverse('home'))

        role_name = kwargs['role_name']
        game_logic.promote_piece(request.user, role_name)
        return HttpResponseRedirect(
            reverse('chess-game', kwargs={'pk': game_id}))
예제 #3
0
    def get_context_data(self, **kwargs):
        context = super(GameView, self).get_context_data(**kwargs)
        user_utils.add_generic_context(context, request=self.request)

        game_id = kwargs['pk']
        game_logic = ChessLogic.ChessGame(user_id=self.request.user,
                                          game_id=game_id)
        if not game_logic:
            context['html_board'] = 'Game not found.'
            return {'context': context}
        else:
            context['user'] = self.request.user
            context['user_is_creator'] = False
            creator_id = int(
                game_logic.game_data.get_data('game_options/creator'))
            if creator_id == self.request.user.id:
                context['user_is_creator'] = True

            context['user_can_play'] = False
            side = game_logic.game_data.get_data('token/step/side')
            if side:
                player_id = game_logic.game_data.get_data('participants/%s/1' %
                                                          side)
                if player_id and player_id == self.request.user.id:
                    context['user_can_play'] = True

                # add contextual data
                if game_logic.board.is_kingchecked(side):
                    context['king_check'] = side
                html_board = game_logic.board.render(context)
                context['html_board'] = html_board
            else:
                rounds = game_logic.game_data.get_data('rounds')
                white_wins = 0
                black_wins = 0
                if rounds:
                    for round_k, round in rounds.items():
                        if round['winner'] == 'white':
                            white_wins += 1
                        elif round['winner'] == 'black':
                            black_wins += 1
                context['game_results'] = {
                    'white_wins': white_wins,
                    'black_wins': black_wins
                }
            context['json_data'] = json2html.convert(
                json=game_logic.game_data.data)
            context['game_logic'] = game_logic
            return {'context': context}
예제 #4
0
    def get(self, request, *args, **kwargs):
        game_id = self.kwargs['pk']
        game_logic = ChessLogic.ChessGame(user_id=self.request.user.id,
                                          game_id=game_id)
        if not game_logic:
            print('PieceActionView.get ERROR : game not found : %s' % game_id)
            return HttpResponseRedirect(reverse('home'))

        action = kwargs['action']
        if action == 'surrender_checkmate':
            game_logic.accept_checkmate()
        elif action == 'declare_withdraw':
            game_logic.accept_checkmate()
        elif action == 'declare_draw':
            game_logic.declare_draw()
        elif action == 'reset_round':
            game_logic.reset_round()
        elif action == 'reset_game':
            game_logic.reset_game()

        elif action == 'update_option':
            option_name = kwargs['name']
            option_value = kwargs['value']
            game_logic.game_data.set_data('game_options/%s' % option_name,
                                          option_value)

        elif action == 'save_board':
            comment = kwargs['value']
            saved_game = {
                'comment': comment,
                'board': game_logic.game_data.get_data('board'),
                'token': game_logic.game_data.get_data('token')
            }
            saved_games = game_logic.game_data.get_data('saved_games')
            new_index = 1
            if saved_games:
                new_index = len(saved_games) + 1
            new_index = '%03d.' % new_index
            game_logic.game_data.set_data('saved_games/%s' % new_index,
                                          saved_game)
        elif action == 'load_previous_log':
            token_logs = game_logic.game_data.get_data('token/logs')
            if token_logs:
                token_logs_len = len(token_logs)
                if token_logs_len > 1:
                    previous_log_index = '%03d.' % (token_logs_len - 1)
                    kwargs['action'] = 'restore_log'
                    kwargs['name'] = '_'
                    kwargs['value'] = previous_log_index
                    return HttpResponseRedirect(
                        reverse('menu-action', kwargs=kwargs))
                else:
                    kwargs['action'] = 'reset_round'
                    return HttpResponseRedirect(
                        reverse('menu-action', kwargs=kwargs))
            else:
                print 'no logs to restore'
        elif action == 'restore_log':
            log_index = kwargs['value']
            self._restore_log(source='logs',
                              log_index=log_index,
                              game_logic=game_logic)
        elif action == 'restore_saved_game':
            log_index = kwargs['value']
            self._restore_log(source='saved_games',
                              log_index=log_index,
                              game_logic=game_logic)
        else:
            print 'unknown action : %s' % action

        return HttpResponseRedirect(
            reverse('chess-game', kwargs={'pk': game_id}))
예제 #5
0
from chess_classes import ChessLogic, ChessBoard
''' Testing ground '''

import json
# import chess_ai
# import game

import rotemai.chess_ai as chess_ai
import rotemai.game as game

from chess_engine.chess_classes import ChessBoard, ChessPiece, ChessLogic

# from chess_classes import ChessLogic

a = ChessLogic.ChessGame(1)

# data = {"participants":{"white":{"1":1},"black":{"1":-1}},"game_options":{"winning_games":"1","name":"Machine11", "creator":1},"token":{"step":{"castle":{"white":["r1","r2"],"black":["r1","r2"]},"name":"waitCellSource","side":"white"}},"board":{"1":{"a":{"s":"w","r":"R","n":"r2"},"c":{"s":"w","r":"B","n":"b2"},"b":{"s":"w","r":"H","n":"h2"},"e":{"s":"w","r":"K","n":"k"},"d":{"s":"w","r":"Q","n":"q"},"g":{"s":"w","r":"H","n":"h1"},"f":{"s":"w","r":"B","n":"b1"},"h":{"s":"w","r":"R","n":"r1"}},"2":{"a":{"s":"w","r":"P","n":"p1"},"c":{"s":"w","r":"P","n":"p3"},"b":{"s":"w","r":"P","n":"p2"},"e":{"s":"w","r":"P","n":"p5"},"d":{"s":"w","r":"P","n":"p4"},"g":{"s":"w","r":"P","n":"p7"},"f":{"s":"w","r":"P","n":"p6"},"h":{"s":"w","r":"P","n":"p8"}},"3":{"a":"-","c":"-","b":"-","e":"-","d":"-","g":"-","f":"-","h":"-"},"4":{"a":"-","c":"-","b":"-","e":"-","d":"-","g":"-","f":"-","h":"-"},"5":{"a":"-","c":"-","b":"-","e":"-","d":"-","g":"-","f":"-","h":"-"},"6":{"a":"-","c":"-","b":"-","e":"-","d":"-","g":"-","f":"-","h":"-"},"7":{"a":{"s":"b","r":"P","n":"p1"},"c":{"s":"b","r":"P","n":"p3"},"b":{"s":"b","r":"P","n":"p2"},"e":{"s":"b","r":"P","n":"p5"},"d":{"s":"b","r":"P","n":"p4"},"g":{"s":"b","r":"P","n":"p7"},"f":{"s":"b","r":"P","n":"p6"},"h":{"s":"b","r":"P","n":"p8"}},"8":{"a":{"s":"b","r":"R","n":"r2"},"c":{"s":"b","r":"B","n":"b2"},"b":{"s":"b","r":"H","n":"h2"},"e":{"s":"b","r":"K","n":"k"},"d":{"s":"b","r":"Q","n":"q"},"g":{"s":"b","r":"H","n":"h1"},"f":{"s":"b","r":"B","n":"b1"},"h":{"s":"b","r":"R","n":"r1"}}}}
data = {
    "participants": {
        "white": {
            "1": 1
        },
        "black": {
            "1": 1
        }
    },
    "game_options": {
        "winning_games": "1",
        "name": "Createting_New_Game_Default_options",
        "creator": 1