Professional Documents
Culture Documents
*;
import javax.microedition.midlet.*;
// Command
static final Command exitCommand = new Command("Exit", Command.STOP, 1);
static final Command okCommand = new Command("Ok", Command.OK,2);
public TicTacToe()
{
iniBoard();
ttt = this;
//create the main Display
theDisplay = Display.getDisplay(this);
}
public void iniBoard(){
board[0][0] ='1';
board[0][1] = '2';
board[0][2] = '3';
board[1][0] = '4';
board[1][1] = '5';
board[1][2] = '6';
board[2][0] = '7';
board[2][1] = '8';
board[2][2] = '9';
}
private void showGame(){
g.setColor( 0, 0, 0 );
g.fillRect( 0, 0, width, height );
g.setColor( 255, 255, 255);
Font fnt =
Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE);
g.setFont(fnt);
String r1 = board[0][0]+"|"+board[0][1]+"|"+board[0][2]+"\n";
String r2 = board[1][0]+"|"+board[1][1]+"|"+board[1][2]+"\n";
String r3 = board[2][0]+"|"+board[2][1]+"|"+board[2][2];
g.drawString(r1,30+55,15+60, Graphics.LEFT|Graphics.TOP);
g.drawString(r2,30+55,30+60, Graphics.LEFT|Graphics.TOP);
g.drawString(r3,30+55,45+60, Graphics.LEFT|Graphics.TOP);
if (winner != "")
{
g.drawString("WINNER:"+winner,55,50,Graphics.LEFT|Graphics.TOP);
}
else
{
g.drawString("TURN: Player"+
(whoseturn+1),0+55,60+80,Graphics.LEFT|Graphics.TOP);
}
}
if (whoseturn == 0)
board[y][x] = 'X';
else
board[y][x] = 'O';
// Handle events.
public void commandAction(Command c, Displayable d)
{
if (c == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
if(c == okCommand){
gameOver = false;
canvas = null;
winner = "";
iniBoard();
showGame();
}
}
boolean tie=true;
// Check Vertically
for (i=0; i<3; i++)
{
numXConsec = numOConsec = 0;
for (j=0; j<3; j++)
{
if (board[j][i] == 'X')
{
numXConsec++;
}
else if (board[j][i] == 'O')
numOConsec++;
else
break;
}
if (numXConsec > 2)
{
GameOver(0);
return;
}
else if (numOConsec > 2)
{
GameOver(1);
return;
}
}