/
DiskGame.py
183 lines (152 loc) · 5.33 KB
/
DiskGame.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
from Board import *
""" Over-engineered three on a line game """
class DiskGame:
"""Game where people have to create a pattern by adding disks at the top of collumns aka three on a line overbuild beta edition"""
def __init__ (self, board_size=(7,7), playercount=2):
"""Initiate with a board_size (height, width)"""
self.board = Board(board_size)
self.history = []
self.names = []
self.playercount = playercount
self.movenumber = 0
self.last_collumn = 0
def ask_names (self):
""" Ask each player hes name and put it in the player list """
while len(self.names) < self.playercount:
self.names.append(input("Wat is de naam van speler " + str(len(self.names) + 1) + "? \n"))
if self.names[-1] == "3secret5me":
self.open_secret_menu()
def print_board (self):
""" Print the board to a terminal """
print(self.board)
def print_history (self):
print("Het spel verliep als volgt:")
for data in self.history:
print("Speler " + self.names[data[0]] + " heeft de schijf laten vallen in kolom " + str(data[1]))
def open_secret_menu (self):
self.names = []
for i in range(0, 15):
print("")
print("=====================================")
print("= Welcome in the super secret menu! =")
print("=====================================")
print("")
print("Possible commands:")
print(" - playercount [int: newplayercount]")
print(" Change the playercount to [newplayercount]")
print("")
print(" - size [int: width] [int: height]")
print(" Change the board size to ([width], [height])")
print("")
print(" - gamemode [str: gamemode]")
print(" Change the gamemode to one of the following: ")
print("")
print(" tryhard: a gamemode thats way too difficult")
print(" threeonaline: a gamemode where the first player does always win")
print(" fouronaline: a gamemode in which the first player does NOT always win")
print(" disksonaline [int: disks]: generalized on a line game")
print("")
print(" - pattern [list: pattern]")
print(" Manually add a win condition, examples:")
print(" [[1], [1], [1], [1]]: four disks vertically")
print(" [[1, 1, 1, 1]]: four disks horizontally")
print(" [[0, 0, 1], [0, 1, 0], [1, 0, 0]]: three disks diagonally")
print("")
print(" - quit")
print(" Exit this menu")
print("")
print("")
while True:
command = input("Please provide me with your command: ").split()
if command[0] == "quit":
for i in range(0, 15):
print("")
break
if command[0] == "reset":
print("Resetting win patterns\n")
self.board.reset_patterns()
if command[0] == "playercount":
try:
self.playercount = int(command[1])
print("New playercount: " + command[1] + "\n")
except:
print("Invalid input! \n")
if command[0] == "size":
try:
self.board.changesize((int(command[1]), int(command[2])))
print("New boardsize: (" + command[1] + ", " + command[2] + ")\n")
except:
print("Invalid input! \n")
if command[0] == "gamemode":
try:
if command[1] == "tryhard":
self.board.add_win_pattern([
[1, 1, 1],
[1, 0, 1]])
print("Adding patterns:\n")
print("Head:")
print(" [1, 1, 1]")
print(" [1, 0, 1]")
if command[1] == "threeonaline":
self.board.add_win_pattern([[1, 1, 1]]) #Horizontal pattern
self.board.add_win_pattern([[1],[1],[1]]) #Vertical pattern
self.board.add_win_pattern([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
])
self.board.add_win_pattern([
[0, 0, 1],
[0, 1, 0],
[1, 0, 0]
])
print("Adding patterns:\n")
print("Horizontal:")
print(" [1, 1, 1]\n")
print("Vertical:")
print(" [[1], [1], [1]]\n")
print("Diagonals:")
print(" [1, 0, 0]")
print(" [0, 1, 0]")
print(" [0, 0, 1] \n")
print(" [0, 0, 1]")
print(" [0, 1, 0]")
print(" [1, 0, 0] \n")
if command[1] == "fouronaline":
print("WIP")
if command[1] == "disksonaline":
print("WIP")
except:
print("Invalid input! \n")
if command[0] == "addpattern":
pass
def ask_target_collumn (self):
"""Ask the player in which collumn he wants to drop hes disk starting at 1"""
while True:
try:
collumn = int(input(self.names[self.movenumber % len(self.names)] + ", in welke kolom wens je je stuk te laten vallen (1 - " + str(self.board.board_size[0]) + ") \n"))
if collumn > 0 and collumn <= self.board.board_size[0]:
return collumn
except:
pass
print("Voer een geldig nummer in tussen 1 en " + str(self.board.board_size[0]))
def add_win_pattern (self, pattern):
self.board.add_win_pattern(pattern)
def do_next_move (self):
""" Ask the player which collumn and put a disk there """
while True:
disk = (self.movenumber % len(self.names)) + 1
collumn = self.ask_target_collumn() - 1
if self.board.drop_disk(disk, collumn):
break
self.history.append((disk - 1, collumn + 1))
self.last_collumn = collumn
self.movenumber += 1
def start_game (self):
self.ask_names()
while (not self.board.check_win(self.last_collumn)) and (not self.board.board_full()):
self.print_board()
self.do_next_move()
self.print_board()
print("Proficiat " + self.names[(self.movenumber - 1) % len(self.names)] + ", u wint! \n")
self.print_history()