Thread: My first python project =)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    903

    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) ")
    Last edited by Desolation; 06-17-2007 at 11:46 PM. Reason: Used a dictionary for symbols and moved globals up at the top.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM