Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fun TicTacToe
#1
Hey, I had to create a game for my comp science class

Check it out!

Give me some feedback if you'd like

I'm currently working on a 'hard' version because this version is dreadfully easy lol

https://trinket.io/python/1fed95551c

here is the link!
Reply
#2
Quote:
import random
import turtle
t = turtle.Turtle()
t.hideturtle()
global x
global y
global A
global B
global C
global D
global E
global F
global G
global H
global I
global score

A = 0
B = 0
C = 0
D = 0
E = 0
F = 0
G = 0
H = 0
I = 0
#1 = X  2 = O

#background
t.speed(5000)
t.penup()
t.color("white")
t.goto(-200,-200)
t.pendown()
t.begin_fill()
t.goto(-200,200)
t.goto(200,200)
t.goto(200,-200)
t.end_fill()
t.penup()

#board
t.goto(200,200)
t.color("black")
t.backward(133)
t.right(90)
t.pendown()
t.forward(400)
t.penup()
t.goto(200,200)
t.left(90)
t.backward(266)
t.right(90)
t.pendown()
t.forward(400)
t.penup()
t.goto(200,-200)
t.right(180)
t.forward(133)
t.left(90)
t.pendown()
t.forward(400)
t.penup()
t.goto(200,-200)
t.right(90)
t.forward(266)
t.left(90)
t.pendown()
t.forward(400)
t.penup()

#numbersonboard
t.penup()
t.goto(-200,180)
t.color('Green')
t.write("A", font=("Arial", 16, "normal"))
t.backward(138)
t.write("B", font=("Arial", 16, "normal"))
t.backward(134)
t.write("C", font=("Arial", 16, "normal"))
t.left(90)
t.forward(135)
t.write("F", font=("Arial", 16, "normal"))
t.forward(135)
t.write("I", font=("Arial", 16, "normal"))
t.left(90)
t.backward(134)
t.write("H", font=("Arial", 16, "normal"))
t.backward(137)
t.write("G", font=("Arial", 16, "normal"))
t.right(90)
t.backward(135)
t.write("D", font=("Arial", 16, "normal"))
t.right(90)
t.backward(135)
t.write("E", font=("Arial", 16, "normal"))

def makex(x,y):
  t.color("mediumslateblue")
  t.penup()
  t.setheading(0)
  t.goto(x,y)
  t.left(45)
  t.forward(50)
  t.right(180)
  t.pendown()
  y0 = t.ycor()
  x0 = t.xcor()
  t.forward(100)
  t.penup()
  t.sety(y0)
  t.left(90)
  t.pendown()
  t.forward(100)

def makeo(x,y):
  t.setheading(180)
  t.color("orangered")
  t.penup()
  t.goto(x,y)
  t.sety(t.ycor()+45)
  t.pendown()
  t.circle(45)
  
def checkWin():
  global score
  if ((A == 1 and B == 1 and C == 1) or (A == 1 and D == 1 and G == 1) or (A == 1 and E == 1 and I == 1) or (B == 1 and E == 1 and H == 1) or (C == 1 and F == 1 and I == 1) or (C == 1 and E == 1 and G == 1) or (D == 1 and E == 1 and F == 1) or (G == 1 and H == 1 and I == 1)):
    score = score + 1
    print "You win!"
    exit()
  elif ((A == 2 and B == 2 and C == 2) or (A == 2 and D == 2 and G == 2) or (A == 2 and E == 2 and I == 2) or (B == 2 and E == 2 and H == 2) or (C == 2 and F == 2 and I == 2) or (C == 2 and E == 2 and G == 2) or (D == 2 and E == 2 and F == 2) or (G == 2 and H == 2 and I == 2)):
    print "You lose!"
    score = score + 1
    exit()

def Choice2():
  global x
  global y
  if choice == "A":
    x = -133
    y = 133
  elif choice == "B":
    x = 0
    y = 133
  elif choice == "C":
    x = 133
    y = 133
  elif choice == "D":
    x = -133
    y = 0
  elif choice == "E":
    x = 0
    y = 0
  elif choice == "F":
    x = 133
    y = 0
  elif choice == "G":
    x = -133
    y = -133
  elif choice == "H":
    x = 0
    y = -133
  elif choice == "I":
    x = 133
    y = -133

