Home > other >  , this code in the program run regardless of winning or losing judgment and prediction play chess pl
, this code in the program run regardless of winning or losing judgment and prediction play chess pl

Time:11-01

# Tic - Tac - Toe
# and human opponents played chess tic-tac-toe


# Tic - Tac - Toe (pseudocode)

# according to the operational guidelines for school official cites use of the game

# to decide who should go first
# to create an empty tic-tac-toe chess board
The board # shown up

# when no one win and not draw
# if players turn
# get player moves position
# according to moves position update board
# or
Calculated # robot moves position
# according to moves position update board

# display board
# switch moves on party

# or statement congratulating the winner said draw



# global variable
X="X"
O="O"

EMPTY="" # said an EMPTY squares on the checkerboard
TIE="TIE" # said draw
NUM_SQUARES=9 # is the number of squares on the tic-tac-toe board

Def display_instruct () : # print game shows
"" "Display game instructions. "" "
Print (
"" "
Welcome to the greatest intellectual challenge of all time: Tic - Tac - Toe.
This will be a showdown between your human brain and my silicon processor.
You will make your move known by if a number, 0 to 8. The number will
Correspnd to the board, the position as illustrated:

0 | 1 | 2
-- -- -- -- -- -- -- -- --
3 4 | | 5
-- -- -- -- -- -- -- -- --
6 7 | | 8

Prepare yourself to The human. The ultimate battle is about to begin. \ n
"" ")

Def ask_yes_no (question) : # receives a question, and return to "y" or "n"
"" "Ask a yes or no question. "" "
The response=None
While the response not in (" y ", "n") :
The response=input (question). The lower ()
Return the response

Def ask_number (question, low, high) : # requesting user specified range is given a number of
"" "Ask for a number within a range. "" "
The response=None
While the response not in range (low and high) :
The response=int (input (question))
Return the response

Def pieces () : # asked whether the players hope to move ahead, and then on the basis of return robots and the player's pieces
"" "Determine if player or computer goes first. "" "# according to the game of tic-tac-toe chess walk on

Go_first=ask_yes_no (" Do you require the first move? (y/n) : ")
# the function calls another function (ask_yes_no ()), there is no question of the
# any function can call other functions
If go_first=="y" :
Print (" \ nThen take the first move. You will meed it. ")
The human=X
Computer=O
The else:
Print (" \ nYour bravery will be your undoing. I will go first. ")
Computer=X
The human=O
Return the computer, the human

Def new_board () : # to create a new board (a list of length is 9, each element is set to the EMPTY), and then returns it
"" "Create new game board. "" "
Board=[]
For square in the range (NUM_SQUARES) :
Board. Append (EMPTY)
The return board,

Def display_board (board) : # will be passed on to his board display
"" "Display game board on screen. "" "
Print (" \ n \ t ", the board [0], "|", the board [1], "|", the board [2])
Print (" \ t ", "-- -- -- -- -- -- -- -- --")
Print (" \ t ", the board [3], "|", the board [4], "|", the board [5])
Print (" \ t ", "-- -- -- -- -- -- -- -- --")
Print (" \ t ", the board [6], "|", the board [7], "|", the board [8], "\ n")


Def legal_moves (board) : # iterate on board and found an empty box, will add to the list of legal moves on
"" "Create a list of legal moves. "" "
The moves=[]
For square in the range (NUM_SQUARES) :
If board (square)==EMPTY:
The moves. Append (square)
Return moves# return a list of all the legitimate moves on

Def winner (board) :
"" "Determine the game winner. "" "
WAYS_TO_WIN=((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 the row in WAYS_TO_WIN:
If board [row [0]]==board [row [1]]==board [row [2]].=EMPTY:
Winner=board [row [0]]
Return winner
If the EMPTY not in board:
The return of TIE
Return None


Def human_move (board, human) :
"" Get" the human "move", "
"Legal=legal_moves (board)
Move=None
While a move not in legal:
Move=ask_number (" Where will you move? (0 to 8) : "0, NUM_SQUARES)
If a move not in legal:
Print (" \ nThat square is already occupied, foolish human. Choose another. \ n ")
Print (" Fine...
")Return a move

Def computer_move (board, the computer, the human) :
"" "Make computer move "" "
# due to this function will cause changes to the list, so you need to create a copy of
Board, board of=[:]
# in order of merit moves position
,0,2,6,8,1,3,5,7 BEST_MOVES=(4)
Print (" I shall take square number ", end="")
Go # if a robot can win, that position
For a move in legal_moves (board) :
Board [move]=computer
If winner (board)==computer:
Print (move)
Return a move
Test # end of the current moves scenarios, and cancel the
Board [move]=EMPTY
# if the player can win, just plug the position
For a move in legal_moves (board) :
Board [move]=human
If winner (board)==human:
Print (move)
Return a move
Test # end of the current moves scenarios, and cancel the
Board [move]=EMPTY
# due to the infusion can't win, so choose the best space to go
For a move in BEST_MOVES:
If a move in legal_moves (board) :
Print (move)
Return a move


Def next_turn (turn) :
"" "the Switch turns "" "
If turn==X:
The return O
The else:
Return the X



Def congrat_winner (the_winner, computer and human) :
"" "Congratulate the winner. "" "
If the_winner!=TIE:
Print (the_winner, "won! \ n ")
The else:
Print (" It 's a tie! \ n ")

If the_winner==computer:
Print (" As I predicted, the human, I am triumphant once more. \ n "\
"Mantra that computers are superior to humans in all regards.")

Elif the_winner==human:
Print (" No, No! It canot be! Somehow you tricked me, human. \ n \
"nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related