-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses_functions.py
235 lines (140 loc) · 4.84 KB
/
classes_functions.py
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import numpy as np
from scipy.stats import norm
from collections import defaultdict
### Construct board ###
### Generate a ship attributes ###
#-------------------------------------------------#
def generate_ship_size(board_length, n_ships):
# Returns ship size when constructing board
board_size = board_length**2
avg_ship_size = board_size/(n_ships*3)
ship_size = norm.ppf(np.random.random(1), loc=avg_ship_size, scale=2).astype(int)[0]
return ship_size
def generate_ship_direction():
return np.random.randint(0,1)
def generate_ship_attributes(board_length, n_ships):
ship_size=generate_ship_size(board_length, n_ships)
ship_direction = generate_ship_direction()
return ship_size, ship_direction
#-------------------------------------------------#
#-------------------------------------------------#
def possible_cords():
...
return # a slice of array ?
def check_if_placing_possible():
return #yes no
def init_ships_for_player(board_length, number_of_ships):
board = np.zeros([board_length, board_length], dtype=int)
ship_container ={}
#For all ships, iterate ship by ship
for i in range(len(number_of_ships)):
#Get a random ship direction and length
ship_size,ship_direction = generate_ship_size(board_length, number_of_ships)
#Place ship randomly on board
#Check for relevent cordinates to place
possible_cords = possible_cords(ship_size,ship_direction,ship_container)
answer = check_if_placing_possible()
#Try to find a sutible ship.location randomly on -possible cordinates-
#If succesfull place
#if not change direction
#do this recursivly until all ships are places
#return an object containing all *ships* to player
return #A ship object
# -------------------------------------------------#
### Construct game ###
def prompt_to_end_loop():
# decription
end = 0
while (end == 0):
end = input("End input generation ? Y/N")
return (1 if end == "N " else 0)
def is_number(s):
# decription
try:
float(s)
return True
except ValueError:
return False
def player_dict_from_user():
# decription
player_dict={}
while(True):
p_name = input("player name")
p_type = input("player type")
player_dict[p_name]=p_type
if (prompt_to_end_loop()==1): break
return player_dict
def get_params_from_user():
# decription
size_of_board = int(input("Input board_length"))
number_of_ships = int(input("Input number_of_ships"))
player_dict = player_dict_from_user()
return player_dict,size_of_board,number_of_ships
def init_game(size_of_board, number_of_ships, player_dict):
# decription
game = Game()
size_of_board, number_of_ships, player_dict = get_params_from_user()
game.init_board(size_of_board, number_of_ships)
game.init_players(player_dict)
return game
def game_menu(self):
# decription
...
def turn_of_one_player(self):
# decription
...
### Classes ###
class ship(object):
def __init__(self, id):
self.id = id
self.length
self.direction
self.location =[]
def set_location(self, location_arr):
self.location= location_arr
def set_direction(self, direction):
self.direction= direction
def set_length(self, length):
self.length= length
class Player(object):
def __init__(self, Name):
self.Name = Name
def Hit(self,x,y):
...
class Human(Player):
def __init__(self, name):
self.name = name
super().__init__(self)
class AI(Player):
def __init__(self, name):
self.name = name
super().__init__(self)
class Game(object):
def __init__(self):
self.ship_dict = {}
self.player_dict = {}
self.board = []
def still_live_players(self):
# if player_dict empty return 0 else 1
...
def make_player(self, name, kind):
# decription
if kind == "human":
return Human(name)
else:
return AI(name)
def add_player(self,name,kind):
# decription
p = self.make_player(name,kind)
self.player_dict[name] = p
def init_players(self, player_dict):
# decription
for player_name in player_dict:
player_kind = player_dict[player_name]
self.add_player(player_name,player_kind)
def init_board(self, board_length, number_of_ships):
# decription
self.board = init_ships_for_player(board_length, number_of_ships)
def kill_player(self):
# If player have no more ships get player out of player loop
...