#眉標=Ajax #副標=Ajax開發工具(6) #大標=運用GWT開發AJAX遊戲 #作者=文/沈炳宏 ==程式1 =========== projectCreator.cmd -eclipse TicTacToe applicationCreator.cmd -eclipse TicTacToe com.runpc.game.client.TictacToe ================ ==程式2 =========== ================ ==程式3 =========== TicTacToe
================ ==程式4 =========== final Button btnReset = new Button("重新開始"); final Image[][] imgTTT = { { new Image(), new Image(), new Image() }, { new Image(), new Image(), new Image() }, { new Image(), new Image(), new Image() } }; final Grid grid = new Grid(3, 3); final Label lblResult = new Label(); final HorizontalPanel hPanel = new HorizontalPanel(); final HorizontalPanel titlePanel = new HorizontalPanel(); final VerticalPanel vPanel = new VerticalPanel(); … final String imgUrlAny = "./images/any.jpeg"; final String imgUrlMaybeCross = "./images/maybecross.jpeg"; final String imgUrlMaybeNought = "./images/maybenought.jpeg"; final String imgUrlCross = "./images/cross.jpeg"; final String imgUrlNought = "./images/nought.jpeg"; ================ ==程式5 =========== public void onModuleLoad() { for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) grid.setWidget(j, k, imgTTT[j][k]); grid.setStyleName("ttt-Grid"); grid.setWidth("100%"); HTML html = new HTML( "

井字遊戲


"); titlePanel.add(html); hPanel.add(btnReset); hPanel.add(lblResult); hPanel.setWidth("100%"); vPanel.add(titlePanel); vPanel.add(hPanel); vPanel.add(rPanel); RootPanel.get("playArea").add(vPanel); btnReset.addClickListener( new ResetClickListener()); for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { ImageEventListener listener = new ImageEventListener(j, k); imgTTT[j][k].addClickListener(listener); imgTTT[j][k].addMouseListener(listener); } resetPlayArea(); } ================ ==程式6 =========== private class ImageEventListener implements ClickListener, MouseListener { private int tttX, tttY; ImageEventListener(int x, int y) { tttX = x; tttY = y; } public void onClick(Widget sender) { final int ts = tileStatus[tttX][tttY]; if (ts == tsAny) { if (gameStatus == gsCrossPlaying) { imgTTT[tttX][tttY].setUrl(imgUrlCross); tileStatus[tttX][tttY] = tsCross; checkGameStatus(); } else if (gameStatus == gsNoughtPlaying) { imgTTT[tttX][tttY].setUrl(imgUrlNought); tileStatus[tttX][tttY] = tsNought; checkGameStatus(); }}} public void onMouseEnter(Widget sender) { final int ts = tileStatus[tttX][tttY]; if (ts == tsAny) { if (gameStatus == gsCrossPlaying) { imgTTT[tttX][tttY].setUrl(imgUrlMaybeCross); } else if (gameStatus == gsNoughtPlaying) { imgTTT[tttX][tttY].setUrl(imgUrlMaybeNought); }}} public void onMouseLeave(Widget sender) { final int ts = tileStatus[tttX][tttY]; if (ts == tsAny) { imgTTT[tttX][tttY].setUrl(imgUrlAny); }}} ================ ==程式7 =========== private void checkGameStatus() { final int[][] rowWins = { { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 }, { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 } }; final int[][] colWins = { { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 }, { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 }, { 0, 1, 2 }, { 2, 1, 0 } }; boolean success, found; for (int k = 0; k < 8; k++) { success = true; found = false; for (int i = 0; i < 3; i++) { success = success && (tileStatus[rowWins[k][i]][colWins[k][i]] == (gameStatus == gsNoughtPlaying ? tsNought : tsCross)); found = found || (tileStatus[rowWins[k][i]][colWins[k][i]] == (gameStatus == gsNoughtPlaying ? tsNought : tsCross)); } if (success && found) { if (gameStatus == gsNoughtPlaying) { lblResult.setText("圈圈贏了!"); gameStatus = gsNoughtWon; } else { lblResult.setText("叉叉贏了!"); gameStatus = gsCrossWon; } showWinningTiles(); return; } } found = false; for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) if (tileStatus[j][k] == tsAny) found = true; if (!found) { lblResult.setText("和局!"); gameStatus = gsNobodyWon; showWinningTiles(); } if (gameStatus == gsCrossPlaying) { gameStatus = gsNoughtPlaying; lblResult.setText("下圈圈"); } else if (gameStatus == gsNoughtPlaying) { gameStatus = gsCrossPlaying; lblResult.setText("下叉叉"); } } ================ ==程式8 =========== projectCreator.cmd -eclipse hangman applicationCreator.cmd -eclipse hangman com.runpc.game.client. hangman ================ ==程式9 =========== ================ ==程式10 =========== hangman
================ ==程式11 =========== public interface WordsService extends RemoteService { public String[] loadDictionary(); } ================ ==程式12 =========== public interface WordsServiceAsync extends RemoteService { public void loadDictionary( AsyncCallback callback); } ================ ==程式13 =========== public class WordsServiceImpl extends RemoteServiceServlet implements WordsService { public static String[] WORDS; private static String fileName = "us_english"; public String[] loadDictionary() { List list = new ArrayList(); try { FileInputStream fileIn = new FileInputStream(new File(fileName + ".dic")); BufferedReader bufRead = new BufferedReader(new InputStreamReader( fileIn)); try { String line; int i = 0; while ((line = bufRead.readLine()) != null) { line = line.toUpperCase(); line = line.trim(); list.add(line); } } catch (IOException e) { System.out.println("讀取檔案錯誤"); } } catch (FileNotFoundException fnf) { System.out.println("找不到檔案:" + fileName); } WORDS = new String[list.size()]; for (int i = 0 ; i < list.size();i++){ WORDS[i] = (String)list.get(i); } return WORDS; } } ================ ==程式14 ===========   private RootPanel _contentPanel; private Panel _titlePanel; private HorizontalPanel _gamePanel; private Grid _wordPanel; private Grid _lettersPanel; private RadioButton _difficultyEasy; private RadioButton _difficultyMedium; private RadioButton _difficultyHard; private Label _guessesRemainingLabel; private Image _hangmanImage; private Label _gameOverLabel; private Button _playAgainButton; private int _newGameGuesses; private int _guesses; private String _word; private boolean _gameOver; private boolean[] _letterGuessed = new boolean[26]; ================ ==程式15 =========== public void onModuleLoad() { _contentPanel = RootPanel.get("contentPanel"); _contentPanel.setWidth("640"); createTitlePanel(); createGamePanel(); _contentPanel.add(_titlePanel); _newGameGuesses = 6; } private void createTitlePanel() { _titlePanel = new VerticalPanel(); Label welcome = new Label("AJAX吊頸遊戲!"); _titlePanel.add(welcome); _titlePanel.add(new HTML("
")); _difficultyEasy = new RadioButton( "difficulty", "簡單 (猜6次)"); … Button playButton = new Button("開始!"); _titlePanel.add(playButton); playButton.addClickListener(new ClickListener() { public void onClick(Widget sender) { newGame(); } }); } private void createGamePanel() { createWordPanel(); createLettersPanel(); _gamePanel = new HorizontalPanel(); VerticalPanel leftPanel = new VerticalPanel(); leftPanel.setVerticalAlignment( HasVerticalAlignment.ALIGN_MIDDLE); _guessesRemainingLabel = new Label("還可以猜幾次: "); … _hangmanImage = new Image("hangman0.png"); _gamePanel.add(_hangmanImage); _gamePanel.setCellHeight(leftPanel, "340"); } ================ ==程式16 =========== private void newGame() { … updateLettersPanel(); wordsService.loadDictionary( new AsyncCallback() { public void onFailure(Throwable caught) { } public void onSuccess(Object result) { String[] tmpWORDS = (String[]) result; setWord(tmpWORDS); … }});} private void setWord(String[] words){ int count = 0; for (int i = 0 ; i < words.length; i++){ if (!words[i].equals(null)) count++; } int wordIndex = (int) (Math.random() * count); _word = words[wordIndex]; } ================ ==程式17 =========== private void updateLettersPanel() { for (char c = 'A'; c <= 'Z'; c++) { int y = (c - 'A') / 13; int x = (c - 'A') % 13; Widget w;     … } } private void guess(char c) { if (_word.indexOf(c) == -1) { _guesses--; } int y = (c - 'A') / 13; int x = (c - 'A') % 13;     … } private boolean isWin() { boolean result = true; for (int i = 0; i < _word.length(); i++) { … } private void updateGuessesRemaining() { _guessesRemainingLabel.setText( "還可以猜幾次: " + _guesses); … } private void updateWordPanel() { … } private void gameOver() { _gameOver = true; updateLettersPanel(); … } ================