Hey guys,

Im trying to learn python and i decided to try and make a text based tic tac toe game.
I dont have the matching up yet (where the rows say there are 3 in a row they win). But I have this so far and as you can use my terrible re-use of code and that bad loopme thing.

I was wondering if anyone could give me a little code refactoring (without making it too confusing), then I can understand how I can realistically use these functions.

#
# Tic Tac Toe
#
import random

#
# This is the simple game board, (Just visualize a tic tac toe board)
#
game = [0,1,2,
        3,4,5,
        6,7,8]

#
# 1. Marks a field (if it is a digit, otherwise an 'x' or 'o' is a taken spot)
# 2. Runs the Mark_Computer() and puts an 'o' randomly someplace
#
def mark(x):
    if x == '0' and game[0] == 0:
        game[0] = 'x'
        mark_computer(True)
    elif x == '1' and game[1] == 1:
        game[1] = 'x'
        mark_computer(True)
    elif x == '2' and game[2] == 2:
        game[2] = 'x'
        mark_computer(True)
    elif x == '3' and game[3] == 3:
        game[3] = 'x'
        mark_computer(True)
    elif x == '4' and game[4] == 4:
        game[4] = 'x'
        mark_computer(True)
    elif x == '5' and game[5] == 5:
        game[5] = 'x'
        mark_computer(True)
    elif x == '6' and game[6] == 6:
        game[6] = 'x'
        mark_computer(True)
    elif x == '7' and game[7] == 7:
        game[7] = 'x'
        mark_computer(True)
    elif x == '8' and game[8] == 8:
        game[8] = 'x'
        mark_computer(True)
    else:
        print 'You gotta type in a digit son'

#
# This counter is in the PARAM because it appears that when I run it once
# the variable saves as False from a previous use.. Almost like a static
# variable I cant figure out yet.
#
def mark_computer(start_counter):
    loopme = start_counter
    
    while loopme == True:
        z = random.randint(0,9)
        
        if game[z] == 'x':
            pass
        else:
            game[z] = 'o'
            # Break out of the loop#
            loopme = False
            


#
# This is the game loop, it prints the board too.
#
while 1:
    a = raw_input('Type a field to mark')

    mark(a)

    print game[0:3]
    print game[3:6]
    print game[6:9]

Also please note, this is not a project or assignment I am learning this for fun and need to give myself projects to keep learning with real examples :)

Here is a little refactoring

#
# Tic Tac Toe
#
import random

#
# This is the simple game board, (Just visualize a tic tac toe board)
#
game = list("012345678")

#
# 1. Marks a field (if it is a digit, otherwise an 'x' or 'o' is a taken spot)
# 2. Runs the Mark_Computer() and puts an 'o' randomly someplace
#
digits = set("012345678")

def mark(x):
    if x in digits:
        ix = int(x)
        if game[ix] == x:
            game[ix] = 'x'
            mark_computer()
            return
        print("Please choose a free spot")
    else:
        print 'You gotta type in a digit son'

def mark_computer():
    while True:
        z = random.randint(0,8)
        if game[z] not in ('o', 'x'):
            game[z] = 'o'
            # Break out of the loop#
            break
            
#
# This is the game loop, it prints the board too.
#
while 1:
    a = raw_input('Type a field to mark: ')

    mark(a)

    for i in range(0, 9, 3):
       print " ".join(game[i:i+3])

THanks!! I am going to go through this :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.