check field correct

This commit is contained in:
Administrator 2022-08-23 23:45:40 +03:00
parent 1863b65138
commit 822d81677a
3 changed files with 50 additions and 1 deletions

View File

@ -1,3 +1,37 @@
def find_borders(x, y, p, horizontal):
if horizontal:
borders = [
(x - 1, y - 1),
(x, y - 1),
(x + 1, y - 1),
(x - 1, y + p),
(x, y + p),
(x + 1, y + p)
]
for i in range(p):
borders.extend([
(x - 1, y + i),
(x + 1, y + i)
])
else:
borders = [
(x - 1, y - 1),
(x - 1, y),
(x - 1, y + 1),
(x + p, y - 1),
(x + p, y),
(x + p, y + 1)
]
for i in range(p):
borders.extend([
(x + i, y - 1),
(x + i, y + 1)
])
for a, b in borders:
if 0 <= a <= 9 and 0 <= b <= 9:
yield a, b
def check_field(field): def check_field(field):
if field.count('o') != 20: if field.count('o') != 20:
return False return False
@ -27,6 +61,10 @@ def check_field(field):
return False return False
for k in range(p): for k in range(p):
cells[i1 + k][j1] = ' ' cells[i1 + k][j1] = ' '
borders = find_borders(i1, j1, p, False)
for x, y in borders:
if cells[x][y] == 'o':
return False
else: else:
if j1 == 9 or cells[i1][j1 + 1] == ' ': # если 1 палуба if j1 == 9 or cells[i1][j1 + 1] == ' ': # если 1 палуба
p = 1 p = 1
@ -44,6 +82,10 @@ def check_field(field):
return False return False
for k in range(p): for k in range(p):
cells[i1][j1 + k] = ' ' cells[i1][j1 + k] = ' '
borders = find_borders(i1, j1, p, True)
for x, y in borders:
if cells[x][y] == 'o':
return False
found = True found = True
if found: if found:
break break

View File

@ -104,3 +104,9 @@ def check_status(request):
'my_turn': player.game.turn == player.number, 'my_turn': player.game.turn == player.number,
'game_finished': 'o' not in player.field or 'o' not in player2.field 'game_finished': 'o' not in player.field or 'o' not in player2.field
}) })
def check_field_correct(request):
return JsonResponse({
'correct': check_field(request.POST['field'])
})

View File

@ -25,5 +25,6 @@ urlpatterns = [
path('api/check_opponent', views.check_opponent), path('api/check_opponent', views.check_opponent),
path('api/attend_game', views.attend_game), path('api/attend_game', views.attend_game),
path('api/place_ships', views.place_ships), path('api/place_ships', views.place_ships),
path('api/shoot', views.shoot) path('api/shoot', views.shoot),
path('api/check_field_correct', views.check_field_correct)
] ]