-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameLogic.py
More file actions
222 lines (191 loc) · 7.93 KB
/
GameLogic.py
File metadata and controls
222 lines (191 loc) · 7.93 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
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
from gameToken import Token
from terminal_text_color import TextColor
import numpy as np
import os
# Initialize Textcolour
tc = TextColor()
# Constraints
Rows = 7
Columns = 6
# Functions
def buildTable():
os.system('cls')
token = Token()
table = np.empty((Rows, Columns,), dtype=Token)
for i in range(Rows):
for j in range(Columns):
table[i][j] = token
return table
def pToken():
while True:
try:
position1 = int(input('Enter horizontal position: '))
if position1 >= 0 and position1 < 7:
break
else:
print('Must be between 0 and 6')
except ValueError:
print('ERROR. Must be an integer between 0 and 6')
while True:
try:
position2 = int(input('Enter vertical position: '))
if position2 >= 0 and position2 < 6:
break
else:
print('Must be between 0 and 5')
except ValueError:
print('ERROR. Must be an integer between 0 and 5')
return position1, position2
def chooseOption(table):
while True:
option = input('\nEnter colour(R/G): ')
if option.upper() == 'R' or option.upper() == 'G':
break
else:
printTable(table)
print('ERROR. Must be RED (R) or green (G)')
return option
def writeRedToken(data):
data.occupied = True
data.colour = tc.default_red('R')
return data
def writegreenToGen(data):
data.occupied = True
data.colour = tc.default_green('G')
return data
def printTable(table):
os.system('cls')
for i in range(Rows):
print('\n')
for j in range(Columns):
print(table[i][j].colour, end=' ')
if i == 6 and j == 5:
print('\n')
def placeToken(table):
token = Token()
option = chooseOption(table)
while True:
pos1, pos2 = pToken()
if option.upper() == 'R' and table[pos1][pos2].occupied == False:
token = writeRedToken(token)
table[pos1][pos2] = token
break
elif option.upper() == 'G' and table[pos1][pos2].occupied == False:
token = writegreenToGen(token)
table[pos1][pos2] = token
break
else:
printTable(table)
print('Space occupied by another token')
print('Chosen colour:', option.upper())
return table
# WIN parameter is a boolean value
def areTokensHorizontal(table, win):
for i in range(Rows):
for j in range(Columns):
if j < 3:
if table[i][j].colour == table[i][j+1].colour == table[i][j+2].colour == table[i][j+3].colour == tc.default_red('R'):
printTable(table)
print('RED TEAM WINS\n')
win = True
else:
if table[i][j].colour == table[i][j+1].colour == table[i][j+2].colour == table[i][j+3].colour == tc.default_green('G'):
printTable(table)
print('GREEN TEAM WINS\n')
win = True
else:
if table[i][j].colour == table[i][j-1].colour == table[i][j-2].colour == table[i][j-3].colour == tc.default_red('R'):
printTable(table)
print('RED TEAM WINS\n')
win = True
else:
if table[i][j].colour == table[i][j-1].colour == table[i][j-2].colour == table[i][j-3].colour == tc.default_green('G'):
printTable(table)
print('GREEN TEAM WINS\n')
win = True
return win
def areTokensVertical(table, win):
for i in range(Rows):
for j in range(Columns):
if i < 4:
if table[i][j].colour == table[i+1][j].colour == table[i+2][j].colour == table[i+3][j].colour == tc.default_red('R'):
printTable(table)
print('RED TEAM WINS\n')
win = True
else:
if table[i][j].colour == table[i+1][j].colour == table[i+2][j].colour == table[i+3][j].colour == tc.default_green('G'):
printTable(table)
print('GREEN TEAM WINS\n')
win = True
else:
if table[i][j].colour == table[i-1][j].colour == table[i-2][j].colour == table[i-3][j].colour == tc.default_red('R'):
printTable(table)
print('RED TEAM WINS\n')
win = True
else:
if table[i][j].colour == table[i-1][j].colour == table[i-2][j].colour == table[i-3][j].colour == tc.default_green('G'):
printTable(table)
print('GREEN TEAM WINS\n')
win = True
return win
def areTokensDiagonalLeftRight(t, win):
for i in range(Rows):
for j in range(Columns):
if i < 4 and j < 3:
if t[i][j].colour == t[i+1][j+1].colour == t[i+2][j+2].colour == t[i+3][j+3].colour == tc.default_red('R'):
printTable(t)
print('RED TEAM WINS\n')
win = True
else:
if t[i][j].colour == t[i+1][j+1].colour == t[i+2][j+2].colour == t[i+3][j+3].colour == tc.default_green('G'):
printTable(t)
print('GREEN TEAM WINS\n')
win = True
else:
if t[i][j].colour == t[i-1][j-1].colour == t[i-2][j-2].colour == t[i-3][j-3].colour == tc.default_red('R'):
printTable(t)
print('RED TEAM WINS\n')
win = True
else:
if t[i][j].colour == t[i-1][j-1].colour == t[i-2][j-2].colour == t[i-3][j-3].colour == tc.default_green('G'):
printTable(t)
print('GREEN TEAM WINS\n')
win = True
return win
def areTokensDiagonalRightLeft(table, win):
for i in range(Rows):
for j in range(Columns):
if i < 4 and j > 2:
if table[i][j].colour == table[i+1][j-1].colour == table[i+2][j-2].colour == table[i+3][j-3].colour == tc.default_red('R'):
printTable(table)
print('RED TEAM WINS\n')
win = True
else:
if table[i][j].colour == table[i+1][j-1].colour == table[i+2][j-2].colour == table[i+3][j-3].colour == tc.default_green('G'):
printTable(table)
print('GREEN TEAM WINS\n')
win = True
elif i > 3 and j < 3:
if table[i][j].colour == table[i-1][j+1].colour == table[i-2][j+2].colour == table[i-3][j+3].colour == tc.default_red('R'):
printTable(table)
print('RED TEAM WINS\n')
win = True
else:
if table[i][j].colour == table[i-1][j+1].colour == table[i-2][j+2].colour == table[i-3][j+3].colour == tc.default_green('G'):
printTable(table)
print('GREEN TEAM WINS\n')
win = True
return win
def runGame():
t = buildTable()
win = False
while not(win):
printTable(t)
placeToken(t)
win = areTokensHorizontal(t, win)
if win == False:
win = areTokensVertical(t, win)
if win == False:
win = areTokensDiagonalLeftRight(t, win)
if win == False:
win = areTokensDiagonalRightLeft(t, win)