Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    13
    Rep 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.
  2. #2
  3. slightly insane code guru
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Location
    Indianapolis, IN
    Posts
    871
    Rep Power
    18
    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?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    13
    Rep Power
    0
    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.
  6. #4
  7. slightly insane code guru
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Location
    Indianapolis, IN
    Posts
    871
    Rep Power
    18
    general syntax for lists:

    Code:
    listvar = [element1, element2, element3, element4, ...]
    valueOfElementX = listvar[X]
    firstThreeElements = listvar[:3]
    et cetera...
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    13
    Rep 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 ?
  10. #6
  11. slightly insane code guru
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Location
    Indianapolis, IN
    Posts
    871
    Rep Power
    18
    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)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    13
    Rep 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
  14. #8
  15. slightly insane code guru
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Location
    Indianapolis, IN
    Posts
    871
    Rep Power
    18
    I'm sorry, but I'm not going to write the entire program for you. Look into the raw_input() and int() functions.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    13
    Rep 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.
  18. #10
  19. slightly insane code guru
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Location
    Indianapolis, IN
    Posts
    871
    Rep Power
    18
    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.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    13
    Rep 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)
  22. #12
  23. slightly insane code guru
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Location
    Indianapolis, IN
    Posts
    871
    Rep Power
    18
    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.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Isle of dogs
    Posts
    68
    Rep Power
    11

    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.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Isle of dogs
    Posts
    68
    Rep Power
    11
    Check out PythonCard.org website they have a Tic tac toe excample. Its implemented in PythonCard of course.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    6
    Rep Power
    0

    Good idea


    I saw last night that you were making a tic tac toe game and thought it was a good idea so made my own atempt. It works although the graphics don't display towards the end. It does use a more recent version of the livewires modules so if anybody wants them email me at philip.debeger@btopenworld.com. If there is code that looks out of place it's because lots of the code was stolen from a draughts game i wrote with a mate. heres the code:
    #sorry it's a little long for a forum but please have a look.
    from livewires import boards
    from livewires import colour
    from pygame.constants import *
    from random import randint
    from livewires import games

    BOX_SIZE = 30
    MARGIN = 50
    N_COLS=3
    N_ROWS=3

    turn=randint(0,1)

    class Our_Board (boards.SingleBoard):
    def __init__ (self, N_COLS, N_ROWS):
    self.init_singleboard ((MARGIN, MARGIN), N_COLS, N_ROWS, BOX_SIZE)
    self.enable_cursor(0,0)
    self.create_directions()

    def handle_keypress(self, key):
    if key == K_RETURN or key == K_SPACE:
    self.cursor.select()


    def new_gamecell (self, i ,j):
    return Our_Cells(self, i, j)


    # create cells
    class Our_Cells (boards.GameCell) :
    def __init__ (self, board, i, j) :
    fill=colour.white
    self.empty=1
    self.x=0
    self.o=0
    self.init_gamecell (board, i, j, fill_colour=fill)

    def select (self):
    global turn

    if turn&1 and self.empty==1:
    (x, y) = self.board.cell_to_coords (self.grid_x, self.grid_y)
    games.Text (self.board, x + BOX_SIZE / 2, y + BOX_SIZE / 2,
    "X",50, colour.red)

    turn=turn+1
    self.empty=0
    self.x=1
    self.check_win()
    else:
    if self.empty==1:
    (x, y) = self.board.cell_to_coords (self.grid_x, self.grid_y)
    games.Text (self.board, x + BOX_SIZE / 2, y + BOX_SIZE / 2,
    "O",50, colour.blue)
    turn=turn+1
    self.empty=0
    self.o=1
    self.check_win()
    else:
    pass

    def win (self):
    global turn
    turn=turn-1
    if turn&1:#is number odd
    print 'red win'
    (x, y) = self.board.cell_to_coords (self.grid_x, self.grid_y)
    games.Text (self.board,BOX_SIZE * N_COLS / 2 + MARGIN, BOX_SIZE * N_COLS / 2 + MARGIN,
    "Red WINS",50, colour.yellow)

    else:
    print 'blue win'
    (x, y) = self.board.cell_to_coords (self.grid_x, self.grid_y)
    games.Text (self.board,BOX_SIZE * N_COLS / 2 + MARGIN, BOX_SIZE * N_COLS / 2 + MARGIN,
    "Blue WINS",50, colour.yellow)

    time=0
    while time <5000000:
    time=time+1
    import sys
    sys.exit()



    def check_win (self):
    #checks left and right
    if self.direction[boards.LEFT] != None and self.direction[boards.RIGHT] != None:
    if self.direction[boards.LEFT].x == self.x and self.direction[boards.LEFT].o == self.o and self.direction[boards.RIGHT].x == self.x and self.direction[boards.RIGHT].o == self.o:
    self.win()

    #checks for win along right
    if self.direction[boards.RIGHT] != None and self.direction[boards.RIGHT].direction[boards.RIGHT] != None:
    if self.direction[boards.RIGHT].x == self.x and self.direction[boards.RIGHT].o == self.o and self.direction[boards.RIGHT].direction[boards.RIGHT].x ==self.x and self.direction[boards.RIGHT].direction[boards.RIGHT].o == self.o:
    self.win()
    #checks for win to the left
    if self.direction[boards.LEFT] != None and self.direction[boards.LEFT].direction[boards.LEFT] != None:
    if self.direction[boards.LEFT].x == self.x and self.direction[boards.LEFT].o == self.o and self.direction[boards.LEFT].direction[boards.LEFT].x ==self.x and self.direction[boards.LEFT].direction[boards.LEFT].o == self.o:
    self.win()

    #checks up and down
    if self.direction[boards.UP] != None and self.direction[boards.DOWN] != None:
    if self.direction[boards.UP].x == self.x and self.direction[boards.UP].o == self.o and self.direction[boards.DOWN].x == self.x and self.direction[boards.DOWN].o == self.o:
    self.win()

    #checks down the line
    if self.direction[boards.DOWN] != None and self.direction[boards.DOWN].direction[boards.DOWN] != None:
    if self.direction[boards.DOWN].x == self.x and self.direction[boards.DOWN].o == self.o and self.direction[boards.DOWN].direction[boards.DOWN].x ==self.x and self.direction[boards.DOWN].direction[boards.DOWN].o == self.o:
    self.win()

    #up
    if self.direction[boards.UP] != None and self.direction[boards.UP].direction[boards.UP] != None:
    if self.direction[boards.UP].x == self.x and self.direction[boards.UP].o == self.o and self.direction[boards.UP].direction[boards.UP].x ==self.x and self.direction[boards.UP].direction[boards.UP].o == self.o:
    self.win()

    #checks UP_left and DOWN_right
    if self.direction[boards.UP_LEFT] != None and self.direction[boards.DOWN_RIGHT] != None:
    if self.direction[boards.UP_LEFT].x == self.x and self.direction[boards.UP_LEFT].o == self.o and self.direction[boards.DOWN_RIGHT].x == self.x and self.direction[boards.DOWN_RIGHT].o == self.o:
    self.win()

    #checks DOWN_left and UP_right
    if self.direction[boards.DOWN_LEFT] != None and self.direction[boards.UP_RIGHT] != None:
    if self.direction[boards.DOWN_LEFT].x == self.x and self.direction[boards.DOWN_LEFT].o == self.o and self.direction[boards.UP_RIGHT].x == self.x and self.direction[boards.UP_RIGHT].o == self.o:
    self.win()

    #checks for win UP_left
    if self.direction[boards.UP_LEFT] != None and self.direction[boards.UP_LEFT].direction[boards.UP_LEFT] != None:
    if self.direction[boards.UP_LEFT].x == self.x and self.direction[boards.UP_LEFT].o == self.o and self.direction[boards.UP_LEFT].direction[boards.UP_LEFT].x ==self.x and self.direction[boards.UP_LEFT].direction[boards.UP_LEFT].o == self.o:
    self.win()

    #checks for win down_left
    if self.direction[boards.DOWN_LEFT] != None and self.direction[boards.DOWN_LEFT].direction[boards.DOWN_LEFT] != None:
    if self.direction[boards.DOWN_LEFT].x == self.x and self.direction[boards.DOWN_LEFT].o == self.o and self.direction[boards.DOWN_LEFT].direction[boards.DOWN_LEFT].x ==self.x and self.direction[boards.DOWN_LEFT].direction[boards.DOWN_LEFT].o == self.o:
    self.win()

    #checks for win up right
    if self.direction[boards.UP_RIGHT] != None and self.direction[boards.UP_RIGHT].direction[boards.UP_RIGHT] != None:
    if self.direction[boards.UP_RIGHT].x == self.x and self.direction[boards.UP_RIGHT].o == self.o and self.direction[boards.UP_RIGHT].direction[boards.UP_RIGHT].x ==self.x and self.direction[boards.UP_RIGHT].direction[boards.UP_RIGHT].o == self.o:
    self.win()

    #checks for win along down right
    if self.direction[boards.DOWN_RIGHT] != None and self.direction[boards.DOWN_RIGHT].direction[boards.DOWN_RIGHT] != None:
    if self.direction[boards.DOWN_RIGHT].x == self.x and self.direction[boards.DOWN_RIGHT].o == self.o and self.direction[boards.DOWN_RIGHT].direction[boards.DOWN_RIGHT].x ==self.x and self.direction[boards.DOWN_RIGHT].direction[boards.DOWN_RIGHT].o == self.o:
    self.win()


    #keep running board
    grid = Our_Board (N_COLS, N_ROWS)
    grid.mainloop ()
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo