-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect_4_with_class.py
149 lines (115 loc) · 3.82 KB
/
connect_4_with_class.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
class Board(object):
def __init__(self, number_of_rows, number_of_columns):
self.number_of_rows = number_of_rows
self.number_of_columns = number_of_columns
self.rows = []
def build(self):
for i in range(0, self.number_of_rows):
row = []
for j in range(0, self.number_of_columns):
row.append(None)
self.rows.append(row)
def __str__(self):
output = ''
for row in self.rows:
line = ''
for column in row:
if column is None:
line += ' |'
else:
line += column.symbol + '|'
line += '\n'
output += line
return output
def show(self):
print(self)
def drop(self, coin, chosen_column):
for i in range(self.number_of_rows - 1, 0, -1):
if self.rows[i][chosen_column] is None:
self.rows[i][chosen_column] = coin
coin.y = i
coin.x = chosen_column
break
pass
def check_for_winner(self, coin):
winner = False
#horizontal check
array_to_check = self.rows[coin.y]
winner = self.check_connected_coins(coin, array_to_check)
if winner:
return True
# vertical check
array_to_check = []
for row in self.rows:
array_to_check.append(row[coin.x])
winner = self.check_connected_coins(coin, array_to_check)
if winner:
return True
#diagonal check_left
array_to_check = []
#diagonal_check_right
pass
def check_connected_coins(self, coin, array):
consecutive_coins = 0
for item in array:
if item is None:
consecutive_coins = 0
elif item.symbol == coin.symbol:
consecutive_coins += 1
if consecutive_coins == 4:
return True
pass
class Coin(object):
def __init__(self, symbol):
self.symbol = symbol
self.x = None
self.y = None
pass
def __str__(self):
return "position x: {0}, y: {1}".format(self.x, self.y)
class Connect4():
def __init__(self):
self.player1 = None
self.player2 = None
self.active_player = None
self.number_of_rows = 6
self.number_of_columns = 7
self.board = Board(self.number_of_rows, self.number_of_columns)
def setup(self):
self.board.build()
self.board.show()
self.player1 = Player("Jonne", "X")
self.player2 = Player("Michele", "O")
self.choose_starting_player()
def choose_starting_player(self):
self.active_player = self.player1
pass
def start(self):
chosen_column_input = ('Choose your column {0} a number from 1 to {1}:'.format(self.active_player.name, self.number_of_columns))
try:
coin = Coin(self.active_player.symbol)
chosen_column = int(input(chosen_column_input)) - 1
self.board.drop(coin, chosen_column)
self.board.show()
if self.board.check_for_winner(coin):
print("{0} won the game".format(self.active_player.name))
return
self.next_turn()
# print("you chose column {0}".format(chosen_column))
except ValueError:
print('invalid input!')
pass
def next_turn(self):
if self.active_player == self.player1:
self.active_player = self.player2
else:
self.active_player = self.player1
self.start()
pass
class Player():
def __init__(self, name, symbol):
self.name = name
self.symbol = symbol
game = Connect4()
game.setup()
game.start()