AySeven
AySeven

Reputation: 63

How do I control where game pieces go for checkerboard in Python?

So, I have asked a similar question before, but I was still unable to solve my problem. I need to make a checkerboard with 6 game pieces evenly on each side. This code was helpful, but it randomly distributes the game pieces, I've played around with it in multiple lines and attempted to understand but it's not clicking for me. How can I get six pieces to be on top half and six to be on lower half with space in the middle? Like a regular checkerboard set up?

from turtle import Turtle, Screen
from random import randrange

CURSOR_SIZE = 20
SQUARE_SIZE = 40
SQUARES_PER_SIDE = 8

def drawRect(color):
    turtle.color(color)
    turtle.pendown()
    turtle.begin_fill()

    for iterations in range(4):
        turtle.forward(SQUARE_SIZE)
        turtle.left(90)

    turtle.end_fill()
    turtle.penup()

def pushTurtleForward():
    turtle.forward(SQUARE_SIZE)

def drawHorizontal(inverted=False):
    if inverted:
        for horizontal in range(SQUARES_PER_SIDE):
            if horizontal > 0:
                if horizontal % 2 == 1:
                    pushTurtleForward()
                    drawRect("white")
                else:
                    pushTurtleForward()
                    drawRect("black")
            else:
                drawRect("black")
    else:
        for horizontal in range(SQUARES_PER_SIDE):
            if horizontal > 0:
                if horizontal % 2 == 1:
                    pushTurtleForward()
                    drawRect("black")
                else:
                    pushTurtleForward()
                    drawRect("white")
            else:
                drawRect("white")

screen = Screen()
screen.bgcolor("Grey")

turtle = Turtle(visible=False)
turtle.speed('fastest')

for drawVertical in range(SQUARES_PER_SIDE):
    turtle.setposition(0, SQUARE_SIZE * drawVertical)

    if drawVertical % 2 == 0:
        drawHorizontal(inverted=True)
    else:
        drawHorizontal()

# Checker graphics demonstration.  Distribute 12 red checkers around
# black squares on board without any two landing on the same spot.

red_checkers = []

for _ in range(12):
    checker = Turtle('circle')
    checker.color('black', 'red')
    checker.shapesize(SQUARE_SIZE / CURSOR_SIZE)
    checker.penup()

    red_checkers.append(checker)

    position = checker.position()  # a position guaranteed to fail

    while any(map(lambda checker, p=position: checker.distance(p) < 
SQUARE_SIZE/2, red_checkers)):
        x, y = 0, 1  # a parity guaranteed to fail

        while x % 2 != y % 2:
            x, y = (SQUARES_PER_SIDE),(SQUARES_PER_SIDE)

        position = (x * SQUARE_SIZE + SQUARE_SIZE/2, y * SQUARE_SIZE + 
SQUARE_SIZE/2)

    checker.goto(position)


screen.mainloop()

Upvotes: 0

Views: 590

Answers (1)

001
001

Reputation: 13533

I think this is what you want. Replace the for _ in range(12): loop with this:

# Returns the drawing co-ords of checker space
def get_square_coords(row, col):
    return (col * SQUARE_SIZE + SQUARE_SIZE/2, row * SQUARE_SIZE + SQUARE_SIZE/2)

# Creates 2 rows of checkers
def create_checkers(row, color):
    checkers = []
    col = 0
    for y in range(row, row+2):
        for x in range(col, SQUARES_PER_SIDE, 2):
            checker = Turtle('circle')
            checker.color('black', color)
            checker.shapesize(SQUARE_SIZE / CURSOR_SIZE)
            checker.penup()
            position = get_square_coords(y, x)
            checker.goto(position)
            checkers.append(checker)
        col += 1
    return checkers

red_checkers = create_checkers(0, 'red')
green_checkers = create_checkers(6, 'green')

It creates this: enter image description here

Upvotes: 1

Related Questions