Forum Archive

Making chess, any tips?

amharder

Hello everyone!
I need your help with a chess game that I am making. I think that with enough time and unpythonic programming, I can make this game possible, although I would like to find a better way.
Here is my main code:

from scene import *
from Chessboard import Chessboardd
class game(Scene):
    def __init__(self):
        Scene.__init__(self)

    def setup(self):
        self.r = Chessboardd(self.bounds.center(), Size(700, 700))
        self.kingw = LabelNode(u"\N{WHITE CHESS KING}", font=('Helvetica', 100), position=(468 + 88,75), parent=self)
    def draw(self):
        background(0,0,0)
        self.r.draw()

    def touch_ended(self, Touch):
        if Touch.location.x < self.kingw.frame.x and Touch.location.x > self.kingw.frame.x - 88 and Touch.location.y < self.kingw.frame.y + 90 and Touch.location.y > self.kingw.frame.y: 
            self.kingw.run_action(Action.move_by(-88,0))
        if Touch.location.x > self.kingw.frame.x + 70 and Touch.location.x < self.kingw.frame.x + 50 + 125 and Touch.location.y < self.kingw.frame.y + 90 and Touch.location.y > self.kingw.frame.y:
            self.kingw.run_action(Action.move_by(88,0))
        if Touch.location.y < self.kingw.frame.y and Touch.location.y > self.kingw.frame.y - 88 and Touch.location.x > self.kingw.frame.x and Touch.location.x < self.kingw.frame.x + 88:
            self.kingw.run_action(Action.move_by(0,-88))
        if Touch.location.y > self.kingw.frame.y + 100 and Touch.location.y < self.kingw.frame.y + 100 + 110 and Touch.location.x > self.kingw.frame.x and Touch.location.x < self.kingw.frame.x + 88:
            self.kingw.run_action(Action.move_by(0,88))
        if Touch.location.x < self.kingw.frame.x and Touch.location.x > self.kingw.frame.x - 90 and Touch.location.y > self.kingw.frame.y + 88 and Touch.location.y < self.kingw.frame.y + 88 + 88:
            self.kingw.run_action(Action.move_by(-88,88))
        if Touch.location.x > self.kingw.frame.x + 88 and Touch.location.x < self.kingw.frame.x + 88 + 88 and Touch.location.y > self.kingw.frame.y + 88 and Touch.location.y < self.kingw.frame.y + 88 + 88:
            self.kingw.run_action(Action.move_by(88,88))
        if Touch.location.x < self.kingw.frame.x and Touch.location.x > self.kingw.frame.x - 88 and Touch.location.y < self.kingw.frame.y and Touch.location.y > self.kingw.frame.y - 88:
            self.kingw.run_action(Action.move_by(-88,-88))
        if Touch.location.x > self.kingw.frame.x + 88 and Touch.location.x < self.kingw.frame.x + 88 + 88 and Touch.location.y < self.kingw.frame.y and Touch.location.y > self.kingw.frame.y - 88:
            self.kingw.run_action(Action.move_by(88,-88))
run(game())

Now, I realize that I could put each chess piece in different classes, and give them the ability to move in their seperate fashion, although I do not know how to do that, because if I created for example, a class named King, and I assigned a king to that with parameter color being black or white that would choose the color of the king, could I simply put a move function within King that allows them to move normally? When I try this, I cannot figure out a simple, or any way to do it. Also, here is my code for the chessboard module:

from scene import *
class Chessboardd:
    def __init__(self, position, size):
        self.position = position
        self.size = size
    def draw(self):
        fill(1,0,0)
        for x in range(8):
            for y in range(8):
                black = (x + y) % 2 == 0 # true if checkerboard square should be black
                if black:
                    fill(.66, .46, .25)
                else:
                    fill(1.0, .7, .38)
                rect(self.position.x + self.size.w*(x/8.0) - self.size.w/2.0, self.position.y + self.size.h*(y/8.0) - self.size.h/2.0, self.size.w/8.0, self.size.h/8.0)

So I guess my question is, how can I give seperate chess pieces their own individual set of moves, because with the method I am using, all pieces could only move in the spaces surrounding. Please help!

ccc

https://github.com/671620616/PhantomChess Probably needs tons of work now...

brumm

Hi amharder,
you could do it in one class, e.g. ChessPiece.
With this class you can create an array of pieces. When a single piece is selected you should check the possible moves (show maybe a blinking square) and wait for the user input. So each piece needs a type/name, color, position, different ways to move, ...
As ccc suggested you might take a look at class ChessPiece

amharder

@brumm @ccc Thank you!

NoBetterName

For me, at least, I think it would be more organized to take this approach:

class King(ChessPiece):
  def availableMoves(self):
    returns place to move to

#Do this with all the chess pieces:
class Queen
class Bishop
class Knight
class Rook
class Pawn

All would inherit from a single class, ChessPiece, so that they could all have the common features while still retaining what makes them different, like available moves. Then, you could create a nested list for your pieces.

R1, R2 = Rook(), Rook()
B1, B2 = Bishop(), Bishop()
K1, K2 = Knight(), Knight()
QQ = Queen()
KK = King()
P1, P2, P3, P4, P5, P6, P7, P8 = Pawn(), Pawn(), Pawn(), Pawn(), Pawn(), Pawn(), Pawn(), Pawn()
NN= 0

Board = [[R1, K1, B1, QQ, KK, B2, K2, R2],
                [P1, P2, ...
                [NN, NN, ...

Continue on like that. That could eliminate your chessboard module.