from random import randint
N = 10
def move():
r = randint(0,1)
if r:
print("Ваш ход")
else:
print("Мой ход")
return r
def field(a):
for i in a:
for el in i:
print(el, end=' ')
print()
def ship(n,a):
f = 0
b = []
while not f:
r1 = randint(0,1)
r2 = randint(0,N-n)
r3 = randint(0,9)
k = 0
if r1:
for i in range(r2,r2+n): #В строку
if a[r3][i] == 0:
k += 1
if k == n:
for i in range(r2,r2+n):
a[r3][i] = n
#b.append((r3,i))
f = 1
else:
for i in range(r2,r2+n): #В столбец
if a[i][r3] == 0:
k += 1
if k == n:
for i in range(r2,r2+n):
a[i][r3] = n
#b.append((i,r3))
f = 1
return a
def check(a):
f = 0
for i in range(len(a)):
for j in range(len(a)):
if a[i][j] == 0:
f = 1
else:
f = 0
return f
def shoot(t,b):
line = "0123456789"
f = 0
s2 = 2
s3 = 3
s4 = 4
if a[t0][t1] == 0:
print("Мимо")
elif a[t0][t1] == 1:
print("Убил")
a[t0][t1] = 0
f = 1
elif a[t0][t1] == 2:
print("Попал")
a[t0][t1] = 0
b.remove((t0,t1))
f = 1
if not b:
print("Убил")
return f
def draw(a):
a = ship(4,a)
a = ship(3,a)
a = ship(3,a)
a = ship(2,a)
a = ship(2,a)
a = ship(2,a)
a = ship(1,a)
a = ship(1,a)
a = ship(1,a)
a = ship(1,a)
field(a)
a = [[0] * 10 for i in range(N)]
draw(a)
f = move()
from random import randint
N = 10
def move():
r = randint(0,1)
if r:
print("Ваш ход")
else:
print("Мой ход")
return r
def field(a):
for i in a:
for el in i:
print(el, end=' ')
print()
def ship(n,a):
f = 0
b = []
while not f:
r1 = 1 #Временно заполняем только в строку
r2 = randint(0,N-n)
r3 = randint(0,9)
k = 0
if r1:
for i in range(r2,r2+n): #В строку
if a[r3][i] == 0:
k += 1
if k == n:
for i in range(r2,r2+n):
a[r3][i] = n
if r3 == 0 and r2 == 0:
a[r3][n] = 5
for i in range(r2,r2+n+1):
a[r3+1][i] = 5
elif r3 == 9 and r2 == 0:
a[r3][n] = 5
for i in range(r2,r2+n+1):
a[r3-1][i] = 5
elif r3 == 0 and r2 == N-n:
a[r3][r2-1] = 5
for i in range(r2-1,r2+n):
a[r3+1][i] = 5
elif r3 == 9 and r2 == N-n:
a[r3][r2-1] = 5
for i in range(r2-1,r2+n):
a[r3-1][i] = 5
elif r3 == 0 and r2 > 0 and r2 < 9:
a[r3][r2-1] = 5
a[r3][r2+n] = 5
for i in range(r2-1,r2+n+1):
a[r3+1][i] = 5
elif r3 == 9 and r2 > 0 and r2 < 9:
a[r3][r2-1] = 5
a[r3][r2+n] = 5
for i in range(r2-1,r2+n+1):
a[r3-1][i] = 5
elif r2 == 0 and r3 > 0 and r3 < 9:
a[r3][n] = 5
for i in range(r2,r2+n+1):
a[r3-1][i] = 5
a[r3+1][i] = 5
elif r2 == 9 and r3 > 0 and r3 < 9:
a[r3][n+1] = 5
for i in range(N-n-1,N):
a[r3-1][i] = 5
a[r3+1][i] = 5
else:
a[r3][r2-1] = 5
a[r3][r2+n] = 5
for i in range(r2-1,r2+n+1):
a[r3+1][i] = 5
a[r3-1][i] = 5
#b.append((r3,i))
f = 1
else:
for i in range(r2,r2+n): #В столбец
if a[i][r3] == 0:
k += 1
if k == n:
for i in range(r2,r2+n):
a[i][r3] = n
#b.append((i,r3))
f = 1
return a
def check(a):
f = 0
for i in range(len(a)):
for j in range(len(a)):
if a[i][j] == 0:
f = 1
else:
f = 0
return f
def shoot(t,b):
line = "0123456789"
f = 0
s2 = 2
s3 = 3
s4 = 4
if a[t0][t1] == 0:
print("Мимо")
elif a[t0][t1] == 1:
print("Убил")
a[t0][t1] = 0
f = 1
elif a[t0][t1] == 2:
print("Попал")
a[t0][t1] = 0
b.remove((t0,t1))
f = 1
if not b:
print("Убил")
return f
def draw(a):
a = ship(4,a)
a = ship(3,a)
a = ship(3,a)
a = ship(2,a)
a = ship(2,a)
a = ship(2,a)
a = ship(1,a)
a = ship(1,a)
a = ship(1,a)
a = ship(1,a)
field(a)
a = [[0] * 10 for i in range(N)]
draw(a)
f = move()
from random import randint, choice
def neighbours(cells):
return {(i, j) for y, x in cells
for i in range(y - 1, y + 2) for j in range(x - 1, x + 2)}
N = 10 # а вот менять не надо
abc, digits = 'abcdefghij', '0123456789'
field, sea_map = {}, [[' '] * N for _ in range(N)]
for size in 4, 3, 3, 2, 2, 2, 1, 1, 1, 1:
while True:
ship = [(randint(0, N - 1), randint(0, N - 1))]
for _ in range(1, size):
ship.append(choice([(i, j) for y, x in ship for i, j in (
(y - 1, x), (y, x - 1), (y, x + 1), (y + 1, x)
) if 0 <= i < N > j >= 0 and (i, j) not in ship]))
if not neighbours(ship) & field.keys():
break
field.update(dict.fromkeys(ship, ship))
fleet = set(field)
for step in range(1, 51): # ограничимся 50ю ходами
print(' 0 1 2 3 4 5 6 7 8 9 \n ┌─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┐')
for ch, row in zip(abc, sea_map):
print(ch, '┤', *row, '├', ch)
print(' └─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┘\n 0 1 2 3 4 5 6 7 8 9')
s = input(f'{step:>2} Ваш ход: ')
try:
y, x = abc.index(s[0]), digits.index(s[1])
ship, row = field.get((y, x), ), sea_map[y]
if ship:
del field[y, x]
row[x] = '*'
print(s, 'Попал\n' if ship & field.keys() else 'Убил\n')
if not field:
print('Победа!')
break
else:
if row[x] == ' ':
row[x] = str(len(fleet & neighbours([(y, x)])))
print(s, 'Мимо\n')
except:
print('Ожидался ход в формате \'a0\', но что-то пошло не так...\n')
else:
print(f'В этот раз не вышло, в строю осталось {len(field)} палуб '
f'на {len({id(ship) for ship in field.values()})} кораблях.')