|
The Supreme Ruler
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 
|
Well, we've been working on a Checkers program in Computer Science 3 for around 3 days now. This is an unfinished, and probably badly coded program, but it works for the most part. It does have it's share of bugs, though. First of all, if you click a piece that can't move anywhere, you're stuck. You have to restart the game. Also, the pieces are able to move in an L-shape at time, which I will fix. Also the king can move 2 squares, so I'll need to fix that, and occasionally the left-most colum crashes the code for some reason. Also, I haven't added code to check for a win yet. I'll do all this later. I'll fix these bugs, but at a later time. I will also attach all the images for the board and the pieces at a later time. This is what I have so far:
Board.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class Board extends JPanel
{
public Piece board[][] = new Piece[8][8];
private Image boardImage = Toolkit.getDefaultToolkit().getImage("Board.JPG");
private Image greyPieceImage = Toolkit.getDefaultToolkit().getImage("greyPiece.JPG");
private Image redPieceImage = Toolkit.getDefaultToolkit().getImage("redPiece.JPG");
private Image redKingImage = Toolkit.getDefaultToolkit().getImage("redKing.jpg");
private Image greyKingImage = Toolkit.getDefaultToolkit().getImage("greyKing.jpg");
public Board(){
for(int x = 0; x < board.length; x++)
for(int y = 0; y < board[x].length; y++)
board[x][y]= new Piece(0, false);
setBoard();
repaint();
show();
}
public void setBoard(){
for(int c = 0; c <= 2; c++){
if(c == 1){
for(int r = 0; r <= 6; r+=2){
board[c][r].setPlayer(1);
}
}
if(c == 0 || c==2){
for(int r = 1; r <= 7; r+=2){
board[c][r].setPlayer(1);
}
}
}
//----------------------------------------------\\
for(int c = 7; c >= 5; c--){
if(c == 7 || c == 5){
for(int r = 0; r <= 6; r+=2){
board[c][r].setPlayer(2);
}
}
if(c == 6){
for(int r = 1; r <= 7; r+=2){
board[c][r].setPlayer(2);
}
}
}
for(int x = 0; x < board.length; x++){
for(int y = 0; y < board[x].length; y++){
System.out.print(board[x][y].getPlayer() + " ");
}
System.out.println();
}
}
public void paint(Graphics window){
window.drawImage(boardImage, 0, 0, 320, 320, this);
for(int i = 0; i<8; i++)
{
for(int j = 0; j<8; j++)
{
if(board[i][j].getPlayer()==1)
{
window.drawImage(redPieceImage, 40*j, 40*i, 39, 39, this);
// System.out.println("1");
}
if(board[i][j].getPlayer()==2)
{
window.drawImage(greyPieceImage, 40*j, 40*i, 39, 39, this);
// System.out.println("2");
}
if(board[i][j].getPlayer()==3)
{
window.drawImage(redKingImage, 40*j, 40*i, 39, 39, this);
}
if(board[i][j].getPlayer()==4)
{
window.drawImage(greyKingImage, 40*j, 40*i, 39, 39, this);
}
}
}
}
}
Checkers.java:
public class Checkers {
public static void main(String[] args) {
// Create application frame.
CheckersFrame frame = new CheckersFrame();
// Show frame
frame.setVisible(true);
}
}
Piece.java:
public class Piece
{
private int player = 0;
private boolean king = false;
public Piece(){
}
public Piece(int x, boolean y){
player = x;
king = y;
}
public void setPlayer(int x){
player = x;
}
public int getPlayer(){
return player;
}
public void setKing(boolean k)
{
king = k;
}
public boolean getKing()
{
return king;
}
}
CheckersFrame.java:
import java.io.*;
import java.lang.*;
import java.util.*;
import java.text.DecimalFormat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.MouseListener;
public class CheckersFrame extends JFrame implements MouseListener
{
Board myBoard = new Board();
int currentPieceValue = -999;
int firstX, firstY;
int turn = 1;
public CheckersFrame() {
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
CheckersFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("Checkers");
setMenuBar(menuBar);
setSize(new Dimension(330, 370));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CheckersFrame.this.windowClosed();
}
}
);
// add MouseListener and multiple value by 40 then pass to drawboard
Container screen;
screen = getContentPane();
JPanel row0 = new JPanel();
row0.setLayout( new GridLayout(1,1,1,1));
screen.add(row0);
row0.removeAll();
row0.add(myBoard);
row0.updateUI();
row0.addMouseListener(this);
}
public void mouseClicked(MouseEvent e)
{
int x = (e.getX()/40);
int y = (e.getY()/40);
if(currentPieceValue == -999)
{
firstX = x;
firstY = y;
currentPieceValue = myBoard.board[y][x].getPlayer();
if(currentPieceValue==0)
{
currentPieceValue = -999;
}
myBoard.board[y][x].setPlayer(0);
System.out.println(currentPieceValue);
}
else if(currentPieceValue != 999 && myBoard.board[y][x].getPlayer() == 0)
{
if(currentPieceValue==1 && (x == firstX+1 || x == firstX-1 || x == firstX+2 || x == firstX-2) && ((y == firstY+1 || y == firstY+2))) //&& turn == 1)
{
if((x == firstX+2 || x == firstX-2) && (y == firstY+2))
{
if(x == firstX + 2 && y == firstY+2 && myBoard.board[y-1][x-1].getPlayer() == 2 || myBoard.board[y-1][x-1].getPlayer() == 4)
{
myBoard.board[y][x].setPlayer(currentPieceValue);
if(y==7)
{
myBoard.board[y][x].setPlayer(3);
}
currentPieceValue = -999;
myBoard.board[y-1][x-1].setPlayer(0);
myBoard.repaint();
}
else if(x == firstX - 2 && y == firstY+2 && myBoard.board[y-1][x+1].getPlayer() == 2 || myBoard.board[y-1][x+1].getPlayer() == 4)
{
myBoard.board[y][x].setPlayer(currentPieceValue);
if(y==7)
{
myBoard.board[y][x].setPlayer(3);
}
currentPieceValue = -999;
myBoard.board[y-1][x+1].setPlayer(0);
myBoard.repaint();
}
}
else
{
myBoard.board[y][x].setPlayer(currentPieceValue);
if(y==7)
{
myBoard.board[y][x].setPlayer(3);
}
currentPieceValue = -999;
turn = 2;
myBoard.repaint();
}
}
/////////////////////////////////////red on top; black on bottom///////////////////
else if(currentPieceValue==2 && (x == firstX+1 || x == firstX+2 || x == firstX-1 || x==firstX-2) && ((y == firstY-1) || y == firstY-2))// && turn == 2)
{
if((x == firstX+2 || x == firstX-2) && (y == firstY-2))
{
if(x == firstX + 2 && y == firstY-2 && myBoard.board[y+1][x-1].getPlayer() == 1 || myBoard.board[y+1][x-1].getPlayer() == 3)
{
myBoard.board[y][x].setPlayer(currentPieceValue);
if(y==0)
{
myBoard.board[y][x].setPlayer(4);
}
currentPieceValue = -999;
myBoard.board[y+1][x-1].setPlayer(0);
myBoard.repaint();
}
else if(x == firstX - 2 && y == firstY-2 && myBoard.board[y+1][x+1].getPlayer() == 1 || myBoard.board[y+1][x+1].getPlayer() == 3)
{
myBoard.board[y][x].setPlayer(currentPieceValue);
if(y==0)
{
myBoard.board[y][x].setPlayer(4);
}
currentPieceValue = -999;
myBoard.board[y+1][x+1].setPlayer(0);
myBoard.repaint();
}
}
else
{
myBoard.board[y][x].setPlayer(currentPieceValue);
if(y==0)
{
myBoard.board[y][x].setPlayer(4);
}
currentPieceValue = -999;
turn = 1;
myBoard.repaint();
}
}
else if(currentPieceValue == 3 && (x == firstX+1 || x == firstX+2 || x == firstX-1 || x==firstX-2) && ((y == firstY-1 || y == firstY-2 || y == firstY+1 || y==firstY+2)))
{
if(x == firstX + 2 && y == firstY-2 && myBoard.board[y+1][x-1].getPlayer() == 2)
{
myBoard.board[y][x].setPlayer(currentPieceValue);
currentPieceValue = -999;
myBoard.board[y+1][x-1].setPlayer(0);
myBoard.repaint();
}
else if(x == firstX - 2 && y == firstY-2 && myBoard.board[y+1][x+1].getPlayer() == 2)
{
myBoard.board[y][x].setPlayer(currentPieceValue);
currentPieceValue = -999;
myBoard.board[y+1][x+1].setPlayer(0);
myBoard.repaint();
}
else if(x == firstX+2 && y == firstY + 2 && myBoard.board[y-1][x-1].getPlayer() == 2)
{
myBoard.board[y][x].setPlayer(currentPieceValue);
currentPieceValue = -999;
myBoard.board[y-1][x-1].setPlayer(0);
myBoard.repaint();
}
else if(x == firstX-2 && y == firstY + 2 && myBoard.board[y-1][x+1].getPlayer() == 2)
{
myBoard.board[y][x].setPlayer(currentPieceValue);
currentPieceValue = -999;
myBoard.board[y-1][x+1].setPlayer(0);
myBoard.repaint();
}
else
{
myBoard.board[y][x].setPlayer(currentPieceValue);
currentPieceValue = -999;
turn = 1;
myBoard.repaint();
}
}
else if(currentPieceValue == 4 && (x == firstX+1 || x == firstX+2 || x == firstX-1 || x==firstX-2) && ((y == firstY-1 || y == firstY-2 || y == firstY+1 || y==firstY+2)))
{
if(x == firstX + 2 && y == firstY-2 && myBoard.board[y+1][x-1].getPlayer() == 1)
{
myBoard.board[y][x].setPlayer(currentPieceValue);
currentPieceValue = -999;
myBoard.board[y+1][x-1].setPlayer(0);
myBoard.repaint();
}
else if(x == firstX - 2 && y == firstY-2 && myBoard.board[y+1][x+1].getPlayer() == 1)
{
myBoard.board[y][x].setPlayer(currentPieceValue);
currentPieceValue = -999;
myBoard.board[y+1][x+1].setPlayer(0);
myBoard.repaint();
}
else if(x == firstX+2 && y == firstY + 2 && myBoard.board[y-1][x-1].getPlayer() == 1)
{
myBoard.board[y][x].setPlayer(currentPieceValue);
currentPieceValue = -999;
myBoard.board[y-1][x-1].setPlayer(0);
myBoard.repaint();
}
else if(x == firstX-2 && y == firstY + 2 && myBoard.board[y-1][x+1].getPlayer() == 1)
{
myBoard.board[y][x].setPlayer(currentPieceValue);
currentPieceValue = -999;
myBoard.board[y-1][x+1].setPlayer(0);
myBoard.repaint();
}
else
{
myBoard.board[y][x].setPlayer(currentPieceValue);
currentPieceValue = -999;
turn = 1;
myBoard.repaint();
}
}
}
}
public void mouseReleased(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}
That's it.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
|