import javax.microedition.lcdui.*;
public class Principal extends MIDlet {
Display meuDisplay = Display.getDisplay(this);
Teclas meuCanvas = new Teclas();
public int keycode;
public void startApp() {
meuDisplay.setCurrent(meuCanvas);
}
public void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
}
private String keyText = null; // Key code text
int[] tabuleiro= new int[9];
int[] resultado= new int[9];
boolean ocupado= false;
boolean ganhou= false;
boolean perdeu= false;
boolean ninguem= false;
Random rd = new Random();
int cont=1;
int jogadas=0;
for (int i =0; i <=8; i++) {
tabuleiro[i]=0;
resultado[i]=0;}
}
linhas(g); if (tabuleiro[Integer.parseInt(keyText)-1]==0 ) {
//quad //left/top/comp/alt
if (keyText.equals("1")) {tabuleiro[0]=1;}
if (keyText.equals("2")) {tabuleiro[1]=1;}
if (keyText.equals("3")) {tabuleiro[2]=1;}
if (keyText.equals("4")) {tabuleiro[3]=1;}
if (keyText.equals("5")) {tabuleiro[4]=1;}
if (keyText.equals("6")) {tabuleiro[5]=1;}
if (keyText.equals("7")) {tabuleiro[6]=1;}
if (keyText.equals("8")) {tabuleiro[7]=1;}
if (keyText.equals("9")) {tabuleiro[8]=1;}
if (cont<=4 ){
jogacomputador();
cont++;
}
}
for (int p=0; p <= 8 ; p++){
g.setColor(0, 0, 0);
if (tabuleiro[0]==1){ g.fillRect(5,5, 50, 50);}
if (tabuleiro[1]==1){ g.fillRect(65,5, 50, 50);}
if (tabuleiro[2]==1){ g.fillRect(125,5, 50, 50);}
if (tabuleiro[3]==1){ g.fillRect(5,65, 50, 50);}
if (tabuleiro[4]==1){ g.fillRect(65,65, 50, 50);}
if (tabuleiro[5]==1){ g.fillRect(125,65, 50, 50);}
if (tabuleiro[6]==1){ g.fillRect(5,125, 50, 50);}
if (tabuleiro[7]==1){ g.fillRect(65,125, 50, 50);}
if (tabuleiro[8]==1){ g.fillRect(125,125, 50, 50);}
g.setColor(10, 10, 255);
if (tabuleiro[0]==2){ g.fillArc(5, 5, 49, 49, 0, 360); } //x,y,w,h, angulo inicio, circuferencia cheia}
if (tabuleiro[1]==2){ g.fillArc(65, 5, 49, 49, 0, 360); }
if (tabuleiro[2]==2){ g.fillArc(125, 5, 49, 49, 0, 360);}
if (tabuleiro[3]==2){ g.fillArc(5, 65, 49, 49, 0, 360);}
if (tabuleiro[4]==2){ g.fillArc(65, 65, 49, 49, 0, 360);}
if (tabuleiro[5]==2){ g.fillArc(125, 65, 49, 49, 0, 360);}
if (tabuleiro[6]==2){ g.fillArc(5, 125, 49, 49, 0, 360);}
if (tabuleiro[7]==2){ g.fillArc(65, 125, 49, 49, 0, 360); }
if (tabuleiro[8]==2){ g.fillArc(125, 125, 50, 50, 0, 360); }
// 3 4 5
// 6 7 8
//rehras ganhar
if ( (tabuleiro[0]==1) &&(tabuleiro[1]==1) &&(tabuleiro[2]==1) ){
ganhou=true;
resultado[0]=1; resultado[1]=1; resultado[2]=1;
}
if ( (tabuleiro[3]==1) &&(tabuleiro[4]==1) &&(tabuleiro[5]==1) ){
ganhou=true;
resultado[3]=1; resultado[4]=1; resultado[5]=1;
}
if ( (tabuleiro[6]==1) &&(tabuleiro[7]==1) &&(tabuleiro[8]==1) ){
ganhou=true;
resultado[6]=1; resultado[7]=1; resultado[8]=1;
}
if ( (tabuleiro[0]==1) &&(tabuleiro[3]==1) &&(tabuleiro[6]==1) ){
ganhou=true;
resultado[0]=1; resultado[3]=1; resultado[6]=1;
}
if ( (tabuleiro[1]==1) &&(tabuleiro[4]==1) &&(tabuleiro[7]==1) ){
ganhou=true;
resultado[1]=1; resultado[4]=1; resultado[7]=1;
}
if ( (tabuleiro[2]==1) &&(tabuleiro[5]==1) &&(tabuleiro[8]==1) ){
ganhou=true;
resultado[2]=1; resultado[2]=1; resultado[2]=1;
}
if ( (tabuleiro[0]==1) &&(tabuleiro[4]==1) &&(tabuleiro[8]==1) ){
ganhou=true;
resultado[0]=1; resultado[4]=1; resultado[8]=1;
}
if ( (tabuleiro[2]==1) &&(tabuleiro[4]==1) &&(tabuleiro[6]==1) ){
ganhou=true;
resultado[2]=1; resultado[4]=1; resultado[6]=1;
}
//regras perder
if ( (tabuleiro[0]==2) &&(tabuleiro[1]==2) &&(tabuleiro[2]==2) ){
perdeu=true;
resultado[0]=2; resultado[1]=2; resultado[2]=2;
}
if ( (tabuleiro[3]==2) &&(tabuleiro[4]==2) &&(tabuleiro[5]==2) ){
perdeu=true;
resultado[3]=2; resultado[4]=2; resultado[5]=2;
if ( (tabuleiro[6]==2) &&(tabuleiro[7]==2) &&(tabuleiro[8]==2) ){
perdeu=true;
resultado[6]=2; resultado[7]=2; resultado[8]=2;
if ( (tabuleiro[0]==2) &&(tabuleiro[3]==2) &&(tabuleiro[6]==2) ){
perdeu=true;
resultado[0]=2; resultado[3]=2; resultado[6]=2;
if ( (tabuleiro[1]==2) &&(tabuleiro[4]==2) &&(tabuleiro[7]==2) ){
perdeu=true;
resultado[1]=2; resultado[4]=2; resultado[7]=2;
if ( (tabuleiro[2]==2) &&(tabuleiro[5]==2) &&(tabuleiro[8]==2) ){
perdeu=true;
resultado[2]=2; resultado[5]=2; resultado[8]=2;
if ( (tabuleiro[0]==2) &&(tabuleiro[4]==2) &&(tabuleiro[8]==2) ){
perdeu=true;
resultado[0]=2; resultado[4]=2; resultado[8]=2;
if ( (tabuleiro[2]==2) &&(tabuleiro[4]==2) &&(tabuleiro[6]==2) ){
perdeu=true;
resultado[2]=2; resultado[4]=2; resultado[6]=2;
if ((ganhou==true)||(perdeu==true)){
LimpaTela(g);
ImpResultado(g);
if (ganhou==true){
g.setColor(0, 0, 0);
g.drawString("Você Ganhou Parabéns!", getWidth()/2, getHeight()/2, Graphics.TOP | Graphics.HCENTER);
}
if (perdeu==true){
g.setColor(0, 0, 0);
g.drawString("Você Perdeu que Pena!", getWidth()/2, getHeight()/2, Graphics.TOP | Graphics.HCENTER);
}
}
ninguem = false;
jogadas=0;
for (int i=0; i<=8;i++){
if ((tabuleiro[i]==1)||(tabuleiro[i]==2)){
jogadas++;
}
}
if (jogadas==8){
ninguem=true;
}
if ((ganhou==false)&&(perdeu==false)&&(ninguem==true)){
//resultado
LimpaTela(g);
g.setColor(0, 0, 0);
g.drawString("Ninguém ganhou,deu empate!", getWidth()/2, getHeight()/2, Graphics.TOP | Graphics.HCENTER);
}
}
}
protected void keyPressed(int keyCode){
keyText = getKeyName(keyCode);
repaint();
}
ocupado = false;
while (ocupado == false) {
int num = rd.nextInt(9);
if ((num >= 0) &&(num<=8)){
if ( tabuleiro[num]==0){
ocupado =true;
tabuleiro[num]=2;
}
}
}
}
public void ImpResultado(Graphics g){
//mostra resultado
g.setColor(220, 255, 255);
if (resultado[0]==1){ g.fillRect(5,5, 50, 50);}
if (resultado[1]==1){ g.fillRect(65,5, 50, 50);}
if (resultado[2]==1){ g.fillRect(125,5, 50, 50);}
if (resultado[3]==1){ g.fillRect(5,65, 50, 50);}
if (resultado[4]==1){ g.fillRect(65,65, 50, 50);}
if (resultado[5]==1){ g.fillRect(125,65, 50, 50);}
if (resultado[6]==1){ g.fillRect(5,125, 50, 50);}
if (resultado[7]==1){ g.fillRect(65,125, 50, 50);}
if (resultado[8]==1){ g.fillRect(125,125, 50, 50);}
if (resultado[0]==2){ g.fillArc(5, 5, 49, 49, 0, 360); } //x,y,w,h, angulo inicio, circuferencia cheia}
if (resultado[1]==2){ g.fillArc(65, 5, 49, 49, 0, 360); }
if (resultado[2]==2){ g.fillArc(125, 5, 49, 49, 0, 360);}
if (resultado[3]==2){ g.fillArc(5, 65, 49, 49, 0, 360);}
if (resultado[4]==2){ g.fillArc(65, 65, 49, 49, 0, 360);}
if (resultado[5]==2){ g.fillArc(125, 65, 49, 49, 0, 360);}
if (resultado[6]==2){ g.fillArc(5, 125, 49, 49, 0, 360);}
if (resultado[7]==2){ g.fillArc(65, 125, 49, 49, 0, 360); }
if (resultado[8]==2){ g.fillArc(125, 125, 50, 50, 0, 360); }
}
public void LimpaTela(Graphics g){
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
}
public void linhas(Graphics g){
//linhas
g.fillRect(0,60, 180, 1);
g.fillRect(0,120, 180, 1);
g.fillRect(60, 0, 1, 180);
g.fillRect(120, 0, 1, 180);
g.setColor(100, 100, 0);
g.drawString("1", 30,25 , Graphics.TOP | Graphics.HCENTER);
g.drawString("2", 90,25 , Graphics.TOP | Graphics.HCENTER);
g.drawString("3", 150,25 , Graphics.TOP | Graphics.HCENTER);
g.drawString("4", 30,85 , Graphics.TOP | Graphics.HCENTER);
g.drawString("5", 90,85 , Graphics.TOP | Graphics.HCENTER);
g.drawString("6", 150,85 , Graphics.TOP | Graphics.HCENTER);
g.drawString("7", 30,140 , Graphics.TOP | Graphics.HCENTER);
g.drawString("8", 90,140 , Graphics.TOP | Graphics.HCENTER);
g.drawString("9", 150,140 , Graphics.TOP | Graphics.HCENTER);
}