Created
April 22, 2013 05:21
-
-
Save anonymous/5432586 to your computer and use it in GitHub Desktop.
tic tac toe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##This tic tac toe game was written by Ted Maldonado | |
import random | |
#Game board | |
board=[0,1,2, | |
3,4,5, | |
6,7,8] | |
def show(): | |
print board[0],'|',board[1],"|", board[2],"|" | |
print "----------" | |
print board[3],'|',board[4],"|", board[5],"|" | |
print "----------" | |
print board[6],'|',board[7],"|", board[8],"|" | |
def checkLine(char, spot1, spot2, spot3): | |
if board[spot1]==char and board[spot2]==char and board[spot3]== char: | |
return True | |
def checkAll(char): | |
if checkLine(char, 0,1,2): | |
return True | |
if checkLine(char, 1,4,7): | |
return True | |
if checkLine(char, 2,5,8): | |
return True | |
if checkLine(char, 6,7,8): | |
return True | |
if checkLine(char, 3,4,5): | |
return True | |
if checkLine(char, 1,2,3): | |
return True | |
if checkLine(char, 2,4,6): | |
return True | |
if checkLine(char, 0,4,8): | |
return True | |
while True: | |
input = raw_input("Select a spot: ") | |
input = int(input) | |
if board[input]!="X" and board[input]!="O": | |
board[input]="X" | |
if checkAll('X')==True: | |
print '--- X Wins---' | |
break; | |
while True: | |
random.seed()#random generator | |
opponent=random.randint(0,8) | |
if board[opponent] != "O" and board[opponent]!="X": | |
board[opponent]="O" | |
if checkAll('O')==True: | |
print '--- O Wins---' | |
break; | |
break; | |
else: | |
print "This spot is taken!" | |
show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment