package CheckersBoard;
/**
*
* @author Chris
*/
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.game.Sprite.*;
import java.io.*;
import javax.microedition.lcdui.Graphics;
public class CheckerBoard extends GameCanvas implements Runnable {
private Display myDisplay;
private long frameRate;
private boolean sleeping;
private Graphics g;
private Sprite[] blackChecker;
private Sprite[] whiteChecker;
private Sprite board;
private Sprite banner;
private final int numberOfCheckers = 12;
int row; // Row number, from 0 to 7
int col; // Column number, from 0 to 7
int x, y; // Top-left corner of square
int z; //Used to store value of square dimensions
int screenWidth = getWidth(); //Stores value of the screen width in px
int screenHeight = getHeight();
int A, B, C, D, E, F, G, H; //X axis Coordinates for grid
int I, J, K, L, M, N, O, P; //Y axis Coordinates for grid
int widthOfSquare = 240 / 8;
int curX = 0;
int curY = 0;
final int BLANK = 0; // location empty
final int WHITE = 1; // white piece
final int BLACK = 2; // blue piece
int grid[][] = new int[8][8]; // create 64 integers
public CheckerBoard(Display d) {
super(true);
myDisplay = d;
frameRate = 40;
g = this.getGraphics();
whiteChecker = new Sprite[numberOfCheckers];
blackChecker = new Sprite[numberOfCheckers];
}
public void start() {
myDisplay.setCurrent(this);
sleeping = false; /*
grid[0][2] = BLANK;
grid[0][4] = BLANK;
grid[0][6] = BLANK;
grid[0][8] = BLANK;
grid[1][1] = BLANK;
grid[1][3] = BLANK;
grid[1][5] = BLANK;
grid[1][7] = BLANK;
grid[2][2] = BLANK;
grid[2][4] = BLANK;
grid[2][6] = BLANK;
grid[2][8] = BLANK;
grid[3][0] = BLANK;
grid[3][1] = BLANK;
grid[3][2] = BLANK;
grid[3][3] = BLANK;
grid[3][4] = BLANK;
grid[3][5] = BLANK;
grid[3][6] = BLANK;
grid[3][7] = BLANK;
grid[4][0] = BLANK;
grid[4][1] = BLANK;
grid[4][2] = BLANK;
grid[4][3] = BLANK;
grid[4][4] = BLANK;
grid[4][5] = BLANK;
grid[4][6] = BLANK;
grid[4][7] = BLANK;
grid[5][0] = BLANK;
grid[5][2] = BLANK;
grid[5][4] = BLANK;
grid[5][6] = BLANK;
grid[6][1] = BLANK;
grid[6][3] = BLANK;
grid[6][5] = BLANK;
grid[6][7] = BLANK;
grid[7][0] = BLANK;
grid[7][2] = BLANK;
grid[7][4] = BLANK;
grid[7][6] = BLANK;
grid[0][1] = BLACK;
grid[0][3] = BLACK;
grid[0][5] = BLACK;
grid[0][7] = BLACK;
grid[1][0] = BLACK;
grid[1][2] = BLACK;
grid[1][4] = BLACK;
grid[1][6] = BLACK;
grid[2][1] = BLACK;
grid[2][3] = BLACK;
grid[2][5] = BLACK;
grid[2][7] = BLACK;
grid[5][1] = WHITE;
grid[5][3] = WHITE;
grid[5][5] = WHITE;
grid[5][7] = WHITE;
grid[6][0] = WHITE;
grid[6][2] = WHITE;
grid[6][4] = WHITE;
grid[6][6] = WHITE;
grid[7][1] = WHITE;
grid[7][3] = WHITE;
grid[7][5] = WHITE;
grid[7][7] = WHITE; */
A = 0;
B = widthOfSquare;
C = widthOfSquare * 2;
D = widthOfSquare * 3;
E = widthOfSquare * 4;
F = widthOfSquare * 5;
G = widthOfSquare * 6;
H = widthOfSquare * 7;
I = 0;
J = widthOfSquare;
K = widthOfSquare * 2;
L = widthOfSquare * 3;
M = widthOfSquare * 4;
N = widthOfSquare * 5;
O = widthOfSquare * 6;
P = widthOfSquare * 7;
try {
Image black = Image.createImage("/res/blackChecker.png");
Image white = Image.createImage("/res/whiteChecker.png");
Image checkerBoard = Image.createImage("/res/checkerBoard.PNG");
Image ban = Image.createImage("/res/banner.png");
board = new Sprite(checkerBoard);
board.setPosition(0, 0);
banner = new Sprite(ban);
banner.setPosition(0, 240);
for (int i = 0; i < numberOfCheckers; i++) {
blackChecker[0] = new Sprite(black);
blackChecker[0].setPosition(B, I);
blackChecker[1] = new Sprite(black);
blackChecker[1].setPosition(D, I);
blackChecker[2] = new Sprite(black);
blackChecker[2].setPosition(F, I);
blackChecker[3] = new Sprite(black);
blackChecker[3].setPosition(H, I);
blackChecker[4] = new Sprite(black);
blackChecker[4].setPosition(A, J);
blackChecker[5] = new Sprite(black);
blackChecker[5].setPosition(C, J);
blackChecker[6] = new Sprite(black);
blackChecker[6].setPosition(E, J);
blackChecker[7] = new Sprite(black);
blackChecker[7].setPosition(G, J);
blackChecker[8] = new Sprite(black);
blackChecker[8].setPosition(B, K);
blackChecker[9] = new Sprite(black);
blackChecker[9].setPosition(D, K);
blackChecker[10] = new Sprite(black);
blackChecker[10].setPosition(F, K);
blackChecker[11] = new Sprite(black);
blackChecker[11].setPosition(H, K);
whiteChecker[0] = new Sprite(white);
whiteChecker[0].setPosition(A, N);
whiteChecker[1] = new Sprite(white);
whiteChecker[1].setPosition(C, N);
whiteChecker[2] = new Sprite(white);
whiteChecker[2].setPosition(E, N);
whiteChecker[3] = new Sprite(white);
whiteChecker[3].setPosition(G, N);
whiteChecker[4] = new Sprite(white);
whiteChecker[4].setPosition(B, O);
whiteChecker[5] = new Sprite(white);
whiteChecker[5].setPosition(D, O);
whiteChecker[6] = new Sprite(white);
whiteChecker[6].setPosition(F, O);
whiteChecker[7] = new Sprite(white);
whiteChecker[7].setPosition(H, O);
whiteChecker[8] = new Sprite(white);
whiteChecker[8].setPosition(A, P);
whiteChecker[9] = new Sprite(white);
whiteChecker[9].setPosition(C, P);
whiteChecker[10] = new Sprite(white);
whiteChecker[10].setPosition(E, P);
whiteChecker[11] = new Sprite(white);
whiteChecker[11].setPosition(G, P);
}
} catch (IOException e) {
System.err.println("Failed loading sprites" + e.getMessage());
}
Thread t = new Thread(this);
t.start();
}
public void stop() {
}
public void run() {
while (!sleeping) {
updateGame();
draw(g);
try {
Thread.sleep(frameRate);
} catch (InterruptedException ie) {
System.out.println(ie.getMessage());
}
}
}
public void draw(Graphics g) {
board.paint(g);
banner.paint(g);
for (int i = 0; i < 12; i++) {
blackChecker[i].paint(g);
whiteChecker[i].paint(g);
}
g.setColor(0, 255, 0);
g.drawRect(curX, curY, 30, 30);
flushGraphics();
}
public void updateGame() {
int keyState = this.getKeyStates();
if ((keyState & LEFT_PRESSED) != 0) {
if ((curX) != 0) {
curX = curX - 30;
}
}
if ((keyState & RIGHT_PRESSED) != 0) {
if ((curX) != 210) {
curX = curX + 30;
}
}
if ((keyState & UP_PRESSED) != 0) {
if ((curY) != 0) {
curY = curY - 30;
}
}
if ((keyState & DOWN_PRESSED) != 0) {
if ((curY) != 210) {
curY = curY + 30;
}
}
try {
Thread.sleep(frameRate);
} catch (InterruptedException ie) {
System.out.println(ie.getMessage());
}
}
}