You are on page 1of 5

import javax.microedition.lcdui.

*;
import javax.microedition.midlet.*;

public class TicTacToe extends MIDlet implements CommandListener {

// The main display elements


private Display theDisplay;
public static TicTacToe ttt = null;

private TicTacToeCanvas canvas;

// Keep track of winner


private String winner = "";
private boolean gameOver = false;

// The game state


private int whoseturn = 0;

/*private char board[][] = { {'1' , '2' , '3'} ,


{'4' , '5' , '6'} ,
{'7' , '8' , '9'} };
*/

private char board[][] = new char[3][3];

// 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(){

canvas = new TicTacToeCanvas();


// Add the exit command
canvas.addCommand(exitCommand);
canvas.setCommandListener(ttt);
theDisplay.setCurrent(canvas);
}

class TicTacToeCanvas extends Canvas


{
TicTacToeCanvas(){}

public void paint(Graphics g)


{

int width = getWidth();


int height = getHeight();
g.setColor( 1, 1, 1 );
g.fillRect( 0, 0, width, height );

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("TIC TAC TOE",15+50,20, Graphics.LEFT|Graphics.TOP);

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);
}
}

// Check for keyboard entry


protected void keyPressed(int keyCode) {

// Fill in the X and Os


if (keyCode >= 49 && keyCode <= 57 && !gameOver)
{
int x = 0, y = 0;
keyCode -= 49;
if (keyCode < 3)
{
x = keyCode;
y = 0;
}
else if (keyCode < 6)
{
x = keyCode-3;
y = 1;
}
else
{
x = keyCode-6;
y = 2;
}
// If this slot is open...
if (board[y][x] != 'X' && board[y][x] != 'O')
{

if (whoseturn == 0)
board[y][x] = 'X';
else
board[y][x] = 'O';

// Next player's turn


whoseturn = 1-whoseturn;
checkForWinner();
// repaint
canvas.repaint();
}
}
}
}

protected void startApp() throws MIDletStateChangeException


{
showGame();
}

protected void pauseApp() { }

public void destroyApp(boolean unconditional)


{
}

// 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();
}
}

private void checkForWinner()


{
int i,j;
int numXConsec = 0; // Number of consecutive Xs
int numOConsec = 0; // Number of consecutive Os

boolean tie=true;

// Check for a tie


for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
if (board[i][j] != 'X' || board[i][j] != 'O')
{
tie = false;
break;
}
}
if (tie)
{
GameOver(-1);
return;
}

// Check Horizontally first


for (i=0; i<3; i++)
{
numXConsec = numOConsec = 0;
for (j=0; j<3; j++)
{
if (board[i][j] == 'X')
numXConsec++;
else if (board[i][j] == 'O')
numOConsec++;
else
break;
}
if (numXConsec > 2)
{
GameOver(0);
return;
}
else if (numOConsec > 2)
{
GameOver(1);
return;
}
}

// 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;
}
}

// Finally Check both diagnals


if ( ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) &&
(board[2][2] == 'X')) ||
((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) &&
(board[2][0] == 'X')) )
{
GameOver(0);
return;
}
if ( ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) &&
(board[2][2] == 'O')) ||
((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) &&
(board[2][0] == 'O')) )
{
GameOver(1);
return;
}
}

private void GameOver(int p)


{
if (p == -1)
winner = "Tie!";
else if (p == 0)
winner = "Player 1!";
else
winner = "Player 2!";
gameOver = true;
canvas.addCommand(okCommand);
}
}

You might also like