-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
84 lines (76 loc) · 3.36 KB
/
main.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
from reversi import Reversi
def main():
# The main function of the algorithm
continueGame = True
rev = Reversi()
print(
"Welcome to 'Reversi'!",
"Reversi is a 2-player game, played on an 8 x 8 board. ",
"Players take turns placing their disks on the board",
"with their assigned colour (Black and White). ",
"Black is the first player to move. A player may place their ",
"disk anywhere on the board, as long as it surrounds a group of the opponents ",
"disks (vertically, horizontally, or diagonally) on opposite sides. ",
"Any disks that you surround will become yours and will flip over to your colour. ",
"The game is over when the current player has no possible legal move.",
"Please enter your moves in the form 'row column', separated by a single space",
"You will be playing against an artificial intelligence (AI)",
"If at any point you would like to stop playing, type 'quit' when prompted for a row.", sep='\n')
# While the program is on
while continueGame:
gameOn = True
rev.newGame()
smartBot = rev.smartOrNot()
playerColour, botColour = rev.setPlayerColour()
# While there is a game on
validMove = (playerColour == 'black')
playersTurn = validMove
while gameOn:
# Don't display the board again if the
# player's input was invalid
if validMove and playersTurn:
rev.displayBoard()
try:
if playersTurn:
if rev.findValidMoves(playerColour) == []:
gameOn = False
else:
playerPosition = input(
"Please enter the position you would like to play. (row column) ")
gameOn = rev.checkPlayerInput(playerPosition)
if gameOn:
playerPosition = (
int(playerPosition[0]), int(playerPosition[2]))
rev.isPositionValid(playerPosition, playerColour)
except AssertionError as e:
validMove = False
print(e.args[0])
except Exception as e:
validMove = False
print("Error: Invalid input. Please enter an integer from 0 to 7.")
else:
validMove = True
if gameOn:
if playersTurn:
rev.makeMovePlayer(playerPosition)
playersTurn = False
else:
if smartBot:
botPosition = rev.makeMoveSmart()
else:
botPosition = rev.makeMoveNaive()
if botPosition == False:
gameOn = False
else:
rev.makeMovePlayer(botPosition, bot=True)
playersTurn = True
rev.getScore(playerColour)
rev.getScore(botColour)
rev.decideWinner()
guess = ''
while guess not in ['Y', 'N']:
guess = input("Would you like to play again? (Y/N) ").upper()
if guess.upper() == 'N':
print('Thanks for playing!')
continueGame = False
main()