3
« on: May 23, 2013, 02:58:38 am »
Hello Evilzone! First time poster, short time lurker here. I have recently started digging into programming and went with Python! Just for educational purposes on my behalf, I want to post this Tic Tac Toe game I have been working on! I am open to any suggestions to improve what may seem like redundant code. Thanks in advance!
from random import randint
import time
import os #For pretty color reasons
raw_input("Welcome to tic-tac-toe! Press Entert to start:")
board = []
numTries, player_wins, computer_wins = 0, 0, 0
def makeBoard():
global board
for i in range(0, 10):
board.append("[ ]")
def printBoard():
#Print the game
global board
print board[1], board[2], board[3]
print board[4], board[5], board[6]
print board[7], board[8], board[9]
def move():
#Prompt the player to make a move
os.system("color a")
global numTries
numTries = 0
#Take input and check if spot is available
try:
x = int(raw_input("\nIt's your turn(1-9):"))
if not board[x] == "[X]" and not board[x] == "[O]":
board[x] = "[X]"
else:
print "That spot is already chosen."
move()
except:
print("You must have screwed up.")
move()
printBoard()
checkForWin()
time.sleep(.5)
computeMove()
def computeMove():
#Generate random spot on the board, make move if available
os.system("color c")
global numTries
global board
numTries += 1
selection = randint(1, 9)
if not board[selection] == "[X]" and not board[selection] == "[O]":
board[selection] = "[O]"
print ("\nComputer has chosen.")
#AI - Place move against you -- each row has 3 combinations
#Only executes if random move is unsuccessful on first try
#Row 1
elif board[1] == "[X]" and board[2] == "[ ]" and board[3] == "[X]":
board[2] = "[O]"
elif board[1] == "[ ]" and board[2] == "[X]" and board[3] == "[X]":
board[1] = "[O]"
elif board[1] == "[X]" and board[2] == "[X]" and board[3] == "[ ]":
board[3] = "[O]"
#Row 2
elif board[4] == "[X]" and board[5] == "[ ]" and board[6] == "[X]":
board[5] = "[O]"
elif board[4] == "[ ]" and board[5] == "[X]" and board[6] == "[X]":
board[4] = "[O]"
elif board[4] == "[X]" and board[5] == "[X]" and board[6] == "[ ]":
board[6] = "[O]"
#Row 3
elif board[7] == "[X]" and board[8] == "[ ]" and board[9] == "[X]":
board[8] = "[O]"
elif board[7] == "[ ]" and board[8] == "[X]" and board[9] == "[X]":
board[7] = "[O]"
elif board[7] == "[X]" and board[8] == "[X]" and board[9] == "[ ]":
board[9] = "[O]"
#Diagonal 1
elif board[1] == "[X]" and board[5] == "[ ]" and board[9] == "[X]":
board[5] = "[O]"
elif board[1] == "[ ]" and board[5] == "[X]" and board[9] == "[X]":
board[1] = "[O]"
elif board[1] == "[X]" and board[5] == "[X]" and board[9] == "[ ]":
board[9] = "[O]"
#Diagonal 2
elif board[3] == "[X]" and board[5] == "[ ]" and board[7] == "[X]":
board[5] = "[O]"
elif board[3] == "[ ]" and board[5] == "[X]" and board[7] == "[X]":
board[3] = "[O]"
elif board[3] == "[X]" and board[5] == "[X]" and board[7] == "[ ]":
board[7] = "[O]"
else:
if numTries > 20:
raw_input("It's a cat's game!")
restart()
computeMove()
printBoard()
checkForWin()
move()
def checkForWin():
#Define win combinations - 8 possible for each X and O
global board
global numTries
global player_wins
global computer_wins
if board[1] == "[X]" and board[2] == "[X]" and board[3] == "[X]":
raw_input("You win!")
player_wins += 1
restart()
if board[4] == "[X]" and board[5] == "[X]" and board[6] == "[X]":
raw_input("You win!")
player_wins += 1
restart()
if board[7] == "[X]" and board[8] == "[X]" and board[9] == "[X]":
raw_input("You win!")
player_wins += 1
restart()
if board[1] == "[X]" and board[4] == "[X]" and board[7] == "[X]":
raw_input("You win!")
player_wins += 1
restart()
if board[2] == "[X]" and board[5] == "[X]" and board[8] == "[X]":
raw_input("You win!")
player_wins += 1
restart()
if board[3] == "[X]" and board[6] == "[X]" and board[9] == "[X]":
raw_input("You win!")
player_wins += 1
restart()
if board[1] == "[X]" and board[5] == "[X]" and board[9] == "[X]":
raw_input("You win!")
player_wins += 1
restart()
if board[3] == "[X]" and board[5] == "[X]" and board[7] == "[X]":
raw_input("You win!")
player_wins += 1
restart()
#Computer's combinations
if board[1] == "[O]" and board[2] == "[O]" and board[3] == "[O]":
raw_input("You LOSE!")
computer_wins += 1
restart()
if board[4] == "[O]" and board[5] == "[O]" and board[6] == "[O]":
raw_input("You LOSE!")
computer_wins += 1
restart()
if board[7] == "[O]" and board[8] == "[O]" and board[9] == "[O]":
raw_input("You LOSE!")
computer_wins += 1
restart()
if board[1] == "[O]" and board[4] == "[O]" and board[7] == "[O]":
raw_input("You LOSE!")
computer_wins += 1
restart()
if board[2] == "[O]" and board[5] == "[O]" and board[8] == "[O]":
raw_input("You LOSE!")
computer_wins += 1
restart()
if board[3] == "[O]" and board[6] == "[O]" and board[9] == "[O]":
raw_input("You LOSE!")
computer_wins += 1
restart()
if board[1] == "[O]" and board[5] == "[O]" and board[9] == "[O]":
raw_input("You LOSE!")
computer_wins += 1
restart()
if board[3] == "[O]" and board[5] == "[O]" and board[7] == "[O]":
raw_input("You LOSE!")
computer_wins += 1
restart()
def whoStarts():
#If even number you start, odd number computer starts
i = randint(0, 10)
if i % 2 == 0:
move()
else:
computeMove()
def restart():
#Restart the game
global board
global player_wins
global computer_wins
os.system("color f")
board = []
print "\nYour score: ", player_wins
print "Computer score: ", computer_wins
raw_input("\n~~~~~~~~~~~~~~~~~~~~~~~~~Press Enter to play again!~~~~~~~~~~~~~~~~~~~~~~~~~")
makeBoard()
whoStarts()
if __name__ == "__main__":
makeBoard()
whoStarts()