Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 19th, 2004, 12:36 AM
cam_k cam_k is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 cam_k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
TIC TAC TOE game

anyone here done a tic tac toe game on python before ?
i need help on how to start this thing. so far i've made the grid.

print "TIC TAC TOE:"
print "0|1|2 | | "
print "----- -----"
print "3|4|5 | | "
print "----- -----"
print "6|7|8 | | "

i only know to keep doing if statements to put where the X and O's would be. but that would be toooooooo much coding. so i need a really shorter way to put the marks where the user tells me to put them.

Reply With Quote
  #2  
Old October 19th, 2004, 03:30 AM
deltacoder's Avatar
deltacoder deltacoder is offline
slightly insane code guru
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2004
Location: Indianapolis, IN
Posts: 871 deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 8 m 36 sec
Reputation Power: 12
Send a message via AIM to deltacoder Send a message via MSN to deltacoder
how about using a list to represent each position of the board, considering that you already have them numbered? then just print out the other characters with the elements of the list concatenated in?
__________________

deltacoder.com :: W3C Standards :: PHP.net :: Google

Reply With Quote
  #3  
Old October 20th, 2004, 12:08 AM
cam_k cam_k is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 cam_k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by deltacoder
how about using a list to represent each position of the board, considering that you already have them numbered? then just print out the other characters with the elements of the list concatenated in?


how would i be able to make the list that u recommended.

Reply With Quote
  #4  
Old October 20th, 2004, 03:04 AM
deltacoder's Avatar
deltacoder deltacoder is offline
slightly insane code guru
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2004
Location: Indianapolis, IN
Posts: 871 deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 8 m 36 sec
Reputation Power: 12
Send a message via AIM to deltacoder Send a message via MSN to deltacoder
general syntax for lists:

Code:
listvar = [element1, element2, element3, element4, ...]
valueOfElementX = listvar[X]
firstThreeElements = listvar[:3]


et cetera...

Reply With Quote
  #5  
Old October 24th, 2004, 04:15 PM
cam_k cam_k is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 cam_k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
so far this is the code i have

Code:
def drawBoard(marks):
    """ Output the board represented by marks."""
    print "TIC TAC TOE:"
    print "0|1|2      | | "
    print "-----     -----"
    print "3|4|5      | | "
    print "-----     -----"
    print "6|7|8      | | "
    return drawBoard

def setPosition(marks):
    """sets the positions to given player's mark."""
s=str(drawBoard)
marks=s[0],s[1],s[2],s[3],s[4],s[5],s[6],s[7],s[8]
drawBoard("X XOXOXO ")


i have made a string named marks that is suppose to be the positions of the X's and O'x on the board. now is i want an X in for example in marks[0]. What command could i add to put the X there ?

Reply With Quote
  #6  
Old October 24th, 2004, 10:50 PM
deltacoder's Avatar
deltacoder deltacoder is offline
slightly insane code guru
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2004
Location: Indianapolis, IN
Posts: 871 deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 8 m 36 sec
Reputation Power: 12
Send a message via AIM to deltacoder Send a message via MSN to deltacoder
consider the following:
Code:
def drawBoard(marks): 
    """ Output the board represented by marks."""
    print "TIC TAC TOE:"
    print "0|1|2     "+marks[0]+"|"+marks[1]+"|"+marks[2]
    print "-----     -----"
    print "3|4|5     "+marks[3]+"|"+marks[4]+"|"+marks[5]
    print "-----     -----"
    print "6|7|8     "+marks[6]+"|"+marks[7]+"|"+marks[8]
    return drawBoard
    
marks = ["X", " ", " ", "O", "X", "O", "X", "O", " "]

drawBoard(marks)

marks[2] = "X"

drawBoard(marks)

Reply With Quote
  #7  
Old October 25th, 2004, 01:14 AM
cam_k cam_k is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 cam_k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
how do i then make a function that will ask the user to choose a position to put the X, and put the user's choice in the right position

Reply With Quote
  #8  
Old October 25th, 2004, 11:57 AM
deltacoder's Avatar
deltacoder deltacoder is offline
slightly insane code guru
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2004
Location: Indianapolis, IN
Posts: 871 deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 8 m 36 sec
Reputation Power: 12
Send a message via AIM to deltacoder Send a message via MSN to deltacoder
I'm sorry, but I'm not going to write the entire program for you. Look into the raw_input() and int() functions.

Reply With Quote
  #9  
Old October 25th, 2004, 02:05 PM
cam_k cam_k is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 cam_k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
i know those commands. but i don't know how to call it from a function.
i have this
[CODE]
def drawBoard(marks):
""" Output the board represented by marks."""
print "TIC TAC TOE:"
print "0|1|2 "+marks[0]+"|"+marks[1]+"|"+marks[2]
print "----- -----"
print "3|4|5 "+marks[3]+"|"+marks[4]+"|"+marks[5]
print "----- -----"
print "6|7|8 "+marks[6]+"|"+marks[7]+"|"+marks[8]
return drawBoard

marks = [" ", " ", " ", " ", " ", " ", " ", " ", " "]

drawBoard(marks)

def setPosition(i, string, marks):
""" Sets the positions of the strings """
if i == 0:
marks = string+marks[1]+marks[2]+marks[3]+marks[4]+marks[5]+marks[6]+marks[7]+marks[8]
if i == 1:
marks = marks[0]+string+marks[2]+marks[3]+marks[4]+marks[5]+marks[6]+marks[7]+marks[8]
if i == 2:
marks = marks[0]+marks[1]+string+marks[3]+marks[4]+marks[5]+marks[6]+marks[7]+marks[8]
if i == 3:
marks = marks[1]+marks[2]+marks[3]+string+marks[4]+marks[5]+marks[6]+marks[7]+marks[8]
if i == 4:
marks = marks[0]+marks[2]+marks[3]+marks[4]+string+marks[5]+marks[6]+marks[7]+marks[8]
if i == 5:
marks = marks[0]+marks[1]+marks[3]+marks[4]+marks[5]+string+marks[6]+marks[7]+marks[8]
if i == 6:
marks = marks[1]+marks[2]+marks[3]+marks[4]+marks[5]+marks[6]+string+marks[7]+marks[8]
if i == 7:
marks = marks[0]+marks[2]+marks[3]+marks[4]+marks[5]+marks[6]+marks[7]+string+marks[8]
if i == 8:
marks = marks[0]+marks[1]+marks[3]+marks[4]+marks[5]+marks[6]+marks[7]+marks[8]+string

def playerTurn(marks):
"""ask the user where they want to move."""
marks=setPosition(i,marks)
return playerTurn

i=str(raw_input("Player X, choose where you want to make your mark: "))
setPosition(i, str, marks)
print drawBoard
[CODE]

