This site requires JavaScript, please enable it in your browser!
Greenfoot back
Cubez
Cubez wrote ...

2011/4/4

Chess Game - Movement?

Cubez Cubez

2011/4/4

#
I'm a high school student trying to make a Greenfoot Chess Game for a project. I'm fairly familiar with greenfoot, not an advanced user i guess. I've so far only done the base chessboard + populating the chessboard with pieces at the start of the scenario. How would i implement methods to make chess pieces move according to their chess type?
davmac davmac

2011/4/4

#
Are you aiming for a game with two human players, or with a computer-controlled player (human vs computer)? I'd have to warn you that doing a computer-controlled chess player is a big and difficult task, so given your level, I wouldn't attempt it. Can you also tell us a bit more about what you have so far - is each piece implemented as a separate class (ie Pawn is one class, Knight is another)? Do they have a common base class?
Cubez Cubez

2011/4/11

#
Thanks for replying so quick, I forgot to check this thread until the next C.T Class. Well I guess i'll do a player v player game because as you said, AI programming is a bit too hard. So a 2 player game. Also so far, I have done a populate world thing. I have Actor: - Black = BPawn, BKing, BQueen, etc. - White = WPawn, WKing, WQueen, etc. sub-classes are black and white and sub-sub-classes are B(chesstype) and W(chesstype). Not sure where to go from here ;/ any help will be appreciated!
Cubez Cubez

2011/4/11

#
AND : How do I do restricting stuff? By restrictions i mean, a pawn can only move one step forward, and "eat" one step diagonally, and a horse can only move in a "L"... How do i make restrictions on their movements across the chess board?
Cubez Cubez

2011/4/11

#
AND how do i make the game detect turns? like when is it a white team turn or a black team turn? :/ thanks!
davmac davmac

2011/4/11

#
First off, color (black or white) is really a property, not a behavioral template; it shouldn't be part of the class hierarchy. That is, I think you should have one class for each piece (Pawn, Bishop, Knight, etc) but not a separate class for each color (no separate BPawn/WPawn). Instead of having separate classes a variable in the actor should track which team the piece is on. You can use the setImage() method to change to the correct image depending on white/black. So, there should be a "ChessPiece" class, and all pieces should be subclasses of that. Next: well, you're asking a lot of questions, but it's not clear if you've actually thought about it and tried anything. How about: add a method to ChessPiece:
boolean canMove(int xpos, int ypos)
{
    return false;
}
... Then you can override it in each of the subclasses. You can determine if the move is valid by checking the current position compared to the destination position (given by xpos and ypos). For instance, a Pawn can only move forward one square, so you might have:
boolean canMove(int xpos, int ypos)
{
    return ypos == getY() + 1;
}
... which is only a start - there are several other considerations. For instance, which way is "forward" depends on the pawn's color. Also, the first move is allowed to be two squares forward, and you may want to allow en-passant moves. Finally, a move is only valid if it enters an unoccupied square! Probably you want the world to track whose turn it is, and which piece is being moved. If a piece is clicked on, it can tell the world; the world then remembers that piece as the one which is clicked or, if it already has a piece being moved (i.e. a piece was already clicked earlier) then it can check whether the move is valid (by calling the canMove() method!) and moving the piece if so.
davmac davmac

2011/4/11

#
(So: write some code, upload it, and post here again if you are stuck and need further help!!).
Cubez Cubez

2011/4/13

#
I'm sorry, not really good at programming compared to other classmates. Don't think I'll be taking CS in my last 2 years of high school then. I don't really get what you mean, but I am trying to code! I'll upload what I have I guess.
davmac davmac

2011/4/13

#
Well, here's one bit of simple advice: Start small. Try to get the game working to the point where you can move pieces around (any piece, any colour, to any location) before you start worrying about whose turn it is, and whether the moves are valid. Once you have then, then you can start to worry about the other things (and you'll feel better about having accomplished something in the meantime).
danpost danpost

2011/5/3

