-
Notifications
You must be signed in to change notification settings - Fork 16
/
Connect4Client.py
executable file
·65 lines (59 loc) · 1.95 KB
/
Connect4Client.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
from PlayerClasses import Player, Human
from AIClass import AI
from GameBoard import GameBoard
class GameClient:
board = None
human = None
ai = None
winnerFound = False
humansTurn = True
currentRound = 1
MAX_ROUNDS = 42 #max number of turns before game baord is full
def __init__(self):
self.board = GameBoard()
self.human = Human('O')
difficulty = int(input("Enter a difficulty from 1 to 6.\nYou can go higher, but performance will take longer.\n> "))
showScores = input("Show scores? (y/n)> ")
self.ai = AI('X', difficulty, showScores)
def play(self):
print("Playing game...")
self.board.printBoard()
winner = "It's a DRAW!"
while self.currentRound <= self.MAX_ROUNDS and not self.winnerFound:
if self.humansTurn:
print("Player's turn...")
playedChip = self.human.playTurn(self.board)
self.winnerFound = self.board.isWinner(self.human.chip)
if self.winnerFound: winner = "PLAYER wins!"
self.humansTurn = False
print("Player played chip at column ", playedChip[1]+1)
else:
print("AI's turn...")
playedChip = self.ai.playTurn(self.board)
self.winnerFound = self.board.isWinner(self.ai.chip)
if self.winnerFound: winner = "AI wins!"
self.humansTurn = True
print("AI played chip at column ", playedChip[1]+1)
self.currentRound += 1
self.board.printBoard()
return winner
def reset(self):
#reset variables
self.currentRound = 1
self.winnerFound = False
self.humansTurn = True
self.board.resetBoard()
difficulty = int(input("Enter a difficulty from 1 to 6.\nYou can go higher, but performance will take longer.\n> "))
self.ai.setDifficulty(difficulty)
def endGame(winner):
print(winner, end=" ")
userInput = input("Play again? (y/n)\n")
return True if userInput == 'y' else False
if __name__ == "__main__":
gameClient = GameClient()
winner = gameClient.play()
playAgain = endGame(winner)
while playAgain:
gameClient.reset()
winner = gameClient.play()
playAgain = endGame(winner)