import javax.microedition.lcdui.*; import java.util.Random; class DrawGame extends Canvas{ int[][] buttons = new int[8][4]; boolean algus = true; boolean playerMove = true; int activeButton; int activePlace; int selectedButton; int activeRow; int activeCell; int targetRow; int targetCell; int otherRow; int otherCell; int computerRow; int computerCell; boolean buttonSelected = false; int places; // Nuppude asukohtade massiivi loomine protected void algseis(){ for (int i = 0; i <= 7; i++){ for (int j = 0; j <= 3; j++){ if (i == 7){ buttons[i][j] = 2; } else { buttons[i][j] = 0; } } } Random x = new Random(); int asukoht; int piir = 3; asukoht = (Math.abs(x.nextInt())) % 4; buttons[0][asukoht] = 1; algus = false; } protected void paint(Graphics g){ int bgLum = 173; // Tausta heledus int freeSpace = 10; // Servast tühjaks jäätav ala int spx, spy, stepx, stepy; // Ruutude andmed // Tausta loomine g.setColor(bgLum, bgLum, bgLum); g.fillRect(0, 0, getWidth(), getHeight()); // Ruutude väljastus spy = freeSpace; stepx = (getWidth()-(2*freeSpace)) / 8; stepy = (getHeight()-(2*freeSpace)) / 8; for (int i = 1; i <= 8; i++){ spx = freeSpace; for (int j = 1; j <= 8; j++){ if (((i + j) % 2) == 0){ g.setColor(255, 255, 255); g.fillRect(spx, spy, stepx, stepy); } else { g.setColor(0, 0, 0); g.fillRect(spx, spy, stepx, stepy); } spx = spx + stepx; } spy = spy + stepy; } // Nuppude paigutus if (algus == true){ algseis(); } // Arvuti käik if (playerMove == false){ int compRow, compCell; boolean muudetud = false; int suva; Random x = new Random(); //suva = (Math.abs(x.nextInt())) % 2; if (computerRow % 2 == 0){ // ise +1 suva = (Math.abs(x.nextInt())) % 2; if (suva == 0){ if (buttons[computerRow + 1][computerCell] == 0){ buttons[computerRow + 1][computerCell] = 1; buttons[computerRow][computerCell] = 0; muudetud = true; } } else { if (buttons[computerRow + 1][computerCell + 1] == 0){ buttons[computerRow + 1][computerCell + 1] = 1; buttons[computerRow][computerCell] = 0; muudetud = true; } } if (muudetud == false){ suva = (Math.abs(x.nextInt())) % 2; if (suva == 0){ if (buttons[computerRow - 1][computerCell] == 0){ buttons[computerRow - 1][computerCell] = 1; buttons[computerRow][computerCell] = 0; muudetud = true; } } else { if (buttons[computerRow - 1][computerCell + 1] == 0){ buttons[computerRow - 1][computerCell + 1] = 1; buttons[computerRow][computerCell] = 0; muudetud = true; } } } } else { //ise -1 suva = (Math.abs(x.nextInt())) % 2; if (suva == 0){ if (buttons[computerRow + 1][computerCell] == 0){ buttons[computerRow + 1][computerCell] = 1; buttons[computerRow][computerCell] = 0; muudetud = true; } } else { if (buttons[computerRow + 1][computerCell - 1] == 0){ buttons[computerRow + 1][computerCell - 1] = 1; buttons[computerRow][computerCell] = 0; muudetud = true; } } if (muudetud == false){ suva = (Math.abs(x.nextInt())) % 2; if (suva == 0){ if (buttons[computerRow - 1][computerCell] == 0){ buttons[computerRow - 1][computerCell] = 1; buttons[computerRow][computerCell] = 0; muudetud = true; } } else { if (buttons[computerRow - 1][computerCell - 1] == 0){ buttons[computerRow - 1][computerCell - 1] = 1; buttons[computerRow][computerCell] = 0; muudetud = true; } } } } //buttons[0][asukoht] = 1; if (computerRow < 7 && muudetud == true){ playerMove = true; } } // Nuppude väljastus int lahter; int button = 1; int place = 1; for (int i = 0; i <= 7; i++){ lahter = 0; if (((i) % 2) == 0){ lahter++; } for (int j = 0; j <= 3; j++){ // Üksiku nupu väljastus if (buttons[i][j] == 1){ computerRow = i; computerCell = j; g.setColor(0,0,255); g.fillRect(freeSpace+((lahter)*stepx),freeSpace+((i)*stepy),10,10); } // Aktiveeritud platside väljastus if (buttons[i][j] == 3){ if (place == activePlace){ g.setColor(bgLum, bgLum, bgLum); g.fillRect(freeSpace+((lahter)*stepx),freeSpace+((i)*stepy),stepx,stepy); targetRow = i; targetCell = j; } else { g.setColor(220,220,0); g.fillRect(freeSpace+((lahter)*stepx),freeSpace+((i)*stepy),10,10); otherRow = i; otherCell = j; } place++; } // Nelja nupu väljastus if (buttons[i][j] == 2){ g.setColor(100,150,200); if (button == activeButton){ g.setColor(0,255,0); } if (button == selectedButton){ g.setColor(200,150,100); activeRow = i; activeCell = j; int newRow = i-1; int newCell = j-1; places = 0; if (newRow >= 0 && buttons[newRow][j] == 0){ places = 1; buttons[newRow][j] = 3; } if (newCell >= 0 && buttons[newRow][j-1] == 0){ places = 2; if (i % 2 == 0){ buttons[newRow][j+1] = 3; } else { buttons[newRow][j-1] = 3; } } if (places != 0){ buttonSelected = true; selectedButton = 0; activePlace = 1; place = 1; } button++; } g.fillRect(freeSpace+((lahter)*stepx),freeSpace+((i)*stepy),10,10); button++; } lahter += 2; } } } protected void keyPressed(int kood){ if (buttonSelected == false){ if(getGameAction(kood)==Canvas.LEFT){ activeButton--; } if(getGameAction(kood)==Canvas.RIGHT){ activeButton++; } if (activeButton > 4){ activeButton = 1; } if (activeButton < 1){ activeButton = 4; } if(getGameAction(kood)==Canvas.FIRE){ selectedButton = activeButton; } } else { if(getGameAction(kood)==Canvas.LEFT){ activePlace--; } if(getGameAction(kood)==Canvas.RIGHT){ activePlace++; } if (activePlace > places){ activePlace = 1; } if (activePlace < 1){ activePlace = places; } if(getGameAction(kood)==Canvas.FIRE){ buttonSelected = false; buttons[activeRow][activeCell] = 0; buttons[targetRow][targetCell] = 2; buttons[otherRow][otherCell] = 0; playerMove = false; } } repaint(); } }