-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.py
63 lines (43 loc) · 1.72 KB
/
TicTacToe.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
#James Warda CS 474 UIC Computer Science Fall 2018
#import board class which handles board opperations such as checking for a win and making a move
from Board import Board
def main():
playAgain = "y"
while playAgain.lower() == 'y':
b = Board(3,3)
print("Type the row and column in which you wish to move")
gameResult = playGame(b)
if gameResult == -1:
playAgain = input("Tie Game! Play again? (y/n): ")
else:
playAgain = input("Player " + str(gameResult) + " Wins! Play again? (y/n) ")
print("Goodbye!")
#function runs the game loop until player has won or tie
def playGame(board):
numTurns = 0
while True:
board.printBoard()
numTurns += 1
player = 1 if numTurns % 2 != 0 else 2
playerPiece = 'X' if player == 1 else 'O'
userInput = getPlayerInput(numTurns, board, player, playerPiece)
board.addMove(userInput[0], userInput[1], userInput[2])
print("")
if board.checkForWin() != 0:#if a player has won or tie
board.printBoard()
return board.checkForWin()
#function gets the users next move
def getPlayerInput(turn, board, player, playerPiece):
userInput = input("Turn " + str(turn) + ": Player " + str(player) + " (" + playerPiece + "), choose your move: ")
while True:
userInput = userInput.strip()
userInput = userInput.split(" ")
if len(userInput) != 2:
userInput = input("Invlaid move, only enter two integers seperated by a space, try again: ")
elif board.isMoveValid(userInput[0], userInput[1]) != "":
userInput = input("Invlaid move, " + board.isMoveValid(userInput[0], userInput[1]) + ", try again: ")
else:
userInput.append(playerPiece)
return userInput
if __name__ == '__main__':
main()