first step check correct

This commit is contained in:
Administrator
2022-08-23 23:20:03 +03:00
parent 364c869467
commit 1863b65138
5 changed files with 126 additions and 2 deletions

55
battleship/utils.py Normal file
View File

@@ -0,0 +1,55 @@
def check_field(field):
if field.count('o') != 20:
return False
cells = list(field)
cells = [cells[x * 10: (x + 1) * 10] for x in range(0, 10)]
ships = [1, 1, 1, 1, 2, 2, 2, 3, 3, 4]
while ships:
found = False
for i in range(10):
for j in range(10):
if cells[i][j] == 'o':
i1, j1 = i, j
if j1 == 9 or cells[i1][j1 + 1] == ' ': # если вертикальный
if i1 == 9 or cells[i1 + 1][j1] == ' ': # если 1 палуба
p = 1
elif i1 + 1 == 9 or cells[i1 + 2][j1] == ' ': # если 2 палубы
p = 2
elif i1 + 2 == 9 or cells[i1 + 3][j1] == ' ': # если 3 палубы
p = 3
elif i1 + 3 != 9 and cells[i1 + 4][j1] == 'o':
return False
else:
p = 4
if p in ships:
ships.remove(p)
else:
return False
for k in range(p):
cells[i1 + k][j1] = ' '
else:
if j1 == 9 or cells[i1][j1 + 1] == ' ': # если 1 палуба
p = 1
elif j1 + 1 == 9 or cells[i1][j1 + 2] == ' ': # если 2 палубы
p = 2
elif j1 + 2 == 9 or cells[i1][j1 + 3] == ' ': # если 3 палубы
p = 3
elif j1 + 3 != 9 and cells[i1][j1 + 4] == 'o':
return False
else:
p = 4
if p in ships:
ships.remove(p)
else:
return False
for k in range(p):
cells[i1][j1 + k] = ' '
found = True
if found:
break
if found:
break
if not found:
return False
return True

View File

@@ -5,6 +5,7 @@ from django.shortcuts import render
from django.utils import timezone
from battleship.models import Game, Player, generate_token
from battleship.utils import check_field
def new_game(request):
@@ -45,8 +46,11 @@ def place_ships(request):
if player.field != ' ' * 100:
return JsonResponse({}, status=403)
player.field = request.POST['field']
player.save()
return JsonResponse({})
if check_field(player.field):
player.save()
return JsonResponse({})
else:
return JsonResponse({}, status=403)
def check_opponent(request):