0

The first function calls to make the tic tac toe board, and the code before creates the screen and turtle.

        #import needed modules
         import turtle
        #make a screen
        ttts = turtle.Screen()
        #set up the pen
        d = turtle.Turtle()
        d.color("indigo")
        d.pensize(4)
        d.speed(5)
        d.penup()
        #make number list
        number_list = ["1","2","3","4","5","6","7","8","9"]
        current_number = "1","2","3","4","5","6","7","8","9"
        #set beginning position
        d.goto(-150,100)
        #make square (function)
        def square(*squares):
          squares = 0
          columns = 0
          rows = 0
          x = -75
          y = 100
          while (squares < 4):
            d.pendown()
            d.forward(225)
            d.right(90)
            squares = squares + 1
          d.right(90)
          while (columns < 2):
            d.goto(x,y)
            d.pendown()
            d.forward(225)
            d.penup()
            x = x + 75
            columns = columns + 1
          d.right(90)
          while (rows < 3):
            d.goto(x,y)
            d.pendown()
            d.forward(225)
            d.penup()
            y = y + -75
            rows = rows + 1
        square()

This code above is fine, I have made sure to debug all the functions below as well. I'm just having trouble calling the functions at the end.

#make the numbers 
def numbers():
  d.penup()
  d.color("yellow")
  d.goto(-120, 50)
  d.write(1, align='left', font = ('arial', 16))
  d.goto(-45, 50)
  d.write(2, align='left', font = ('arial', 16))
  d.goto(30, 50)
  d.write(3, align='left', font = ('arial', 16))
  d.goto(-120, -25)
  d.write(4, align='left', font = ('arial', 16))
  d.goto(-45, -25)
  d.write(5, align='left', font = ('arial', 16))
  d.goto(30, -25)
  d.write(6, align='left', font = ('arial', 16))
  d.goto(-120, -100)
  d.write(7, align='left', font = ('arial', 16))
  d.goto(-45, -100)
  d.write(8, align='left', font = ('arial', 16))
  d.goto(30, -100)
  d.write(9, align='left', font = ('arial', 16))
  d.hideturtle()
numbers()
#function for defining symbols(x and o)
def draw_symbol(number_list): 
  number_list
  symbolc()
  symbolxs()
#functions for x and o
c = turtle.Turtle()
xs = turtle.Turtle()
def symbolc(circle):
  circle = 0
  def check_number_1():
      if (current_number == "1"):
        c.goto(-120,70)
  ttts.onkeypress(check_number_1, "1")
  def check_number_2():
    if (current_number == "2"):
      c.goto(-45,70)
  ttts.onkeypress(check_number_2, "2")
  def check_number_3():
    if (current_number == "3"):
      c.goto(30,70)
  ttts.onkeypress(check_number_3, "3")
  def check_number_4():
    if (current_number == "4"):
      c.goto(-120,-5)
  ttts.onkeypress(check_number_4, "4")
  def check_number_5():
    if (current_number == "5"):
      c.goto(-45,-5)
  ttts.onkeypress(check_number_5, "5")
  def check_number_6():
    if (current_number == "6"):
      c.goto(30,-5)
  ttts.onkeypress(check_number_6, "6")
  def check_number_7():
    if (current_number == "7"):
      c.goto(-120,-80)
  ttts.onkeypress(check_number_7, "7")
  def check_number_8():
    if (current_number == "8"):
      c.goto(-45,-80)
  ttts.onkeypress(check_number_8, "8")
  def check_number_9():
    if (current_number == "9"):
      c.goto(30,-80)
  ttts.onkeypress(check_number_9, "9")
#--- main ---
  if  (circle < 1):
    c.penup()
    c.color("red")
    c.circle(20)
    circle = circle + 1
#crosses
def symbolxs(cross):
  cross = 0
  while (cross < 4):
    def check_number_1():
      if (current_number == "1"):
        xs.goto(-120,70)
    ttts.onkeypress(check_number_1, "1")
    def check_number_2():
      if (current_number == "2"):
        xs.goto(-45,70)
    ttts.onkeypress(check_number_2, "2")
    def check_number_3():
      if (current_number == "3"):
        xs.goto(30,70)
    ttts.onkeypress(check_number_3, "3")
    def check_number_4():
      if (current_number == "4"):
        xs.goto(-120,-5)
    ttts.onkeypress(check_number_4, "4")
    def check_number_5():
      if (current_number == "5"):
        xs.goto(-45,-5)
    ttts.onkeypress(check_number_5, "5")
    def check_number_6():
      if (current_number == "6"):
        xs.goto(30,-5)
    ttts.onkeypress(check_number_6, "6")
    def check_number_7():
      if (current_number == "7"):
        xs.goto(-120,-80)
    ttts.onkeypress(check_number_7, "7")
    def check_number_8():
      if (current_number == "8"):
        xs.goto(-45,-80)
    ttts.onkeypress(check_number_8, "8")
    def check_number_9():
      if (current_number == "9"):
        xs.goto(30,-80)
    ttts.onkeypress(check_number_9, "9")
    xs.penup()
    xs.color("lime")
    xs.right(45)
    xs.pendown()
    xs.forward(10)
    cross = cross + 1
