Chess,Java & OOP (a few questions, please answer)?
Question by Alex: Chess,Java & OOP (a few questions, please answer)?
It’s going to look kinda like this – A [class] has-a/is-a [object/class]:
A game has-a board
A board has-a square
A square has-a piece
A rook, knight, queen, pawn, bishop, king is-a piece.
So the game is going to have an array of squares.
So, from here on I have a lot of questions. I already asked a few yesterday here but trying to phrase them better and ask more this time. I wasn’t sure if to use a square class and a piece class or just go with the piece class alone, but I guess having both is more OOP. The thing is I have trouble imagining how to design the classes, what methods they should have and where, etc.
Has-a relationship, means that the class has an object (right?). Therefor the square class should have a Piece object. An array of Squares should hold null or any kind of piece but since the Piece class is abstract, I won’t be able to create a Piece object. How do I do this thing then? How do I design the Square class? Could the square class consist of an array of Piece?
Another issue. How do I code the selection of empty squares and-or selection of pieces on the chess board? I assume it has something to do with an event listener such as MouseListener?
If I click on a piece, it should be ready for a move, how?
…and somehow, if a piece is selected, then when I click on an empty square or an enemy piece it shouldn’t de-select my piece but move/attack, how could I do this?
Another question. How do I do the graphics part? If I have an array of squares, then every second element should be black/white? Also, if the square is a solid black/white square, then how do I add the piece graphics on-top of it?
Last question. The game isn’t going to use any AI to start with, it’s going to be a two-player game but from the same computer. So player One makes a move, and then player Two makes a move, on the same computer. But how do I do the coding part for when it’s the player ones turn and player two’s turn?
Sorry if it’s confusing and a lot of questions but it’s pretty confusing for me too, hope someone has the time to help me with this!
Not going to use dragging, click to select and then click to move.
Why is Game a Collection? I was thinking more along the lines that Game is the manager, it has an instance of the chess board and then I don’t know..
I don’t know much about Collections yet, just arrays.
Best answer:
Answer by deonejuan
There is enough graphics that you you download a chess font. Java can load TrueType fonts .ttf.
You will need 6 White and 6 Black .png graphics if you wanted dragging. The 6 White are the PRNBQK, on a transparent square the size of the black square, furthermore the pieces have a white knockout behind the line drawing of that piece.
Board is a class. Consider it GameManager. All it does is update.
Collection Game is probably HashMap, could be HashSet
In a Sprite2D game you want an array or a Collection that you refer to after Update. The GameManager runs down the array tweaking State(s).
Inasfaras WhoseTurn… refer to the folder your JDK1.5_updateXXX /demo /applets /TicTacToe
The above thoughts is Model and View. The Controller — Swing being MVC by design — the Controller is the GUI. There is a MouseListener.
I’m too brain-dead to visualize class Piece fully but it has members Character, String bPos, Vector
To insert the logic to make the computer Black…. well, that takes more than 2 weeks just to explain.
Add your own answer in the comments!
Related posts:
- Understanding Chess Move By Move
- Understanding Chess Move By Move
- Why do post video game questions in the boardgame section?
- Q&A: I want to be able to type something and have my computer answer me, how do I do this?
- I have a couple of questions concerning Chess books and Strategies?
Tagged with: answer. • ChessJava • please • questions
Filed under: Computer Chess Game
Like this post? Subscribe to my RSS feed and get loads more!

