-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
36 lines (30 loc) · 1.2 KB
/
views.py
File metadata and controls
36 lines (30 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from django.shortcuts import render, redirect
from .models import Game
from .forms import MoveForm
def create_game(request):
game = Game.objects.create()
return redirect('play_game', game_id=game.id)
def play_game(request, game_id):
game = Game.objects.get(id=game_id)
winner = game.check_winner()
if winner:
game.status = 'finished'
game.winner = winner
game.save()
if request.method == 'POST':
form = MoveForm(request.POST)
if form.is_valid():
position = form.cleaned_data['position']
player = form.cleaned_data['player']
# Solo permitimos un movimiento en posiciones vacías
moves = game.moves.split(',')
if position >= len(moves):
moves.extend([''] * (position + 1 - len(moves))) # Ampliamos la lista de movimientos
if moves[position] == '':
moves[position] = player
game.moves = ','.join(moves)
game.save()
return redirect('play_game', game_id=game.id)
else:
form = MoveForm()
return render(request, 'game/play_game.html', {'game': game, 'form': form, 'winner': winner})