Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tic-Tac-Toe
- # global constants
- X = "X"
- O = "O"
- EMPTY = ""
- TIE = "TIE"
- NUM_SQUARES = 9
- def display_instruct():
- """ Display game instructions."""
- print \
- """
- Welcome to the greatest intellectual challenge of all time: Tic-Tac-Toe.
- This showdown will be a battle between your human brain and my processor.
- You will make your move known by entering a number, 0-8. The number will
- correspond to the board position as follows:
- 0 | 1 | 2
- -----------
- 3 | 4 | 5
- -----------
- 6 | 7 | 8
- Prepare yourself. The ultimate battle is about to begin. \n
- """
- def bquery(question):
- answer = None
- while answer not in ("y","n"):
- answer = raw_input(question).lower()
- return answer
- def ask_number():
- """ Gets a number from the player."""
- move = None
- while move not in range(0,9):
- move = raw_input("Where do you want to move? ")
- move = int(move)
- return move
- def go_first():
- """ Asks player to go first or not."""
- answer = bquery("Do you want to go first? ")
- if answer == "y":
- human = X
- computer = O
- else:
- human = O
- computer = X
- return human, computer
- def new_board():
- """ Creates a new blank game board."""
- board = []
- for square in range(0,9):
- board.append(EMPTY)
- return board
- def display_board(board):
- """ Display game board on screen."""
- print "\n\t", board[0], " |", board[1], " |", board[2]
- print "\t", "------------"
- print "\t", board[3], " |", board[4], " |", board[5]
- print "\t", "------------"
- print "\t", board[6], " |", board[7], " |", board[8], "\n"
- def winning_board(board):
- """ Checks to see if a player has won."""
- 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)]
- for row in win_rows:
- if board[row[0]] == board[row[1]] == board[row[2]] != EMPTY:
- winner = board[row[0]]
- return winner
- if EMPTY not in board:
- return TIE
- else:
- return None
- def legal_moves(board):
- """ Creates series of legal moves."""
- legal = []
- for square in range(0,9):
- if board[square] == EMPTY:
- legal.append(square)
- print "[debug] legal moves: ", legal
- return legal
- def human_move(board, human):
- """ Takes player's move + place on board."""
- move = None
- legal = legal_moves(board)
- while move not in legal:
- move = ask_number()
- board[move] = human
- return board
- def cpu_move(board, computer, human):
- """ Takes computer's move + places on board."""
- # make a copy of the board
- board = board[:]
- bestmoves = (0,2,6,8,4,3,5,1,7)
- # if computer can win, play that square:
- for move in legal_moves(board):
- board[move] = computer
- if winning_board(board) == computer:
- print "[debug] cpu win:", move
- return move
- # undo the move because it was empty before
- board[move] = EMPTY
- # if player can win, block that square:
- for move in legal_moves(board):
- board[move] = human
- if winning_board(board) == human:
- print "[debug] block human:", move
- return move
- board[move] = EMPTY
- # chose first best move that is legal
- for move in bestmoves:
- if move in legal_moves(board):
- board[move] = computer
- print "[debug] next best move:", move
- return move
- def change_turn(turn):
- if turn == X:
- return O
- else:
- return X
- def main():
- human, computer = go_first()
- board = new_board()
- display_board(board)
- turn = X
- while winning_board(board) == None:
- if human == turn:
- board = human_move(board, human)
- turn = change_turn(turn)
- display_board(board)
- else:
- board = cpu_move(board, computer, human)
- turn = change_turn(turn)
- display_board(board)
- main()
- raw_input("Press Enter to exit.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement