| |
|
|
||||
![]() |
||||||
| |
|
|||||
|
Code
Listing: Tic Tac Toe import javax.microedition.midlet.*; public class TicTacToe extends MIDlet implements CommandListener { // The main display elements private Display theDisplay; 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'} }; // Command static final Command exitCommand = new Command("Exit", Command.STOP, 1); public TicTacToe() { // Create the main Display theDisplay = Display.getDisplay(this); } class TicTacToeCanvas extends Canvas { TicTacToeCanvas() { } public void paint(Graphics g) { int width = getWidth(); int height = getHeight(); g.setColor( 0, 0, 0 ); g.fillRect( 0, 0, width, height ); g.setColor( 255, 255, 255); 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,0, Graphics.LEFT|Graphics.TOP); g.drawString(r1,30,15, Graphics.LEFT|Graphics.TOP); g.drawString(r2,30,30, Graphics.LEFT|Graphics.TOP); g.drawString(r3,30,45, Graphics.LEFT|Graphics.TOP); if (winner != "") { g.drawString("WINNER: "+winner,0,60,Graphics.LEFT|Graphics.TOP); } else { g.drawString("TURN: Player "+(whoseturn+1),0,60,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 { canvas = new TicTacToeCanvas(); // Add the exit command canvas.addCommand(exitCommand); canvas.setCommandListener(this) ; theDisplay.setCurrent(canvas); } protected void pauseApp() { } public void destroyApp(boolean unconditional) { } // Handle events. public void commandAction(Command c, Displayable d) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } } 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; } } ________________________________________________________
|
|||||||||||||
|
|