Use Python to write a simple tic-tac-toe game tutorial _python

Source: Internet
Author: User
Tags numeric value

In this tutorial, we will show you how to create a well word game with Python. We will use functions, arrays, if conditional statements, while loop statements, and error trapping.

First we need to create two functions, the first function to display the game board:

Def print_board (): For
  I in range (0,3):
    for J in Range (0,3):
      print map[2-i][j],
      if J!= 2:
        print "|", C6/>print ""

Here we use two for loops to traverse the map, which is a two-dimensional array that contains location information.

The game board looks like this:

|  | 
|  | 
|  |
 
X | X | 
O | X | O
 | O | X
 
x | X | X
x | X | X
x | X | X


Below we need a function check_done () to check whether the game is over. If it ends, returns true and prints the message.

Def check_done (): For
  I in range (0,3):
    if map[i][0] = = map[i][1] = = map[i][2]!= "" \
    or map[0][i] = = map[1][ I] = = Map[2][i]!= "":
      Print Turn, "won!!!"
      Return True
     
  if map[0][0] = = map[1][1] = = map[2][2]!= "" \
  or map[0][2] = = map[1][1] = = Map[2][0]!= "":
    pri NT turn, "won!!!"
    return True if "' not in map[0] and" ' Not in map[1 ' and ' not in
 
  map[2]:
    print ' Draw ' return
    true
   
    return False

   

There are several places to check, first check the horizontal and vertical direction, whether one row or column is not empty and contain three identical symbols, and then we check the skew direction. If the top has a direction to meet, the game ends and prints "Won!!!". Notice that the variable change is used to mark which player is currently.

At the same time we need to check whether the current game board is filled and no one wins, the game draws.

With the above two functions, we create 3 variables:

turn = "X"
map = [["," "," "], [", "", ""
    ], [","
    , ""]] done
= False

Turn: Who's your turn?
Map: Game board
Done: Whether the game is over

Start the game now:

While doing!= true:
  print_board ()
   
  print turn, "' Turn"
  print
 
  moved = False while
  moved!= True:

This uses a while loop until the game ends and returns true. In this loop, another while loop is used to check whether the player is moving, and if the player does not move, the program jumps to the next loop.

The next step is to tell the player how to play:

Print "Please select position by typing in a number between 1 and 9, and the below for which number this is which position ..."
    print "7|8| 9 "
    print" 4|5|6 "
    print" 1|2|3 "
    print
 
try:
      pos = Input (" SELECT: ")
      if POS <=9 and POS >=1:

We expect the player to enter a number to check whether the number is between 1 and 9. In addition, we need an error-handling logic here, and we need to check if the player can move to a location:

y = pos/3
        x = pos%3
        if x!= 0:
          x-=1
        Else:
           x = 2
           Y-=1

The following are all the code:

Def print_board (): For I in Range (0,3): for J in Range (0,3): Print Map[2-i][j], if J!= 2:prin T "|", print "" Def Check_done (): For I in Range (0,3): if map[i][0] = = map[i][1] = = map[i][2]!= "" \ O
      r Map[0][i] = = map[1][i] = = Map[2][i]!= "": Print Turn, "won!!!" Return True if map[0][0] = = map[1][1] = = map[2][2]!= "" \ or map[0][2] = = map[1][1] = = Map[2][0]!= "": P
    Rint turn, "won!!!"
     
 
  return true if "' not in map[0] and" ' Not in map[1 ' and "not in map[2]: print ' Draw ' return True return False turn = "X" map = [["," "," "], [", "", ""], [",", ","]]  NE!= true:print_board () print turn, "' s turn" print moved = False while moved!= True:print " Select position by typing in a number between 1 and 9,\ the below for which number, which is which position ... "pri NT "7|8| 9 "print" 4|5|6 "print" 1|2|3 "Print Try:pos = Input (" SELECT: ") if POS <=9 and pos >=1:y = pos/3 X = P os%3 if X!= 0:x-=1 else:x = 2 Y-=1 if map[y][x] =
            = "": map[y][x] = turn moved = True done = Check_done () if done = = False:
      if turn = = "X": Turn = "O" Else:turn = "x" except:
 Print "Need to add a numeric value"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.