-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect4.py
130 lines (116 loc) · 4.57 KB
/
connect4.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
class Connect4:
def __init__(self):
self.numberOfColumns = 7
self.numberOfLines = 6
self.board = [ [ ' ' for _ in range( self.numberOfColumns)] for _ in range(self.numberOfLines) ]
def displayBoard(self):
for i, line in enumerate(self.board):
# Printing the line separators
print("_" * self.numberOfColumns * 4)
# Printing the line
print(*line, sep=' |')
# Printing numbers
print(' '.join(str(x) for x in range(self.numberOfColumns)))
def isAvailable(self, line, column):
if line[column] == ' ':
return True
return False
def player_choice(self):
choice = int(input("Please select an empty space between 0 and 6 : "))
while self.board[0][choice] != ' ':
choice = int(input("This column is full. Please choose between 0 and 6 : "))
return choice
def player_input(self):
player1 = input("Please pick a marker 'X' or 'O' ")
while True:
if player1.upper() == 'X':
player2='O'
print("You've choosen " + player1 + ". Player 2 will be " + player2)
return player1.upper(),player2
elif player1.upper() == 'O':
player2='X'
print("You've choosen " + player1 + ". Player 2 will be " + player2)
return player1.upper(),player2
else:
player1 = input("Please pick a marker 'X' or 'O' ")
def checkLines(self, marker, board=None):
if board is None:
board=self.board
# Checkin lines
for line in board:
for i in range(0,len(line)):
if i < len(line) - 3:
if line[i] == line[i+1] == line[i+2] == line[i+3] == " " + marker:
return True
def checkDiags(self, marker):
diagBoard = []
for i, line in enumerate(self.board):
for idx, item in enumerate(line):
# Find of there is some marker
if item == ' ' + marker:
diagBoard.append(int(str(i)+str(idx)))
for item in diagBoard:
if int(item) + 11 in diagBoard and int(item) + 22 in diagBoard and int(item) + 33 in diagBoard:
return True
for item in reversed(diagBoard):
if int(item) - 9 in diagBoard and int(item) - 18 in diagBoard and int(item) - 27 in diagBoard:
return True
def generateReversedBoard(self):
reversedBoard = []
for line in self.board:
for index, item in enumerate(line):
try:
reversedBoard[index].append(item)
except:
reversedBoard.append([])
reversedBoard[index].append(item)
return reversedBoard
def play(self, playercolumn, marker):
for item in reversed(self.board):
if self.isAvailable(item, playercolumn):
item[playercolumn] = " " + marker
return True
return False
c = Connect4()
# First while lopp init
game = True
while game:
# Choose your marker
players = c.player_input()
# Display the board
c.displayBoard()
# Second while loop init
win = False
i = 1
while not win:
# Start Playing
if i % 2 == 0:
currentPlayer = "Player1"
marker = players[1]
else:
currentPlayer = "Player2"
marker = players[0]
# Player to choose where to put the mark
position = c.player_choice()
if not c.play(position, marker):
print(f"Column {position} full")
# Generate the reversed board
reversedBoard = c.generateReversedBoard()
# Check if won
if c.checkLines(marker) or c.checkLines(marker, reversedBoard) or c.checkDiags(marker):
# update the win to exit the second while loop
win = True
c.displayBoard()
print(f"Game won by {currentPlayer}")
# Ask for replay.
# If no, change the first loop game = True to False
# If yes, reset our class with fresh new datas
replay = input("Do you want to play again (Y/N) ? ")
if replay.lower() == 'n':
game = False
print("Game ended !")
else:
c = Connect4()
break
c.displayBoard()
i += 1