#ticTacToe.py
#pro sandy
#Python 3, ver: 3.3.2
#Wing IDE 101, ver: 5.0.1-1 (rev 30370)
#This is a simple Tic Tac Toe game with only command line interface. It operates
#both 1 and 2 player. For 1 player, the computer opponent has three difficulty
#levels, easy, medium, and hard.
#
#For easy level, the computer picks his next move at random.
#
#For medium level, the computer checks first for the possibility for him to win,
#then checks for the ability to block the opponent from winning.
#
#For hard level, the computer implements the perfect game strategy.
#
################################################################################
#Edit Log
#Created February 21, 2014
#
#February 23, 2014 - added medium level. The algorithm is designed to first for
#attempt for the ability to win in 1 move. If not possible, it attempts to block
#the user from winning in 1 move.
#
#March 13, 2014 - began work on hard level, added player names
#
#March 16, 2014 - corrected error in hard level for blocking fork
#
#To add: Other algorithms for computer for harder levels, ask
#for user names, develop graphical interface, add comments, organize layout of
#code
#
################################################################################
#modules
import sys
from random import randrange
from time import sleep
def initialize():
initialBoard = [[" ","|"," ","|"," "],["-","-","-","-","-",],
[" ","|"," ","|"," "], ["-","-","-","-","-",],
[" ","|"," ","|"," "]]
board = list(initialBoard)
return board
def displayBoard(board):
print("\n")
for r in range(0,5,1):
print(board[r][0], board[r][1], board[r][2],
board[r][3], board[r][4])
print("\n")
return
def displayCleanBoard(board):
for i in board:
for item in i:
numbers = ["1","2","3","4","5","6","7","8","9",1,2,3,4,5,6,7,8,9]
if item in numbers:
iIndex = board.index(i)
itemIndex = board[iIndex].index(item)
board[iIndex][itemIndex] = " "
displayBoard(board)
return
def getNames(typeGame):
names = ["",""]
if typeGame == "single":
names[0] = input("\nWhat is your name? ")
elif typeGame == "double":
names[0] = input("\nPlayer 1, enter your name: ")
names[1] = input("\nPlayer 2, enter your name: ")
return names
def whosFirst():
return randrange(0,2,1) #Computer is user 1
def checkCat(board):
CAT = False
if len(spacesAvailableTracker(board)) == 0:
CAT = True
return CAT
def catEnd(board):
displayBoard(board)
print("\nSorry, scratch this one up for Cat.")
return
def checkWinner(board):
winner = False
if board[0][0] == board[0][2] == board[0][4] == "X":
winner = True
if board[2][0] == board[2][2] == board[2][4] == "X":
winner = True
if board[4][0] == board[4][2] == board[4][4] == "X":
winner = True
if board[0][0] == board[2][0] == board[4][0] == "X":
winner = True
if board[0][2] == board[2][2] == board[4][2] == "X":
winner = True
if board[0][4] == board[2][4] == board[4][4] == "X":
winner = True
if board[0][0] == board[2][2] == board[4][4] == "X":
winner = True
if board[0][4] == board[2][2] == board[4][0] == "X":
winner = True
if board[0][0] == board[0][2] == board[0][4] == "O":
winner = True
if board[2][0] == board[2][2] == board[2][4] == "O":
winner = True
if board[4][0] == board[4][2] == board[4][4] == "O":
winner = True
if board[0][0] == board[2][0] == board[4][0] == "O":
winner = True
if board[0][2] == board[2][2] == board[4][2] == "O":
winner = True
if board[0][4] == board[2][4] == board[4][4] == "O":
winner = True
if board[0][0] == board[2][2] == board[4][4] == "O":
winner = True
if board[0][4] == board[2][2] == board[4][0] == "O":
winner = True
return winner
def winnerEnd(typePlay, board, winner, playerNames):
if typePlay == "single":
if winner == 0:
print("Sorry, %s. The computer one this round" % playerNames[0])
else:
print("Tic Tac Toe! Three in a row!\nCongratulations! You won, %s\n!" %playerNames[0])
else:
if winner == 0:
print("Tic Tac Toe! Three in a row!\n%s wins! Congratulations!\n" %playerNames[0])
else:
print("Tic Tac Toe! Three in a row!\n%s wins! Congratulations!\n" %playerNames[1])
return
def spacesAvailableTracker(board):
spacesAvailable = []
XO = ["X","O"]
if board[0][0] not in XO:
spacesAvailable.append(0)
if board[0][2] not in XO:
spacesAvailable.append(1)
if board[0][4] not in XO:
spacesAvailable.append(2)
if board[2][0] not in XO:
spacesAvailable.append(3)
if board[2][2] not in XO:
spacesAvailable.append(4)
if board[2][4] not in XO:
spacesAvailable.append(5)
if board[4][0] not in XO:
spacesAvailable.append(6)
if board[4][2] not in XO:
spacesAvailable.append(7)
if board[4][4] not in XO:
spacesAvailable.append(8)
return spacesAvailable
def userMakeMove(board, userToken, typePlay, playerID, names):
spacesAvailable = spacesAvailableTracker(board)
for item in spacesAvailable:
if item == 0:
board[0][0] = 1
elif item == 1:
board[0][2] = 2
elif item == 2:
board[0][4] = 3
elif item == 3:
board[2][0] = 4
elif item == 4:
board[2][2] = 5
elif item == 5:
board[2][4] = 6
elif item == 6:
board[4][0] = 7
elif item == 7:
board[4][2] = 8
elif item == 8:
board[4][4] = 9
else:
print("Unexpected Error. spacesAvailable in userMakeMore out of sync")
sys.exit()
inputNotValid = True
while inputNotValid:
displayBoard(board)
if typePlay == "single":
userMoveChoice = input("Make your move! Choose a square from the board.\nSquare Number: ")
else:
if playerID == 1:
userMoveChoice = input("%s, make your move! Choose a square from the board.\nSquare Number: " % names[0])
else:
userMoveChoice = input("%s, make your move! Choose a square from the board.\nSquare Number: " % names[1])
try:
userMoveChoice = int(userMoveChoice)
except ValueError:
print("\nSorry, invalid input.\n")
if userMoveChoice-1 not in spacesAvailable:
print("\nSorry, invalid input.\n")
else:
inputNotValid = False
if userMoveChoice == 1:
row = 0
column = 0
elif userMoveChoice == 2:
row = 0
column = 2
elif userMoveChoice == 3:
row = 0
column = 4
elif userMoveChoice == 4:
row = 2
column = 0
elif userMoveChoice == 5:
row = 2
column = 2
elif userMoveChoice == 6:
row = 2
column = 4
elif userMoveChoice == 7:
row = 4
column = 0
elif userMoveChoice == 8:
row = 4
column = 2
elif userMoveChoice == 9:
row = 4
column = 4
else:
print("Error. userMoveChoice passed while not between 1-9.")
sys.exit()
board[row][column] = userToken
displayCleanBoard(board)
return board
def easyComp(spacesAvailable):
pick = randrange(0,len(spacesAvailable),1)
return pick
def mediumComp(board, spacesAvailable, compToken):
if compToken == "X":
notCompToken = "O"
else:
notCompToken = "X"
if board[0][0] == board[0][2] == notCompToken and spacesAvailable.count(2) != 0:
pick = spacesAvailable.index(2)
elif board[0][0] == board[0][4] == notCompToken and spacesAvailable.count(1) != 0:
pick = spacesAvailable.index(1)
elif board[0][2] == board[0][4] == notCompToken and spacesAvailable.count(0) != 0:
pick = spacesAvailable.index(0)
elif board[2][0] == board[2][2] == notCompToken and spacesAvailable.count(5) != 0:
pick = spacesAvailable.index(5)
elif board[2][0] == board[2][4] == notCompToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[2][2] == board[2][4] == notCompToken and spacesAvailable.count(3) != 0:
pick = spacesAvailable.index(3)
elif board[4][0] == board[4][2] == notCompToken and spacesAvailable.count(8) != 0:
pick = spacesAvailable.index(8)
elif board[4][0] == board[4][4] == notCompToken and spacesAvailable.count(7) != 0:
pick = spacesAvailable.index(7)
elif board[4][2] == board[4][4] == notCompToken and spacesAvailable.count(6) != 0:
pick = spacesAvailable.index(6)
elif board[0][0] == board[2][0] == notCompToken and spacesAvailable.count(6) != 0:
pick = spacesAvailable.index(6)
elif board[0][0] == board[4][0] == notCompToken and spacesAvailable.count(3) != 0:
pick = spacesAvailable.index(3)
elif board[2][0] == board[4][0] == notCompToken and spacesAvailable.count(0) != 0:
pick = spacesAvailable.index(0)
elif board[0][2] == board[2][2] == notCompToken and spacesAvailable.count(7) != 0:
pick = spacesAvailable.index(7)
elif board[0][2] == board[4][2] == notCompToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[2][2] == board[4][2] == notCompToken and spacesAvailable.count(1) != 0:
pick = spacesAvailable.index(1)
elif board[0][4] == board[2][4] == notCompToken and spacesAvailable.count(8) != 0:
pick = spacesAvailable.index(8)
elif board[0][4] == board[4][4] == notCompToken and spacesAvailable.count(5) != 0:
pick = spacesAvailable.index(5)
elif board[2][4] == board[4][4] == notCompToken and spacesAvailable.count(2) != 0:
pick = spacesAvailable.index(2)
elif board[0][0] == board[4][4] == notCompToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[0][0] == board[2][2] == notCompToken and spacesAvailable.count(8) != 0:
pick = spacesAvailable.index(8)
elif board[2][2] == board[4][4] == notCompToken and spacesAvailable.count(0) != 0:
pick = spacesAvailable.index(0)
elif board[0][4] == board[4][0] == notCompToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[0][4] == board[2][2] == notCompToken and spacesAvailable.count(6) != 0:
pick = spacesAvailable.index(6)
elif board[2][2] == board[4][0] == notCompToken and spacesAvailable.count(2) != 0:
pick = spacesAvailable.index(2)
else:
pick = randrange(0,len(spacesAvailable),1)
if board[0][0] == board[0][2] == compToken and spacesAvailable.count(2) != 0:
pick = spacesAvailable.index(2)
elif board[0][0] == board[0][4] == compToken and spacesAvailable.count(1) != 0:
pick = spacesAvailable.index(1)
elif board[0][2] == board[0][4] == compToken and spacesAvailable.count(0) != 0:
pick = spacesAvailable.index(0)
elif board[2][0] == board[2][2] == compToken and spacesAvailable.count(5) != 0:
pick = spacesAvailable.index(5)
elif board[2][0] == board[2][4] == compToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[2][2] == board[2][4] == compToken and spacesAvailable.count(3) != 0:
pick = spacesAvailable.index(3)
elif board[4][0] == board[4][2] == compToken and spacesAvailable.count(8) != 0:
pick = spacesAvailable.index(8)
elif board[4][0] == board[4][4] == compToken and spacesAvailable.count(7) != 0:
pick = spacesAvailable.index(7)
elif board[4][2] == board[4][4] == compToken and spacesAvailable.count(6) != 0:
pick = spacesAvailable.index(6)
elif board[0][0] == board[2][0] == compToken and spacesAvailable.count(6) != 0:
pick = spacesAvailable.index(6)
elif board[0][0] == board[4][0] == compToken and spacesAvailable.count(3) != 0:
pick = spacesAvailable.index(3)
elif board[2][0] == board[4][0] == compToken and spacesAvailable.count(0) != 0:
pick = spacesAvailable.index(0)
elif board[0][2] == board[2][2] == compToken and spacesAvailable.count(7) != 0:
pick = spacesAvailable.index(7)
elif board[0][2] == board[4][2] == compToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[2][2] == board[4][2] == compToken and spacesAvailable.count(1) != 0:
pick = spacesAvailable.index(1)
elif board[0][4] == board[2][4] == compToken and spacesAvailable.count(8) != 0:
pick = spacesAvailable.index(8)
elif board[0][4] == board[4][4] == compToken and spacesAvailable.count(5) != 0:
pick = spacesAvailable.index(5)
elif board[2][4] == board[4][4] == compToken and spacesAvailable.count(2) != 0:
pick = spacesAvailable.index(2)
elif board[0][0] == board[4][4] == compToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[0][0] == board[2][2] == compToken and spacesAvailable.count(8) != 0:
pick = spacesAvailable.index(8)
elif board[2][2] == board[4][4] == compToken and spacesAvailable.count(0) != 0:
pick = spacesAvailable.index(0)
elif board[0][4] == board[4][0] == compToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[0][4] == board[2][2] == compToken and spacesAvailable.count(6) != 0:
pick = spacesAvailable.index(6)
elif board[2][2] == board[4][0] == compToken and spacesAvailable.count(2) != 0:
pick = spacesAvailable.index(2)
else:
pass
return pick
def hardComp(board, spacesAvailable, compToken):
if compToken == "X":
notCompToken = "O"
else:
notCompToken = "X"
#initialize with random, ensures a choice is always made.
pick = randrange(0,len(spacesAvailable),1)
#take empty side
emptySide = []
if 1 in spacesAvailable:
emptySide.append(1)
if 3 in spacesAvailable:
emptySide.append(3)
if 5 in spacesAvailable:
emptySide.append(5)
if 7 in spacesAvailable:
emptySide.append(7)
if len(emptySide) > 0:
pick = spacesAvailable.index(emptySide[randrange(0,len(emptySide),1)])
#take empty corner
emptyCorner = []
if 0 in spacesAvailable:
emptyCorner.append(0)
if 2 in spacesAvailable:
emptyCorner.append(2)
if 6 in spacesAvailable:
emptyCorner.append(6)
if 8 in spacesAvailable:
emptyCorner.append(8)
if len(emptyCorner) > 0:
pick = spacesAvailable.index(emptyCorner[randrange(0,len(emptyCorner),1)])
#take empty opposite corner
emptyOppositeCorner = []
if board[0][0] == notCompToken and spacesAvailable.count(8) != 0:
emptyOppositeCorner.append(8)
if board[0][4] == notCompToken and spacesAvailable.count(6) != 0:
emptyOppositeCorner.append(6)
if board[4][0] == notCompToken and spacesAvailable.count(2) != 0:
emptyOppositeCorner.append(2)
if board[4][4] == notCompToken and spacesAvailable.count(0) != 0:
emptyOppositeCorner.append(0)
if len(emptyOppositeCorner) > 0:
pick = spacesAvailable.index(emptyOppositeCorner[randrange(0,len(emptyOppositeCorner),1)])
#take center
if 4 in spacesAvailable:
pick = spacesAvailable.index(4)
#block fork
if board[0][0] == board[4][4] == notCompToken and board[2][2] == compToken:
emptySide = []
if 1 in spacesAvailable:
emptySide.append(1)
if 3 in spacesAvailable:
emptySide.append(3)
if 5 in spacesAvailable:
emptySide.append(5)
if 7 in spacesAvailable:
emptySide.append(7)
if len(emptySide) > 0:
pick = spacesAvailable.index(emptySide[randrange(0,len(emptySide),1)])
if board[0][4] == board[4][0] == notCompToken and board[2][2] == compToken:
emptySide = []
if 1 in spacesAvailable:
emptySide.append(1)
if 3 in spacesAvailable:
emptySide.append(3)
if 5 in spacesAvailable:
emptySide.append(5)
if 7 in spacesAvailable:
emptySide.append(7)
if len(emptySide) > 0:
pick = spacesAvailable.index(emptySide[randrange(0,len(emptySide),1)])
if board[0][0] == board[2][2] == notCompToken and board[4][4] == compToken:
emptyCorner = []
if 2 in spacesAvailable:
emptyCorner.append(2)
if 6 in spacesAvailable:
emptyCorner.append(6)
if len(emptyCorner) > 0:
pick = spacesAvailable.index(emptyCorner[randrange(0,len(emptyCorner),1)])
if board[0][4] == board[2][2] == notCompToken and board[4][0] == compToken:
emptyCorner = []
if 0 in spacesAvailable:
emptyCorner.append(0)
if 8 in spacesAvailable:
emptyCorner.append(8)
if len(emptyCorner) > 0:
pick = spacesAvailable.index(emptyCorner[randrange(0,len(emptyCorner),1)])
print(pick)
if board[4][0] == board[2][2] == notCompToken and board[0][4] == compToken:
emptyCorner = []
if 0 in spacesAvailable:
emptyCorner.append(0)
if 8 in spacesAvailable:
emptyCorner.append(8)
if len(emptyCorner) > 0:
pick = spacesAvailable.index(emptyCorner[randrange(0,len(emptyCorner),1)])
if board[4][4] == board[2][2] == notCompToken and board[0][0] == compToken:
emptyCorner = []
if 2 in spacesAvailable:
emptyCorner.append(2)
if 6 in spacesAvailable:
emptyCorner.append(6)
if len(emptyCorner) > 0:
pick = spacesAvailable.index(emptyCorner[randrange(0,len(emptyCorner),1)])
#fork
#block
if board[0][0] == board[0][2] == notCompToken and spacesAvailable.count(2) != 0:
pick = spacesAvailable.index(2)
elif board[0][0] == board[0][4] == notCompToken and spacesAvailable.count(1) != 0:
pick = spacesAvailable.index(1)
elif board[0][2] == board[0][4] == notCompToken and spacesAvailable.count(0) != 0:
pick = spacesAvailable.index(0)
elif board[2][0] == board[2][2] == notCompToken and spacesAvailable.count(5) != 0:
pick = spacesAvailable.index(5)
elif board[2][0] == board[2][4] == notCompToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[2][2] == board[2][4] == notCompToken and spacesAvailable.count(3) != 0:
pick = spacesAvailable.index(3)
elif board[4][0] == board[4][2] == notCompToken and spacesAvailable.count(8) != 0:
pick = spacesAvailable.index(8)
elif board[4][0] == board[4][4] == notCompToken and spacesAvailable.count(7) != 0:
pick = spacesAvailable.index(7)
elif board[4][2] == board[4][4] == notCompToken and spacesAvailable.count(6) != 0:
pick = spacesAvailable.index(6)
elif board[0][0] == board[2][0] == notCompToken and spacesAvailable.count(6) != 0:
pick = spacesAvailable.index(6)
elif board[0][0] == board[4][0] == notCompToken and spacesAvailable.count(3) != 0:
pick = spacesAvailable.index(3)
elif board[2][0] == board[4][0] == notCompToken and spacesAvailable.count(0) != 0:
pick = spacesAvailable.index(0)
elif board[0][2] == board[2][2] == notCompToken and spacesAvailable.count(7) != 0:
pick = spacesAvailable.index(7)
elif board[0][2] == board[4][2] == notCompToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[2][2] == board[4][2] == notCompToken and spacesAvailable.count(1) != 0:
pick = spacesAvailable.index(1)
elif board[0][4] == board[2][4] == notCompToken and spacesAvailable.count(8) != 0:
pick = spacesAvailable.index(8)
elif board[0][4] == board[4][4] == notCompToken and spacesAvailable.count(5) != 0:
pick = spacesAvailable.index(5)
elif board[2][4] == board[4][4] == notCompToken and spacesAvailable.count(2) != 0:
pick = spacesAvailable.index(2)
elif board[0][0] == board[4][4] == notCompToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[0][0] == board[2][2] == notCompToken and spacesAvailable.count(8) != 0:
pick = spacesAvailable.index(8)
elif board[2][2] == board[4][4] == notCompToken and spacesAvailable.count(0) != 0:
pick = spacesAvailable.index(0)
elif board[0][4] == board[4][0] == notCompToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[0][4] == board[2][2] == notCompToken and spacesAvailable.count(6) != 0:
pick = spacesAvailable.index(6)
elif board[2][2] == board[0][4] == notCompToken and spacesAvailable.count(2) != 0:
pick = spacesAvailable.index(2)
else:
pass
if board[0][0] == board[0][2] == compToken and spacesAvailable.count(2) != 0:
pick = spacesAvailable.index(2)
elif board[0][0] == board[0][4] == compToken and spacesAvailable.count(1) != 0:
pick = spacesAvailable.index(1)
elif board[0][2] == board[0][4] == compToken and spacesAvailable.count(0) != 0:
pick = spacesAvailable.index(0)
elif board[2][0] == board[2][2] == compToken and spacesAvailable.count(5) != 0:
pick = spacesAvailable.index(5)
elif board[2][0] == board[2][4] == compToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[2][2] == board[2][4] == compToken and spacesAvailable.count(3) != 0:
pick = spacesAvailable.index(3)
elif board[4][0] == board[4][2] == compToken and spacesAvailable.count(8) != 0:
pick = spacesAvailable.index(8)
elif board[4][0] == board[4][4] == compToken and spacesAvailable.count(7) != 0:
pick = spacesAvailable.index(7)
elif board[4][2] == board[4][4] == compToken and spacesAvailable.count(6) != 0:
pick = spacesAvailable.index(6)
elif board[0][0] == board[2][0] == compToken and spacesAvailable.count(6) != 0:
pick = spacesAvailable.index(6)
elif board[0][0] == board[4][0] == compToken and spacesAvailable.count(3) != 0:
pick = spacesAvailable.index(3)
elif board[2][0] == board[4][0] == compToken and spacesAvailable.count(0) != 0:
pick = spacesAvailable.index(0)
elif board[0][2] == board[2][2] == compToken and spacesAvailable.count(7) != 0:
pick = spacesAvailable.index(7)
elif board[0][2] == board[4][2] == compToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[2][2] == board[4][2] == compToken and spacesAvailable.count(1) != 0:
pick = spacesAvailable.index(1)
elif board[0][4] == board[2][4] == compToken and spacesAvailable.count(8) != 0:
pick = spacesAvailable.index(8)
elif board[0][4] == board[4][4] == compToken and spacesAvailable.count(5) != 0:
pick = spacesAvailable.index(5)
elif board[2][4] == board[4][4] == compToken and spacesAvailable.count(2) != 0:
pick = spacesAvailable.index(2)
elif board[0][0] == board[4][4] == compToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[0][0] == board[2][2] == compToken and spacesAvailable.count(8) != 0:
pick = spacesAvailable.index(8)
elif board[2][2] == board[4][4] == compToken and spacesAvailable.count(0) != 0:
pick = spacesAvailable.index(0)
elif board[0][4] == board[4][0] == compToken and spacesAvailable.count(4) != 0:
pick = spacesAvailable.index(4)
elif board[0][4] == board[2][2] == compToken and spacesAvailable.count(6) != 0:
pick = spacesAvailable.index(6)
elif board[2][2] == board[4][0] == compToken and spacesAvailable.count(2) != 0:
pick = spacesAvailable.index(2)
else:
pass
return pick
def compMakeMove(board, compToken, level):
print("\nPlease wait while the computer moves.")
sleep(3)
blankList = []
spacesAvailable = spacesAvailableTracker(board)
if level == "easy":
pick = easyComp(spacesAvailable)
elif level == "medium":
pick = mediumComp(board, spacesAvailable, compToken)
elif level == "hard":
pick = hardComp(board, spacesAvailable, compToken)
else:
print("Error, level not passed in compMakeMove")
sys.exit()
if spacesAvailable[pick] == 0:
row = 0
column = 0
elif spacesAvailable[pick] == 1:
row = 0
column = 2
elif spacesAvailable[pick] == 2:
row = 0
column = 4
elif spacesAvailable[pick] == 3:
row = 2
column = 0
elif spacesAvailable[pick] == 4:
row = 2
column = 2
elif spacesAvailable[pick] == 5:
row = 2
column = 4
elif spacesAvailable[pick] == 6:
row = 4
column = 0
elif spacesAvailable[pick] == 7:
row = 4
column = 2
elif spacesAvailable[pick] == 8:
row = 4
column = 4
else:
print("Error in compMakeMove. pick outside range 0-8.")
sys.exit()
board[row][column] = compToken
displayCleanBoard(board)
sleep(1)
return board
def singleIntro():
print("\nSingle Player Tic Tac Toe play\n")
sleep(1)
level = pickLevel()
pNames = getNames("single")
print("\nWho starts will be chosen by random.\n")
first = whosFirst()
if first == 0:
compToken = "X"
userToken = "O"
print("The computer will be first.\nHe will be X, you will be O.")
else:
userToken = "X"
compToken = "O"
print("You will be first.\nYou will be X, The computer will be O.")
nextMove = first
return compToken, userToken, nextMove, level, pNames
def doubleIntro():
pNames = getNames("double")
print("Two Player Tic Tac Toe play\n\nWho starts will be chosen by random.\n")
sleep(2)
first = whosFirst()
if first == 0:
compToken = "X"
userToken = "O"
print("%s will be first.\n%s will be X, %s will be O." % (pNames[0], pNames[0], pNames[1]))
else:
userToken = "X"
compToken = "O"
print("%s will be first.\n%s will be X, %s will be O." % (pNames[1], pNames[1], pNames[0]))
nextMove = first
return compToken, userToken, nextMove, pNames
def pickLevel():
easyChoices = ["e", "easy"]
mediumChoices = ["m", "medium"]
hardChoices = ["h", "hard"]
choices = easyChoices + mediumChoices + hardChoices
inputNotValid = True
while inputNotValid:
level = input("What level do you want to play? (easy, medium, hard): ")
if level.lower() not in choices:
print("Sorry, invalid input.")
else:
inputNotValid = False
if level.lower() in easyChoices:
level = "easy"
elif level.lower() in mediumChoices:
level = "medium"
elif level.lower() in hardChoices:
level = "hard"
else:
print("Error in pickLevel. level passed not in choices.")
sys.exit()
return level.lower()
def playAgain():
YES = ["YES", "Y"]
NO = ["NO", "N"]
YNOPTIONS = YES + NO
nonvalidInput = True
while nonvalidInput:
repeat = input("Would you like to play again? ")
if repeat.upper() in YNOPTIONS:
nonvalidInput = False
if repeat.upper() in YES:
return True
else:
return False
else:
print("Sorry, input not valid.")
def singlePlayerGameConsole():
currentBoard = initialize()
spacesAvailableTracker(currentBoard)
computerToken, userToken, nextMove, level, names = singleIntro()
noWinner = True
while noWinner:
if nextMove == 0:
currentBoard = compMakeMove(currentBoard, computerToken, level)
winner = checkWinner(currentBoard)
else:
currentBoard = userMakeMove(currentBoard, userToken, "single", "", names)
winner = checkWinner(currentBoard)
if winner:
winnerEnd("single", currentBoard, nextMove, names)
noWinner = False
if checkCat(currentBoard):
catEnd(currentBoard)
noWinner = False
nextMove += 1
nextMove %= 2
def twoPlayerGameConsole():
currentBoard = initialize()
spacesAvailableTracker(currentBoard)
user1Token, user2Token, nextMove, names = doubleIntro()
noWinner = True
while noWinner:
if nextMove == 0:
currentBoard = userMakeMove(currentBoard, user1Token, "double",
nextMove + 1, names)
winner = checkWinner(currentBoard)
else:
currentBoard = userMakeMove(currentBoard, user2Token, "double",
nextMove + 1, names)
winner = checkWinner(currentBoard)
if winner:
winnerEnd("double",currentBoard, nextMove, names)
noWinner = False
if checkCat(currentBoard):
catEnd(currentBoard)
noWinner = False
nextMove += 1
nextMove %= 2
def gameConsole():
singleOption = ["1","S","SINGLE","ONE","O"]
doubleOption = ["2","D","DOUBLE","TWO","T"]
typeOptions = singleOption + doubleOption
try:
print("Welcome to Tic-Tac-Toe!\n3 in a row wins.\n")
again = True
while(again):
inputNotValid = True #variable used to control while loop
while inputNotValid:
playType = input("1 or 2 players? ")
if playType not in typeOptions:
print("Sorry, input not valid.")
else:
inputNotValid = False
if playType in singleOption:
singlePlayerGameConsole()
else:
twoPlayerGameConsole()
again = playAgain()
if again:
print("\nStarting a new game.\n")
else:
print("\nThanks for playing!")
sys.exit()
except SystemExit:
print("Program terminated.")
except KeyboardInterrupt:
print("\nKeyboardInterrupt. Program terminated.")
except:
print("Unexpected error. %s Program terminated." % sys.exc_info()[0])
#main
if __name__ == "__main__":
gameConsole()