Esempio n. 1
0
    def test_game_result(self):
        '''
        This test will simulate n games between a computer and a human where 
        n is determing by the NUMBER_OF_GAMES settings and make sure that the computer never loses
        '''
        for i in xrange(NUMBER_OF_GAMES):
            board = [0] * 9
            game = Game(board)
            while True:
                available_moves = game.get_blank_boxes()
                # Make a random player move
                box = choice(available_moves)
                game.make_move(box, PLAYER)

                result, winning_combination = game.check_game_over()
                if result:
                    break

                # Make computer's move
                box = game.best_next_move(COMPUTER)
                game.make_move(box, COMPUTER)

                result, winning_combination = game.check_game_over()
                if result:
                    break

            self.assertIsNot(result, PLAYER)
Esempio n. 2
0
    def test_game_result(self):
        '''
        This test will simulate n games between a computer and a human where 
        n is determing by the NUMBER_OF_GAMES settings and make sure that the computer never loses
        '''
        for i in xrange(NUMBER_OF_GAMES):
            board = [0] * 9
            game = Game(board)
            while True:
                available_moves = game.get_blank_boxes()
                # Make a random player move
                box = choice(available_moves)
                game.make_move(box, PLAYER)

                result, winning_combination = game.check_game_over()
                if result:
                    break

                # Make computer's move
                box = game.best_next_move(COMPUTER)
                game.make_move(box, COMPUTER)

                result, winning_combination = game.check_game_over()
                if result:
                    break

            self.assertIsNot(result, PLAYER)
Esempio n. 3
0
def make_move(request):
    """
    Make the human player's move and then calculate and make computer's move
    """
    # Load board and move information
    board = json.loads(request.GET['board'])
    box = int(request.GET['box'].replace("box_",""))
    game_history_id = request.GET['game_history_id']

    # Make player's move and check game result
    game = Game(board)
    game.make_move(box, PLAYER)
    game_over, winning_combination = game.check_game_over()
    computer_played = False

    # If game is still on calculate computer's move and make it
    if not game_over:
        box = game.best_next_move(COMPUTER)
        game.make_move(box, COMPUTER)
        computer_played = True
        game_over, winning_combination = game.check_game_over()

    # If game is over save game history
    if game_over:
        game_history = GameHistory.objects.get(pk=game_history_id)
        game_history.finish_game(game_over)

    result = {}
    result['computer_played'] = computer_played
    result['box'] = str(box)
    result['game_over'] = game_over
    result['board'] = json.dumps(game.get_board())

    # set winning combinations for highlighting
    result['winning_combination'] = winning_combination

    return HttpResponse(json.dumps(result), mimetype='application/json')
Esempio n. 4
0
def make_move(request):
    """
    Make the human player's move and then calculate and make computer's move
    """
    # Load board and move information
    board = json.loads(request.GET['board'])
    box = int(request.GET['box'].replace("box_", ""))
    game_history_id = request.GET['game_history_id']

    # Make player's move and check game result
    game = Game(board)
    game.make_move(box, PLAYER)
    game_over, winning_combination = game.check_game_over()
    computer_played = False

    # If game is still on calculate computer's move and make it
    if not game_over:
        box = game.best_next_move(COMPUTER)
        game.make_move(box, COMPUTER)
        computer_played = True
        game_over, winning_combination = game.check_game_over()

    # If game is over save game history
    if game_over:
        game_history = GameHistory.objects.get(pk=game_history_id)
        game_history.finish_game(game_over)

    result = {}
    result['computer_played'] = computer_played
    result['box'] = str(box)
    result['game_over'] = game_over
    result['board'] = json.dumps(game.get_board())

    # set winning combinations for highlighting
    result['winning_combination'] = winning_combination

    return HttpResponse(json.dumps(result), mimetype='application/json')