0
from graphics import * 
import pygame
import time
# Create a window 
global win  
win = GraphWin("Checkers", 630, 630)
win.setBackground("white")

# Put colours in a list
colours= ["black", "red"]

# Draw checkerboard
for i in range(8):
        for j in range(8):
            if (i + j) % 2 == 0:
                color = "red"
            else:
                color = "black"

            square = Rectangle(Point(i * 75, j * 75), Point((i + 1) * 75, (j + 1) * 75))
            square.setFill(color)
            square.draw(win)

#Number and Lettered Columns 

#Letters

A = Text(Point(35, 610), "A")
A.draw(win)
A.setSize(15)

B = Text(Point(110, 610), "B")
B.draw(win)
B.setSize(15)

C = Text(Point(190, 610), "C")
C.draw(win)
C.setSize(15)

D = Text(Point(265, 610), "D")
D.draw(win)
D.setSize(15)

E = Text(Point(338, 610), "E")
E.draw(win)
E.setSize(15)

F = Text(Point(415, 610), "F")
F.draw(win)
F.setSize(15)

G = Text(Point(490, 610), "G")
G.draw(win)
G.setSize(15)

H = Text(Point(560, 610), "H")
H.draw(win)
H.setSize(15)

#Numbers

ONE = Text(Point(610, 35), "1")
ONE.draw(win)
ONE.setSize(15)

TWO = Text(Point(610, 110), "2")
TWO.draw(win)
TWO.setSize(15)

THREE = Text(Point(610, 190), "3")
THREE.draw(win)
THREE.setSize(15)

FOUR = Text(Point(610, 265), "4")
FOUR.draw(win)
FOUR.setSize(15)

FIVE = Text(Point(610, 338), "5")
FIVE.draw(win)
FIVE.setSize(15)

SIX = Text(Point(610, 415), "6")
SIX.draw(win)
SIX.setSize(15)

SEVEN = Text(Point(610, 490), "7")
SEVEN.draw(win)
SEVEN.setSize(15)

EIGHT = Text(Point(610, 560), "8")
EIGHT.draw(win)
EIGHT.setSize(15)

#checkerBoard Pieces

#Peice 1
cir1 = Circle(Point(38, 111), 30)
cir1.setFill("red")
cir1.draw(win)

#Peice 2
cir2 = Circle(Point(188, 111), 30) 
cir2.setFill("red")
cir2.draw(win)

#Peice 3
cir3 = Circle(Point(338, 111), 30) 
cir3.setFill("red")
cir3.draw(win)

#Peice 4

cir4 = Circle(Point(488, 111), 30) 
cir4.setFill("red")
cir4.draw(win)

#Peice 5
cir5 = Circle(Point(111, 38), 30) 
cir5.setFill("red")
cir5.draw(win)

#Peice 6
cir6 = Circle(Point(262, 38), 30) 
cir6.setFill("red")
cir6.draw(win)

#Peice 7
cir7 = Circle(Point(412, 38), 30) 
cir7.setFill("red")
cir7.draw(win)

#Peice 8
cir8 = Circle(Point(564, 38), 30) 
cir8.setFill("red")
cir8.draw(win)

#Peice 9
cir9 = Circle(Point(112, 488), 30)
cir9.setOutline("red")
cir9.setFill("black")
cir9.draw(win)

#Peice 10
cir10 = Circle(Point(262, 488), 30)
cir10.setOutline("red") 
cir10.setFill("black")
cir10.draw(win)
                
#Peice 11
cir11 = Circle(Point(412,488), 30) 
cir11.setOutline("red")
cir11.setFill("black")
cir11.draw(win)

#Peice 12
cir12 = Circle(Point(562, 488), 30) 
cir12.setOutline("red")
cir12.setFill("black")
cir12.draw(win)

#Peice 13
cir13 = Circle(Point(488, 562), 30)
cir13.setOutline("red")
cir13.setFill("black")
cir13.draw(win)

#Peice 14
cir14 = Circle(Point(338, 562), 30)
cir14.setOutline("red")
cir14.setFill("black")
cir14.draw(win)

#Peice 15
cir15 = Circle(Point(188, 562), 30)
cir15.setOutline("red")
cir15.setFill("black")
cir15.draw(win)

#Peice 16
cir16 = Circle(Point(38, 562), 30)
cir16.setOutline("red")
cir16.setFill("black")
cir16.draw(win)


win.getMouse()

#Gather Input from User - Moves

from tkinter import *
import tkinter.messagebox

main = Tk()

Label(main, text="Which red checkers piece do you want to move?").grid(row=0)
Label(main, text="Where do you want to move this piece?").grid(row=1)

piece = Entry(main)
move = Entry(main)

piece.grid(row=0,column=1)
move.grid(row=1,column=1)

Button(main,text='Quit',command=main.destroy).grid(row=4,column=0,sticky=W,pady=4)

def move_piece():
    if (piece  == 'A2') or (piece == 'a2'):
        if (move == 'B3') or (move== 'b3'):
            cir1.move(75,75)

Button(main,text='Play Move',command=move_piece).grid(row=4,column=1,sticky=W,pady=4)
    
mainloop()

My code will still work even if 'move' and piece are not a2 or b3, does anyone know why? Is the if statement formatted incorrectly? Or is the variable for the input wrong? Did I misplace some of the code? I am trying to; if so the typed-in answer in the input box when clicked on the play move button the graphic desired graphical circle will move.

imaan
  • 1
  • 1
  • Missing indentation? and `if (piece == 'A2') or (piece == 'a2'): ...` – Corralien Jan 22 '23 at 16:32
  • 1
    @Corralien I think my first choice for a dup target was not the best. I think better would be [Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?](https://stackoverflow.com/q/20002503) (maybe you can add this) – jps Jan 22 '23 at 16:37
  • 1
    @jps. Your first link is good to avoid nested tests and use `and` – Corralien Jan 22 '23 at 16:39
  • line 19, in move_piece cir1.move(75,75) ^^^^ NameError: name 'cir1' is not defined – toyota Supra Jan 22 '23 at 17:17
  • def move_piece(): x = piece.get() print(x) y = move.get() print(y) if x == "A2" or "a2": if y == "B3" or "b3": cir1.move(75,75) – toyota Supra Jan 22 '23 at 17:23

1 Answers1

0

Press enter after the functions colon, I think you forgot to add some space for the if statment to be in the function.

Joseph
  • 101
  • 1
  • 1
  • 9