forked from BattleshipAdmirals/Version1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattleshipAdmiralsScripts 1.txt
175 lines (139 loc) · 5.2 KB
/
BattleshipAdmiralsScripts 1.txt
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import math
import random
from IPython.display import clear_output
import time
def validate_size(size: int):
side_length = int(math.sqrt(size))
if side_length * side_length != size:
raise ValueError("Size must create perfect square")
def print_game(grids, shots, hits):
print(" Battleship Admirals")
print()
print(" Player 1 Player 2")
print_dual_grids(grids[0], grids[1])
print(f' Shots: {shots[0]} Hits: {hits[0]} Shots: {shots[1]} Hits: {hits[1]}')
print()
print_dual_grids(grids[2], grids[3])
print()
def print_grid(grid: list):
row_labels = 'A B C D E F G H I J K L M N'.split()
print (' ' + ' '.join([f'{i}' for i in range(len(grid))]))
for i, row in enumerate(grid):
print(f'{row_labels[i]:2}', end=' ')
for cell in row:
print(cell, end=' ')
print()
def print_dual_grids(grid1: list, grid2: list):
row_labels = 'A B C D E F G H I J K L M N'.split()
print (' ' + ' '.join([f'{i}' for i in range(len(grid1))]) + ' ' + ' '.join([f'{i}' for i in range(len(grid2))]))
for i in range(len(grid1)):
print(f'{row_labels[i]:2}', end=' ')
for cell in grid1[i]:
print(cell, end=' ')
print(' ', end= '')
print(f'{row_labels[i]:2}', end=' ')
for cell in grid2[i]:
print(cell, end=' ')
print()
def bitstring_to_grid(bitstring: str):
size = len(bitstring)
validate_size(size)
side_length = int(math.sqrt(size))
grid = [
['.' if bitstring[i * side_length + j] == '1' else 'O' for j in range(side_length)]
for i in range(side_length)
]
return grid
def grid_to_bitstring(grid: list):
bitstring = ''.join('1' if cell == '.' else '0' for row in grid for cell in row)
return bitstring
def bitstring_to_int(bitstring: str):
return int(bitstring,2)
def int_to_bitstring(value: int, length=100):
bitstring = f'{value:0{length}b}'
return bitstring
def blank_bitstring(size=100):
validate_size(size)
return '1' * size
def random_bit():
return random.choice([0,1])
def random_int(max: int):
return random.randint(0,max)
def random_location(ship_size: int, grid_size: int):
return random_int(grid_size-ship_size)
def random_ship_placement(ship_size: int, grid: list):
clear = 0
while clear == 0:
orientation = random_bit()
row = random_location(ship_size, len(grid))
column = random_location(ship_size, len(grid[0]))
if orientation == 0:
for i in range(ship_size):
if grid[row+i][column] == '.':
clear = 1
else:
clear = 0
break
if clear == 1:
for i in range(ship_size):
grid[row+i][column] = ship_size
else:
continue
else:
for i in range(ship_size):
if grid[row][column+i] == '.':
clear = 1
else:
clear = 0
break
if clear == 1:
for i in range(ship_size):
grid[row][column+i] = ship_size
else:
continue
return grid
def setup_game(grid_size: int, ships: list):
bit_string = blank_bitstring(grid_size*grid_size)
ship_grid1 = bitstring_to_grid(bit_string)
ship_grid2 = bitstring_to_grid(bit_string)
shot_grid1 = bitstring_to_grid(bit_string)
shot_grid2 = bitstring_to_grid(bit_string)
for i in range(len(ships)):
ship_grid1 = random_ship_placement(ships[i], ship_grid1)
for i in range(len(ships)):
ship_grid2 = random_ship_placement(ships[i], ship_grid2)
return shot_grid1, shot_grid2, ship_grid1, ship_grid2
def play_turn(grids, turn):
clear = 0
while clear ==0:
row = random_int(len(grids[turn])-1)
column = random_int(len(grids[turn][0])-1)
#print("trying: ", turn , row, column)
if grids[turn][row][column] == '.':
clear = 1
return row, column
def play(grid_size, ships):
grids = setup_game(grid_size, ships)
hits_to_win = sum(ships)
max_shots = grid_size*grid_size
shots = [0, 0]
hits = [0, 0]
turn = 0
while shots[0] < max_shots and shots[1] < max_shots and hits[0] < hits_to_win and hits[1] < hits_to_win:
clear_output(wait=True)
current_shot = play_turn(grids, turn)
if grids[turn+2][current_shot[0]][current_shot[1]] == '.':
grids[turn][current_shot[0]][current_shot[1]] = 'O'
else:
grids[turn][current_shot[0]][current_shot[1]] = 'X'
hits[turn] = hits[turn] + 1
shots[turn] = shots[turn] + 1
if turn == 0:
turn = 1
else:
turn = 0
print_game(grids, shots, hits)
time.sleep(1)
grid_size = 10
ships = [5, 4, 3, 3, 2]
play(grid_size, ships)