package CheckersBoard;
/**
*
* @author Chris
*/
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.io.*;
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 cursor;
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 curPositionX = getWidth()/2-20;
int curPositionY = getHeight()/2-60;
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;
if (screenWidth <= screenHeight) //Checks screen width is greater than screen height
z = screenWidth/8; //Divides screen width by 8
else
z = screenHeight/8; //Divides screen height by 8
for ( row = 0; row < 8; row++ ) {
for ( col = 0; col < 8; col++) {
x = col * z; //Set the square width to 1/8 of the screen width
y = row * z; //Set the square height to 1/8 of the screen height
if ( (row % 2) == (col % 2) ){ //Every other square
g.setColor(139,0,0); //Colours the square red
try{
Image black = Image.createImage("/res/blackChecker.png");
Image white = Image.createImage("/res/whiteChecker.png");
Image hand = Image.createImage("/res/cursor.PNG");
cursor = new Sprite(hand);
cursor.setPosition(curPositionX, curPositionY);
for (int i=0; i<numberOfCheckers; i++){
blackChecker[0] = new Sprite(black);
blackChecker[0].setPosition(30,0);
blackChecker[1] = new Sprite(black);
blackChecker[1].setPosition(90,0);
blackChecker[2] = new Sprite(black);
blackChecker[2].setPosition(150,0);
blackChecker[3] = new Sprite(black);
blackChecker[3].setPosition(210,0);
blackChecker[4] = new Sprite(black);
blackChecker[4].setPosition(0,30);
blackChecker[5] = new Sprite(black);
blackChecker[5].setPosition(60,30);
blackChecker[6] = new Sprite(black);
blackChecker[6].setPosition(120,30);
blackChecker[7] = new Sprite(black);
blackChecker[7].setPosition(180,30);
blackChecker[8] = new Sprite(black);
blackChecker[8].setPosition(30,60);
blackChecker[9] = new Sprite(black);
blackChecker[9].setPosition(90,60);
blackChecker[10] = new Sprite(black);
blackChecker[10].setPosition(150,60);
blackChecker[11] = new Sprite(black);
blackChecker[11].setPosition(210,60);
whiteChecker[0] = new Sprite(white);
whiteChecker[0].setPosition(0,210);
whiteChecker[1] = new Sprite(white);
whiteChecker[1].setPosition(60,210);
whiteChecker[2] = new Sprite(white);
whiteChecker[2].setPosition(120,210);
whiteChecker[3] = new Sprite(white);
whiteChecker[3].setPosition(180,210);
whiteChecker[4] = new Sprite(white);
whiteChecker[4].setPosition(30,180);
whiteChecker[5] = new Sprite(white);
whiteChecker[5].setPosition(90,180);
whiteChecker[6] = new Sprite(white);
whiteChecker[6].setPosition(150,180);
whiteChecker[7] = new Sprite(white);
whiteChecker[7].setPosition(210,180);
whiteChecker[8] = new Sprite(white);
whiteChecker[8].setPosition(0,150);
whiteChecker[9] = new Sprite(white);
whiteChecker[9].setPosition(60,150);
whiteChecker[10] = new Sprite(white);
whiteChecker[10].setPosition(120,150);
whiteChecker[11] = new Sprite(white);
whiteChecker[11].setPosition(180,150);
}
}
catch(IOException e){
System.err.println("Failed loading sprites" + e.getMessage());
}
}
else //Every non red square
g.setColor(0,0,0); //Colours the square black
g.fillRect(x, y, z, z); //Draws the grid
}
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){
for (int i=0; i<12; i++){
blackChecker[i].paint(g);
whiteChecker[i].paint(g);
}
flushGraphics();
cursor.paint(g);
}
public void updateGame(){
int keyState = this.getKeyStates();
if ((keyState & LEFT_PRESSED) != 0)
{
curPositionX-=1;
cursor.setPosition(curPositionX,curPositionY);
}
if ((keyState & RIGHT_PRESSED) != 0)
{
curPositionX+=1;
cursor.setPosition(curPositionX,curPositionY);
}
if ((keyState & UP_PRESSED) != 0)
{
curPositionY-=1;
cursor.setPosition(curPositionX,curPositionY);
}
if ((keyState & DOWN_PRESSED) != 0)
{
curPositionY+=1;
cursor.setPosition(curPositionX,curPositionY);
}
checkBoundary(cursor);
}
public void checkBoundary(Sprite s) {
if (s.getX() < -s.getWidth())
s.setPosition(this.getWidth(),s.getY());
else if (s.getX() > this.getWidth())
s.setPosition(-s.getWidth(),s.getY());
if (s.getY() < - s.getHeight())
s.setPosition(s.getX(), this.getHeight());
else if (s.getY() > this.getHeight())
s.setPosition(s.getX(), - s.getHeight());
}
}