package com.sajilo.games; import java.io.IOException; import java.util.Random; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Font; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import javax.microedition.lcdui.StringItem; import javax.microedition.lcdui.game.GameCanvas; import javax.microedition.midlet.MIDlet; import javax.microedition.media.Manager; import javax.microedition.media.Player; /** *Sajilo bagchal * Sajilo.com has been online since year 2000 * we are a teen protal from Nepal we hope to release the mini computer game for * every traditional games of Nepal * Bagchal was firt time written as a computer game by Mr. Rajhu Nath Dhungel in * C or may be C++ * * We wrote the bagchal again in Macromedia(Adobe) flash as a single player game * * We wrote the server system using PHP and mysql for the multiplayer version of the * Bagchal due to the slow speed of the game we were not able to provide a smooth * game play option. * * This time we have planned the Bagchal mobile edition * CLDC 1.0 * MIDP 2.0 * is the basic requirement of the game the jar size of release * 2007-8-1 is 74 KB which is increasing day by day as we are moving on adding * new resources * * @author justniraj@hotmail.com * Niraj Manandhar is currently working for pcs (http://pcs.com.np) as a senior * Java Developer , in Nepal * @author bocchhi@gmail.com * Rupesh Bocchibaya is currently taking his masters in computer Science from * OSU , okhlama USA * * please contact us if you have some queries and find bug in the code below * you may use the code below for compiling your own game but if you want to distribute * the code then we request you to credit us in what ever form for our work * * * * */ public class BagChal extends MIDlet { WelcomeScreen screen = new WelcomeScreen(false); Display display; HelpWin helpWin; Board board; AboutWin about; Player play; void startGame(int playas) { try { try { play = Manager.createPlayer(getClass().getResourceAsStream("/sound.wav"), "audio/x-wav"); play.setLoopCount(-1); play.start(); } catch (Exception ms) { try { play = Manager.createPlayer(getClass().getResourceAsStream("/sound.mp3"), "audio/x-mp3"); play.setLoopCount(-1); play.start(); } catch (Exception mm) { } } board = new Board(false, playas, this); helpWin = new HelpWin(this); this.about = new AboutWin(this); display.setCurrent(board); } catch (Exception ex) { } } public void startApp() { screen.chal = this; display = Display.getDisplay(this); display.setCurrent(screen); } public void pauseApp() { } public void destroyApp(boolean unconditional) { try { this.play.stop(); } catch (Exception ms) { } this.notifyDestroyed(); } } class Clip { public static final int GOAT = 1; public static final int LION = 2; public static final int EMPTY = 0; public int state; public int xpos; public int ypos; public int arrPos; public boolean selected; public boolean marked; public Board board; boolean justDied = false; private Image subs; Clip(Board board) { this.board = board; } public void drawIt(Graphics g) { Image toDraw = this.board.clipGoat; subs = null; if ((selected || marked) && this.state == EMPTY) { if (selected) { g.setColor(0x0000ff); } else if (marked) { g.setColor(0xfffff00); } g.fillRect(this.xpos - 2, this.ypos - 2, 17, 17); } if ((selected || marked) && this.state == LION) { if (selected) { g.setColor(0x0000ff); subs = this.board.clipTigerSelected; } else if (marked) { g.setColor(0xfffff00); subs = this.board.clipTigerMarked; } g.fillRect(this.xpos - 2, this.ypos - 2, 17, 17); } if ((selected || marked) && this.state == GOAT) { if (selected) { g.setColor(0x0000ff); subs = this.board.clipGoatSelected; } else if (marked) { g.setColor(0xfffff00); subs = this.board.clipGoatMarked; } g.fillRect(this.xpos - 2, this.ypos - 2, 17, 17); } if (!selected && !marked) { if (this.state == LION) { g.setColor(0xff0000); toDraw = this.board.clipTiger; } else if (this.state == GOAT) { g.setColor(0x00ff00); toDraw = this.board.clipGoat; } else if (this.state == EMPTY) { toDraw = null; g.setColor(0xffffff); } } else { toDraw = null; } if (justDied) { toDraw = this.board.justDied; } // g.fillRect(xpos, ypos, 15, 15); if (subs != null) { g.drawImage(subs, xpos + 5, ypos - 3, Graphics.TOP | Graphics.HCENTER); } if (toDraw != null) { g.drawImage(toDraw, xpos + 5, ypos - 3, Graphics.TOP | Graphics.HCENTER); } } } class Board extends GameCanvas implements Runnable, CommandListener { private Image confirmWindow; private boolean invalidMove; private boolean confirmStatus; private boolean isGoatKilled = false; private static final int boardHeight = 154; private static final int boardWidth = 154; private static final int totalGoat = 20; private int currentSelected = 0; private boolean inited = false; private static final int GOAT = 1; private static final int LION = 2; private int TURN; private int[] currentBags = {1, 5, 21, 25}; private int playAs; private Clip[] clip = new Clip[25]; private int[] posArray = {4, 38, 78, 119, 153}; private Image board; private Image invalid; private Image killed; private Image bagWins; private Image goatWins; Image justDied; private int clickCount = 0; private int currentmarked; private int goatPlaced; private int goatKilled = 0; private int Winner = 0; private Command exit; private Command help; private Command about; private Command ok; private Command cancel; private Command SoundONOFF; Image clipTiger; Image clipGoat; Image clipTigerSelected; Image clipGoatSelected; Image clipTigerMarked; Image clipGoatMarked; private BagChal chal; private int[][] arr = new int[41][3]; private boolean stopSequence = false; private int justKilledThat = 0; private int[] randomSteps = new int[25]; private Image buffer; private Graphics offline; Image finalDraw; Image tmp; Graphics g; Image resizedImage; int srcHeight = 180; int srcWidth = 180; int screenHeight; int screenWidth; int ratio; int pos; int x; int y; boolean soundON = true; private void recordBagMove(int from, int to) { randomBags(); for (int counter = 0; counter < this.currentBags.length; counter++) { if (from == this.currentBags[counter]) { this.currentBags[counter] = to; System.out.println(currentBags[0] + " " + currentBags[1] + " " + currentBags[2] + " " + currentBags[3]); return; } } } private void processResult() { } private void goatPlaced() { } private void goatKilled(int killedSpot) { try { this.clip[killedSpot - 1].justDied = true; this.justKilledThat = killedSpot; this.chal.display.vibrate(1); this.stopSequence = true; this.goatKilled++; this.isGoatKilled = true; this.chal.display.vibrate(0); AlertType.CONFIRMATION.playSound(this.chal.display); if (this.goatKilled > 8) { this.Winner = LION; this.processResult(); } } catch (Exception ex) { ex.printStackTrace(); } } private int possibleBagCanEat(int from, int to) { int eatenGoat = 0; //horizontal rules for (int counter = 0; counter < arr.length; counter++) { //front check if (arr[counter][0] == from && arr[counter][2] == to && this.clip[arr[counter][2] - 1].state == Clip.EMPTY && this.clip[arr[counter][0] - 1].state == Clip.LION) { return arr[counter][1]; } if (arr[counter][0] == to && arr[counter][2] == from && this.clip[arr[counter][0] - 1].state == Clip.EMPTY && this.clip[arr[counter][2] - 1].state == Clip.LION) { return arr[counter][1]; } } return eatenGoat; } private int bagCanEat(int from, int to) { int eatenGoat = 0; //horizontal rules for (int counter = 0; counter < arr.length; counter++) { //front check if (arr[counter][0] == from && arr[counter][2] == to && this.clip[arr[counter][1] - 1].state == Clip.GOAT && this.clip[arr[counter][2] - 1].state == Clip.EMPTY && this.clip[arr[counter][0] - 1].state == Clip.LION) { return arr[counter][1]; } if (arr[counter][0] == to && arr[counter][2] == from && this.clip[arr[counter][1] - 1].state == Clip.GOAT && this.clip[arr[counter][0] - 1].state == Clip.EMPTY && this.clip[arr[counter][2] - 1].state == Clip.LION) { return arr[counter][1]; } } return eatenGoat; } private int getSavePoint() { for (int counter = 0; counter < this.currentBags.length; counter++) { int selectedBag = this.currentBags[counter]; for (int hunter = 1; hunter <= 25; hunter++) { int killedGoat = this.bagCanEat(selectedBag, hunter); if (clip[hunter - 1].state != Clip.EMPTY) { killedGoat = 0; } if (killedGoat != 0) { return hunter; } } } return 0; } private boolean isEatenByBag(int boka, boolean check) { for (int counter = 0; counter < this.currentBags.length; counter++) { int selectedBag = this.currentBags[counter]; for (int hunter = 1; hunter <= 25; hunter++) { int killedGoat = 0; if (!check) { killedGoat = this.bagCanEat(selectedBag, hunter); } else { killedGoat = this.possibleBagCanEat(selectedBag, hunter); } if (clip[hunter - 1].state != Clip.EMPTY) { killedGoat = 0; } if (killedGoat == boka) { return true; } } } return false; } private boolean moveGoatAI() { if (this.goatPlaced < Board.totalGoat) { /**sa * * saving your fren matter * */ int savePoint = this.getSavePoint(); /* but you cannot save when you die */ if (savePoint != 0 && !this.isEatenByBag(savePoint, true)) { this.clip[savePoint - 1].state = Clip.GOAT; TURN = LION; goatPlaced++; if (!this.moveBagAI(true)) { this.Winner = GOAT; } return true; } /* why don't you feel the spot */ /*try for the where no bag eats */ for (int counter = 1; counter <= 25; counter++) { if (this.clip[counter - 1].state == Clip.EMPTY) { if (!isEatenByBag(counter, true)) { this.clip[counter - 1].state = Clip.GOAT; TURN = LION; goatPlaced++; if (!this.moveBagAI(true)) { this.Winner = GOAT; } return true; } } //for } //every where it eats lets surrender for (int counter = 1; counter <= 25; counter++) { if (this.clip[counter - 1].state == Clip.EMPTY) { this.clip[counter - 1].state = Clip.GOAT; TURN = LION; goatPlaced++; if (!this.moveBagAI(true)) { this.Winner = GOAT; } return true; } } } else { //try save your friend int safePoint = this.getSavePoint(); for (int counter = 1; counter <= 25; counter++) { if (isValidMoveFor(counter, safePoint)) { if (!isEatenByBag(safePoint, true)) { clip[safePoint - 1].state = Clip.GOAT; clip[counter - 1].state = Clip.EMPTY; clip[counter - 1].selected = false; clip[counter - 1].marked = false; TURN = LION; if (!this.moveBagAI(true)) { this.Winner = GOAT; } return true; } } } for (int counter = 1; counter <= 25; counter++) { int currentGoat = 0; if (this.clip[counter - 1].state == Clip.GOAT) { currentGoat = counter; } else { continue; } for (int hunter = 1; hunter <= 25; hunter++) { if (isValidMoveFor(currentGoat, hunter) && this.clip[hunter - 1].state == Clip.EMPTY) { if (!isEatenByBag(hunter, true)) { clip[hunter - 1].state = Clip.GOAT; clip[currentGoat - 1].state = Clip.EMPTY; clip[currentGoat - 1].selected = false; clip[currentGoat - 1].marked = false; TURN = LION; if (!this.moveBagAI(true)) { this.Winner = GOAT; } return true; } } } } //OK LETS SURRENDER for (int counter = 1; counter <= 25; counter++) { int currentGoat = 0; if (this.clip[counter - 1].state == Clip.GOAT) { currentGoat = counter; } else { continue; } for (int hunter = 1; hunter <= 25; hunter++) { if (isValidMoveFor(currentGoat, hunter) && this.clip[hunter - 1].state == Clip.EMPTY) { clip[hunter - 1].state = Clip.GOAT; clip[currentGoat - 1].state = Clip.EMPTY; clip[currentGoat - 1].selected = false; clip[currentGoat - 1].marked = false; TURN = LION; if (!this.moveBagAI(true)) { this.Winner = GOAT; } return true; } } } } this.Winner = LION; return false; } private boolean moveBagAI(boolean checkWin) { /* why don't you kill first */ for (int counter = 0; counter < this.currentBags.length; counter++) { int selectedBag = this.currentBags[counter]; for (int hunter = 1; hunter <= 25; hunter++) { int killedGoat = this.bagCanEat(selectedBag, hunter); if (clip[hunter - 1].state != Clip.EMPTY) { killedGoat = 0; } if (killedGoat != 0) { if (!checkWin) { clip[hunter - 1].state = Clip.LION; clip[selectedBag - 1].state = Clip.EMPTY; clip[selectedBag - 1].selected = false; clip[selectedBag - 1].marked = false; clip[killedGoat - 1].state = Clip.EMPTY; goatKilled++; clickCount = 0; TURN = GOAT; goatKilled(killedGoat); recordBagMove(selectedBag, hunter); } return true; } } } //cannot kill move only for (int counter = 0; counter < this.currentBags.length; counter++) { int selectedBag = this.currentBags[counter]; for (int hunter = 1; hunter <= 25; hunter++) { if (isValidMoveFor(selectedBag, hunter)) { if (clip[hunter - 1].state != Clip.EMPTY) { continue; } if (!checkWin) { clip[hunter - 1].state = Clip.LION; clip[selectedBag - 1].state = Clip.EMPTY; clip[selectedBag - 1].selected = false; clip[selectedBag - 1].marked = false; recordBagMove(selectedBag, hunter); TURN = GOAT; } return true; } } } /* ok i surrendder */ return false; } private boolean isValidMoveFor(int from, int to) { boolean isValid = false; if (from % 5 != 0) { if (from + 1 == to) { return true; } } if (from <= 20) { if (from + 5 == to) { return true; } } if (from != 1 && from != 6 && from != 11 && from != 21 && from != 16) { if (from - 1 == to) { return true; } } if (from > 5) { if (from - 5 == to) { return true; } } if (from == 3 || from == 7 || from == 9 || from == 13 || from == 17 || from == 19) { if (from + 4 == to || from + 6 == to) { return true; } } if (from == 23 || from == 17 || from == 19 || from == 7 || from == 9 || from == 13) { if (from - 4 == to || from - 6 == to) { return true; } } if (from == 15 || from == 9 || from == 19 || from == 13 || from == 7 || from == 17) { if (from - 6 == to || from + 4 == to) { return true; } } if (from == 11 || from == 7 || from == 17 || from == 13 || from == 9 || from == 19) { if (from + 6 == to || from - 4 == to) { return true; } } if (from == 1 || from == 7 || from == 13 || from == 19 || from == 25) { if (from + 6 == to || from - 6 == to) { return true; } } if (from == 5 || from == 9 || from == 13 || from == 17 || from == 21) { if (from == 5) { return from + 4 == to; } else if (from + 4 == to || from - 4 == to) { return true; } } return isValid; } public void keyPressed(int keyCode) { // int gameaction=this.getGameAction(keyCode); handleEvent(keyCode); // } private void handleEvent(int keycode) { if (Winner != 0) { try { this.chal.play.close(); } catch (Exception ms) { } this.chal.startApp(); } else if (!invalidMove) { if (keycode == Board.KEY_NUM6 ||getGameAction( keycode) ==Canvas.RIGHT) { if (currentSelected != 5 && currentSelected != 10 && currentSelected != 15 && currentSelected != 20 & currentSelected != 25) { this.clip[currentSelected - 1].selected = false; currentSelected++; this.clip[currentSelected - 1].selected = true; repaint(); } } if (keycode == Board.KEY_NUM4 || getGameAction( keycode) ==Canvas.LEFT) { if (currentSelected != 1 && currentSelected != 11 && currentSelected != 16 && currentSelected != 21 & currentSelected != 6) { this.clip[currentSelected - 1].selected = false; currentSelected--; this.clip[currentSelected - 1].selected = true; repaint(); } } if (keycode == Board.KEY_NUM2 || getGameAction( keycode) ==Canvas.UP) { if (currentSelected != 1 && currentSelected != 2 && currentSelected != 3 && currentSelected != 4 & currentSelected != 5) { this.clip[currentSelected - 1].selected = false; currentSelected -= 5; this.clip[currentSelected - 1].selected = true; repaint(); } } if (keycode == Board.KEY_NUM8 || getGameAction( keycode) ==Canvas.DOWN) { if (currentSelected != 21 && currentSelected != 22 && currentSelected != 23 && currentSelected != 24 & currentSelected != 25) { this.clip[currentSelected - 1].selected = false; currentSelected += 5; this.clip[currentSelected - 1].selected = true; repaint(); } } if (keycode == Board.KEY_NUM5 ||getGameAction( keycode) ==Canvas.FIRE) { if (isGoatKilled) { this.isGoatKilled = false; repaint(); } else if (confirmStatus) { return; } else if (!invalidMove) { mainLogic(); repaint(); } } } else if (invalidMove) { this.invalidMove = false; repaint(); } } private void invalid() { try { this.invalidMove = true; this.chal.display.vibrate(1); Thread.sleep(10); AlertType.ERROR.playSound(this.chal.display); this.chal.display.vibrate(0); // repaint(); } catch (InterruptedException ex) { ex.printStackTrace(); } // repaint(); } private void mainLogic() { if (TURN == Board.GOAT) { if (goatPlaced < totalGoat) { if (this.clip[currentSelected - 1].state != Clip.EMPTY) { invalid(); return; } goatPlaced++; this.clip[currentSelected - 1].state = GOAT; clickCount = 0; TURN = Board.LION; if (playAs == GOAT) { if (!this.moveBagAI(false)) { this.Winner = GOAT; return; } } } else { if (clickCount == 0) { if (clip[currentSelected - 1].state != Clip.GOAT) { this.invalid(); return; } clip[currentSelected - 1].selected = false; clip[currentSelected - 1].marked = true; currentmarked = currentSelected; clickCount = 1; return; } else if (clickCount == 1) { if (currentSelected == currentmarked) { clip[currentSelected - 1].marked = false; clickCount = 0; return; } if (clip[currentSelected - 1].state != Clip.EMPTY) { invalid(); return; } if (isValidMoveFor(currentmarked, currentSelected)) { clip[currentSelected - 1].state = Clip.GOAT; clip[currentmarked - 1].state = Clip.EMPTY; clip[currentmarked - 1].selected = false; clip[currentmarked - 1].marked = false; clickCount = 0; TURN = LION; if (playAs == GOAT) { if (!this.moveBagAI(false)) { this.Winner = GOAT; return; } } return; } invalid(); return; } } return; } if (TURN == LION) { if (clickCount == 0) { if (clip[currentSelected - 1].state != LION) { invalid(); return; } clip[currentSelected - 1].selected = false; clip[currentSelected - 1].marked = true; currentmarked = currentSelected; clickCount = 1; return; } else if (clickCount == 1) { if (currentSelected == currentmarked) { clip[currentSelected - 1].marked = false; clickCount = 0; return; } if (clip[currentSelected - 1].state != Clip.EMPTY) { invalid(); return; } int goatPlacedAt = bagCanEat(currentmarked, currentSelected); if (goatPlacedAt != 0) { clip[currentSelected - 1].state = Clip.LION; clip[currentmarked - 1].state = Clip.EMPTY; clip[currentmarked - 1].selected = false; clip[currentmarked - 1].marked = false; clip[goatPlacedAt - 1].state = Clip.EMPTY; goatKilled++; clickCount = 0; TURN = GOAT; goatKilled(goatPlacedAt); recordBagMove(currentmarked, currentSelected); if (playAs == LION) { moveGoatAI(); } return; } if (isValidMoveFor(currentmarked, currentSelected)) { clip[currentSelected - 1].state = Clip.LION; clip[currentmarked - 1].state = Clip.EMPTY; clip[currentmarked - 1].selected = false; clip[currentmarked - 1].marked = false; clickCount = 0; recordBagMove(currentmarked, currentSelected); TURN = GOAT; if (playAs == LION) { moveGoatAI(); } return; } invalid(); return; } } } public void paint(Graphics graph) { if (!inited) { return; } graph.setColor(0x000000); graph.fillRect(0, 0, screenWidth + 500, screenHeight + 500); this.offline.setColor(0x000000); this.offline.fillRect(0, 0, this.getWidth(), this.getHeight()); this.offline.drawImage(board, 10, 10, Graphics.LEFT | Graphics.TOP); drawPos(this.offline); if (this.stopSequence) { finalDraw = this.resizeImage(buffer); graph.drawImage(finalDraw, 0, 0, Graphics.TOP | Graphics.LEFT); return; } if (invalidMove) { this.offline.drawImage(invalid, boardWidth / 2 + 10, boardHeight / 2 - 10 + 10, Graphics.HCENTER | Graphics.TOP); } if (isGoatKilled) { this.offline.drawImage(killed, boardWidth / 2 + 10, boardHeight / 2 - 10 + 10, Graphics.HCENTER | Graphics.TOP); } if (confirmStatus) { this.offline.drawImage(confirmWindow, boardWidth / 2 + 10, boardHeight / 2 - 10 + 10, Graphics.HCENTER | Graphics.TOP); } if (this.Winner == LION) { this.offline.drawImage(bagWins, boardWidth / 2 + 10, boardHeight / 2 - 10 + 10, Graphics.HCENTER | Graphics.TOP); } if (this.Winner == GOAT) { this.offline.drawImage(goatWins, boardWidth / 2 + 10, boardHeight / 2 - 10 + 10, Graphics.HCENTER | Graphics.TOP); } finalDraw = this.resizeImage(buffer); graph.drawImage(finalDraw, 0, 0, Graphics.TOP | Graphics.LEFT); } private Image resizeImage(Image src) { g = tmp.getGraphics(); g.setColor(0x000000); ratio = (srcWidth << 16) / screenWidth; pos = ratio / 2; g.fillRect(0, 0, screenWidth, screenHeight); //Horizontal Resize for (x = 0; x < screenWidth; x++) { g.setClip(x, 0, 1, srcHeight); g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP); pos += ratio; } g = resizedImage.getGraphics(); g.setColor(0x000000); g.fillRect(0, 0, screenWidth, screenHeight); ratio = (srcHeight << 16) / screenHeight; pos = ratio / 2; //Vertical resize for (y = 0; y < screenHeight; y++) { g.setClip(0, y, screenWidth, 1); g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP); pos += ratio; } return resizedImage; } //resize image private void drawPos(Graphics g) { if (g == null) { return; } for (int counter = 0; counter < clip.length; counter++) { clip[counter].drawIt(g); } } private void init() throws IOException { board = Image.createImage("/1.png"); invalid = Image.createImage("/invalid.png"); killed = Image.createImage("/eaten.png"); confirmWindow = Image.createImage("/confirm.png"); bagWins = Image.createImage("/tigerwins.png"); goatWins = Image.createImage("/goatWins.png"); this.clipGoat = Image.createImage("/goat1.png"); this.clipTiger = Image.createImage("/tiger2.png"); this.justDied = Image.createImage("/goatKillled.png"); this.clipGoatSelected = Image.createImage("/goat1selected.png"); this.clipTigerSelected = Image.createImage("/tiger2selected.png"); this.clipGoatMarked = Image.createImage("/goat1marked.png"); this.clipTigerMarked = Image.createImage("/tiger2marked.png"); this.buffer = Image.createImage(180, 180); this.offline = this.buffer.getGraphics(); arr[0] = new int[]{1, 2, 3}; arr[1] = new int[]{2, 3, 4}; arr[2] = new int[]{3, 4, 5}; arr[3] = new int[]{6, 7, 8}; arr[4] = new int[]{7, 8, 9}; arr[5] = new int[]{8, 9, 10}; arr[6] = new int[]{11, 12, 13}; arr[7] = new int[]{12, 13, 14}; arr[8] = new int[]{13, 14, 15}; arr[9] = new int[]{16, 17, 18}; arr[10] = new int[]{17, 18, 19}; arr[11] = new int[]{18, 19, 20}; arr[12] = new int[]{21, 22, 23}; arr[13] = new int[]{22, 23, 24}; arr[14] = new int[]{23, 24, 25}; //verticals arr[15] = new int[]{1, 6, 11}; arr[16] = new int[]{6, 11, 16}; arr[17] = new int[]{11, 16, 21}; arr[18] = new int[]{2, 7, 12}; arr[19] = new int[]{7, 12, 17}; arr[20] = new int[]{12, 17, 22}; arr[21] = new int[]{3, 8, 13}; arr[22] = new int[]{8, 13, 18}; arr[23] = new int[]{13, 18, 23}; arr[24] = new int[]{4, 9, 14}; arr[25] = new int[]{9, 14, 19}; arr[26] = new int[]{14, 19, 24}; arr[27] = new int[]{5, 10, 15}; arr[28] = new int[]{10, 15, 20}; arr[29] = new int[]{15, 20, 25}; //back slashes arr[30] = new int[]{11, 17, 23}; arr[31] = new int[]{1, 7, 13}; arr[32] = new int[]{7, 13, 19}; arr[33] = new int[]{13, 19, 25}; arr[34] = new int[]{3, 9, 15}; //front slash arr[35] = new int[]{11, 7, 3}; arr[36] = new int[]{5, 9, 13}; arr[37] = new int[]{9, 13, 17}; arr[38] = new int[]{13, 17, 21}; arr[39] = new int[]{15, 19, 23}; // arr[40] = new int[]{19, 18, 17}; this.exit = new Command("Leave", Command.EXIT, 1); this.help = new Command("Help", Command.HELP, 1); this.about = new Command("About", Command.ITEM, 2); this.ok = new Command("YES", Command.OK, 2); this.cancel = new Command("Cancel", Command.CANCEL, 1); this.SoundONOFF = new Command("Sound OFF/ON", Command.ITEM, 3); this.addCommand(this.exit); this.addCommand(this.help); this.addCommand(this.about); this.addCommand(this.SoundONOFF); this.setCommandListener(this); int hori = 0; int vert = 0; for (int counter = 0; counter < clip.length; counter++) { clip[counter] = new Clip(this); clip[counter].arrPos = counter + 1; clip[counter].xpos = posArray[hori]; clip[counter].ypos = posArray[vert]; hori++; if (hori == 5) { hori = 0; vert++; } if (vert == 5) { vert = 0; } if ((clip[counter].arrPos == 1) || (clip[counter].arrPos == 5) || (clip[counter].arrPos == 21) || (clip[counter].arrPos == 25)) { clip[counter].state = Clip.LION; } else { clip[counter].state = Clip.EMPTY; } } if (this.playAs == GOAT) { clip[1].selected = true; this.currentSelected = 2; } else { clip[0].selected = true; this.currentSelected = 1; } this.inited = true; new Thread(this).start(); } private void randomBags() { java.util.Random rand = new Random(); int random1 = Math.abs(rand.nextInt()) % 4; int temp = this.currentBags[random1]; int random2 = Math.abs(rand.nextInt()) % 4; this.currentBags[random1] = this.currentBags[random2]; this.currentBags[random2] = temp; } public Board(boolean painter, int playas, BagChal chal) { super(painter); this.playAs = playas; this.chal = chal; try { this.init(); this.TURN = this.playAs; screenWidth = this.getWidth(); screenHeight = this.getHeight(); tmp = Image.createImage(screenWidth, 180); resizedImage = Image.createImage(screenWidth, screenHeight); } catch (Exception ms) { ms.printStackTrace(); } } public void run() { try { while (true) { this.repaint(); if (!stopSequence) { Thread.sleep(200); } else { Thread.sleep(700); this.stopSequence = false; this.clip[this.justKilledThat - 1].justDied = false; Thread.sleep(200); } } } catch (Exception ms) { } } public void commandAction(Command arg0, Displayable arg1) { if (arg0.equals(this.cancel)) { this.confirmStatus = false; this.removeCommand(this.ok); this.removeCommand(this.cancel); this.addCommand(exit); this.addCommand(about); this.addCommand(help); this.addCommand(this.SoundONOFF); return; } if (arg0.equals(this.ok)) { this.chal.destroyApp(false); } if (arg0.equals(this.exit)) { if (!this.confirmStatus) { this.removeCommand(exit); this.removeCommand(about); this.removeCommand(help); this.removeCommand(this.SoundONOFF); this.addCommand(this.ok); this.addCommand(this.cancel); this.confirmStatus = true; repaint(); return; } } if (arg0.equals(this.help)) { this.chal.display.setCurrent(this.chal.helpWin); } if (arg0.equals(this.about)) { this.chal.display.setCurrent(this.chal.about); } if (arg0.equals(this.SoundONOFF)) { try { if (this.soundON) { this.soundON = false; this.chal.play.stop(); } else { this.soundON = true; this.chal.play.start(); } } catch (Exception ms) { } } } } class WelcomeScreen extends GameCanvas implements Runnable, CommandListener { public int mode; public static final int GOAT = 1; public static final int LION = 2; public BagChal chal; Command cmm = new Command("Play Tiger", Command.OK, 1); Command cmm2 = new Command("Play Goat", Command.CANCEL, 1); public WelcomeScreen(boolean ispainted) { super(ispainted); this.addCommand(cmm); this.addCommand(cmm2); this.setCommandListener(this); } public void keyReleased(int keyCode) { if (keyCode == Canvas.KEY_NUM8) { this.mode = GOAT; this.chal.startGame(this.mode); } else if (keyCode == Canvas.KEY_NUM5) { this.mode = LION; this.chal.startGame(this.mode); } } public void paint(Graphics g) { g.setColor(0x000000); g.fillRect(0, 0, 400, 400); g.setColor(0xffffff); String str = "Welcome to Bagchal"; Font font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE); g.setFont(font); g.drawString(str, font.stringWidth(str) / 2 + 3, 10, Graphics.HCENTER | Graphics.TOP); g.setColor(0xff0000); font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM); g.setFont(font); String message = "http://sajilo.com"; g.drawString(message, font.stringWidth(message) / 2 + 3, 30, Graphics.HCENTER | Graphics.TOP); g.setColor(0x00ff00); message = "Press 5 : Tiger "; g.drawString(message, font.stringWidth(message) / 2 + 3, 60, Graphics.HCENTER | Graphics.TOP); message = "Press 8 : Goat "; g.drawString(message, font.stringWidth(message) / 2 + 3, 80, Graphics.HCENTER | Graphics.TOP); } public void run() { } public void commandAction(Command arg0, Displayable arg1) { if (arg0.equals(this.cmm)) { this.mode = LION; this.chal.startGame(this.mode); } else { this.mode = GOAT; this.chal.startGame(this.mode); } } } class HelpWin extends Form implements CommandListener { BagChal chal; Command ok; HelpWin(BagChal chal) { super("Bagchal help"); this.chal = chal; this.box = new StringItem("Help", "\n2 up\n8 down\n4 left\n6 right \n5 Select and place \n5 on selected one to unselect"); this.append(box); this.append("\n"); this.append("Bagchal is a game from Nepal" + " please visit sajilo.com for more infomation and rules about the game\n\n" + ""); this.ok = new Command("OK", Command.OK, 1); this.addCommand(ok); setCommandListener(this); } StringItem box; public void commandAction(Command arg0, Displayable arg1) { this.chal.display.setCurrent(this.chal.board); } } class AboutWin extends Form implements CommandListener { BagChal chal; Command ok; AboutWin(BagChal chal) { super("About Bagchal"); this.chal = chal; this.append("This game is developed by Niraj\n email: regniraj@gmail.com \n\n" + "\"I made this game for my Moti , I love you maya\" \n" + "Thank you Niran Manandhar My brother, Rupesh, Prateek for their support \n\n" + "Please contact me if you want to advertise in this game \n\n " + "Multiplayer(2 Player) version of this game is available for the phones with BlueTooth" + " please visit http://sajilo.com for more info "); this.ok = new Command("OK", Command.OK, 1); this.addCommand(ok); setCommandListener(this); } StringItem box; public void commandAction(Command arg0, Displayable arg1) { this.chal.display.setCurrent(this.chal.board); } }