I have created two classes for checkers game. One is board and the other is pieces. The difficulty that I am having is that with the pieces I cannot seem to get them to move and neither can I get them to stay in that spot correctly and that I am actually unable to go any further due to the fact that I do not understand what is going wrong here. Can someone please correct me on what I am doing wrong.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Board extends JPanel {
private int[][] state;
public final static int EMPTY = 0;
public final static int RED = 1;
public final static int BLUE = 2;
public final static int BLUE_KING = 3;
public final static int RED_KING = 4;
public final static int HIGHLIGHT = 5;
public Board() {
state = new int[8][8];
setSize(400, 400);
}
public void paint(Graphics g){
for(int y=0;y<8;y++){
for(int x=0;x<8;x++){
if(x%2 == y%2){
g.setColor(Color.WHITE);
}
else{
g.setColor(Color.GRAY);
}
g.fillRect(x*50,y*50,50,50);
switch(state[x][y]){
//6 cases that will be available for this game
/*
* Empty will be the empty spot; Red will be the red checkers. Blue will be the blue checkers. The blue king will occur when it reaches other side
* and the red king will occur when it reaches other side. The highlight will show the possible moves that can occur when it [red/blue] king will be selected
*
*/
case EMPTY: break;
case RED:
g.setColor(Color.RED);
g.fillOval(x*50,y*50,50,50);
break;
case BLUE:
g.setColor(Color.BLACK);
g.fillOval(x*50,y*50,50,50);
break;
case BLUE_KING:
g.setColor(Color.BLACK);
g.fillOval(x*50,y*50,50,50);
g.setColor(Color.WHITE);
g.drawString("K",x*50+25,y*50+25);
break;
case RED_KING:
g.setColor(Color.RED);
g.fillOval(x*50,y*50,45,45);
g.setColor(Color.WHITE);
g.drawString("K",x*50+25,y*50+25);
break;
case HIGHLIGHT:
g.setColor(Color.YELLOW);
g.drawOval(x*50,y*50,50,50);
break;
}
}
}
}
public void setState(int x,int y, int s){
state[x][y]=s;
}
public int getState(int x,int y){
return state[x][y];
}
}
Also here's the pieces class
import javax.swing.*;
import java.awt.*;
import javax.swing.ImageIcon;
import java.awt.event.*;
public class Checkers implements MouseListener
{
int p1pieces = 12;
int p2pieces = 12;
int xcord;
int ycord;
int exist;
int exist2;
int leftx = 0;
int lefty = 1;
int rightx = 0;
int righty = 1;
int oldXvalue, oldYvalue;
Board b=new Board();
JFrame frame = new JFrame("Checkers");
JPanel panel = new JPanel();
JButton newgame = new JButton("New Game");
JTextField turn = new JTextField("Players one turn");
JTextField pieces = new JTextField("Player 1 has " +p1pieces + " pieces. Player 2 has " + p2pieces + " pieces.");
public Checkers()
{
frame.setSize(500,500);
frame.add(b);
frame.add(panel);
frame.setVisible(true);
frame.addMouseListener(this);
b.setState(0,0, Board.BLUE);
b.setState(2,0, Board.BLUE);
b.setState(4,0, Board.BLUE);
b.setState(6,0, Board.BLUE);
b.setState(7,1, Board.BLUE);
b.setState(5,1, Board.BLUE);
b.setState(3,1, Board.BLUE);
b.setState(1,1, Board.BLUE);
b.setState(0,2, Board.BLUE);
b.setState(2,2, Board.BLUE);
b.setState(4,2, Board.BLUE);
b.setState(6,2, Board.BLUE);
b.setState(7,7, Board.RED);
b.setState(5,7, Board.RED);
b.setState(3,7, Board.RED);
b.setState(1,7, Board.RED);
b.setState(0,6, Board.RED);
b.setState(2,6, Board.RED);
b.setState(4,6, Board.RED);
b.setState(6,6, Board.RED);
b.setState(1,5, Board.RED);
b.setState(3,5, Board.RED);
b.setState(5,5, Board.RED);
b.setState(7,5, Board.RED);
}
public void mouseEntered(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mousePressed(MouseEvent e)
{
Object o = e.getSource();
{
oldXvalue = xcord;
oldYvalue = ycord;
}
xcord = (e.getX() / 50);
ycord = (e.getY() / 50) ;
exist = b.getState(xcord,ycord);
if(exist == 1 || exist == 2)
highlightleft();
highlightright();
move();
b.repaint();
}
public void highlightleft()
{
exist = b.getState(xcord, ycord);
if(exist == 1)
{
exist2 = b.getState(xcord-1, ycord -1);
}
if(exist == 2)
{
exist2 = b.getState(xcord-1, ycord +1);
}
if(exist == 1 && exist2 == 0)
{
b.setState(xcord-1,ycord -1, Board.HIGHLIGHT);
leftx = xcord -1;
lefty = ycord -1;
}
else if(exist == 2 && exist2 == 0)
{
b.setState(xcord -1, ycord + 1 ,Board.HIGHLIGHT);
leftx = xcord -1;
lefty = ycord +1;
}
}
public void highlightright()
{
exist = b.getState(xcord, ycord);
if(exist == 1)
{
exist2 = b.getState(xcord+1, ycord -1);
}
if(exist == 2)
{
exist2 = b.getState(xcord+1, ycord +1);
}
if(exist == 1 && exist2 == 0)
{
b.setState(xcord+1,ycord -1, Board.HIGHLIGHT);
rightx = xcord+1;
righty = ycord -1;
}
else if(exist == 2 && exist2 == 0)
{
b.setState(xcord +1, ycord + 1 ,Board.HIGHLIGHT);
rightx = xcord+1;
righty=ycord +1;
}
}
public void removehighlight()
{
exist = b.getState(xcord,ycord);
b.setState(leftx,lefty,Board.EMPTY);
b.setState(rightx,righty,Board.EMPTY);
}
public void move()
{
exist = b.getState(xcord,ycord);
exist2 = b.getState(oldXvalue,oldYvalue);
if(exist == 5)
{
if(exist2 == 1)
{
b.setState(oldXvalue, oldYvalue, Board.EMPTY);
b.setState(xcord,ycord, Board.EMPTY);
b.setState(xcord,ycord, Board.RED);
}
else if(exist2 == 2)
{
b.setState(oldXvalue, oldYvalue, Board.EMPTY);
b.setState(xcord,ycord, Board.EMPTY);
b.setState(xcord,ycord, Board.BLUE);
}
/*
* I think that there should be something here for the code to be cleared and then you can repeat it
*/
}
}
public static void main(String [] args){
new Checkers();
}
}
I have no idea when you get your errors so its hard to say anything without knowing what the actual error is.
But I would say your highlighting code could remove any red blue checker piece location information, you don't check if there is information there already. Your highlight method has a couple of ifs and and an if else statement. The if's will all be checked as well as the if else block.
But I could he wrong on this assumption because its hard to know what all your == code means as you don't use constants.