#turns - Circles goes first always. I hope you know how to play tic tac toe.
c.stamp()
xs.stamp()
c.stamp()
xs.stamp()
c.stamp()
xs.stamp()
c.stamp()
xs.stamp()
c.stamp()
ttts.listen()
turtle.mainloop()

If you see an immediate error, please comment on enter code hereit. However, keep in mind that this is code made by a very young (early teen) student. Thanks a bunch! ps, sorry if the code looks bad on this. I do not know how to use this app, as it is my first question here. :)

  • I have adjusted the code by moving the number_list and current_number variables, and have deleted the d def draw_symbol(number_list): number_list symbolc() symbolxs() and a syntax error now shows up as needing one positional argument. – IM-A-STUDENT Apr 30 '22 at 16:37
  • I'm unsure. are you asking because you have a specific problem or because you want us to debug your code? Note: you might want to call the function to listen to keypresses. also no need to tell everyone your age, and you have parameters in functions without using them. – ItzTheDodo Apr 30 '22 at 17:22
  • Thanks, ItzTheDodo for the reply! I'm not asking for you to debug the code, I'm just asking what you think I should do to have this operational, like just giving an idea or two. Also, I know I do not have to specify my age, however, I tried another website without saying so, and was called "an (r-word)ed adult with no original ideas and no skill." – IM-A-STUDENT Apr 30 '22 at 18:09

1 Answers1

1

Ok, some things to think about when coding:

OOP (Object orientated programming):

OOP is creating your own object which will make your code cleaner and mean you don't use excessive global variables when using a structured/function approach.

In python, OOP is done by creating a class:

# OOP Example

class MyObject:

    def __init__(self): # this is the constructor class and it is called when the object is initialised

        # Below are two types of useful variable types that can be accessed in the object. They are properties of the object and act as global variables across all of the functions in the class (with the exception of static functions)
        self.example_public_variable = None  # public variable (can be accessed from the object variable
        self._example_private_variable = None  # private variable (can only be accessed from within the object)
    
    # This is a function as part of the object and can be called from an object instance. It takes one parameter that can be passed in. A parameter is a variable that the function can access from the script that has called it.
    def example_function(self, param):
        pass

obj = MyObject() # I am creating a variable of my new object type which is calling the __init__ function. NOTE: the self parameter is the current instance of the object and is automatically passed in.
var = obj.example_public_variable # accessing the public variable
obj.example_function() # calling an object's function

Object Orientated programming is very helpful in keeping your code neat and can be used in your case like this:

class Game:

    def __init__(self):
        # do the turtle initialisation, window drawing and listener binds

    def _on_right_click(self, event):
        # handle right click event

    # ... (and so on)

if __name__ == "__main__": # clean way of starting your executable code
    Game()

OOP is quite advanced and you can find help here:

IDE (Integrated Development Environment):

It is significantly easier to code with help, and luckily, there are special text editors dedicated to this. IDEs check for syntax errors, tell you how you could improve the look and style of your code and it tells you how to follow PEP. I would recommend Pycharm for python. Jetbrains also do a student programme where you can get the pro version for free as well as their other tools which could help you if you move on to a more complicated language.

Pycharm: https://www.jetbrains.com/pycharm/

Nested functions:

I see you have used a lot of nested functions which is not best practice because it negates the need for functions in the first place as they are for cleaning up your code and making pieces of code accessible again and again.

Global Variables:

If you don't want to use OOP, then try to stay away from too many global variables. These litter your namespace and make it harder to keep track of the variables your current function is interested in. Try to have all external variables passed in as parameters. This also makes it easier for you to keep track of the variables needed to call a function and reduces your chances of coming across one of those annoying bugs that don't throw an error.

Don't repeat code:

A rule I go by is: 'if I have to rewrite the code, even once, throw it into a function. It is cleaner and easier to understand.'

Use __name__:

This is more for when you start writing libraries or you have multiple files, use the statement if __name__ == "__main__": before writing any code that is run when the file is run. It doesn't litter the namespace when imported and means the user that imports your code doesn't call any functions that they don't want to.

Create meaningful variable names:

A lot of your variables do not describe what they contain (e.g. c, d, tttk). To make it easier for yourself, name variables appropriately.

This will all make your code easier to debug and read.

ItzTheDodo
  • 100
  • 8