001: /*
002: * $RCSfile: FourByFour.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.4 $
041: * $Date: 2007/02/09 17:21:37 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.four_by_four;
046:
047: import java.applet.Applet;
048: import java.awt.*;
049: import java.awt.event.*;
050: import java.io.*;
051: import java.net.*;
052: import javax.media.j3d.*;
053: import javax.vecmath.*;
054: import com.sun.j3d.utils.universe.SimpleUniverse;
055: import com.sun.j3d.utils.applet.MainFrame;
056: import org.jdesktop.j3d.examples.Resources;
057:
058: /**
059: * Class FourByFour
060: *
061: * Description: High level class for the game FourByFour
062: *
063: * Version: 1.2
064: *
065: */
066: public class FourByFour extends Applet implements ActionListener {
067:
068: // To write scores to scores file
069: private static final boolean writeScoresFile = false;
070:
071: String host; // Host from which this applet came from
072: int port; // Port number for writing high scores
073: Image backbuffer2D; // Backbuffer image used for 2D double buffering
074: int width, height; // Size of the graphics window in pixels
075: int score; // Final game score
076: int level_weight; // Weighting factor for skill level
077: int move_weight; // Weighting factor for number of moves to win
078: int time_weight; // Weighting factor for amount of time it took to win
079: int skill_level; // Skill level, 0 - 4
080: Canvas2D canvas2D; // 2D rendering canvas
081: Canvas3D canvas3D; // 3D rendering canvas
082: Board board; // Game board object
083: Panel b_container; // Container to hold the buttons
084: Panel c_container; // Container to hold the canvas
085: Panel l_container; // Container to hold the labels
086: Panel skill_panel; // Panel to hold skill levels
087: Panel instruct_panel; // Panel to hold instructions
088: Panel winner_panel; // Panel to hold winner announcement
089: Panel high_panel; // Panel to hold high scores
090: Button instruct_button; // Instructions button
091: Button new_button; // New Game button
092: Button skill_button; // Skill Level button
093: Button high_button; // High Scores button
094: Button undo_button; // Undo Move button
095: Label skill_label; // Label on skill panel
096: Label winner_label; // Label on winner panel
097: Label winner_score_label; // Score label on winner panel
098: Label winner_name_label; // Name label on winner panel
099: Label winner_top_label; // Top 20 label on winner panel
100: Label high_label; // High score label
101: Label high_places[]; // Labels to hold places
102: Label high_names[]; // Labels to hold names
103: Label high_scores[]; // Labels to hold scores
104: TextArea instruct_text; // TextArea object that holds instructions
105: TextArea high_text; // TextArea object that holds top 20 scores
106: TextField winner_name; // TextField object that holds winner's name
107: Button instruct_return_button; // Return button for instruction panel
108: Button skill_return_button; // Return button for skill level panel
109: Button winner_return_button; // Return button for winner panel
110: Button high_return_button; // Return button for high scores panel
111: CheckboxGroup group; // CheckboxGroup object for skill level panel
112: InputStream inStream; // Input stream for reading instructions and high scores
113: static boolean appletFlag = true; // Applet flag
114: boolean winner_flag = false; // Winner flag
115: byte text[]; // Temporary storage area for reading instructions file
116: byte outText[]; // Temporary storage area for writing high scores file
117: String textString; // Storage area for instructions
118: String scoresString; // String used for writing high scores file
119: int places[]; // Storage area for high score places
120: int scores[]; // Storage area for high score scores
121: String names[]; // Storage area for high score names
122: Positions positions; // Positions object, used to render player positions
123:
124: private SimpleUniverse universe = null;
125:
126: /**
127: * Initialization
128: */
129: public void init() {
130:
131: // Set the port number.
132: port = 4111;
133:
134: // Set the graphics window size.
135: width = 350;
136: height = 350;
137:
138: // Set the weighting factors used for scoring.
139: level_weight = 1311;
140: move_weight = 111;
141: time_weight = 1000;
142:
143: // Create the "base" color for the AWT components.
144: setBackground(new Color(200, 200, 200));
145:
146: // Read the instructions file.
147: if (appletFlag) {
148:
149: // Get the host from which this applet came.
150: host = getCodeBase().getHost();
151:
152: try {
153: URL instrURL = Resources
154: .getResource("four_by_four/instructions.txt");
155: inStream = new BufferedInputStream((instrURL)
156: .openStream(), 8192);
157: text = new byte[5000];
158: int character = inStream.read();
159: int count = 0;
160: while (character != -1) {
161: text[count++] = (byte) character;
162: character = inStream.read();
163: }
164: textString = new String(text);
165: inStream.close();
166: } catch (Exception e) {
167: System.out.println("Error: " + e.toString());
168: }
169: } else {
170:
171: try {
172: URL instrURL = Resources
173: .getResource("four_by_four/instructions.txt");
174: inStream = new BufferedInputStream((instrURL)
175: .openStream(), 8192);
176: text = new byte[5000];
177: int character = inStream.read();
178: int count = 0;
179: while (character != -1) {
180: text[count++] = (byte) character;
181: character = inStream.read();
182: }
183: textString = new String(text);
184: inStream.close();
185: } catch (Exception e) {
186: System.out.println("Error: " + e.toString());
187: }
188: }
189:
190: // Read the high-scores file.
191: places = new int[20];
192: scores = new int[20];
193: names = new String[20];
194: if (appletFlag) {
195: try {
196: URL scoreURL = Resources
197: .getResource("four_by_four/scores.txt");
198: inStream = new BufferedInputStream((scoreURL)
199: .openStream(), 8192);
200: Reader read = new BufferedReader(new InputStreamReader(
201: inStream));
202: StreamTokenizer st = new StreamTokenizer(read);
203: st.whitespaceChars(32, 44);
204: st.eolIsSignificant(false);
205:
206: int count = 0;
207: int token = st.nextToken();
208: boolean scoreFlag = true;
209: String string;
210: while (count < 20) {
211: places[count] = (int) st.nval;
212: string = new String("");
213: token = st.nextToken();
214: while (token == StreamTokenizer.TT_WORD) {
215: string += st.sval;
216: string += " ";
217: token = st.nextToken();
218: }
219: names[count] = string;
220: scores[count] = (int) st.nval;
221: token = st.nextToken();
222: count++;
223: }
224: inStream.close();
225: } catch (Exception e) {
226: System.out.println("Error: " + e.toString());
227: }
228: } else {
229: try {
230: URL scoreURL = Resources
231: .getResource("four_by_four/scores.txt");
232: inStream = new BufferedInputStream((scoreURL)
233: .openStream(), 8192);
234: Reader read = new BufferedReader(new InputStreamReader(
235: inStream));
236: StreamTokenizer st = new StreamTokenizer(read);
237: st.whitespaceChars(32, 44);
238: st.eolIsSignificant(false);
239:
240: int count = 0;
241: int token = st.nextToken();
242: boolean scoreFlag = true;
243: String string;
244: while (count < 20) {
245: places[count] = (int) st.nval;
246: string = new String("");
247: token = st.nextToken();
248: while (token == StreamTokenizer.TT_WORD) {
249: string += st.sval;
250: string += " ";
251: token = st.nextToken();
252: }
253: names[count] = string;
254: scores[count] = (int) st.nval;
255: token = st.nextToken();
256: count++;
257: }
258: inStream.close();
259: } catch (Exception e) {
260: System.out.println("Error: " + e.toString());
261: }
262: }
263:
264: // The positions object sets up the switch nodes which
265: // control the rendering of the player's positions.
266: positions = new Positions();
267:
268: // Create the game board object which is responsible
269: // for keeping track of the moves on the game board
270: // and determining what move the computer should make.
271: board = new Board(this , positions, width, height);
272: positions.setBoard(board);
273:
274: // Create a 2D graphics canvas.
275: canvas2D = new Canvas2D(board);
276: canvas2D.setSize(width, height);
277: canvas2D.setLocation(width + 10, 5);
278: canvas2D.addMouseListener(canvas2D);
279: board.setCanvas(canvas2D);
280:
281: // Create the 2D backbuffer
282: backbuffer2D = createImage(width, height);
283: canvas2D.setBuffer(backbuffer2D);
284:
285: // Create a 3D graphics canvas.
286: canvas3D = new Canvas3D(SimpleUniverse
287: .getPreferredConfiguration());
288: canvas3D.setSize(width, height);
289: canvas3D.setLocation(5, 5);
290:
291: // Create the scene branchgroup.
292: BranchGroup scene3D = createScene3D();
293:
294: // Create a universe with the Java3D universe utility.
295: universe = new SimpleUniverse(canvas3D);
296: universe.addBranchGraph(scene3D);
297:
298: // Use parallel projection.
299: View view = universe.getViewer().getView();
300: view.setProjectionPolicy(View.PARALLEL_PROJECTION);
301:
302: // Set the universe Transform3D object.
303: TransformGroup tg = universe.getViewingPlatform()
304: .getViewPlatformTransform();
305: Transform3D transform = new Transform3D();
306: transform.set(65.f, new Vector3f(0.0f, 0.0f, 400.0f));
307: tg.setTransform(transform);
308:
309: // Create the canvas container.
310: c_container = new Panel();
311: c_container.setSize(720, 360);
312: c_container.setLocation(0, 0);
313: c_container.setVisible(true);
314: c_container.setLayout(null);
315: add(c_container);
316:
317: // Add the 2D and 3D canvases to the container.
318: c_container.add(canvas2D);
319: c_container.add(canvas3D);
320:
321: // Turn off the layout manager, widgets will be sized
322: // and positioned explicitly.
323: setLayout(null);
324:
325: // Create the button container.
326: b_container = new Panel();
327: b_container.setSize(720, 70);
328: b_container.setLocation(0, 360);
329: b_container.setVisible(true);
330: b_container.setLayout(null);
331:
332: // Create the buttons.
333: instruct_button = new Button("Instructions");
334: instruct_button.setSize(135, 25);
335: instruct_button.setLocation(10, 10);
336: instruct_button.setVisible(true);
337: instruct_button.addActionListener(this );
338:
339: new_button = new Button("New Game");
340: new_button.setSize(135, 25);
341: new_button.setLocation(150, 10);
342: new_button.setVisible(true);
343: new_button.addActionListener(this );
344:
345: undo_button = new Button("Undo Move");
346: undo_button.setSize(135, 25);
347: undo_button.setLocation(290, 10);
348: undo_button.setVisible(true);
349: undo_button.addActionListener(this );
350:
351: skill_button = new Button("Skill Level");
352: skill_button.setSize(135, 25);
353: skill_button.setLocation(430, 10);
354: skill_button.setVisible(true);
355: skill_button.addActionListener(this );
356:
357: high_button = new Button("High Scores");
358: high_button.setSize(135, 25);
359: high_button.setLocation(570, 10);
360: high_button.setVisible(true);
361: high_button.addActionListener(this );
362:
363: b_container.add(new_button);
364: b_container.add(undo_button);
365: b_container.add(skill_button);
366: b_container.add(high_button);
367: b_container.add(instruct_button);
368:
369: // Add the button container to the applet.
370: add(b_container);
371:
372: // Create the "Skill Level" dialog box.
373: skill_panel = new Panel();
374: skill_panel.setSize(400, 300);
375: skill_panel.setLocation(200, 20);
376: skill_panel.setLayout(null);
377:
378: skill_label = new Label("Pick your skill level:");
379: skill_label.setSize(200, 25);
380: skill_label.setLocation(25, 20);
381: skill_label.setVisible(true);
382: skill_panel.add(skill_label);
383:
384: group = new CheckboxGroup();
385: Checkbox skill_1 = new Checkbox("Babe in the Woods ",
386: group, false);
387: Checkbox skill_2 = new Checkbox("Walk and Chew Gum ",
388: group, false);
389: Checkbox skill_3 = new Checkbox("Jeopardy Contestant ",
390: group, false);
391: Checkbox skill_4 = new Checkbox("Rocket Scientist ",
392: group, false);
393: Checkbox skill_5 = new Checkbox("Be afraid, be very afraid",
394: group, true);
395: skill_1.setSize(170, 25);
396: skill_1.setLocation(80, 60);
397: skill_1.setVisible(true);
398: skill_2.setSize(170, 25);
399: skill_2.setLocation(80, 100);
400: skill_2.setVisible(true);
401: skill_3.setSize(170, 25);
402: skill_3.setLocation(80, 140);
403: skill_3.setVisible(true);
404: skill_4.setSize(170, 25);
405: skill_4.setLocation(80, 180);
406: skill_4.setVisible(true);
407: skill_5.setSize(170, 25);
408: skill_5.setLocation(80, 220);
409: skill_5.setVisible(true);
410: skill_return_button = new Button("Return");
411: skill_return_button.setSize(120, 25);
412: skill_return_button.setLocation(300, 370);
413: skill_return_button.setVisible(false);
414: skill_return_button.addActionListener(this );
415: skill_panel.add(skill_1);
416: skill_panel.add(skill_2);
417: skill_panel.add(skill_3);
418: skill_panel.add(skill_4);
419: skill_panel.add(skill_5);
420: skill_panel.setVisible(false);
421: add(skill_return_button);
422: add(skill_panel);
423:
424: // Create the "Instructions" panel.
425: instruct_return_button = new Button("Return");
426: instruct_return_button.setLocation(300, 370);
427: instruct_return_button.setSize(120, 25);
428: instruct_return_button.setVisible(false);
429: instruct_return_button.addActionListener(this );
430: instruct_text = new TextArea(textString, 100, 200,
431: TextArea.SCROLLBARS_VERTICAL_ONLY);
432: instruct_text.setSize(715, 350);
433: instruct_text.setLocation(0, 0);
434: instruct_text.setVisible(false);
435: add(instruct_text);
436:
437: add(instruct_return_button);
438:
439: high_panel = new Panel();
440: high_panel.setSize(715, 350);
441: high_panel.setLocation(0, 0);
442: high_panel.setVisible(false);
443: high_panel.setLayout(null);
444:
445: high_label = new Label("High Scores");
446: high_label.setLocation(330, 5);
447: high_label.setSize(200, 30);
448: high_label.setVisible(true);
449: high_panel.add(high_label);
450:
451: high_places = new Label[20];
452: high_names = new Label[20];
453: high_scores = new Label[20];
454: for (int i = 0; i < 20; i++) {
455: high_places[i] = new Label(Integer.toString(i + 1));
456: high_places[i].setSize(20, 30);
457: high_places[i].setVisible(true);
458: high_names[i] = new Label(names[i]);
459: high_names[i].setSize(150, 30);
460: high_names[i].setVisible(true);
461: high_scores[i] = new Label(Integer.toString(scores[i]));
462: high_scores[i].setSize(150, 30);
463: high_scores[i].setVisible(true);
464: if (i < 10) {
465: high_places[i].setLocation(70, i * 30 + 40);
466: high_names[i].setLocation(100, i * 30 + 40);
467: high_scores[i].setLocation(260, i * 30 + 40);
468: } else {
469: high_places[i].setLocation(425, (i - 10) * 30 + 40);
470: high_names[i].setLocation(455, (i - 10) * 30 + 40);
471: high_scores[i].setLocation(615, (i - 10) * 30 + 40);
472: }
473: high_panel.add(high_places[i]);
474: high_panel.add(high_names[i]);
475: high_panel.add(high_scores[i]);
476: }
477: high_return_button = new Button("Return");
478: high_return_button.setSize(120, 25);
479: high_return_button.setLocation(300, 370);
480: high_return_button.setVisible(false);
481: high_return_button.addActionListener(this );
482: add(high_return_button);
483: add(high_panel);
484:
485: // Create the "Winner" dialog box
486: winner_panel = new Panel();
487: winner_panel.setLayout(null);
488: winner_panel.setSize(600, 500);
489: winner_panel.setLocation(0, 0);
490: winner_return_button = new Button("Return");
491: winner_return_button.setSize(120, 25);
492: winner_return_button.setLocation(300, 360);
493: winner_return_button.addActionListener(this );
494: winner_panel.add(winner_return_button);
495: winner_label = new Label("");
496: winner_label.setSize(200, 30);
497: winner_label.setLocation(270, 110);
498: winner_score_label = new Label("");
499: winner_score_label.setSize(200, 30);
500: winner_top_label = new Label("You have a score in the top 20.");
501: winner_top_label.setSize(200, 25);
502: winner_top_label.setLocation(260, 185);
503: winner_top_label.setVisible(false);
504: winner_name_label = new Label("Enter your name here:");
505: winner_name_label.setSize(150, 25);
506: winner_name_label.setLocation(260, 210);
507: winner_name_label.setVisible(false);
508: winner_name = new TextField("");
509: winner_name.setSize(200, 30);
510: winner_name.setLocation(260, 240);
511: winner_name.setVisible(false);
512: winner_panel.add(winner_label);
513: winner_panel.add(winner_score_label);
514: winner_panel.add(winner_top_label);
515: winner_panel.add(winner_name_label);
516: winner_panel.add(winner_name);
517: winner_panel.setVisible(false);
518: add(winner_panel);
519: }
520:
521: public void destroy() {
522: universe.cleanup();
523: }
524:
525: /**
526: * Create the scenegraph for the 3D view.
527: */
528: public BranchGroup createScene3D() {
529:
530: // Define colors
531: Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
532: Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
533: Color3f red = new Color3f(0.80f, 0.20f, 0.2f);
534: Color3f ambient = new Color3f(0.25f, 0.25f, 0.25f);
535: Color3f diffuse = new Color3f(0.7f, 0.7f, 0.7f);
536: Color3f specular = new Color3f(0.9f, 0.9f, 0.9f);
537: Color3f ambientRed = new Color3f(0.2f, 0.05f, 0.0f);
538: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
539:
540: // Create the branch group
541: BranchGroup branchGroup = new BranchGroup();
542:
543: // Create the bounding leaf node
544: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
545: 0.0, 0.0), 1000.0);
546: BoundingLeaf boundingLeaf = new BoundingLeaf(bounds);
547: branchGroup.addChild(boundingLeaf);
548:
549: // Create the background
550: Background bg = new Background(bgColor);
551: bg.setApplicationBounds(bounds);
552: branchGroup.addChild(bg);
553:
554: // Create the ambient light
555: AmbientLight ambLight = new AmbientLight(white);
556: ambLight.setInfluencingBounds(bounds);
557: branchGroup.addChild(ambLight);
558:
559: // Create the directional light
560: Vector3f dir = new Vector3f(-1.0f, -1.0f, -1.0f);
561: DirectionalLight dirLight = new DirectionalLight(white, dir);
562: dirLight.setInfluencingBounds(bounds);
563: branchGroup.addChild(dirLight);
564:
565: // Create the pole appearance
566: Material poleMaterial = new Material(ambient, black, diffuse,
567: specular, 110.f);
568: poleMaterial.setLightingEnable(true);
569: Appearance poleAppearance = new Appearance();
570: poleAppearance.setMaterial(poleMaterial);
571:
572: // Create the transform group node
573: TransformGroup transformGroup = new TransformGroup();
574: transformGroup
575: .setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
576: transformGroup
577: .setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
578: branchGroup.addChild(transformGroup);
579:
580: // Create the poles
581: Poles poles = new Poles(poleAppearance);
582: transformGroup.addChild(poles.getChild());
583:
584: // Add the position markers to the transform group
585: transformGroup.addChild(positions.getChild());
586:
587: // Let the positions object know about the transform group
588: positions.setTransformGroup(transformGroup);
589:
590: // Create the mouse pick and drag behavior node
591: PickDragBehavior behavior = new PickDragBehavior(canvas2D,
592: canvas3D, positions, branchGroup, transformGroup);
593: behavior.setSchedulingBounds(bounds);
594: transformGroup.addChild(behavior);
595:
596: return branchGroup;
597: }
598:
599: public void actionPerformed(ActionEvent event) {
600:
601: Object target = event.getSource();
602:
603: // Process the button events.
604: if (target == skill_return_button) {
605: skill_panel.setVisible(false);
606: skill_return_button.setVisible(false);
607: c_container.setVisible(true);
608: b_container.setVisible(true);
609: newGame();
610: } else if (target == winner_return_button) {
611: if (winner_flag) {
612: String name = winner_name.getText();
613: String tmp_name = new String("");
614: int tmp_score = 0;
615: boolean insert_flag = false;
616: winner_flag = false;
617: for (int i = 0; i < 20; i++) {
618: if (insert_flag) {
619: name = names[i];
620: score = scores[i];
621: names[i] = tmp_name;
622: scores[i] = tmp_score;
623: tmp_name = name;
624: tmp_score = score;
625: }
626: if (!insert_flag && score > scores[i]) {
627: tmp_name = names[i];
628: tmp_score = scores[i];
629: scores[i] = score;
630: names[i] = name;
631: insert_flag = true;
632: }
633: high_names[i].setText(names[i]);
634: high_scores[i].setText(Integer.toString(scores[i]));
635: }
636: scoresString = new String("");
637: int place;
638: for (int i = 0; i < 20; i++) {
639: place = (int) places[i];
640: scoresString += Integer.toString(place);
641: scoresString += "\t";
642: scoresString += names[i];
643: scoresString += " ";
644: scoresString += Integer.toString(scores[i]);
645: scoresString += "\n";
646: }
647:
648: if (writeScoresFile) {
649: if (appletFlag) {
650: try {
651: OutputStreamWriter outFile = new OutputStreamWriter(
652: new FileOutputStream("scores.txt"));
653: outFile.write(scoresString);
654: outFile.flush();
655: outFile.close();
656: outFile = null;
657: } catch (IOException ioe) {
658: System.out.println("Error: "
659: + ioe.toString());
660: } catch (Exception e) {
661: System.out
662: .println("Error: " + e.toString());
663: }
664:
665: } else {
666:
667: try {
668:
669: OutputStreamWriter outFile = new OutputStreamWriter(
670: new FileOutputStream("scores.txt"));
671: outFile.write(scoresString);
672: outFile.flush();
673: outFile.close();
674: outFile = null;
675: } catch (IOException ioe) {
676: System.out.println("Error: "
677: + ioe.toString());
678: }
679: }
680: }
681: }
682: winner_panel.setVisible(false);
683: winner_return_button.setVisible(false);
684: winner_label.setVisible(false);
685: winner_score_label.setVisible(false);
686: winner_name_label.setVisible(false);
687: winner_top_label.setVisible(false);
688: winner_name.setVisible(false);
689: c_container.setVisible(true);
690: b_container.setVisible(true);
691: } else if (target == high_return_button) {
692: high_return_button.setVisible(false);
693: high_panel.setVisible(false);
694: c_container.setVisible(true);
695: b_container.setVisible(true);
696: } else if (target == instruct_return_button) {
697: instruct_text.setVisible(false);
698: instruct_return_button.setVisible(false);
699: instruct_text.repaint();
700: c_container.setVisible(true);
701: b_container.setVisible(true);
702: } else if (target == undo_button) {
703: board.undo_move();
704: canvas2D.repaint();
705: } else if (target == instruct_button) {
706: c_container.setVisible(false);
707: b_container.setVisible(false);
708: instruct_text.setVisible(true);
709: instruct_return_button.setVisible(true);
710: } else if (target == new_button) {
711: newGame();
712: } else if (target == skill_button) {
713: c_container.setVisible(false);
714: b_container.setVisible(false);
715: skill_panel.setVisible(true);
716: skill_return_button.setVisible(true);
717: } else if (target == high_button) {
718: // Read the high scores file.
719: if (appletFlag) {
720: try {
721: URL scoreURL = Resources
722: .getResource("four_by_four/scores.txt");
723: inStream = new BufferedInputStream(scoreURL
724: .openStream(), 8192);
725: Reader read = new BufferedReader(
726: new InputStreamReader(inStream));
727: StreamTokenizer st = new StreamTokenizer(read);
728: st.whitespaceChars(32, 44);
729: st.eolIsSignificant(false);
730:
731: int count = 0;
732: int token = st.nextToken();
733: boolean scoreFlag = true;
734: String string;
735: while (count < 20) {
736: places[count] = (int) st.nval;
737: string = new String("");
738: token = st.nextToken();
739: while (token == StreamTokenizer.TT_WORD) {
740: string += st.sval;
741: string += " ";
742: token = st.nextToken();
743: }
744: names[count] = string;
745: scores[count] = (int) st.nval;
746: token = st.nextToken();
747: count++;
748: }
749: inStream.close();
750: } catch (Exception ioe) {
751: System.out.println("Error: " + ioe.toString());
752: }
753: } else {
754: try {
755: URL scoreURL = Resources
756: .getResource("four_by_four/scores.txt");
757: inStream = new BufferedInputStream(scoreURL
758: .openStream(), 8192);
759: Reader read = new BufferedReader(
760: new InputStreamReader(inStream));
761: StreamTokenizer st = new StreamTokenizer(read);
762: st.whitespaceChars(32, 44);
763: st.eolIsSignificant(false);
764:
765: int count = 0;
766: int token = st.nextToken();
767: boolean scoreFlag = true;
768: String string;
769: while (count < 20) {
770: places[count] = (int) st.nval;
771: string = new String("");
772: token = st.nextToken();
773: while (token == StreamTokenizer.TT_WORD) {
774: string += st.sval;
775: string += " ";
776: token = st.nextToken();
777: }
778: names[count] = string;
779: scores[count] = (int) st.nval;
780: token = st.nextToken();
781: count++;
782: }
783: inStream.close();
784: } catch (Exception ioe) {
785: System.out.println("Error: " + ioe.toString());
786: }
787: }
788: c_container.setVisible(false);
789: b_container.setVisible(false);
790: high_panel.setVisible(true);
791: high_return_button.setVisible(true);
792: }
793:
794: Checkbox box = group.getSelectedCheckbox();
795: String label = box.getLabel();
796: if (label.equals("Babe in the Woods ")) {
797: board.set_skill_level(0);
798: } else if (label.equals("Walk and Chew Gum ")) {
799: board.set_skill_level(1);
800: } else if (label.equals("Jeopardy Contestant ")) {
801: board.set_skill_level(2);
802: } else if (label.equals("Rocket Scientist ")) {
803: board.set_skill_level(3);
804: } else if (label.equals("Be afraid, be very afraid")) {
805: board.set_skill_level(4);
806: }
807: }
808:
809: public void newGame() {
810: board.newGame();
811: canvas2D.repaint();
812: }
813:
814: public void start() {
815: if (appletFlag)
816: showStatus("FourByFour");
817: }
818:
819: public void winner(int player, int level, int nmoves, long time) {
820:
821: if (player == 1) {
822: score = level * level_weight + (66 - nmoves) * move_weight
823: - (int) Math.min(time * time_weight, 5000);
824: winner_label.setText("Game over, you win!");
825: winner_label.setLocation(290, 90);
826: winner_score_label.setText("Score = " + score);
827: winner_score_label.setVisible(true);
828: winner_score_label.setLocation(315, 120);
829: if (score > scores[19]) {
830: winner_name_label.setVisible(true);
831: winner_top_label.setVisible(true);
832: winner_name.setVisible(true);
833: winner_flag = true;
834: }
835: } else {
836: winner_label.setText("Game over, the computer wins!");
837: winner_label.setLocation(250, 150);
838: }
839: c_container.setVisible(false);
840: b_container.setVisible(false);
841: winner_panel.setVisible(true);
842: winner_label.setVisible(true);
843: winner_return_button.setVisible(true);
844: repaint();
845: }
846:
847: /**
848: * Inner class used to "kill" the window when running as
849: * an application.
850: */
851: static class killAdapter extends WindowAdapter {
852: public void windowClosing(WindowEvent event) {
853: System.exit(0);
854: }
855: }
856:
857: /**
858: * Main method, only used when running as an application.
859: */
860: public static void main(String[] args) {
861: FourByFour.appletFlag = false;
862: new MainFrame(new FourByFour(), 730, 450);
863: }
864:
865: }
|