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 :)