2 Replies - 1993 Views - Last Post: 17 June 2015 - 05:40 AM Rate Topic: -----

#1 StrategicGaming   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-June 15

Help with Python (Tic-Tac-Toe)

Posted 01 June 2015 - 10:07 AM

Hi,

Im currently working on a project for school (Tic-Tac-Toe) and I cant get the actual game to get displayed on the pygame window.
Sometimes the screen wont respond and eventually freeze phyton. Im terrible at coding, I tried everything google, help from friends, etc. and nothing seems to solve my issue. Im open for comments on my game I know its not great but im just a beginner. Here is my Code:

import pygame
(width, height) = (640, 480)
screen=pygame.display.set_mode([640,480])
pygame.display.flip()

pygame.display.set_caption("P3T Project")

green = (0,255,0)




screen.fill(green)
def print_board():
    for i in range(0,3):
        for j in range(0,3):
            print map[2-i][j],
            if j != 2:
                print "|",
        print ""


def check_done():
    for i in range(0,3):
        if map[i][0] == map[i][1] == map[i][2] != " " \
        or map[0][i] == map[1][i] == map[2][i] != " ":
            print turn, "won!!!"
            return True
        
    if map[0][0] == map[1][1] == map[2][2] != " " \
    or map[0][2] == map[1][1] == map[2][0] != " ":
        print turn, "won!!!"
        return True

    if " " not in map[0] and " " not in map[1] and " " not in map[2]:
        print "Draw"
        return True
        

    return False

pygame.display.update()











turn = "X"
map = [[" "," "," "],
       [" "," "," "],
       [" "," "," "]]
done = True


while done == True:
##    def print_board():
##        for i in range(0,3):
##            for j in range(0,3):
##                print map[2-i][j],
##                if j != 2:
##                    print "|",
##            print ""
##
##
##    def check_done():
##        for i in range(0,3):
##            if map[i][0] == map[i][1] == map[i][2] != " " \
##            or map[0][i] == map[1][i] == map[2][i] != " ":
##                print turn, "won!!!"
##                return True
##            
##        if map[0][0] == map[1][1] == map[2][2] != " " \
##        or map[0][2] == map[1][1] == map[2][0] != " ":
##            print turn, "won!!!"
##            return True
##
##        if " " not in map[0] and " " not in map[1] and " " not in map[2]:
##            print "Draw"
##            return True
##            
##
##        return False
    print_board()
    pygame.event.get()
    
    print turn, "'s turn"
    print

    moved = False

    while moved != True:
        pygame.event.get()

        try:
            pos = input("Select: ")
            if pos <=9 and pos >=1:
                Y = pos/3
                X = pos%3
                if X != 0:
                    X -=1
                else:
                     X = 2
                     Y -=1
                    
                if map[Y][X] == " ":
                    map[Y][X] = turn
                    moved = True
                    done = check_done()

                    if done == False:
                        if turn == "X":
                            turn = "O"
                        else:
                            turn = "X"

                
            
        except:
            print "You need to add a numeric value"
        pygame.display.update()




Thanks I appreciate your help

-StrategicGaming

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Python (Tic-Tac-Toe)

#2 witeboy724   User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 208
  • Joined: 21-June 12

Re: Help with Python (Tic-Tac-Toe)

Posted 01 June 2015 - 01:18 PM

To get something to display on your window you need to blit it onto the screen. You can use images for the board and X/O or you can draw lines and render text. Check out some of the pygame tutorials on their site. Here is one that shows how to blit an image: Pygame Moveit Example

Also, your game loop ends after you set moved to True, which is not what you want. I would just use a while True loop and then break out of the loop when the game is over.
Was This Post Helpful? 1
  • +
  • -

#3 pythonuser007   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 25-January 14

Re: Help with Python (Tic-Tac-Toe)

Posted 17 June 2015 - 05:40 AM

HI

Just a few points on your code.
Firstly i would make every square a class with its own co-ordinates and values, then you can just evoke the class on the square you are interested in and do whatever without any care as to where it is on the screen or what other things are happening.

Second I would tidy up your code and make it more modular as you are going to get lost quite quickly as it is. I struggle to follow your code as is and would stress modularity and structure over spagetti.


Third, you need to make some plan and stick to it, at the moment you can tell that there was no plan from the start and it was just coded right out of the starting gate. I would start by making a flow chart of the program you want then plan the functions from there and then the rest.

Fourth, there are ways of accessing your board which is quite easy and you can make your functions make some spectacular AI decisions with generic functions that do a lot of heavy lifting for you. But you must plan and structure your code first to do this.

If you continue to study coding you will find all these things will help you make more complex programs, with the payoff that your code will be very readable and others will follow your code easier.

Regards
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1