• Post Reply Bookmark Topic Watch Topic
  • New Topic

Failure at Java, need help with Chess superclasses and abstract classes  RSS feed

 
Will Downie
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so as mentioned in the title, I am not very experienced with Java. I am making a Chess project, or I am attempting to, anyway. I have a super class Piece, then the other pieces, Rook, Pawn, etc. which extend Piece. Now, I plan on making a isValid() method to essentially move the pieces...except I have no idea which parameters I should use...or what I should even do anyway. Below is the code for Piece, and Rook and Pawn, which are unfinished. And yeah, the grid[][] in Pawn was my failed attempt at actually trying to do something.

 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Will,
Welcome to the Ranch!

you should have the classes Rook and Pawn as concrete classes. For the isValid, the pieces need to know the position of the board - so, you might have to pass the position info to the isValid method.

PS: Please UseCodeTags when posting code. I have added them for you this time.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Will,

certainly not an easy task you are trying to achieve! I am myself working on a program that enables the import of so called 'pgn' files, and lets you replay the games.
It certainly contributed to the number of grey hairs, but I have gained quite some experience with handling chess boards.

As Ranganathan explained, to determine whether a piece can move to a certain field (or 'square', as the English speaking people say?), you need to have quite some information:

1) the full board position (so for each field, what piece is occupying it?
2) whose turn is it?
3) is castling (long and/or short) possible?
4) is 'en passant' possible?
5) is the player whose turn it is in check?
6) is the piece you want to play pinned in any way?
7) and last but not least: is the move legal? So, f.i., Bishop e2 to c3 is certainly not legal.

So, for instance, you might want to know if the white move Be2-b5 is allowed. Well, maybe there's a white king on e1 and a black queen on e4, pinning the bishop.
Or maybe there's a (white or black) pawn at c4, et cetera.

So I think it is not a good idea to include a list of possible moves into the 'Piece' class. First of all, you need too much information to determine
if a certain move is allowed, and, secondly (and maybe even more important), a certain piece move is not a general property of a piece, but of a board position.

But nevertheless, you are in for a long and interesting job!

Greetings,
Piet
 
John Damien Smith
Ranch Hand
Posts: 357
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I advise starting with a simpler game and working your way up to chess.

For example tic-tac-toe or a pawn game (e.g. a chess game which only uses pawns).

Also implement first a version without AI, then add AI (a computer opponent) in later.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch :

I have shortened your lines by breaking the long // comment into two lines. Long lines can make code difficult to read.
By the way: it was inappropriate to have a // comment there in the first place. It should have been a /**…*/ comment.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Will,

Greetings, As John said you should probably start of with a simpler game as it will make you more comfortable with Java of course and will also boost up your confidence.

Anyways,

A chess game is more difficult to develop than to play i guess ...

I would suggest some smart points for you,

1. Add the simplest rules first. (like moving the pieces to squares)
2. Prioritize your move on the basis of a valid movement ... e.g.
P1. What possible moves does this piece has?
P2. Which squares can be occupied with this move?
P3. Which squares are free or vacant?
3. make sure that the previous code can be used with the next harder moves

cheers :-)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will Downie wrote:Ok, so as mentioned in the title, I am not very experienced with Java. I am making a Chess project...

Like pretty much everybody else has said, that statement is a bit like saying "I'm not very experienced with carpentry, but I want to build a Chippendale cabinet".

Start small and expand gradually. If you'd like something that involves a "chess-like" Board - why not start with Checkers?

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!