#
First time ever in 'Discuss' for me today. Noticed what you were working on. I uploaded a Chess scenario yesterday. I am fairly familiar with programming, but never worked with Java or Greenfoot previously. In the past, I have programmed mainly in Basic and Assembly (before Windows was introduced); also had touched on Fortran. This was my first project using Greenfoot and the Java language. Actually both scenarios that I uploaded were the same program, but I set the int mode variable in World which can be set using method resetBoard(int mode). I will eventually add the neccessary buttons and info boxes to make it more friendly and combine the scenarios. I did control TURN in World, but I also controlled the board there. It has a STATIC String variable of length 64 that describes the position: 'B' for White Bishop, 'b' for Black Bishop; 'R' for White Rook, 'r' for Black Rook; etc. (Uppercase characters for White pieces and lowercase for Black); spaces signifies an empty square. In the Actor class 'Piece', I put a STATIC String variable of length 1, which contains the type of piece represented the same way as in position. I used the Switch statement in the method Integer canMove(int, int) and returned -1 for cannot move here, 0 for OK to move here, 1 for en passant OK, 2 for castling OK, and passed that value to the method Boolean isLegalMove(int Xval, int Yval, int canMoveVal) to implement the move, verify no self-check, and take-back the move using the World variable position. (Actually, I set myPosition to World.position, made the move in World.position, ensured validity, and reset World.position with myPosition. When a piece is clicked on, I run a check for all squares it can possibly move (also checking to make sure that player's King is not in check after the move). If can move anywhere, I remove any borders (another Actor class) displayed, and put new borders at all square locations it can legally move, and also put a different color border on the piece clicked on. Then if a border is clicked on, we can move the piece to that square, and also make any other position changes required if needed (for castling, en passant, pawn promotion). NOTE: for en passant, I created an STATIC Integer variable in World which contains -1, unless a pawn in pushed two squares on its first move, in which case its value changes to the rank number of the push (any other move reverts its value back to -1). This allows for checking for en passant on the next move. For pawn promotion, I set a border on the new Queen, and if clicked on changes (rotating) to next possible piece to promote to. If any questions, comments, or concerns -- post back.
Cubez Cubez

2011/5/5

#
Wow, i just looked at your scenario, it is really well made! Good job on that! Like i said before, I'm a beginner so the way i decided to do it in my scenario was to change the picture everytime its clicked on, but your way looks much better. It was kind of a pain for me to keep using paint/photoshop to edit pictures. Could you possible upload your source code for me to check out? Also, how did you get your images for your chess board? did you make it yourself? Thanks Danpost!
danpost danpost

2011/5/7

#
Actually, I did create the images myself using Paint, and it was very tedious. I created one image for a white piece on a white square; then, changed the outside color for a white piece on a black square; then, changed the piece colors for a black piece on a black square; and, finally, changed the outside color again for a black piece on a white square. In total, four images for each type of piece. I flirted with the string filename to control which image to display ( (rank# + file#) % 2 controls the color of the square) and the following gives an integer value to each piece: "kqrbnp PNBRQK".indexOf(char pieceChar) - 6; positive values are white piece, negative are black; whitespace is a null piece (empty square = 0); pawns are +/- 1; knights are +/- 2; bishops are +/- 3; etc. I probably should have used switch (squareColor * 13 + "kqrbnp PNBRQK".indexOf(pieceChar)) and cased each value from 0 to 25 to the filenames (instead of fidgetting with the filenames: pawn0 through pawn3, knight0 through knight3, etc. By the way, an empty square doesn't get an image -- it's part of the background. My code is not documented very well, if at all; but, my variables are named fairly well. I'll add some documentation before uploading it.
Cubez Cubez

2011/5/16

#
Thanks Dan! Please check your email (: I have some more inquiries
Transpire Transpire

2011/5/16

#
Um, I'm not too sure about what you're trying to do, but wouldn't it be better to create an abstract class "Piece" with declared GreenfootImage blackImage and whiteImage, and then in the constructor have a blackImage and a whiteImage parameter, this way with superclass constructor chaining you can ensure every Piece will have pictures? Then, whenever the piece is moved, check the color of the square it is moving it to (again, with a superclass method somewhat like this...
move(int x, int y)
{
    int z = x + y;
    if(z % 2 == 0)
        setImage(whiteImage);
    else
        setImage(blackImage);
    setLocation(x, y);
}
and set it's image to the appropriate image.
You need to login to post a reply.