示例#1
0
def game(request):
    board_set = Board.objects.filter(name="game")
    if len(board_set) < 1:
        last_board = Board.objects.latest('date')
        board = Board.create_and_init_board("game", 4)
        board.save()
        messages.success(request, last_board.get_winner_name())
        return redirect('/board/home')
    else:
        board = board_set[0]

    if board.p1 and board.p2:
        currentPlayerId = board.nbTurns % 2 + 1
        if currentPlayerId == 1:
            currentPlayer = board.p1
            isCurrentPlayerAI = board.p1.isAI
        else:
            currentPlayer = board.p2
            isCurrentPlayerAI = board.p2.isAI

        if isCurrentPlayerAI:
            best_ai_move = currentPlayer.ai.get_move(board)
            board.move(currentPlayerId, str(best_ai_move))
            time.sleep(0.5)

        print(f"Player 1 : {board.p1} - Player 2 : {board.p2}")

    return render(request, 'board/test.html', {
        'grid': board.print_board(),
        'nbTurns': board.nbTurns,
        'board': board
    })
示例#2
0
def settings(request):
    if request.method == 'POST':
        form = PlayersChoiceForm(request.POST)

        if form.is_valid():
            p1 = form.cleaned_data.get('p1')
            p2 = form.cleaned_data.get('p2')
            if p1 == p2:
                messages.error(request,
                               'You cannot play with both the same player')
                return redirect('/board/game/settings')
            else:
                if (p1.isAI):
                    p1.init_ai(1)

                if (p2.isAI):
                    p2.init_ai(2)

            board_set = Board.objects.filter(name="game")
            if len(board_set) < 1:
                board = Board.create_and_init_board("game", 4)
                board.save()
            else:
                board = board_set[0]

            board.p1 = p1
            board.p2 = p2
            board.save()
            return redirect('game')
    else:
        form = PlayersChoiceForm()
        return render(request, 'board/settings.html', {'form': form})
示例#3
0
def resetGame(request):
    board = Board.objects.get(name="game")
    p1 = board.p1
    p2 = board.p2
    board.delete()

    board = Board.create_and_init_board("game", 4)
    board.p1 = p1
    board.p2 = p2
    board.save()

    return redirect('/board/game')