示例#1
0
    def test_take_win(self):
        "The AI should always take a winning move if available."
        # Row
        g = Game()
        g.board_state[0][0] = g.board_state[0][1] = 'X'
        perform_move(g)
        self.assertEquals(g.board_state[0][2], 'X')

        # Col
        g = Game()
        g.board_state[0][0] = g.board_state[1][0] = 'X'
        perform_move(g)
        self.assertEquals(g.board_state[2][0], 'X')

        # Diagonals
        g = Game()
        g.board_state[0][2] = g.board_state[2][0] = 'X'
        perform_move(g)
        self.assertEquals(g.board_state[1][1], 'X')

        g = Game()
        g.board_state[0][0] = g.board_state[1][1] = 'X'
        perform_move(g)
        self.assertEquals(g.board_state[2][2], 'X')

        g = Game()
        g.board_state[2][0] = g.board_state[1][1] = 'X'
        perform_move(g)
        self.assertEquals(g.board_state[0][2], 'X')
示例#2
0
    def test_take_block(self):
        "The AI should always block the player if possible."
        # Row
        g = Game()
        g.board_state[0][0] = g.board_state[0][1] = 'O'
        perform_move(g)
        self.assertEquals(g.board_state[0][2], 'X')

        # Col
        g = Game()
        g.board_state[0][0] = g.board_state[1][0] = 'O'
        perform_move(g)
        self.assertEquals(g.board_state[2][0], 'X')

        # Diagonals
        g = Game()
        g.board_state[0][2] = g.board_state[2][0] = 'O'
        perform_move(g)
        self.assertEquals(g.board_state[1][1], 'X')

        g = Game()
        g.board_state[0][0] = g.board_state[1][1] = 'O'
        perform_move(g)
        self.assertEquals(g.board_state[2][2], 'X')

        g = Game()
        g.board_state[2][0] = g.board_state[1][1] = 'O'
        perform_move(g)
        self.assertEquals(g.board_state[0][2], 'X')
示例#3
0
    def test_second_move(self):
        """
        The AI should always try for the middle cell, but fallback to any
        corner if it is taken.
        """
        g = Game()
        g.board_state[0][1] = 'O'
        perform_move(g)
        self.assertEquals(g.board_state[1][1], 'X')

        g = Game()
        g.board_state[1][1] = 'O'
        perform_move(g)
        corners = [
            g.board_state[0][0], g.board_state[0][2],
            g.board_state[2][0], g.board_state[2][2]
        ]
        self.assertTrue('X' in set(corners))
示例#4
0
def make_move(request, game_id):
    """
    Make a move.

    This method should be called asynchronously and will return the new game
    board state rendered as HTML.
    """
    game = get_object_or_404(Game, pk=game_id)

    if 'row' not in request.POST or 'column' not in request.POST:
        # Can't do anything if we don't know which cell was clicked.
        return render_to_response("game/_board.html", { 'game': game })

    row, column = int(request.POST['row']), int(request.POST['column'])
    if game.board_state[row][column] is None:
        game.board_state[row][column] = 'O'
        if not game.is_game_over():
            perform_move(game)
        game.save();

    return render_to_response("game/_board.html", { 'game': game })
示例#5
0
def new_game(request):
    "Create a new game and redirect to its url."
    game = Game()
    perform_move(game)  # Computer goes first
    game.save()
    return redirect(game)
示例#6
0
 def test_first_move(self):
     "The AI should always take the middle cell if it is the first move."
     g = Game()
     perform_move(g)
     self.assertEquals(g.board_state[1][1], 'X')