import random #Game Instructions def game_instructions(): print "Welcome to Kris' Automatic Random Tic Tac Toe Game. In this game Player 1 uses X's, and Player 2 uses O's." #create possible play area variables spot1,spot2,spot3,spot4,spot5,spot6,spot7,spot8,spot9 = 0,0,0,0,0,0,0,0,0 #function that checks for winning conditions def check_for_win(): if spot1 == 1 and spot2 == 1 and spot3 == 1: print "Game Over. Player 1 wins!" return 0 elif spot4 == 0 and spot5 == 1 and spot6 == 1: print "Game Over. Player 1 wins!" return 0 elif spot7 == 1 and spot8 == 1 and spot9 == 1: print "Game Over. Player 1 wins!" return 0 elif spot1 == 1 and spot4 == 1 and spot7 == 1: print "Game Over. Player 1 wins!" return 0 elif spot2 == 1 and spot5 == 1 and spot8 == 1: print "Game Over. Player 1 wins!" return 0 elif spot3 == 1 and spot6 == 1 and spot9 == 1: print "Game Over. Player 1 wins!" return 0 elif spot1 == 1 and spot5 == 1 and spot9 == 1: print "Game Over. Player 1 wins!" return 0 elif spot3 == 1 and spot5 == 1 and spot7 == 1: print "Game Over. Player 1 wins!" return 0 elif spot1 == 2 and spot2 == 2 and spot3 == 2: print "Game Over. Player 2 wins!" return 0 elif spot4 == 2 and spot5 == 2 and spot6 == 2: print "Game Over. Player 2 wins!" return 0 elif spot7 == 2 and spot8 == 2 and spot9 == 2: print "Game Over. Player 2 wins!" return 0 elif spot1 == 2 and spot4 == 2 and spot7 == 2: print "Game Over. Player 2 wins!" return 0 elif spot2 == 2 and spot5 == 2 and spot8 == 2: print "Game Over. Player 2 wins!" return 0 elif spot3 == 2 and spot6 == 2 and spot9 == 2: print "Game Over. Player 2 wins!" return 0 elif spot1 == 2 and spot5 == 2 and spot9 == 2: print "Game Over. Player 2 wins!" return 0 elif spot3 == 2 and spot5 == 2 and spot7 == 2: print "Game Over. Player 2 wins!" return 0 elif spot1>0 and spot2>0 and spot3>0 and spot4>0 and spot5>0 and spot6>0 and spot7>0 and spot8>0 and spot9>0: print "Game Over. Its a tie!" return 0 else: return 1 #function that prints the board after a play def print_current_board(): print "+ - - + - - + - - +" print "| %s | %s | %s |" % (spot1,spot2,spot3) print "+ - - + - - + - - +" print "| %s | %s | %s |" % (spot4,spot5,spot6) print "+ - - + - - + - - +" print "| %s | %s | %s |" % (spot7,spot8,spot9) print "+ - - + - - + - - +" #create selection list selection_list = [1,2,3,4,5,6,7,8,9] #logic for Player 1 def player1_move(): selection = random.choice(selection_list) if selection == 1: spot1 = 1 elif selection == 2: spot2 = 1 elif selection == 3: spot3 = 1 elif selection == 4: spot4 = 1 elif selection == 5: spot5 = 1 elif selection == 6: spot6 = 1 elif selection == 7: spot7 = 1 elif selection == 8: spot8 = 1 elif selection == 9: spot9 = 1 del selection_list[selection] #logic for Player 2 def player2_move(): selection = random.choice(selection_list) del selection_list[selection] if selection == 1: spot1 = 2 elif selection == 2: spot2 = 2 elif selection == 3: spot3 = 2 elif selection == 4: spot4 = 2 elif selection == 5: spot5 = 2 elif selection == 6: spot6 = 2 elif selection == 7: spot7 = 2 elif selection == 8: spot8 = 2 elif selection == 9: spot9 = 2 #Create/run game logic def run_game(): while check_for_win() = 0: player1_move() player2_move() player1_move() player2_move() player1_move() player2_move() player1_move() player2_move() player1_move()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question