i have tried
[CODE]
def playerTurn(marks):
a=str(raw_input("Player X, select the position that u want/
insert your X: "))
return playerTurn
[CODE]
but that doesn't work, its not like that i'm not trying anythin, its just that i couldn't get it, and that's y i'm asking for your help. And i thank you a lot for helping me so far already.

Reply With Quote
  #10  
Old October 25th, 2004, 06:26 PM
deltacoder's Avatar
deltacoder deltacoder is offline
slightly insane code guru
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2004
Location: Indianapolis, IN
Posts: 871 deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 8 m 36 sec
Reputation Power: 12
Send a message via AIM to deltacoder Send a message via MSN to deltacoder
Ok, a few things.

1. You do not always need to return a value from a function. For instance, drawBoard() does not need to return anything - it just displays output.

2. Your setPosition function is overly complicated. How about the simple
Code:
def setPosition(num, string, marks):
    marks[num]=string
    return marks


3. To have the user input a number, try
Code:
num = int(raw_input("Please enter a number:"))


4. Put any code you post inside CODE and /CODE (not two CODE) tags. Otherwise indentation doesn't show up, which is critical for python.

Reply With Quote
  #11  
Old October 25th, 2004, 10:32 PM
cam_k cam_k is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 cam_k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
This is the code i have right now. its suppose to ask user for position to place the X or O, (playerTurn function) then with the setPosition function, the X or O should be placed in. then with the updated marks, print the new grid.

Code:

def drawBoard(marks): 
    """ Output the board represented by marks."""
    print "0|1|2     "+marks[0]+"|"+marks[1]+"|"+marks[2]
    print "-----     -----"
    print "3|4|5     "+marks[3]+"|"+marks[4]+"|"+marks[5]
    print "-----     -----"
    print "6|7|8     "+marks[6]+"|"+marks[7]+"|"+marks[8]
    return drawBoard

def setPosition(i, string, marks):
    """ Sets the positions of the strings """
    if i == 0:
        marks = string+marks[1]+marks[2]+marks[3]\
        +marks[4]+marks[5]+marks[6]+marks[7]+marks[8]
    if i == 1:
        marks = marks[0]+string+marks[2]+marks[3]\
        +marks[4]+marks[5]+marks[6]+marks[7]+marks[8]
    if i == 2:
        marks = marks[0]+marks[1]+string+marks[3]\
        +marks[4]+marks[5]+marks[6]+marks[7]+marks[8]
    if i == 3:
        marks = marks[0]+marks[1]+marks[2]+string\
        +marks[4]+marks[5]+marks[6]+marks[7]+marks[8]
    if i == 4:
        marks = marks[0]+marks[1]+marks[2]+\
        marks[3]+string+marks[5]+marks[6]+marks[7]+marks[8]
    if i == 5:
        marks = marks[0]+marks[1]+marks[2]+marks[3]\
        +marks[4]+string+marks[6]+marks[7]+marks[8]
    if i == 6:
        marks = marks[0]+marks[1]+marks[2]+marks[3]\
        +marks[4]+marks[5]+string+marks[7]+marks[8]
    if i == 7:
        marks = marks[0]+marks[1]+marks[2]+marks[3]\
        +marks[4]+marks[5]+marks[6]+string+marks[8]
    if i ==8:
        marks = marks[0]+marks[1]+marks[2]+marks[3]\
        +marks[4]+marks[5]+marks[6]+marks[7]+string
    return marks
    


def playerTurn(s, marks):
    """  when given the players symbol (X or O), asks the player for his or her choice.
    It should then put the the players mark on the board. """
    
    if s == "X":
        x= int(raw_input("Player X, choose where you want to make your mark (0-8): "))
        if x>=0 and x<=8: 
            marks = setPosition(x, "X", marks)
            return marks

s="X"
marks="         "

print "TIC TAC TOE:"
drawBoard(marks)
playerTurn(s, marks)
drawBoard(marks)

Reply With Quote
  #12  
Old October 26th, 2004, 03:21 AM
deltacoder's Avatar
deltacoder deltacoder is offline
slightly insane code guru
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2004
Location: Indianapolis, IN
Posts: 871 deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 8 m 36 sec
Reputation Power: 12
Send a message via AIM to deltacoder Send a message via MSN to deltacoder
you need to change one line:

Code:
playerTurn(s, marks)


should be
Code:
marks = playerTurn(s, marks)


also, i'd highly recommend using a list instead of a string for marks, which then allows the shorter
Code:
def setPosition(num, string, marks):
    marks[num]=string
    return marks


as it's much more consise. if you do that, the overall program becomes the (much shorter):

Code:
def drawBoard(marks): 
    """ Output the board represented by marks."""
    print "0|1|2     "+marks[0]+"|"+marks[1]+"|"+marks[2]
    print "-----     -----"
    print "3|4|5     "+marks[3]+"|"+marks[4]+"|"+marks[5]
    print "-----     -----"
    print "6|7|8     "+marks[6]+"|"+marks[7]+"|"+marks[8]
    return drawBoard

def setPosition(num, string, marks): 
    marks[num] = string
    return marks

def playerTurn(s, marks): 
    """  when given the players symbol (X or O), asks the player for his or her choice.
    It should then put the the players mark on the board. """
    
    if s == "X": 
        x = int(raw_input("Player X, choose where you want to make your mark (0-8): "))
        if x >= 0 and x <= 8: 
            marks = setPosition(x, "X", marks)
            return marks

s = "X"
marks = [" ", " ", " ", " ", " ", " ", " ", " ", " "]

print "TIC TAC TOE:"
drawBoard(marks)
marks = playerTurn(s, marks)
drawBoard(marks)

Last edited by deltacoder : October 28th, 2004 at 07:53 PM.

Reply With Quote
  #13  
Old October 27th, 2004, 04:00 PM
Tkinter_Bell Tkinter_Bell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Isle of dogs
Posts: 68 Tkinter_Bell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
mmm

Mybe the best way is to do it VB style and create a nice user interface first, then create an AI if you want to after.

Reply With Quote