1

If I'm working on designing a simple chess game code and I've already created my board layout, I'm wondering how it is that I implement the pieces (P, p, R, r, N, n, B, b, Q, q, K, k) (capitals represent white pieces , lowercase are black pieces ) so that the ChessBoard object updates to store the piece at that inputted location?

Should I declare the pieces as instance variables? Or should I just declare them in the main?

As far as the user inputting a location that is already taken, I'm totally lost as to how to translate that into code.

I did however implement a part of the code that displays an error message if the input from the user is outside of the board range.

(this is all in java, by the way)

Any help would be greatly appreciated!! Thanks in advance! Let me know if seeing some of my code would help.

matt
  • 515,959
  • 87
  • 875
  • 1,141
SEG
  • 29
  • 1
  • 5
  • 1
    See also this [example](http://stackoverflow.com/a/2562685/230513) and [variation](http://stackoverflow.com/a/2563350/230513). – trashgod Apr 09 '13 at 21:44
  • You probably wouldn't want to declare the pieces in the main. Main should just run stuff. – Caffeinated Apr 09 '13 at 21:46
  • 2
    `capitals represent pieces on white squares, lowercase are pieces on black squares` makes no sense. Probably you meant "capitals represent pieces from White player, lowercase represents pieces of black player". Otherwise, I do not think that changing the representation of the piece based on where it is located is a good idea. – SJuan76 Apr 09 '13 at 21:48
  • 1
    I'm sorry, I meant black and white pieces, not squares, my mistake – SEG Apr 09 '13 at 21:50
  • 1
    Start coding. When you get about halfway through, throw it out and start over. It's the only way. – Hot Licks Apr 09 '13 at 21:53
  • In regards to `As far as the user inputting a location that is already taken` - what you could do is associate a boolean value with any square - `isTaken` . BTW yea we don't mind seeing code, but usually code snippets that you've narrowed down – Caffeinated Apr 09 '13 at 21:53
  • @matt - OP accepted that edit!!!! – Caffeinated Apr 09 '13 at 22:00
  • 1
    @Adel my bad, saw that as soon as I'd done it, rolled my own edit back to yours. – matt Apr 09 '13 at 22:19

1 Answers1

2

I "implemented" a chess game for a job interview a couple years ago. Each piece was an instance variable, and had a move() method that determined what the piece's legal moves were. The board contained information on where all of the pieces were located, and a piece would query the board when determining what its legal moves were (e.g. a pawn would query the board to see if there were any diagonally located opposing pieces that it could capture). Piece was an abstract class (and the board contained a grid of Piece objects), and Pawn, Knight, etc extended Piece.

If you need to implement en passant, then you'll probably want a separate rules engine that the pieces can query for their legal moves - the pieces would be stateless, and the rules engine would carry the state information needed to determine if pawns could capture via en passant, or if a draw were in effect due to three move repetition.

Zim-Zam O'Pootertoot
  • 17,888
  • 4
  • 41
  • 69