D10d3

Tic-Tac-Toe.py for Python 2.7

Sep 28th, 2014
261
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Python Tic-Tac-Toe by D10d3
  2. # Goals:
  3. #   play tic tac toe with computer
  4. # Optional future goals
  5. #   ask to play again
  6. #   keep score of games
  7. #   ask player if X or O
  8. #   ask player if they want to go first
  9. #   Better Computer Logic
  10. #**********************************************************************************************
  11.  
  12. # SETUP
  13. import os
  14. os.system('cls') #Clears the screen on windows machines
  15. import random
  16. board = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
  17.  
  18. #Sub Routines
  19. def ClearScreen():
  20.     os.system('cls') #Clears the screen on windows machines
  21.    
  22. def ClearBoard():# assign board variables using a list numbered 0-8 for the 9 slots, defaulting to ' ' as empty
  23.     board = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
  24.  
  25. def PrintExampleBoard (): #Example board to for player move selection
  26.     print "-------------"
  27.     print "| 1 | 2 | 3 |"
  28.     print "-------------"
  29.     print "| 4 | 5 | 6 |"
  30.     print "-------------"
  31.     print "| 7 | 8 | 9 |"
  32.     print "-------------\n"
  33.    
  34. def PrintBoard (): #Prints the current board
  35.     print "Current Board Positions:"
  36.     print "-------------"
  37.     print "|", board[0] , "|" , board[1] , "|" , board[2] , "|"
  38.     print "-------------"
  39.     print "|", board[3] , "|" , board[4] , "|" , board[5] , "|"
  40.     print "-------------"
  41.     print "|", board[6] , "|" , board[7] , "|" , board[8] , "|"
  42.     print "-------------\n"
  43.  
  44. def PlayerTurn(board): #player selects move and it checks if it's valid and applies it to the board
  45.     valid = False
  46.     while not valid:
  47.         print "Select a square to mark (1-9):"
  48.         p_choice = raw_input('>')
  49.         if p_choice == '1' or '2' or '3' or '4' or '5' or '6' or '7' or '8' or '9':
  50.             valid = True
  51.         else:
  52.             print '"' , p_choice , 'is not a valid move'
  53.             valid = False
  54.         if p_choice == "1" and board[0] == " ":
  55.             board[0] = "X"
  56.         elif p_choice == "2" and board[1] == " ":
  57.             board[1] = "X"
  58.         elif p_choice == "3" and board[2] == " ":
  59.             board[2] = "X"
  60.         elif p_choice == "4" and board[3] == " ":
  61.             board[3] = "X"
  62.         elif p_choice == "5" and board[4] == " ":
  63.             board[4] = "X"
  64.         elif p_choice == "6" and board[5] == " ":
  65.             board[5] = "X"
  66.         elif p_choice == "7" and board[6] == " ":
  67.             board[6] = "X"
  68.         elif p_choice == "8" and board[7] == " ":
  69.             board[7] = "X"
  70.         elif p_choice == "9" and board[8] == " ":
  71.             board[8] = "X"
  72.         else:
  73.             print '"' , p_choice , '" is not a valid move'
  74.             valid = False
  75.     return board
  76.  
  77. def ComputerTurn(board): #Computer selects move and it checks if it's valid and applies it to the board
  78.     valid = False
  79.     while not valid:
  80.         ComputerOptions = ['1','2','3','4','5','6','7','8','9']
  81.         p_choice = random.choice(ComputerOptions)
  82.         valid = True
  83.         if p_choice == "1" and board[0] == " ":
  84.             board[0] = "O"
  85.         elif p_choice == "2" and board[1] == " ":
  86.             board[1] = "O"
  87.         elif p_choice == "3" and board[2] == " ":
  88.             board[2] = "O"
  89.         elif p_choice == "4" and board[3] == " ":
  90.             board[3] = "O"
  91.         elif p_choice == "5" and board[4] == " ":
  92.             board[4] = "O"
  93.         elif p_choice == "6" and board[5] == " ":
  94.             board[5] = "O"
  95.         elif p_choice == "7" and board[6] == " ":
  96.             board[6] = "O"
  97.         elif p_choice == "8" and board[7] == " ":
  98.             board[7] = "O"
  99.         elif p_choice == "9" and board[8] == " ":
  100.             board[8] = "O"
  101.         else:
  102.             valid = False
  103.     return board
  104.  
  105. def DidPlayerWin(board,Playing):
  106.     # 3 Across Wins
  107.     if board[0] == "X" and board [1] == "X" and board[2] == "X":
  108.         ClearScreen()
  109.         PrintBoard()
  110.         print "You Win!"
  111.         quit()
  112.     elif board[3] == "X" and board [4] == "X" and board[5] == "X":
  113.         ClearScreen()
  114.         PrintBoard()
  115.         print "You Win!"
  116.         quit()
  117.     elif board[6] == "X" and board [7] == "X" and board[8] == "X":
  118.         ClearScreen()
  119.         PrintBoard()
  120.         print "You Win!"
  121.         quit()
  122.     # 3 Down Wins
  123.     elif board[0] == "X" and board [3] == "X" and board[6] == "X":
  124.         ClearScreen()
  125.         PrintBoard()
  126.         print "You Win!"
  127.         quit()
  128.     elif board[1] == "X" and board [4] == "X" and board[7] == "X":
  129.         ClearScreen()
  130.         PrintBoard()
  131.         print "You Win!"
  132.         quit()
  133.     elif board[2] == "X" and board [5] == "X" and board[8] == "X":
  134.         ClearScreen()
  135.         PrintBoard()
  136.         print "You Win!"
  137.         quit()
  138.     # Diagonal Wins
  139.     elif board[0] == "X" and board [4] == "X" and board[8] == "X":
  140.         ClearScreen()
  141.         PrintBoard()
  142.         print "You Win!"
  143.         quit()
  144.     elif board[2] == "X" and board [4] == "X" and board[6] == "X":
  145.         ClearScreen()
  146.         PrintBoard()
  147.         print "You Win!"
  148.         quit()
  149.     # No Win
  150.     else:
  151.         Playing = True
  152.     return Playing
  153.    
  154. def DidComputerWin(board,Playing):
  155.     # 3 Across Wins
  156.     if board[0] == "O" and board [1] == "O" and board[2] == "O":
  157.         ClearScreen()
  158.         PrintBoard()
  159.         print "The Computer Wins"
  160.         quit()
  161.     elif board[3] == "O" and board [4] == "O" and board[5] == "O":
  162.         ClearScreen()
  163.         PrintBoard()
  164.         print "The Computer Wins"
  165.         quit()
  166.     elif board[6] == "O" and board [7] == "O" and board[8] == "O":
  167.         ClearScreen()
  168.         PrintBoard()
  169.         print "The Computer Wins"
  170.         quit()
  171.     # 3 Down Wins
  172.     elif board[0] == "O" and board [3] == "O" and board[6] == "O":
  173.         ClearScreen()
  174.         PrintBoard()
  175.         print "The Computer Wins"
  176.         quit()
  177.     elif board[1] == "O" and board [4] == "O" and board[7] == "O":
  178.         ClearScreen()
  179.         PrintBoard()
  180.         print "The Computer Wins"
  181.         quit()
  182.     elif board[2] == "O" and board [5] == "O" and board[8] == "O":
  183.         ClearScreen()
  184.         PrintBoard()
  185.         print "The Computer Wins"
  186.         quit()
  187.     # Diagonal Wins
  188.     elif board[0] == "O" and board [4] == "O" and board[8] == "O":
  189.         ClearScreen()
  190.         PrintBoard()
  191.         print "The Computer Wins"
  192.         quit()
  193.     elif board[2] == "O" and board [4] == "O" and board[6] == "O":
  194.         ClearScreen()
  195.         PrintBoard()
  196.         print "The Computer Wins"
  197.         quit()
  198.     # No Win
  199.     else:
  200.         Playing = True
  201.     return Playing
  202.  
  203. def IsDraw(board,Playing):#Determines if the game ended in a draw
  204.     if board[0] != " " and board[1] != " " and board[2] != " " and board[3] != " " and board[4] != " " and board[5] != " " and board[6] != " " and board[7] != " " and board[8] != " ":
  205.         ClearScreen()
  206.         PrintBoard()
  207.         print "The Game Ended in a Draw"
  208.         quit()
  209.     else:
  210.         Playing = True
  211.     return Playing
  212.    
  213. #Introduction
  214. print "lets play Tic-Tac-Toe"
  215. print "You are X's and get to go first\n"
  216. raw_input ("Press Any Key To Begin")
  217.  
  218. #Game Loop 
  219. Playing = True
  220. while Playing == True:
  221.     ClearScreen()
  222.     PrintBoard()
  223.     PrintExampleBoard()
  224.     board = PlayerTurn(board)
  225.     Playing = DidPlayerWin(board,Playing)
  226.     Playing = IsDraw(board,Playing)
  227.     board = ComputerTurn(board)
  228.     Playing = DidComputerWin(board,Playing)
  229.     Playing = IsDraw(board,Playing)
RAW Paste Data