Jump to content
  • Advertisement
Sign in to follow this  
HeyHoHey

noob game looking for input/direction(python)

This topic is 4164 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

okay well i just finished my monte door game where one of three doors is randomly selected then the user clicks on a door it opens up and says if it was correct or not and which one is correct. that was my most complex program/game so far. so now i decided to try to create a tic tac toe game. i made the board basically a bunch of rectangle/squares that are essientially buttons. its a two player game not vs computer. so far i got the board set up and the first click creates an x, and the second and o. so i plan to add a loop to this so i can go back and forth between the two players clicking. now i just was wondering how do i make the game realize that a player won?(3 x's or 3 o's in a row). do i createa a method in my x or y class?(x and y basically x and o). well heres my code so far. if anyone has any idea or recommendations i would appreciate that. also if my code can be improved in any way please say so. here it is....
# Create tic tac toe board with graphics
from graphics import *


class X:
    def __init__(self, location, win):
        self.location = location
        self.win = win
    def drawX(self):
        Line(Point(self.location.p1.getX(), self.location.p1.getY()), Point(self.location.p2.getX(), self.location.p2.getY())).draw(self.win)
        Line(Point((self.location.p1.getX() + 1), self.location.p1.getY()), Point((self.location.p2.getX() - 1), self.location.p2.getY())).draw(self.win)

class Y:
    def __init__(self, location, win):
        self.location = location
        self.win = win
    def drawO(self):
        self.point = Point((self.location.p1.getX() + .5), (self.location.p1.getY() + .5))
        Circle(self.point, .5).draw(self.win)
        
def main():
    # Intro/Welcome Message
    print "Welcome to my Tic-Tac-Toe computer game!"
    print "This is a two player game, its extremely simple, just click where you would like to put your X or O. \n Whoever goes first is always O."
    print
    raw_input("To open the game window press <enter>, be sure to have fun!")

    # Create Board/Window
    win = GraphWin("Tic-Tac-Toe Board!", 500, 500)
    win.setCoords(0, 0, 3, 3)
    win.setBackground('pink')


    # Creates Board in square by square
    BottomLeft = Rectangle(Point(0, 0), Point(1, 1))
    BottomLeft.draw(win)


    BottomMiddle = Rectangle(Point(1, 0), Point(2, 1))
    BottomMiddle.draw(win)

    BottomRight = Rectangle(Point(2, 0), Point(3, 1))
    BottomRight.draw(win)

    MiddleLeft = Rectangle(Point(0, 1), Point(1, 2))
    MiddleLeft.draw(win)

    MiddleMiddle = Rectangle(Point(1, 1), Point(2, 2))
    MiddleMiddle.draw(win)

    MiddleRight = Rectangle(Point(2, 1), Point(3, 2))
    MiddleRight.draw(win)

    TopLeft = Rectangle(Point(0, 2), Point(1, 3))
    TopLeft.draw(win)

    TopMiddle = Rectangle(Point(1, 2), Point(2, 3))
    TopMiddle.draw(win)

    TopRight = Rectangle(Point(2, 2), Point(3, 3))
    TopRight.draw(win)


    # first player is always X

    playerX = win.getMouse()

    # determines which square user clicked in then creates object from class X
    # it then calls the method to draw an X
    if 0 <= playerX.getX() <= 1 and 0 <= playerX.getY() <= 1:
        x = X(BottomLeft, win)
        x.drawX()
    elif 1 <= playerX.getX() <= 2 and 0 <= playerX.getY() <= 1:
        x = X(BottomMiddle, win)
        x.drawX()
    elif 2 <= playerX.getX() <= 3 and 0 <= playerX.getY() <= 1:
        x = X(BottomRight, win)
        x.drawX()
    elif 0 <= playerX.getX() <= 1 and 1 <= playerX.getY() <= 2:
        x = X(MiddleLeft, win)
        x.drawX()
    elif 1 <= playerX.getX() <= 2 and 1 <= playerX.getY() <= 2:
        x = X(MiddleMiddle, win)
        x.drawX()
    elif 2 <= playerX.getX() <= 3 and 1 <= playerX.getY() <= 2:
        x = X(MiddleRight, win)
        x.drawX()
    elif 0 <= playerX.getX() <= 1 and 2 <= playerX.getY() <= 3:
        x = X(TopLeft, win)
        x.drawX()
    elif 1 <= playerX.getX() <= 2 and 2 <= playerX.getY() <= 3:
        x = X(TopMiddle, win)
        x.drawX()
    elif 2 <= playerX.getX() <= 3 and 2 <= playerX.getY() <= 3:
        x = X(TopRight, win)
        x.drawX()

    # Player 2 is always 0's
    # same thing as above just for second players click

    playerY = win.getMouse()

    if 0 <= playerY.getX() <= 1 and 0 <= playerY.getY() <= 1:
        y = Y(BottomLeft, win)
        y.drawO()
        
    elif 1 <= playerY.getX() <= 2 and 0 <= playerY.getY() <= 1:
        y = Y(BottomMiddle, win)
        y.drawO()
    elif 2 <= playerY.getX() <= 3 and 0 <= playerY.getY() <= 1:
        y = Y(BottomRight, win)
        y.drawO()
    elif 0 <= playerY.getX() <= 1 and 1 <= playerY.getY() <= 2:
        y = Y(MiddleLeft, win)
        y.drawO()
    elif 1 <= playerY.getX() <= 2 and 1 <= playerY.getY() <= 2:
        y = Y(MiddleMiddle, win)
        y.drawO()
    elif 2 <= playerY.getX() <= 3 and 1 <= playerY.getY() <= 2:
        y = Y(MiddleRight, win)
        y.drawO()
    elif 0 <= playerY.getX() <= 1 and 2 <= playerY.getY() <= 3:
        y = Y(TopLeft, win)
        y.drawO()
    elif 1 <= playerY.getX() <= 2 and 2 <= playerY.getY() <= 3:
        y = Y(TopMiddle, win)
        y.drawO()
    elif 2 <= playerY.getX() <= 3 and 2 <= playerY.getY() <= 3:
        y = Y(TopRight, win)
        y.drawO()

    win.getMouse()

        


    
    win.close()


thanks hey ho hey

Share this post


Link to post
Share on other sites
Advertisement
Personally, I wouldn't use separate classes for those X's and O's. A Tile class would suffice. I'd then use a simple 3x3 grid (a list containing lists, which contain Tile objects - instead of separate objects, as that saves you writing some repetetive code), which stores a value that either represents 'empty', 'X' or 'O'. Using a list also makes checking a winning condition easier, although the core idea is still the same.

To check if a player has won, simply check each row, collumn and diagonal to see if they contain the same value. If there's any of these does contain the same values, you've got a winner: if the values are all 'X', then player X has won, if they're all 'O', then player O has won. Of course, you'd put this in a function, and call it every time a tile has been placed. A bit more efficient would be to only check the changed row, collumn and diagonal, but performance isn't really an issue in this game, so I wouldn't bother doing that. ;)


Anyway, you've got a lot of duplicate code going on. Consider using loops and lists more often, and write functions for code that gets executed frequently - the different objects it gets executed on can be given as parameters.

Share this post


Link to post
Share on other sites
thank you for the post. this is very useful. im gonna get started on changing the game the way you suggested.

thanks
heyhohey

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!