My first python project =)
Alright guys. Since ASM has failed on me, I tried giving a shot at Python. I've tried writing a tic-tac-toe game and I ended up succeeding in 30-45 minutes ! That's not quite bad as I read documentation for about 15-20 minutes only ! =D
Here's the code. It must be pretty darn ugly code... but still, it compiles and works as intended.
Code:
symbols = { 0:' ', 1:'x', 2:'o' }
table = [0,0,0,0,0,0,0,0,0]
wantsToPlay = 'y'
def displayTable(table):
for i in range(len(table)):
if (i+1)%3 is 0:
print symbols[table[i]]
else:
print symbols[table[i]], '|',
def resetTable(table):
for i in range(len(table)):
table[i] = 0
def isWinner(table, player):
if (table[0] is player and table[1] is player and table[2] is player) or \
(table[3] is player and table[4] is player and table[5] is player) or \
(table[6] is player and table[7] is player and table[8] is player) or \
(table[0] is player and table[3] is player and table[6] is player) or \
(table[1] is player and table[4] is player and table[7] is player) or \
(table[2] is player and table[5] is player and table[8] is player) or \
(table[0] is player and table[4] is player and table[8] is player) or \
(table[2] is player and table[4] is player and table[6] is player):
return 1
else:
return 0
while(wantsToPlay is 'y'):
resetTable(table)
playerTurn = 2
while(isWinner(table, 1) is not 1 and isWinner(table, 2) is not 1):
displayTable(table)
playerTurn = (2 - playerTurn) + 1
while 1 is 1:
coord = int(raw_input("What coord do you want to play ? (0-8) "))
if table[coord] is 0:
table[coord] = playerTurn
break
print "Congratulations player %d you have won !" % (playerTurn)
wantsToPlay = raw_input("Do you want to play again ? (y/n) ")