ciao io volevo sapere come posso fare a unire i due codici,uno e'il gioco tris :
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class Tris extends MIDlet implements CommandListener {
// gli elementi per la visualizzazionme
private Display IlDisplay;
private TrisCanvas canvas;
// Traccia del vincitore
private String winner = "";
private boolean FineGioco = false;
// Gli stati di gioco
private int whoseturn = 0;
private char board[][] = { {'1' , '2' , '3'} ,
{'4' , '5' , '6'} ,
{'7' , '8' , '9'}};
// Comandi
static final Command exitCommand = new Command("Chiudi", Command.STOP, 1);
public Tris()
{
// Definise l'oggetto Display
IlDisplay = Display.getDisplay(this);
}
class TrisCanvas extends Canvas
{
TrisCanvas()
{
}
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("TRIS",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("Vince:"+winner,0,60,Graphics.LEFT|Gr aphics.TOP);
}
else
{
g.drawString("Turno: Giocatore"+(whoseturn+1),0,60,Graphics.LEFT|Graphi cs.TOP);
}
}
// Controlla i tasti premuti
protected void keyPressed(int keyCode) {
// Inserisce i simboli X e O
if (keyCode >= 49 && keyCode <= 57 && !FineGioco)
{
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 TrisCanvas();
// Aggiunge il comando perl'uscita
canvas.addCommand(exitCommand);
canvas.setCommandListener(this) ;
IlDisplay.setCurrent(canvas);
}
protected void pauseApp() { }
public void destroyApp(boolean unconditional)
{
}
// cattura gli eventi
public void commandAction(Command c, Displayable d)
{
if (c == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
private void checkForWinner()
{
int i,j;
int numXConsec = 0; // Numenro di X consecutive
int numOConsec = 0; // Numenro di O consecutivi
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)
{
FineGioco(-1);
return;
}
// Controllo orizzonatale
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)
{
FineGioco(0);
return;
}
else if (numOConsec > 2)
{
FineGioco(1);
return;
}
}
// Controllo verticale
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)
{
FineGioco(0);
return;
}
else if (numOConsec > 2)
{
FineGioco(1);
return;
}
}
// Controllo delle diagonali
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')) )
{
FineGioco(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')) )
{
FineGioco(1);
return;
}
}
private void FineGioco(int p)
{
if (p == -1)
winner = "Patta!";
else if (p == 0)
winner = "Giocatore 1!";
else
winner = " Giocatore 2!";
FineGioco = true;
}
}
e l altro e'un codice che mi crea dei pulsanti sto parlando di j2me:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class PulsantiEMenu extends MIDlet implements CommandListener{
private Display display;
private Form fmMain;
private Command cmExit;
private Command cmBack;
private Command cmPrimo;
private Command cmSecondo;
private Command cmTerzo;
private Command cmQuarto;
private Command cmQuinto;
private Command cmSesto;
private Command cmSettimo;
private TextBox tbAction;
public PulsantiEMenu(){
}
public void pauseApp(){
}
public void destroyApp(boolean unconditional){
}
public void startApp(){
display=Display.getDisplay(this);
cmExit=new Command("Exit", Command.EXIT, 1);
cmBack=new Command("Back", Command.BACK, 1);
cmTerzo=new Command("Rubrica", Command.SCREEN, 4);
cmPrimo=new Command("Messaggi", Command.SCREEN, 2);
cmSecondo=new Command("Giochi", Command.SCREEN, 3);
cmQuarto=new Command("Toni", Command.SCREEN, 5);
cmQuinto=new Command("Impostazioni", Command.SCREEN, 6);
cmSesto=new Command("Orologio", Command.SCREEN, 7);
cmSettimo=new Command("Agenda", Command.SCREEN, 8);
fmMain=new Form("Giovedi 04/06/03 10.50 am");
fmMain.addCommand(cmExit);
fmMain.addCommand(cmPrimo);
fmMain.addCommand(cmSecondo);
fmMain.addCommand(cmTerzo);
fmMain.addCommand(cmQuarto);
fmMain.addCommand(cmQuinto);
fmMain.addCommand(cmSesto);
fmMain.addCommand(cmSettimo);
fmMain.setCommandListener(this);
tbAction = new TextBox("TextBox", "Inserisci i tuoi dati", 25, 0);
tbAction.addCommand(cmBack);
tbAction.setCommandListener(this);
display.setCurrent(fmMain);
}
public void commandAction(Command c, Displayable s){
if (c == cmExit){
destroyApp(false);
notifyDestroyed();
}else if (c == cmPrimo || c == cmSecondo || c == cmTerzo || c == cmQuarto || c == cmQuinto || c == cmSesto || c == cmSettimo)
display.setCurrent(tbAction);
else if (c == cmBack)
display.setCurrent(fmMain);
}
}
Ora il mio problema e'quello di fare in modo che al comando di cliccare sul pulsante Giochi mi apra il tris quindi in sintesi vorrei che il codice trism venga inserito nell altro almeno credo sia un metodo,mi potete dare una mano grazie antonio.
Se possibile spiegare passo per passo dove va e se ce'da aggiungere qualcosa grazie