global clist
clist = ['A','B','C','D','E','F','G','H','I']

def compTurn():
  global choice
  global x
  global y
  global clist
  global A
  global B
  global C
  global D
  global E
  global F
  global G
  global H
  global I
  global score
  if (len(clist) == 0):
    score = score + 1
    print "Tie game!"
    return
  choice = random.choice(clist)
  clist.remove(choice)
  Choice2()
  makeo(x,y)
  if choice == "A":
    A = A + 2
  if choice == "B":
    B = B + 2
  if choice == "C":
    C = C + 2
  if choice == "D":
    D = D + 2
  if choice == "E":
    E = E + 2
  if choice == "F":
    F = F + 2
  if choice == "G":
    G = G + 2
  if choice == "H":
    H = H + 2
  if choice == "I":
    Choice2()
    I = I + 2

  
  
  
#center of A is (-133,133)
#center of B is (0, 133)
#center of C is (133, 133)
#center of D is (-133, 0)
#center of E is (0,0)
#center of F is (133, 0)
#center of G is (-133,-133)
#center of H is (0, -133)
#center of I is (133, -133)

score = 0
print "Welcome to TicTacToe, you will be playing against the computer. Good Luck!"
#t.speed(2)
while score == 0:
  
  global choice
  choice = str(raw_input("Choose where you would like your mark 'Pick a letter'"))

  if choice != "A" and choice != "B" and choice != "C" and choice != "D" and choice != "E" and choice != "F" and choice != "G" and choice != "H" and choice != "I":
    print "Your input is invalid"
    continue
  
  if A == 0 and choice == "A":
    Choice2()
    A = A + 1
    clist.remove('A')
  elif A != 0 and choice == "A":
    print "Your input is invalid"
    continue
  
  if B == 0 and choice == "B":
    Choice2()
    B = B + 1
    clist.remove('B')
  elif B != 0 and choice == "B":
    print "Your input is invalid"
    continue
  
  if C == 0 and choice == "C":
    Choice2()
    C = C + 1
    clist.remove('C')
  elif C != 0 and choice == "C":
    print "Your input is invalid"
    continue
  
  if D == 0 and choice == "D":
    Choice2()
    D = D + 1
    clist.remove('D')
  elif D != 0 and choice == "D":
    print "Your input is invalid"
    continue
  
  if E == 0 and choice == "E":
    Choice2()
    E = E + 1
    clist.remove('E')
  elif E != 0 and choice == "E":
    print "Your input is invalid"
    continue
  
  if F == 0 and choice == "F":
    Choice2()
    F = F + 1
    clist.remove('F')
  elif F != 0 and choice == "F":
    print "Your input is invalid"
    continue
  
  if G == 0 and choice == "G":
    Choice2()
    G = G + 1
    clist.remove('G')
  elif G != 0 and choice == "G":
    print "Your input is invalid"
    continue
  
  if H == 0 and choice == "H":
    Choice2()
    H = H + 1
    clist.remove('H')
  elif H != 0 and choice == "H":
    print "Your input is invalid"
    continue
  
  if I == 0 and choice == "I":
    Choice2()
    I = I + 1
    clist.remove('I')
  elif I != 0 and choice == "I":
    print "Your input is invalid"
    continue
  
  makex(x,y)
  checkWin()
  compTurn()
  checkWin()

I copied your code here so others don't need to go fishing to see it :p

I have two comments without actually looking at the code too much...
1) Globals are bad, you should avoid them as much as possible, and
2) You only accept capital input, so a lowercase "b" is not valid.  If you just make everything uppercase, that'd be nicer for the user:
>>> text = input("say something! ")
say something! spam
>>> text
'spam'
>>> text.upper()
'SPAM'
>>> text = text.upper()
>>> text
'SPAM'
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020