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

View File

@ -1,6 +1,16 @@
asgiref==3.5.2 asgiref==3.5.2
attrs==22.1.0
Django==3.2.15 Django==3.2.15
importlib-metadata==4.12.0
iniconfig==1.1.1
packaging==21.3
pluggy==1.0.0
psycopg2==2.9.3 psycopg2==2.9.3
py==1.11.0
pyparsing==3.0.9
pytest==7.1.2
pytz==2022.2.1 pytz==2022.2.1
sqlparse==0.4.2 sqlparse==0.4.2
tomli==2.0.1
typing_extensions==4.3.0 typing_extensions==4.3.0
zipp==3.8.1

0
tests/__init__.py Normal file
View File

55
tests/test_check_field.py Normal file
View File

@ -0,0 +1,55 @@
from battleship.utils import check_field
def test_simple():
field = [
['o', 'o', 'o', 'o', ' ', 'o', 'o', 'o', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', 'o', ' ', 'o', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o', ' ', 'o'],
]
field = [''.join(a) for a in field]
field = ''.join(field)
assert check_field(field)
def test_incorrect():
field = [
['o', ' ', 'o', 'o', ' ', 'o', 'o', 'o', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', 'o', ' ', 'o', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o', ' ', 'o'],
]
field = [''.join(a) for a in field]
field = ''.join(field)
assert not check_field(field)
def test_incorrect2():
field = [
['o', 'o', 'o', 'o', ' ', 'o', 'o', 'o', ' ', 'o'],
[' ', ' ', 'o', ' ', ' ', ' ', ' ', ' ', ' ', 'o'],
[' ', ' ', 'o', ' ', ' ', 'o', ' ', ' ', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o', ' ', 'o'],
]
field = [''.join(a) for a in field]
field = ''.join(field)
assert not check_field(field)