📄 battleshipmidlet.java
字号:
import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.util.Enumeration;import java.util.Hashtable;import javax.microedition.io.Connector;import javax.microedition.io.HttpConnection;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Choice;import javax.microedition.lcdui.ChoiceGroup;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Form;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.StringItem;import javax.microedition.midlet.MIDlet;public class BattleshipMIDlet extends MIDlet implements CommandListener { private Display display; private Form addBoatForm; private StringItem position; private ChoiceGroup size; private ChoiceGroup direction; private StringItem message; private StringItem result; private Command exitCommand; private Command startCommand; private Command nextCommand; private Command addCommand; private Command opponentCommand; private Command fireCommand; private Command continueCommand; private Command newGameCommand; private BattleCanvas addBoatCanvas; private BattleCanvas own; private BattleCanvas opponent; private Form startForm; private Form messageForm; private Form waitForm; private Form gameOverForm; private String webform; private ConnectionWorker worker; private Thread workerThread; // Required methods public void startApp() { display = Display.getDisplay(this); exitCommand = new Command("Exit", Command.EXIT, 1); createStartForm(); createAddBoatForms(); createBattleCanvases(); createMessageForm(); createGameOverForm(); worker = new ConnectionWorker(); workerThread = new Thread(worker); workerThread.start(); waitForm = new Form("Waiting..."); display.setCurrent(startForm); } public void pauseApp() {} public void destroyApp(boolean unconditional) {} // Initialization public void createStartForm() { startForm = new Form("Start"); startForm.setTitle("Welcome"); startForm.append("Start the Battleship Game"); startCommand = new Command("Start", Command.OK, 0); startForm.addCommand(startCommand); startForm.addCommand(exitCommand); startForm.setCommandListener(this); } public void createAddBoatForms() { addBoatCanvas = new BattleCanvas(); addBoatCanvas.setTitle("Select Position"); nextCommand = new Command("Next", Command.OK, 0); addBoatCanvas.addCommand(nextCommand); addBoatCanvas.addCommand(exitCommand); addBoatCanvas.setCommandListener(this); addBoatForm = new Form("Add Boat"); direction = new ChoiceGroup("Direction", Choice.EXCLUSIVE); size = new ChoiceGroup("Size", Choice.EXCLUSIVE); position = new StringItem("", null); addBoatForm.append(direction); addBoatForm.append(size); addBoatForm.append(position); addCommand = new Command("Add", Command.OK, 0); addBoatForm.addCommand(addCommand); addBoatForm.addCommand(exitCommand); addBoatForm.setCommandListener(this); } public void createBattleCanvases() { own = new BattleCanvas(); own.setTitle("Your battleground"); opponent = new BattleCanvas(); opponent.setTitle("Your opponent"); opponentCommand = new Command("Opponent", Command.OK, 0); own.addCommand(opponentCommand); own.addCommand(exitCommand); own.setCommandListener(this); fireCommand = new Command("Fire", Command.OK, 0); opponent.addCommand(fireCommand); opponent.addCommand(exitCommand); opponent.setCommandListener(this); } public void createMessageForm() { messageForm = new Form("Message"); message = new StringItem("", null); messageForm.append(message); continueCommand = new Command("Continue", Command.OK, 0); messageForm.addCommand(continueCommand); messageForm.addCommand(exitCommand); messageForm.setCommandListener(this); } public void createGameOverForm() { gameOverForm = new Form("Game Over"); result = new StringItem("", null); gameOverForm.append(result); newGameCommand = new Command("New Game", Command.OK, 0); gameOverForm.addCommand(newGameCommand); gameOverForm.addCommand(exitCommand); gameOverForm.setCommandListener(this); } // Commands public void commandAction(Command c, Displayable s) { if (c == startCommand) doStart(); else if (c == nextCommand) doNext(); else if (c == addCommand) doAdd(); else if (c == continueCommand) doContinue(); else if (c == opponentCommand) doOpponent(); else if (c == fireCommand) doFire(); else if (c == newGameCommand) doNewGame(); else if (c == exitCommand) notifyDestroyed(); } public void doStart() { connect("setup.faces", null); } public void doNext() { position.setText("Position: " + addBoatCanvas.getString()); display.setCurrent(addBoatForm); } public void doAdd() { Hashtable request = new Hashtable(); request.put("size", size.getString(size.getSelectedIndex())); request.put("direction", direction.getString(direction.getSelectedIndex())); request.put("own", addBoatCanvas.getString()); request.put("form", "setup"); request.put("submit", ""); connect("setup.faces", request); } public void doContinue() { display.setCurrent(addBoatCanvas); } public void doOpponent() { display.setCurrent(opponent); } public void doFire() { Hashtable request = new Hashtable(); request.put("own", own.getString()); request.put("opponent", opponent.getString()); request.put("form", "turn"); request.put("fire", ""); connect("turn.faces", request); } public void doNewGame() { Hashtable request = new Hashtable(); request.put("form", webform); request.put("newgame", ""); connect(webform + ".faces", request); } // Connection public void connect(String url, Hashtable request) { display.setCurrent(waitForm); worker.connect(url, request); } public void connectionCompleted(Hashtable response) { webform = (String) response.get("form"); if (webform.equals("setup")) showSetup(response); else if (webform.equals("turn")) showTurn(response); else if (webform.equals("win")) showGameOver(response); else if (webform.equals("lose")) showGameOver(response); } // Navigation public void showSetup(Hashtable response) { select(size, response, "size"); select(direction, response, "direction"); addBoatCanvas.parse((String) response.get("own")); String msg = (String) response.get("messages.own"); if (msg != null) { message.setText(msg); display.setCurrent(messageForm); return; } display.setCurrent(addBoatCanvas); } public void showGameOver(Hashtable response) { result.setText((String) response.get("result")); display.setCurrent(gameOverForm); } public void showTurn(Hashtable response) { own.parse((String) response.get("own")); opponent.parse((String) response.get("opponent")); display.setCurrent(own); } private static void select(ChoiceGroup group, Hashtable response, String name) { String value = (String) response.get(name); int i = 0; String label; group.deleteAll(); while ((label = (String) response.get(name + ".label." + i)) != null) { group.append(label, null); if (label.equals(value)) group.setSelectedIndex(i, true); i++; } } private class ConnectionWorker implements Runnable { private String url; private String urlPrefix = "http://localhost:8080/phonebattle/"; private Hashtable request; private Hashtable response; private String sessionCookie; private boolean busy = false; public synchronized void run() { try { for (;;) { while (!busy) wait(); try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -