-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.jl
106 lines (91 loc) · 2.99 KB
/
logic.jl
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
# sinkships for julia
module bslogic
function create_ships(mapsize, shiplist)
# shiplist gives the number of ships for each size
# e.g. [2,0,0,1] means 2 1-field and one 4-field ship
initmap = -ones(mapsize)
m,n = mapsize[1], mapsize[2]
#println(initmap)
#println("Following number of ships with increasing number are being created ", shiplist)
maxshiplength = length(shiplist)
# ship tensor is an overlay of single-ship maps
ship_tensor = zeros(m, n, sum(shiplist))
ship_count = 0
isminusone(x) = x .==-1
for (i, n_ships) in enumerate(reverse(shiplist))
for j = 1:n_ships
shipsize = maxshiplength - i + 1
#println("Inserting ship of size ", shipsize)
for count = 1:100
# randomized ship position
rand_hor_vert = rand(0:1)
if rand_hor_vert == 0
randrow = rand(1:m-shipsize + 1)
randcol = rand(1:n)
else
randrow = rand(1:m)
randcol = rand(1:n-shipsize + 1)
end
#println(randrow, randcol, rand_hor_vert)
if rand_hor_vert == 0
row, col = randrow:randrow+shipsize-1, randcol
extended_row, extended_col = max(randrow -1, 1):min(randrow+shipsize, m), max(randcol-1, 1):min(randcol + 1, n)
else
row, col = randrow, randcol:randcol+shipsize-1
extended_row, extended_col = max(randrow-1, 1):min(randrow + 1, m), max(randcol -1, 1):min(randcol+shipsize, n)
end
# tentative ship placement (limited trials given by count)
shipslot = initmap[row, col]
if sum(shipslot) == -shipsize
# fill vicinity of shipslot with water
initmap[extended_row, extended_col] = 0
# place ship inside water
initmap[row,col] = 1
#println(initmap)
# add ship to ship tensor
ship_count += 1
ship_tensor[row, col, ship_count] = 1
break
end
if count == 1000
println("WARNING, no space found on map. A ship of size ", shipsize,
" has not been placed. Make sure the map is big enough to contain all the ships. ",
"Bare in mind that ships need to be separated by at least one field of water ",
"(also diagonally)")
end
end
end
end
# fill the rest of the map with water
emptyfields = find(initmap .== -1)
initmap[emptyfields] = 0
return initmap, ship_tensor
end
function update_game!(guess, info_map, actual_map, ship_tensor)
#println("Guess was ", guess)
row_guess = guess[1]
col_guess = guess[2]
info_map[row_guess, col_guess] = actual_map[row_guess, col_guess]
#println(actual_map[row_guess, col_guess])
# check when there is a hit
if actual_map[row_guess, col_guess] == 1
game_state = 1
# check whether a ship was sunk
ship_id = findn(ship_tensor[row_guess,col_guess, :])
#println("ship number ", ship_id, " was hit")
diffmap = ship_tensor[:,:, ship_id] - info_map
if maximum(diffmap) < 2
game_state = 2
#println("ship number ", ship_id, " was sunk")
end
# check whether all ships are sunk
diffmap = actual_map - info_map
if maximum(diffmap) < 2
game_state = 3
end
else
game_state = 0
end
return info_map, game_state
end
end # end module