-
Notifications
You must be signed in to change notification settings - Fork 3
/
Battle_ships.py
88 lines (70 loc) · 2.32 KB
/
Battle_ships.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
# Kata link:
# https://www.codewars.com/kata/58d06bfbc43d20767e000074
# -------------------------------------
# Solution 1:
import math
def damaged_or_sunk(board, attacks):
result = {
'sunk': 0,
'damaged': 0,
'not_touched': 0,
'points': 0
}
# how many boats there are and length [length, damaged, tauch_control, total_length]
boats = {el:[0, 0, 1, 0] for b in board for el in b if el > 0}
for i in boats.keys():
result['not_touched'] += 1
for b in board:
boats[i][0] += b.count(i)
boats[i][3] += boats[i][0]
for a in attacks:
pos = board[a[1] * -1][a[0] - 1]
if pos != 0:
# Decrease boat length
boats[pos][0] -= 1
# damaged of boat
boats[pos][1] += 0.5
# change touch
boats[pos][2] = 0
if boats[pos][0] == 0:
# Sunken boats
result['sunk'] += 1
result['points'] += 1
for tp in boats.values():
if tp[2] == 0:
result['not_touched'] -= 1
if tp[0] > 0:
result['damaged'] += tp[1]
result['points'] += 0.5 * (len(boats.keys()) - result['not_touched'] - result['sunk'])
result['points'] -= result['not_touched']
if math.modf(result['points'])[0] == 0.0:
result['points'] = round(result['points'])
if math.modf(result['damaged'])[0] == 0.5:
result['damaged'] = math.ceil(result['damaged'])
else:
result['damaged'] = round(result['damaged'])
return result
# -------------------------------------
# Basic Tests
board = [ [0, 0, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 0] ]
attacks = [[3, 1], [3, 2], [3, 3]];
result = damaged_or_sunk(board, attacks)
print("Game 1 result: { 'sunk': 1, 'damaged': 0 , 'not_touched': 0, 'points': 1}")
print(result['sunk'], 1)
print(result['damaged'], 0)
print(result['not_touched'], 0)
print(result['points'], 1)
board = [ [3, 0, 1],
[3, 0, 1],
[0, 2, 1],
[0, 2, 0] ]
attacks = [[2, 1], [2, 2], [ 3, 2], [3, 3]]
result = damaged_or_sunk(board, attacks)
print("Game 2 result: { 'sunk': 1, 'damaged': 1 , 'not_touched': 1, 'points': 0.5}")
print(result['sunk'], 1)
print(result['damaged'], 1)
print(result['not_touched'], 1)
print(result['points'], 0.5)
print(result)