Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.13 KB | None | 0 0
  1. # Tic-Tac-Toe
  2.  
  3. # global constants
  4. X = "X"
  5. O = "O"
  6. EMPTY = ""
  7. TIE = "TIE"
  8. NUM_SQUARES = 9
  9.  
  10. def display_instruct():
  11.     """ Display game instructions."""
  12.     print \
  13.           """
  14.    Welcome to the greatest intellectual challenge of all time: Tic-Tac-Toe.
  15.    This showdown will be a battle between your human brain and my processor.
  16.  
  17.    You will make your move known by entering a number, 0-8. The number will
  18.    correspond to the board position as follows:
  19.  
  20.    
  21.            0 | 1 | 2
  22.           -----------
  23.            3 | 4 | 5
  24.           -----------
  25.            6 | 7 | 8
  26.  
  27.    Prepare yourself. The ultimate battle is about to begin. \n
  28.    """
  29.  
  30. def bquery(question):
  31.     answer = None
  32.     while answer not in ("y","n"):
  33.         answer = raw_input(question).lower()
  34.     return answer
  35.  
  36. def ask_number():
  37.     """ Gets a number from the player."""
  38.     move = None
  39.     while move not in range(0,9):
  40.         move = raw_input("Where do you want to move? ")
  41.         move = int(move)
  42.     return move
  43.  
  44. def go_first():
  45.     """ Asks player to go first or not."""
  46.     answer = bquery("Do you want to go first? ")
  47.     if answer == "y":
  48.         human = X
  49.         computer = O
  50.     else:
  51.         human = O
  52.         computer = X
  53.     return human, computer
  54.  
  55. def new_board():
  56.     """ Creates a new blank game board."""
  57.     board = []
  58.     for square in range(0,9):
  59.         board.append(EMPTY)
  60.     return board
  61.  
  62. def display_board(board):
  63.     """ Display game board on screen."""
  64.     print "\n\t", board[0], "  |", board[1], " |", board[2]
  65.     print "\t", "------------"
  66.     print "\t", board[3], "  |", board[4], " |", board[5]
  67.     print "\t", "------------"
  68.     print "\t", board[6], "  |", board[7], " |", board[8], "\n"
  69.  
  70. def winning_board(board):
  71.     """ Checks to see if a player has won."""
  72.     win_rows = [(0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6)]
  73.     for row in win_rows:
  74.         if board[row[0]] == board[row[1]] == board[row[2]] != EMPTY:
  75.             winner = board[row[0]]
  76.             return winner
  77.     if EMPTY not in board:
  78.         return TIE
  79.     else:
  80.         return None
  81.  
  82. def legal_moves(board):
  83.     """ Creates series of legal moves."""
  84.     legal = []
  85.     for square in range(0,9):
  86.         if board[square] == EMPTY:
  87.             legal.append(square)
  88.     print "[debug] legal moves: ", legal
  89.     return legal
  90.  
  91. def human_move(board, human):
  92.     """ Takes player's move + place on board."""
  93.     move = None
  94.     legal = legal_moves(board)
  95.     while move not in legal:
  96.         move = ask_number()
  97.     board[move] = human
  98.     return board
  99.  
  100. def cpu_move(board, computer, human):
  101.     """ Takes computer's move + places on board."""
  102.     # make a copy of the board
  103.     board = board[:]
  104.     bestmoves = (0,2,6,8,4,3,5,1,7)
  105.     # if computer can win, play that square:
  106.     for move in legal_moves(board):
  107.         board[move] = computer
  108.         if winning_board(board) == computer:
  109.             print "[debug] cpu win:", move
  110.             return move
  111.         # undo the move because it was empty before
  112.         board[move] = EMPTY
  113.     # if player can win, block that square:
  114.     for move in legal_moves(board):
  115.         board[move] = human
  116.         if winning_board(board) == human:
  117.             print "[debug] block human:", move
  118.             return move
  119.         board[move] = EMPTY
  120.         # chose first best move that is legal
  121.     for move in bestmoves:
  122.         if move in legal_moves(board):
  123.             board[move] = computer
  124.             print "[debug] next best move:", move
  125.             return move
  126.        
  127.  
  128. def change_turn(turn):
  129.     if turn == X:
  130.         return O
  131.     else:
  132.         return X
  133.  
  134. def main():
  135.     human, computer = go_first()
  136.     board = new_board()
  137.     display_board(board)
  138.     turn = X
  139.     while winning_board(board) == None:
  140.         if human == turn:
  141.             board = human_move(board, human)
  142.             turn = change_turn(turn)
  143.             display_board(board)
  144.         else:
  145.             board = cpu_move(board, computer, human)
  146.             turn = change_turn(turn)
  147.             display_board(board)
  148.  
  149. main()
  150.    
  151. raw_input("Press Enter to exit.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement