-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2017-BIO-2.py
More file actions
163 lines (137 loc) · 5.59 KB
/
2017-BIO-2.py
File metadata and controls
163 lines (137 loc) · 5.59 KB
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
# Dots and Boxes
class Grid:
def __init__(self, width, height):
self.width = width
self.height = height
self.size = width * height
# Create arrays for vertical and horizontal lines
vertical_lines = []
for x in range(width):
column = []
for y in range(height-1):
column.append(False)
vertical_lines.append(column)
horizontal_lines = []
for y in range(height):
row = []
for x in range(width-1):
row.append(False)
horizontal_lines.append(row)
self.horizontal_lines = horizontal_lines
self.vertical_lines = vertical_lines
# Create arrays for boxes won
boxes_won = []
for y in range(height-1):
row = []
for x in range(width-1):
row.append(0)
boxes_won.append(row)
self.boxes_won = boxes_won
def display_boxes_won(self):
chars = ["*","X","O"]
for row in self.boxes_won:
row_str = ""
for col in row:
row_str += chars[col]
print(row_str)
def draw_line(self, player, position, direction):
# Turn position into x, y
x = position % self.width
y = position // self.width
# Get x, y of line
if(direction == 0):
# Upwards
if(y == 0):
return False # impossible
else:
y -= 1
elif (direction == 2):
# Down
if (y == (self.height-1)):
return False # impossible
# Don't need to change y
elif (direction == 3):
# Left
if (x == 0):
return False # impossible
else:
x -= 1
elif (direction == 1):
# Right
if (x == (self.width - 1)):
return False # impossible
# Don't need to change x
if(direction & 1 == 0):
# Even - vertical lines
if(self.vertical_lines[x][y]): return False # Already taken
self.vertical_lines[x][y] = True
if(x != (self.width - 1) and y != 0):
if(self.horizontal_lines[y][x] and self.horizontal_lines[y+1][x] and self.vertical_lines[x+1][y]):
# Box to right
print(player, x, y, "right", self.horizontal_lines, self.vertical_lines)
self.boxes_won[y][x] = player
if(x != 0 and y != (self.height - 1)):
if (self.horizontal_lines[y][x-1] and self.horizontal_lines[y + 1][x-1] and self.vertical_lines[x - 1][y]):
# Box to left
print(player, x, y, "left", self.horizontal_lines, self.vertical_lines)
self.boxes_won[y][x-1] = player
else:
# Odd - horizontal lines
if (self.horizontal_lines[y][x]): return False # Already taken
self.horizontal_lines[y][x] = True
if (y != (self.height - 1) and x != (self.width - 1)):
if (self.vertical_lines[x][y] and self.vertical_lines[x + 1][y] and self.horizontal_lines[y + 1][x]):
# Box below
print(player, x, y, "below", self.horizontal_lines, self.vertical_lines)
self.boxes_won[y][x] = player
if (y != 0 and x != (self.width - 1)):
if (self.vertical_lines[x][y - 1] and self.vertical_lines[x + 1][y - 1] and self.horizontal_lines[y - 1][x]):
# Box above
print(player, x, y, "above", self.horizontal_lines, self.vertical_lines)
self.boxes_won[y - 1][x] = player
return True
class Player:
def __init__(self, id, position, modifier, anticlockwise):
self.position = position-1 # zero-indexed
self.modifier = modifier
self.anticlockwise = anticlockwise
self.id = id
def turn(self, grid):
# Increase pos by modifier
self.position += self.modifier
self.position = self.position % grid.size
for i in range(grid.size): # Keep trying for every position in grid if not successful
if(self.anticlockwise):
for direction in range(0, -4, -1):
if(grid.draw_line(self.id, self.position, direction%4)):
print(self.id, self.position, "urdl"[direction])
return # Successful
else:
for direction in range(4):
if(grid.draw_line(self.id, self.position, direction)):
print(self.id, self.position, "urdl"[direction])
return # Successful
# Not successful - increment
self.position += 1
self.position = self.position % grid.size
class Game:
def __init__(self, grid, turns, *players): # Player classes w/ pos and mod
self.grid = grid
if(turns > 60):
self.turns = 60
elif(turns < 1):
self.turns = 1
else:
self.turns = turns
self.players = players
self.player_n = len(players)
def play(self):
for i in range(self.turns):
self.players[i % self.player_n].turn(self.grid)
grid = Grid(6, 6)
player_1 = Player(1, int(input("Player 1 Pos: ")), int(input("Player 1 Mod: ")), False)
player_2 = Player(2, int(input("Player 2 Pos: ")), int(input("Player 2 Mod: ")), True)
game = Game(grid, int(input("No. of Turns: ")), player_1, player_2)
game.play()
grid.display_boxes_won()
# Still bugs