Ok, so I am creating a simplified chess game. In my game, it's the same board and rules of chess, except there's only pawns. I have a GUI displaying a board, but I need some help. How would I make a MouseListener that would enable me to move one pawn from one square to another? Additionally, I will run into the problem of whether or not that pawn's path is blocked. I guess my problem right now is being able to use the board I have, and somehow assign it a coordinate grid? Or something? I figured a coordinate grid would enable simple pawn movement, like y + 1 to move it forward, or something. I'm not sure.
I guess if anyone had any old code laying around to help me out I would really appreciate it.
Will Downie wrote:Ok, so I am creating a simplified chess game. In my game, it's the same board and rules of chess, except there's only pawns. I have a GUI displaying a board, but I need some help. How would I make a MouseListener that would enable me to move one pawn from one square to another? Additionally, I will run into the problem of whether or not that pawn's path is blocked. I guess my problem right now is being able to use the board I have, and somehow assign it a coordinate grid? Or something? I figured a coordinate grid would enable simple pawn movement, like y + 1 to move it forward, or something. I'm not sure.
asking for existing code isn't very useful. Maneesh makes this very clear, but apart from that:
many people in the past have programmed games, and each has used his own ideas and code. So
any existing code would for certain not fit into what you are planning to do.
So, instead, I hope to give you some ideas and show you some possible directions to follow. I have
been doing this chess thingy myself, so I gained some experience and learned a thing or two.
First of all: if you are a beginner, forget about chess! This game is very hard to do, the rules are
very complicated, let alone showing the game on a board, complete with animation and timers...
this is asking for a lot of frustration! My advice is: start with something much easier. I would say:
start with the game of checkers. It is played on the same 8 x 8 board, there are only two different pieces
(four, if you count the kings), and the playing rules are very easy.
When you have managed that, and learned all about the GUI involved, including animating
a stone's move, THEN it is a good moment to turn your attention towards chess.
But nevertheless, my advises and directions, for what they are worth.
1) many people start with an integer matrix, for instance
Now imagine that board = square 'a1', board[1,0] = square 'b1', et cetera. So
board = 'h1', and board = 'h8'.
2) suppose that you define a white pawn to have the value of 1. You then could say:
board = 1. In effect, you've just put a white pawn on square a2!
3) if board[x][y] = 0, then there's no piece on this square.
4) by giving each piece a certain value, you can then set any position you like by just storing
the defined values in your board.
I use the following: white king = 6, white queen = 5, rook = 4, bishop 3, knight 2 and pawn = 1.
For the black pieces I use the same value, but then negated.
5) So, having now a way to define some chess position, let's suppose we have a white
pawn at field b2, or in code:
Now, if we want to move that pawn, we must have that square b3 is empty. So we check:
6) leaving now the question of how to represent a board, let's go to your next question: what
to do if we have spotted a mouse click on the panel that contains the GUI-board?
The idea is simple: notice the X and the Y position of the mouse click (as you do in your code;
it doesn't matter if you use MousePressed, MouseReleased or MouseClicked for this).
Then you must find out on what square this click was. So you have to translate the X and the Y
to BoardSquareX and BoardSquareY coordinates.
Having done this, and knowing on what square has been clicked, you then inspect
board[squareX][squareY], and you know then exactly what's on this square!
7) next, there is the question whether the click was on a 'From' square or on a 'To' square. In the
first case, you might draw a rectangle on the clicked square on the screen, and check whether the
square that was clicked on contains a piece of the right colour (if not, protest!). And in the second case,
a move may have been indicated by the user! And then the fun starts. Is this a legal move? If not, what to do?
And if it was, how do I implement that move?
Implementation might involve:
8) for the GUI I use a subclass of JLabel, with which I build the graphics of
the board. This I find a very convenient method, which I will describe in a minute.
The definition of JLabel is (simplified) as follows:
Now, I fill my board panel with something like this
Now, the beauty of this is:
if you click on one of the board squares, you click in fact on one of these
JLabels. Now, you can simply say:
And if you wat a certain piece on this square, you simply say: