Adding game scoring logic to python turtle tictactoe game

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 753 Views - Last Post: 19 October 2018 - 05:25 AM Rate Topic: -----

#16 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7288
  • View blog
  • Posts: 15,171
  • Joined: 16-October 07

Re: Adding game scoring logic to python turtle tictactoe game

Posted 19 October 2018 - 01:38 AM

View PostDK3250, on 18 October 2018 - 05:06 PM, said:

Please note that baavgai made a small typo in post #11, naming board position 0, 1, 1 - should be 0, 1, 2

Damn. Thanks for pointing that out. I'll go fix that, to hopefully avoid (more) confusion.
Was This Post Helpful? 0
  • +
  • -

#17 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 462
  • View blog
  • Posts: 1,467
  • Joined: 27-December 13

Re: Adding game scoring logic to python turtle tictactoe game

Posted 19 October 2018 - 02:54 AM

@hanssarpei06: Your last full code (post #9) is a mix of your original code and baavgai's code from post #8; the last part more or less just pasted below your own part.

Let me see if I can help untangle this a bit.

baavgai's code (post #8) is fully playable if only the missing is_win() function is added. But it is console based i.e. no nice graphics.
Contrary, your code provide nice Turtle graphics, but not much play.

So, the idea is to make a combination.

The most important thing missing in your code is a test: For each new input (X or O) check if a winner is found.
The intention here in Dream-In-Code is not to handle out finished code but help you to find the solution yourself.
However, in this case an almost finished solution to the is_win() function is provided (post #11 and #14).

It is your task now, to finish the is_win() function and integrate it to your Turtle code.

Your original question in post #1 about game strategy is not solved here, - the 'AI' provided in post #8 is just for demo, purely random placements.
But before going down that road, you need the ability to detect a win and stop the game.
Was This Post Helpful? 0
  • +
  • -

#18 hanssarpei06   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-October 18

Re: Adding game scoring logic to python turtle tictactoe game

Posted 19 October 2018 - 04:03 AM

thanks for all your useful replies! but my checkwin is not working where is my mistake! and i dont want you to do my work i want to learn it! thank you so much
from turtle import Screen, Turtle

# X's and O's


def cross(x, y):
    turtle.penup()
    turtle.goto(x + 30, y - 35)
    turtle.setheading(-45)
    turtle.pendown()
    turtle.forward(50)
    turtle.penup()
    turtle.goto(x + 60, y - 30)
    turtle.setheading(-130)
    turtle.pendown()
    turtle.forward(50)
    turtle.penup()

def nought(x, y):
    turtle.penup()
    turtle.goto(x + 50, y - 70)
    turtle.setheading(0)
    turtle.pendown()
    turtle.circle(20)
    turtle.penup()

# Playing the game

def drawSquares():
    screen.tracer(False)  # because this routine is a graphics bottleneck

    x, y = -150, 150

    for square in board:
        if square == "X":
            cross(x, y)
        elif square == "O":
            nought(x, y)

        x += 100

        if x > 50:
            x = -150
            y -= 100

    screen.tracer(True)

def clicked(x, y):
    global turn
    if all(board):
        print("All taken!")
        return
    vertical = (x + 150) // 100
    horizontal = (150 - y) // 100

    Area = int(vertical + horizontal * 3)
    print("You clicked area number", Area, end="")
    if board[Area]:
        print(" -- already taken!")
        return
    else:
        print()

    board [Area] = turn

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

    drawSquares()

# Draw the board

screen = Screen()
screen.setup(330, 330)
screen.bgcolor("black")

turtle = Turtle(visible=False)
turtle.pensize(10)
turtle.color("white")
turtle.speed('fastest')

turtle.penup()
turtle.goto(-150, 50)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(-150, -50)
turtle.pendown()
turtle.forward(300)

turtle.setheading(-90)

turtle.penup()
turtle.goto(-50, 150)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(50, 150)
turtle.pendown()
turtle.forward(300)
turtle.penup()

# Start the game

turtle.color("yellow")

board = ["","","","","","","",""]

turn = "X"

drawSquares()

screen.onscreenclick(clicked)





import random


Player_X,Player_O, Player_None= "","X","O"
player=Player_X,Player_O,Player_None

def check_win(board,player):
    if board[0]==player and board[1]==player and board [2]==player:
        return True
    if board[3]==player and board[4]==player and board [5]==player:
        return True
    if board [6]==player and board [7]==player and board [8]==player:
        return True
    if board [0]==player and board [3]==player and board [6]==player:
        return True
    if board [1]==player and board [4]==player and board [7]==player:
        return True
    if board [2]==player and board [5]==player and board [8]==player:
        return True
    if board [0]==player and board [4]==player and board [8]==player:
        return True
    if board [2]==player and board [4]==player and board [6]==player:
        return True
    if check_win(board,"X"):
        print("player 1 wins")
    elif check_win(board,"O"):
        print("player 2 wins")
    else:
        print("no winner")


screen.mainloop()


Was This Post Helpful? 0
  • +
  • -

#19 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7288
  • View blog
  • Posts: 15,171
  • Joined: 16-October 07

Re: Adding game scoring logic to python turtle tictactoe game

Posted 19 October 2018 - 05:25 AM

Here's a quick refactor. Please, read the comments:
from turtle import Screen, Turtle
import random # moved here, even though you aren't using it

# use these consistently
# um, wait, wtf, do you understand the mapping?
# Player_X,Player_O, Player_None= "","X","O"
# compare
PLAYER_NONE, PLAYER_X, PLAYER_O = None, 'X', 'O'
# the None is there for your all check to work
# do you understand how that works?

# X's and O's

def cross(x, y):
    turtle.penup()
    turtle.goto(x + 30, y - 35)
    turtle.setheading(-45)
    turtle.pendown()
    turtle.forward(50)
    turtle.penup()
    turtle.goto(x + 60, y - 30)
    turtle.setheading(-130)
    turtle.pendown()
    turtle.forward(50)
    turtle.penup()

def nought(x, y):
    turtle.penup()
    turtle.goto(x + 50, y - 70)
    turtle.setheading(0)
    turtle.pendown()
    turtle.circle(20)
    turtle.penup()

# Playing the game

def drawSquares():
    screen.tracer(False)  # because this routine is a graphics bottleneck

    x, y = -150, 150

    for square in board:
        if square == PLAYER_X: # consistency!! "X":
            cross(x, y)
        elif square == PLAYER_O: # "O":
            nought(x, y)

        x += 100

        if x > 50:
            x = -150
            y -= 100

    screen.tracer(True)


# moved from below
# this is good
def check_win(board,player):
    if board[0]==player and board[1]==player and board [2]==player:
        return True
    if board[3]==player and board[4]==player and board [5]==player:
        return True
    if board [6]==player and board [7]==player and board [8]==player:
        return True
    if board [0]==player and board [3]==player and board [6]==player:
        return True
    if board [1]==player and board [4]==player and board [7]==player:
        return True
    if board [2]==player and board [5]==player and board [8]==player:
        return True
    if board [0]==player and board [4]==player and board [8]==player:
        return True
    if board [2]==player and board [4]==player and board [6]==player:
        return True
    # this is not good
    # wft is it just sitting there for?
    # what does it magically do?
    # if check_win(board,"X"):
    #     print("player 1 wins")
    # elif check_win(board,"O"):
    #     print("player 2 wins")
    # else:
    #     print("no winner")
    # don't forget to return false if no winner
    return False



def clicked(x, y):
    global turn
    if all(board):
        print("All taken!")
        return
    vertical = (x + 150) // 100
    horizontal = (150 - y) // 100

    Area = int(vertical + horizontal * 3)
    print("You clicked area number", Area, end="")
    if board[Area]:
        print(" -- already taken!")
        return
    else:
        print()

    board [Area] = turn
    # you just made a move
    # doesn't now seem like a good idea
    # to check if it was a winning move?

    # note, this isn't doing your any favors
    # using a number would be easier
    if turn == PLAYER_X: # "X":
        turn = PLAYER_O # "O"
    else:
        turn = PLAYER_X # "X"

    drawSquares()


# Draw the board
# so much global!?!
screen = Screen()
screen.setup(330, 330)
screen.bgcolor("black")

turtle = Turtle(visible=False)
turtle.pensize(10)
turtle.color("white")
turtle.speed('fastest')

turtle.penup()
turtle.goto(-150, 50)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(-150, -50)
turtle.pendown()
turtle.forward(300)

turtle.setheading(-90)

turtle.penup()
turtle.goto(-50, 150)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(50, 150)
turtle.pendown()
turtle.forward(300)
turtle.penup()

# Start the game

turtle.color("yellow")

# No
# board = ["","","","","","","",""]
# maybe
# board = [PLAYER_NONE,PLAYER_NONE,PLAYER_NONE,PLAYER_NONE,PLAYER_NONE,PLAYER_NONE,PLAYER_NONE,PLAYER_NONE]
# best
board = [PLAYER_NONE for _ in range(9)]
turn = PLAYER_X # "X"

drawSquares()

screen.onscreenclick(clicked)

# no, moved import random

# moved to top Player_X,Player_O, Player_None= "","X","O"
# huh? player=Player_X,Player_O,Player_None

screen.mainloop()



Now, I implore you, take a step back and review the basics you've already learned. You are still throwing code at the problem and hoping something, anything, will stick. You won't know everything all at once, but if you keep trying to barrel ahead, you may not know anything.

Look at code in small chunks and try to understand what those small chunks are doing. If you see a line like x, y, z = 1, 2, 3 and don't know what it means, look it up. Or, hell, ask someone here. If you have code that doesn't make sense to you, write the smallest possible example and make it reveal its meaning.

Good luck.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2