Python tic tac toe

Started by
6 comments, last by Zahlman 18 years, 11 months ago
hey, I am new to programming, and i want to know how to make a game (using python). I decided to do tic tac toe. After reading tutorials on how to get started and all, i started to make the game. Heres how it looks so far.
#Tic tac toe
def printgame(g):
    print " %s | %s | %s     1 | 2 | 3" % (g[1], g[2], g[3])
    print "-----------   -----------"
    print " %s | %s | %s     4 | 5 | 6" % (g[4], g[5], g[6])
    print "-----------   -----------"
    print " %s | %s | %s     7 | 8 | 9" % (g[7], g[8], g[9])
    print

def gethumansmove(g):
    empty = g[0]
    while 1:
        s = raw_input("which square (1..9): ")
        try:
            square = int(s)
        except:
            square = 0
            
        if square > 0 and square <= 9:
            if g[square] == empty:
                g[square] = "X"

                break
            else:
                print "that square is taken"
        else:
            print "please enter a number between 1 and 9"
print "Hello, X is first!"
turn = 1
while 1:
    empty = " "
    game = 10 * [empty]
    while turn <= 9:
        printgame(game)
        gethumansmove(game)
        turn = turn + 1
        

I had much help from sources, but now im stuck. My goal is to make a human vs. human. Also, may i ask if anyone knows how to "clear" the board. My brother tells me clearing the board is what i should do. However, he does C++ and doesnt know what commands are for python for that. can someone help me?
Advertisement
listen
i do C++ as well, but what you probably need to do is keep printing new lines with nothing on them so the old text moves up and disappears
then, after user input, you redraw(or rewrite) the board
hope this helps, but hey, i'm a noob too
the fuction to clear the screen in C++ is
system( "cls" );
As my bro tells me, and basically, i need to know how to that same function in Python.
Quote:Original post by lib
the fuction to clear the screen in C++ is
system( "cls" );
As my bro tells me, and basically, i need to know how to that same function in Python.

import os
os.system( "cls" )

or, if you'd prefer:

from os import system
system( "cls" )


(or "clear" in *nix. - since you're using a language that is very cross-platform friendly, may as well keep those things in mind.)

Along those lines, here's a light example of building it to work either way.
Here's the python equivalent of that C code:
import osos.system("cls")

EDIT:
If you wan't a semi-cross platform way to do this, or if you don't want to incurr the runtime cost of invoking a shell and running another program, you could use this:
def cls():    print "\n"*24

It assumes that there are 24 rows in a shell window, which is really only always true on Windows, but most of the time it will be fine.
Thanks so much, ive been trying to find this command for a while.
Now all I have to do is figure out how to make a winner (including all the combinations). Im not totally sure how to do this, but it will be great if you help me out (ill try right now)
Also, I would like to know how to put this in a black screen window(not the normal white python shell)
#Tic tac toedef printgame(g):    print " %s | %s | %s     1 | 2 | 3" % (g[1], g[2], g[3])    print "-----------   -----------"    print " %s | %s | %s     4 | 5 | 6" % (g[4], g[5], g[6])    print "-----------   -----------"    print " %s | %s | %s     7 | 8 | 9" % (g[7], g[8], g[9])    printdef gethumansmove(g):    empty = g[0]    while 1:        s = raw_input("which square (1..9): ")        try:            square = int(s)        except:            square = 0                    if square > 0 and square <= 9:            if g[square] == empty:                g[square] = "X"                break            else:                print "that square is taken"        else:            print "please enter a number between 1 and 9"def main( ):    print "Hello, X is first!"    turn = 1    while 1:        empty = " "        game = 10 * [empty]        while turn <= 9:            printgame(game)            gethumansmove(game)            turn = turn + 1# If this is being run as a standalone, ie invoked from commandline        if __name__ == "__main__":   main( )

then cd to the directory and "python yourfile.py"

As to victory, I won't steal the fun from you.

As a hint, when a player places a piece, you know that he hasn't won yet, so only check combinations of victory that the current move could make. If no victory conditions from it are matched, he hasn't won.

To clarify, if the player places a piece in the top left, check against the top row, the left column and the southeast diagonal to determine whether he's won as those are the only possibilities.
To have the humans take turns, note that X moves on odd-numbered turns (since you are starting your counting at 1) and O on even-numbered turns. So all you need to do is make the gethumansmove() aware of whose turn it is, and make the appropriate move.

Note that as you currently have it, the turn counter will advance even if the move is invalid. You will probably want to change that.

This topic is closed to new replies.

Advertisement