1 17 18 package hangman; 19 20 import java.util.*; 21 22 import Jmc.baseTools.*; 23 import Jmc.baseGui.*; 24 import Jmc.commonGui.*; 25 26 34 35 public class Visit implements base_executableObject_if 36 { 37 41 private WordSource _wordSource = new WordSource(); 42 private base_appl_if pem_appl = null; 43 private base_guiListener pem_cm = null; 44 45 48 private Game _game = new Game(); 49 50 52 public class ChoiceMade implements base_guiListener 53 { 54 public void pcmf_execListener(base_guiObj xParam) throws Exception 55 { 56 59 String res = Visit.this.makeGuess(xParam.pcmf_getName().toCharArray()[0]); 60 61 63 Visit.this.pcmf_buildGuessLine(); 64 65 67 Visit.this.pcmf_updateGuessesLeft(); 68 69 71 xParam.pcmf_hide(); 72 73 75 if (res.equals("lose")) 76 { 77 Visit.this.pcmf_lose(); 78 } 79 else 80 if (res.equals("win")) 81 { 82 Visit.this.pcmf_win(); 83 } 84 } 85 } 86 87 89 public class RestartHangMan implements base_guiListener 90 { 91 public void pcmf_execListener(base_guiObj xParam) throws Exception 92 { 93 95 Visit.this.startGame(); 96 97 99 Visit.this.pcmf_buildGuessLine(); 100 101 103 Visit.this.pcmf_updateGuessesLeft(); 104 105 107 ((base_treeNode)base_registredObject.pcmf_getObjByName("board")).pcmf_unhide(); 108 ((base_treeNode)base_registredObject.pcmf_getObjByName("board")).pcmf_unHideAllChildren(); 109 110 ((base_treeNode)base_registredObject.pcmf_getObjByName("chooseLabel")).pcmf_unhide(); 111 112 ((base_treeNode)base_registredObject.pcmf_getObjByName("you-win")).pcmf_hide(); 113 ((base_treeNode)base_registredObject.pcmf_getObjByName("you-lose")).pcmf_hide(); 114 ((base_treeNode)base_registredObject.pcmf_getObjByName("startLink")).pcmf_hide(); 115 ((base_treeNode)base_registredObject.pcmf_getObjByName("play-againLink")).pcmf_hide(); 116 117 119 if (Visit.this.pem_appl.pcmf_getApplType() == gui_objFactory.SWING) 120 xParam.pcmf_getAppl().pcmf_getGuiObj().pcmf_repaint(); 121 } 122 } 123 124 127 public Visit() 128 { 129 131 this.pem_cm = new ChoiceMade(); 132 133 135 base_registredObject.pcmf_register("RestartHangMan", new RestartHangMan()); 136 } 137 138 public Object pcmf_execObj(Object xObj) 141 { 142 try 143 { 144 146 this.pem_appl = (base_appl_if)xObj; 147 148 150 this.startGame(); 151 152 154 this.pcmf_buildBoard(); 155 156 158 Visit.this.pcmf_buildGuessLine(); 159 160 162 Visit.this.pcmf_updateGuessesLeft(); 163 } 164 catch(Exception e) 165 { 166 168 base_log.pcmf_logException(this.pem_appl.pcmf_getGuiObj().pcmf_getName(), this, e); 169 this.pem_appl.pcmf_close(); 170 } 171 return (null); 172 } 173 174 private ArrayList pem_letters = new ArrayList(); 175 176 178 public void pcmf_buildGuessLine() throws Exception 179 { 180 182 char[] l_letters = this.getGame().getLetters(); 183 184 186 base_form_if l_form = (base_form_if)base_registredObject.pcmf_getObjByName("guessline"); 187 188 191 Iterator l_it = this.pem_letters.iterator(); 192 while (l_it.hasNext()) 193 { 194 base_guiObj l_obj = (base_guiObj)l_it.next(); 195 l_form.pcmf_removeWidget(l_obj); 196 197 199 l_obj.pcmf_delete(); 200 } 201 this.pem_letters.clear(); 202 203 205 for (int i = 0; i < l_letters.length; i++) 206 { 207 209 base_image_if l_image = (base_image_if)base_registredObject.pcmf_getObjByName(String.valueOf(l_letters[i])); 210 211 214 base_image_if l_letter = this.pem_appl.pcmf_getGuiObjFactory().pcmf_createImage(this.pem_appl.pcmf_getApplType(), String.valueOf(l_letters[i]), l_image.pcmf_getValue().toString(), pem_appl); 215 216 218 this.pem_letters.add(l_letter); 219 220 222 l_form.pcmf_addWidget(String.valueOf(l_letters[i]), l_letter); 223 224 226 l_form.pcmf_setGuiObjPosition(l_letter, i, 0, 1, 1, "WEST"); 227 } 228 } 229 230 235 public void pcmf_buildBoard() 236 { 237 239 base_form_if l_form = (base_form_if)base_registredObject.pcmf_getObjByName("board"); 240 241 int x = 0; 242 int y = 0; 243 for (char i = 'a'; i <= 'z'; i++) 244 { 245 247 base_link_if l_link = (base_link_if)this.pem_appl.pcmf_getGuiObjFactory().pcmf_createLink(this.pem_appl.pcmf_getApplType(), "", pem_appl); 248 249 251 l_link.pcmf_addListener(this.pem_cm); 252 l_link.pcmf_setBgColor("transparent"); 253 l_link.pcmf_setIcon((base_image_if)base_registredObject.pcmf_getObjByName(String.valueOf(i))); 254 l_link.pcmf_setToolTip(String.valueOf(i).toUpperCase()); 255 256 258 l_form.pcmf_addWidget(String.valueOf(i).toUpperCase(), l_link); 259 l_form.pcmf_setGuiObjPosition(l_link, x, y, 1, 1, "WEST"); 260 261 x++; 262 if (x == 6) 263 { 264 x = 0; 265 y++; 266 } 267 } 268 } 269 270 273 public void pcmf_updateGuessesLeft() 274 { 275 277 int l_left = this.getGame().getIncorrectGuessesLeft(); 278 279 281 base_text_if l_sc = (base_text_if)base_registredObject.pcmf_getObjByName("$scaffold"); 282 283 285 l_sc.pcmf_setIcon((base_image_if)base_registredObject.pcmf_getObjByName("sc"+l_left)); 286 287 289 base_text_if l_gl = (base_text_if)base_registredObject.pcmf_getObjByName("$guessesLeft"); 290 l_gl.pcmf_setIcon((base_image_if)(base_treeNode)base_registredObject.pcmf_getObjByName("_"+l_left)); 291 292 294 if (this.pem_appl.pcmf_getApplType() == gui_objFactory.SWING) 295 ((base_guiObj)base_registredObject.pcmf_getObjByName("playfield")).pcmf_repaint(); 296 } 297 298 300 public void pcmf_win() 301 { 302 304 this.pcmf_setEnd("you-win"); 305 306 308 if (this.pem_appl.pcmf_getApplType() == gui_objFactory.SWING) 309 ((base_guiObj)base_registredObject.pcmf_getObjByName("playfield")).pcmf_repaint(); 310 } 311 312 314 public void pcmf_lose() 315 { 316 318 base_text_if l_sc = (base_text_if)base_registredObject.pcmf_getObjByName("$scaffold"); 319 320 322 l_sc.pcmf_setIcon((base_image_if)base_registredObject.pcmf_getObjByName("sc")); 323 324 326 this.pcmf_setEnd("you-lose"); 327 328 330 if (this.pem_appl.pcmf_getApplType() == gui_objFactory.SWING) 331 ((base_guiObj)base_registredObject.pcmf_getObjByName("playfield")).pcmf_repaint(); 332 } 333 334 336 public void pcmf_setEnd(String xMessage) 337 { 338 340 ((base_treeNode)base_registredObject.pcmf_getObjByName(xMessage)).pcmf_unhide(); 341 ((base_treeNode)base_registredObject.pcmf_getObjByName("play-againLink")).pcmf_unhide(); 342 ((base_treeNode)base_registredObject.pcmf_getObjByName("board")).pcmf_hide(); 343 ((base_treeNode)base_registredObject.pcmf_getObjByName("chooseLabel")).pcmf_hide(); 344 } 345 346 public String startGame() 347 { 348 _game.start(_wordSource.nextWord()); 349 350 return "newGame"; 351 } 352 353 358 public String makeGuess(char ch) 359 { 360 361 364 if (_game.makeGuess(ch)) 365 return "notDone"; 366 367 return _game.isWin() ? "win" : "lose"; 368 } 369 370 376 377 public Game getGame() 378 { 379 return _game; 380 } 381 } | Popular Tags |