SHARE
TWEET

Untitled

a guest May 19th, 2017 37 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. X = "X"
  3. O = "O"
  4. EMPTY = " "
  5. TIE = "TIE"
  6. human = X
  7. computer = O
  8. grid = 9
  9. position = 0
  10. def display_instructions():
  11.     print("The tic tac toe you will be playing on will\n",
  12.           "be a 3 by 3 grid and will look like this\n",
  13.           " 0 | 1 | 2\n",
  14.           " ---------\n",
  15.           " 3 | 4 | 5\n",
  16.           " ---------\n",
  17.           " 6 | 7 | 8\n",
  18.           "You will start off by going first as X's\n",
  19.           "use 0-8 to position where you would like to go")
  20.    
  21. def display_board(board):
  22.     print(board[0], "|", board[1], "|", board[2])
  23.     print("---------")
  24.     print(board[3], "|", board[4], "|", board[5])
  25.     print("---------")
  26.     print(board[6], "|", board[7], "|", board[8])
  27.    
  28. def new_board():
  29.     board = []
  30.     for position in range(grid):
  31.         board.append(EMPTY)
  32.     return board
  33.  
  34. def winner(board):
  35.     winner = "none"
  36.     while winner != X or O:
  37.         if board[0] == board[1] == board[2] != EMPTY:
  38.             winner = board[0]
  39.             return winner
  40.         elif board[3] == board[4] == board[5] != EMPTY:
  41.             winner = board[3]
  42.             return winner
  43.         elif board[5] == board[6] == board[7] != EMPTY:
  44.             winner = board[5]
  45.             return winner
  46.         elif board[0] == board[3] == board[6] != EMPTY:
  47.             winner = board[0]
  48.             return winner
  49.         elif board[1] == board[4] == board[7] != EMPTY:
  50.             winner = board[1]
  51.             return winner
  52.         elif board[2] == board[5] == board[8] != EMPTY:
  53.             winner = board[2]
  54.             return winner
  55.         elif board[0] == board[4] == board[8] != EMPTY:
  56.             winner = board[0]
  57.             return winner
  58.         elif board[2] == board[4] == board[6] != EMPTY:
  59.             winner = board[2]
  60.             return winner
  61.        
  62.         elif EMPTY not in board:
  63.             return TIE            
  64.         return None
  65.    
  66. def next_turn(turn):
  67.     if turn == X:
  68.         return 0
  69.     else:
  70.         return X
  71.  
  72.    
  73. def human_move(board):
  74.     print("Where will you move?(0-8): ")
  75.     move=int(input())
  76.     turn = X
  77.     while turn == X:
  78.         if move in range(grid) and board[move] == EMPTY:
  79.             board[move] = X
  80.             turn = next_turn(turn)
  81.         else:
  82.             print("Move not valid, please choose an empty space using 0-8")
  83.             move=int(input())
  84.     return move
  85.  
  86. def computer_move(board):
  87.     turn = O
  88.     move = random.randint(0,8)
  89.     while turn == O:
  90.         if move in range(grid) and board[move] == EMPTY:
  91.             board[move] = O
  92.             turn = next_turn(turn)
  93.         else:
  94.             move = random.randint(0,8)
  95.     return move
  96.  
  97.    
  98. def legal_move(board):
  99.     move = []
  100.     if board[move] in range(grid):
  101.             if board[move]== EMPTY:
  102.                 move.append(position)
  103.     return move
  104.  
  105. def main():
  106.     display_instructions()
  107.     board = new_board()
  108.     while not winner(board):
  109.             human_move(board)
  110.             computer_move(board)
  111.             display_board(board)
  112.     the_winner = winner(board)
  113.     if the_winner == human:
  114.         print("A winner is you!")
  115.     if the_winner == computer:
  116.         print("You Fail it! \n"
  117.               "Your skill is not enough \n"
  118.               "see you next time \n"
  119.               "bye-bye!\n")
  120.     import os
  121.     os.system('pause')
  122. main()
RAW Paste Data
Want to get better at Python?
Learn to code Python in 2017
Top