Results 1 to 2 of 2
Thread: Chess program
- 10-25-2010, 11:52 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
Chess program
Hey everyone, I'm trying to make a simple human vs. human chess program using BlueJ.
I am very new to Java and just trying stuff out.
My issue is that I don't know how I should structure the program. At the moment I have just a class called Board which has an array which says either null or bQ, bR, wP etc in each cell. So this says where the pieces on the board are. The class initializes the starting positions.
This class has a function which displays the board and pieces in the terminal (as seen in the picture below). The second part of the picture shows the board with the array element numbers (another function).

The users are going to be able to make moves by inputting the reference of the piece they want to move (e.g. C5) and the place they are moving it to. I don't have any problems with writing code to check if pieces can be moved where the user wants etc. but I don't know if I should have the pieces as individual objects from a separate class or something else.
For example, if the user says they want to move a bishop to a certain place should I have the board class identify the type of piece and then use the bishopMoveCheck function in the Pieces class to see if it's a legal move and then have a function in the Board class make the move? Or should I have the bishop, as an object in the Pieces class, do this in another way? I just don't know what the best way is.
As a newbie I probably don't fully understand the usefulness of classes and objects. I'd probably just write everything in one class and without objects if left to my own devices!
Any guidance would be ace!
Thanks, Alex
PS, I'm studying comp sci at University but this is my own private project.
- 10-26-2010, 09:20 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
You would definately benefit from a more object oriented aproach. You could have a class Piece, and make subclasses Pawn, Bishop etc... This way, it would be much easier to define legal moves the pieces can make. If you just make a String array, you'd need a monstrosity like this to determine the moves:
And so on for every piece. If your structure would be like this:Java Code:if(piece.equals("bQ")) { if(targetPosition.isValidBlackQueenMove()) //tra la la } else if(piece.equals("bP") { if(targetPosition.isValidBlackPawnMove()) //bla bla bla }
This way, you can have an array of Piece objects, and each one knows how it can move via the overridden isValidMove() method.Java Code:public abstract class Piece { private char color; //'b' and 'w' private int posX, posY; //constructor public abstract boolean isValidMove(int x, int y); } public class Bishop extends Piece { public Bishop(int x, int y, char color) { super(x,y,color); } public boolean isValidMove(int x, int y) { //determine the validity of the move } }Ever seen a dog chase its tail? Now that's an infinite loop.
Similar Threads
-
a good java chess program
By rico16135 in forum New To JavaReplies: 10Last Post: 11-06-2010, 12:25 PM -
Chess, Variable Loosing it.s Value
By michail in forum New To JavaReplies: 6Last Post: 01-24-2010, 01:54 PM -
chess game
By michail in forum New To JavaReplies: 13Last Post: 01-12-2010, 07:24 AM -
Chess game
By michail in forum New To JavaReplies: 4Last Post: 12-23-2009, 06:51 PM -
New Chess-Like game
By jSarK0Y in forum Reviews / AdvertisingReplies: 3Last Post: 06-10-2009, 03:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks