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

Source: Internet
Author: User
In this tutorial, we will show you how to create a tic-tac-word game with Python. Where 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 "|", C5/>print ""

We use two for loops to traverse the map, which is a two-dimensional array with location information.

The game board looks like this:

|  | |  | |  | X | X | O | X | O | O | X x | X | XX | X | XX | 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 "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 to check the horizontal and vertical directions, if there is one row or column that is not empty and contains three identical symbols, and then we check the skew direction. If there is a direction to meet, the game ends and prints "Won!!!". Notice that the variable is checked to mark which player is currently in the list.

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

With the two functions above, we create 3 variables:

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

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

Start the game now:

While do! = true:  print_board ()     print turn, "' s turn"  print   moved = False while moved!  = true:

The while loop is used here until the game ends and returns true. In this loop, another while loop is used to check if 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, see 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 if the number is between 1 and 9. In addition, we need a bit of 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

Here's the whole code:

Def print_board (): For I in Range (0,3): for J in Range (0,3): Print Map[2-i][j], if J! = 2:print "|" , print "" 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]! = "": Print    Turn, "won!!!" return true if "map[0" and "not in map[1] and" "Not in Map[2]: print" Draw "return True Retu RN False turn = "X" map = [["", "", ""], ["," "," "], [" "," "," ""]]done = False while doing! = True:print_b Oard () Print turn, "' s turn" print moved = False while moved! = true:print "Please select position by typing I n a number between 1 and 9,\ see 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:y = POS/3 x = pos%3 if X! = 0:x-=1 Else: X = 2 Y-=1 if map[y][x] = = "": map[y][x] = Turn moved = True do              NE = Check_done () if done = = false:if Turn = = "X": Turn = "O" Else: turn = "X" Except:print "You need to add a numeric value"
  • Related Article

    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.