import com.sun.lwuit.Button; import com.sun.lwuit.Dialog; import com.sun.lwuit.Display; import com.sun.lwuit.Form; import com.sun.lwuit.Label; import com.sun.lwuit.events.ActionEvent; import com.sun.lwuit.events.ActionListener; import com.sun.lwuit.layouts.GridLayout; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import java.util.Random; public class TicTacToe extends MIDlet implements ActionListener { private Button[] mButtons = new Button[9]; private Label[] mLabels = new Label[6]; private int index; private int players; private int level; public TicTacToe() { } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO Auto-generated method stub } protected void pauseApp() { // TODO Auto-generated method stub } protected void startApp() throws MIDletStateChangeException { // TODO Auto-generated method stub Display.init(this); Form f = new Form("Tic-Tac-Toe"); f.setLayout(new GridLayout(4,4)); mLabels[0]= new Label("1"); mLabels[1]= new Label("2"); mLabels[2]= new Label("3"); mLabels[3]= new Label("A"); mLabels[4]= new Label("B"); mLabels[5]= new Label("C"); for( int i =0;i<9;i++){ mButtons[i] = new Button(""); mButtons[i].setName(Integer.toString(i)); mButtons[i].addActionListener(this); } index = 1; players = 0; f.addComponent(mButtons[0]); f.addComponent(mButtons[1]); f.addComponent(mButtons[2]); f.addComponent(mLabels[0]); f.addComponent(mButtons[3]); f.addComponent(mButtons[4]); f.addComponent(mButtons[5]); f.addComponent(mLabels[1]); f.addComponent(mButtons[6]); f.addComponent(mButtons[7]); f.addComponent(mButtons[8]); f.addComponent(mLabels[2]); f.addComponent(mLabels[3]); f.addComponent(mLabels[4]); f.addComponent(mLabels[5]); if(Dialog.show("Game mode:", "Select player count?", "1", "2")== false){ players = 2; } else{ players = 1; } if(Dialog.show("Game Difficulty Level:", "Select Artificial-?", "Nonsense", "Intelligence")== false){ level = 2; //Intelligence } else{ level = 1; //Nonsense } f.show(); } public void CleanScreen() { for( int i =0;i<9;i++){ mButtons[i].setText(""); } index=1; } public void HandleRetryExit(String caption){ // Create Dialog, add text and image, associate a sound if(Dialog.show(caption, "Try again ?", "Yes", "No")== false){ Display.getInstance().exitApplication(); } else{ // Start again CleanScreen(); } } public int random(){ //Generate random integers in range 0..8. Random randomGenerator = new Random(); return randomGenerator.nextInt(9); } public int checkWinner() { int ret = 0; // check rows for(int i = 0; i<3; i++){ if( mButtons[0+(i*3)].getText() == mButtons[1+(i*3)].getText() && mButtons[0+(i*3)].getText() == mButtons[2+(i*3)].getText() ){ if(mButtons[0+(i*3)].getText() == "X"){ ret = 1; } else if(mButtons[0+(i*3)].getText() == "O") { ret = 2; } } } // check columns for(int i = 0; i<3; i++){ if( mButtons[0+i].getText() == mButtons[3+i].getText() && mButtons[0+i].getText() == mButtons[6+i].getText() ){ if(mButtons[0+i].getText() == "X"){ ret = 1; } else if(mButtons[0+i].getText() == "O") { ret = 2; } } } // check cross line \ if( mButtons[0].getText() == mButtons[4].getText() && mButtons[0].getText() == mButtons[8].getText() ){ if(mButtons[0].getText() == "X"){ ret = 1; } else if(mButtons[0].getText() == "O"){ ret = 2; } } // check cross line / if( mButtons[2].getText() == mButtons[4].getText() && mButtons[2].getText() == mButtons[6].getText() ){ if(mButtons[2].getText() == "X"){ ret = 1; } else if(mButtons[2].getText() == "O"){ ret = 2; } } // Check game state if(ret == 1){ HandleRetryExit("Winner is X."); } else if(ret == 2) { HandleRetryExit("Winner is O."); } else if (index == 10){ HandleRetryExit("The game is draw."); ret = 3; } return ret; } public void artificialNonsense(){ int button; // randomize all turns while(mButtons[button=random()].getText().length() != 0){ ; } mButtons[button].setText("X"); index++; // next turn is human player } public void artificialIntelligence(){ // Use real logic // Find 2 pair lines -> no -> set random -> otherwise block // check rows index++; // next turn is human player // check columns for(int i = 1; i<8; i=i+3){ // columns 1 and 2 if( mButtons[i].getText().length() != 0 ){ if( mButtons[i].getText() == mButtons[i-1].getText() ){ if(mButtons[i+1].getText() == ""){ mButtons[i+1].setText("X"); return; } } // columns 2 and 3 if( mButtons[i].getText() == mButtons[i+1].getText() ){ if(mButtons[i-1].getText() == ""){ mButtons[i-1].setText("X"); return; } } // columns 1 and 3 if( mButtons[i-1].getText() == mButtons[i+1].getText() ){ if(mButtons[i].getText() == ""){ mButtons[i].setText("X"); return; } } } } // check rows for(int i = 3; i<6; i++){ // rows 1 and 2 if( mButtons[i].getText().length() != 0 ){ if( mButtons[i].getText() == mButtons[i-3].getText() ){ if(mButtons[i+3].getText() == ""){ mButtons[i+3].setText("X"); return; } } // rows 2 and 3 if( mButtons[i].getText() == mButtons[i+3].getText() ){ if(mButtons[i-3].getText() == ""){ mButtons[i-3].setText("X"); return; } } } // rows 1 and 3 if( mButtons[i-3].getText().length() != 0 ){ if( mButtons[i-3].getText() == mButtons[i+3].getText() ){ if(mButtons[i].getText() == ""){ mButtons[i].setText("X"); return; } } } } // check cross lines if( mButtons[4].getText().length() != 0 ){ // line 0-4 if( mButtons[0].getText() == mButtons[4].getText() ){ if(mButtons[8].getText() == ""){ mButtons[8].setText("X"); return; } } // line 4-8 if( mButtons[4].getText() == mButtons[8].getText() ){ if(mButtons[0].getText() == ""){ mButtons[0].setText("X"); return; } } // line 6-4 if( mButtons[6].getText() == mButtons[4].getText() ){ if(mButtons[2].getText() == ""){ mButtons[2].setText("X"); return; } } // line 4-2 if( mButtons[4].getText() == mButtons[2].getText() ){ if(mButtons[6].getText() == ""){ mButtons[6].setText("X"); return; } } } // line 0-8 if( mButtons[0].getText().length() != 0 && mButtons[0].getText() == mButtons[8].getText() ){ if(mButtons[4].getText() == ""){ mButtons[4].setText("X"); return; } } // line 2-6 if( mButtons[2].getText().length() != 0 && mButtons[2].getText() == mButtons[6].getText() ){ if(mButtons[4].getText() == ""){ mButtons[4].setText("X"); return; } } // use nonsense logic index--; artificialNonsense(); } public void actionPerformed(ActionEvent arg0) { // Check action int status; Button b = (Button) arg0.getSource(); if(b.getText().length() == 0 ){ if(index % 2 == 0){ b.setText("X"); } else{ b.setText("O"); } index++; status=checkWinner(); if (status == 0 && players == 1){ // set computer move if(level == 1){ artificialNonsense(); } else{ artificialIntelligence(); } status=checkWinner(); } } } }