//****************************************************************************** // TriviaGame.java: Applet // //****************************************************************************** import java.applet.*; import java.awt.*; //============================================================================== // Main Class for applet TriviaGame // //============================================================================== public class TriviaGame extends Applet { // Number of Images in imgCourse final int numImages = 19; // Number of Holes on a golf Course and in our game final int numHoles = 18; final int numQues = 18; int HoleCounter = 0; /* Defines Controls Panel to be placed in south posistion in Borderlayout Layout manager. */ ControlPanel controls; // Image anchor Point anchor; // height and width of all images. Images will be resized to these values int holewidth, holeheight; // Grass color Color grassgreen = new Color(0, 100, 0); // Current Hole Number boolean tfHole[]; // Define array of images containing images for the course Image CourseImages[]; int CurrQuesNum = -1; // Define a two dimentional array of strings String QA[][] = new String[numQues][6]; // Array of the current questions correct radiobutton array index + 1 int CorrAnsNum[] = new int[numQues]; // Defines an array of par for each hole int Par[] = new int[numHoles]; // Number of tries at the question int Try = 0; int HoleScore = 0; int TotalScore = 0; boolean GameStarted = false; boolean RightWrong = false; boolean EndOfGame = false; // TriviaGame Class Constructor //-------------------------------------------------------------------------- public TriviaGame() { } // APPLET INFO SUPPORT: // The getAppletInfo() method returns a string describing the applet's // author, copyright date, or miscellaneous information. //-------------------------------------------------------------------------- public String getAppletInfo() { return "Name: Golf Course Trivia Game\r\n" + "Author: Anthony\r\n" + "Created for the 1997 ThinkQuest Internet Contest.\r\n" + "Team:\tJames Bishop\r\n" + " \tAnthony\r\n" + " \tEugene\r\n"; } // The init() method is called by the AWT when an applet is first loaded or // reloaded. Override this method to perform whatever initialization your // applet needs, such as initializing data structures, loading images or // fonts, creating frame windows, setting the layout manager, or adding UI // components. //-------------------------------------------------------------------------- public void init() { // If you use a ResourceWizard-generated "control creator" class to // arrange controls in your applet, you may want to call its // CreateControls() method from within this method. Remove the following // call to resize() before adding the call to CreateControls(); // CreateControls() does its own resizing. //---------------------------------------------------------------------- resize(640, 495); setBackground(Color.white); anchor = new Point(10,10); holewidth = 450; holeheight = 190; setLayout(new BorderLayout()); controls = new ControlPanel(); // Initializes all to false inittfHole(); // Initializes the path to all images initImages(); // Initializes all questions initQA(); // Initializes the par for each hole initPar(); tfHole[0] = true; // Insert all crontols at the bottom of the screen add("South", controls); } // This initializes the array tfHoles to false public void inittfHole() { tfHole = new boolean[numImages]; // Initialize tfHole to false for (int i = 0; i < numImages; i++) { tfHole[i] = false; } } void initImages() { // Create an array of images called CourseImages CourseImages = new Image[numImages]; // Temperarily assign image path to imgSrc till stored in CourseImage array String imgSrc; // Set 0 index to whole course graphic imgSrc = "course.jpg"; CourseImages[0] = getImage(getCodeBase(), imgSrc); // set image of hole number i to its respective array index number for (int i = 1; i <= numHoles; i++) { if(i < 10) { imgSrc = "hole0" + i + ".jpg"; CourseImages[i] = getImage(getCodeBase(), imgSrc); } else { imgSrc = "hole" + i + ".jpg"; CourseImages[i] = getImage(getCodeBase(), imgSrc); } } } void initQA() { /* \n */ /* \n */ QA[0][0] = "In what order were golf balls developed?"; QA[0][1] = "Feather, Wood, Rubber."; QA[0][2] = "Rubber, Wood, Feather."; QA[0][3] = "Wood, Feather, Rubber."; QA[0][4] = "Rubber, Feather, Wood."; QA[0][5] = "Simple par 3. There is a wind coming from behind so watch your own strength."; CorrAnsNum[0] = 3; QA[1][0] = "During war time, London posted rules that said?"; QA[1][1] = "A score for a player shot does not count."; QA[1][2] = "During gunfire or bombings, players may take cover."; QA[1][3] = "All bomb shelters are still in play."; QA[1][4] = "Divots made by bombs are still in play."; QA[1][5] = "This is a tough one. No wind right now but this hole has a hard dog leg right."; CorrAnsNum[1] = 2; QA[2][0] = "In his day, Bob Hope had a handicap as low as five or six. Who else is\nthe only other entertainer to be in the PGA World Golf Hall of Fame?"; QA[2][1] = "Charley Chaplin."; QA[2][2] = "Jimmy Stewart."; QA[2][3] = "Bing Crosby."; QA[2][4] = "John Wayne."; QA[2][5] = "Par 4. Pretty straight foward. Just watch that northeast wind."; CorrAnsNum[2] = 3; QA[3][0] = "Who was the golfer that won the first 3 Disney Golf Tournaments?"; QA[3][1] = "Arnold Palmer."; QA[3][2] = "Chi Chi Rodriguez."; QA[3][3] = "Jack Nicklaus."; QA[3][4] = "Al Geiberger."; QA[3][5] = "Short and sweet. Be warry of the sand trap completely surrounding the green."; CorrAnsNum[3] = 3; QA[4][0] = "Who was the first black golfer invited to the Masters?"; QA[4][1] = "Lee Elder."; QA[4][2] = "Tiger Woods."; QA[4][3] = "Bill Spiller."; QA[4][4] = "Arnold Palmer."; QA[4][5] = "Stay on the straight and narrow."; CorrAnsNum[4] = 1; QA[5][0] = "This golfer hit a shot that hit a rock and landed on the green. He threw his club\nup into the air, hit him on the head, and knocked himself out. Who is he?"; QA[5][1] = "Al Geiberger."; QA[5][2] = "Nick Price."; QA[5][3] = "Lon Hinkle."; QA[5][4] = "Bobby Cruickshank."; QA[5][5] = "Dog leg right although not as sharp as hole two."; CorrAnsNum[5] = 4; QA[6][0] = "1989 US Open, four golfers scored a hole-in-one in one day-and on the same hole!\n(Odds: 8.7 million-to-1) Which one of these men was not one of the four?"; QA[6][1] = "Doug Weaver."; QA[6][2] = "Arnold Palmer."; QA[6][3] = "Jerry Pate."; QA[6][4] = "Nick Price."; QA[6][5] = "We keep going with these dog legs, this time to the left. Don't get lost in the forest to your left and riht."; CorrAnsNum[6] = 2; QA[7][0] = "How long is the longest golf course in the world?"; QA[7][1] = "8325 yards"; QA[7][2] = "9765 yards"; QA[7][3] = "7533 yards"; QA[7][4] = "8768 yards"; QA[7][5] = "Dog leg once again to the right. Don't hit too hard or you'll find yourself in a sand trap."; CorrAnsNum[7] = 1; QA[8][0] = "What color is the jacket that the winner of the Masters receives?"; QA[8][1] = "Green."; QA[8][2] = "Red."; QA[8][3] = "Blue."; QA[8][4] = "Black."; QA[8][5] = "Hit hard off the tee. If you fall short you'll find yourself swimming with the fish."; CorrAnsNum[8] = 1; QA[9][0] = "What is the longest hole-in-one straight-away ever recorded by a woman? "; QA[9][1] = "253 yards"; QA[9][2] = "295 yards"; QA[9][3] = "393 yards"; QA[9][4] = "346 yards"; QA[9][5] = "Enjoy the scenery with the lovely stream to your right but don't get caught inside of it."; CorrAnsNum[9] = 3; QA[10][0] = "What is the maximum number of clubs you're allowed to carry in a golf match?"; QA[10][1] = "13 clubs"; QA[10][2] = "14 clubs"; QA[10][3] = "15 clubs"; QA[10][4] = "16 clubs"; QA[10][5] = "Control your strength on this one."; CorrAnsNum[10] = 2; QA[11][0] = "With a score of 59, this player holds the lowers score in a PGA event?"; QA[11][1] = "Arnold Palmer"; QA[11][2] = "Ben Hogan"; QA[11][3] = "Jack Nicklaus"; QA[11][4] = "Al Geigerger."; QA[11][5] = "Not too tough. Don't stray from the beaten path."; CorrAnsNum[11] = 4; QA[12][0] = "A golf hole should be how many inches deep? "; QA[12][1] = "3 inches"; QA[12][2] = "5 inches"; QA[12][3] = "6 inches"; QA[12][4] = "4 inches"; QA[12][5] = "They're getting easier. Always let your conscience be your guide."; CorrAnsNum[12] = 4; QA[13][0] = "He is the only one to win all four of the top pro tournaments at least twice?"; QA[13][1] = "Tiger Woods"; QA[13][2] = "Arnold Palmer"; QA[13][3] = "Ben Hogan"; QA[13][4] = "Jack Nicklaus"; QA[13][5] = "The north star will lead you home."; CorrAnsNum[13] = 4; QA[14][0] = "This player cut the dogleg on the par-5 8th Hole by hitting onto the 17th\nfairway. A tree was planted to block the shortcut. Who was this player?\n(1979 U.S. Open)"; QA[14][1] = "Byron Nelson"; QA[14][2] = "Francis Ouimet"; QA[14][3] = "Lon Hinkle"; QA[14][4] = "Gary Player"; QA[14][5] = "You can very easily get caught in the woods crying wolf with even the slightest bit of wind."; CorrAnsNum[14] = 3; QA[15][0] = "The first player to capture the U.S. Amateur and U.S. Open in one year was?"; QA[15][1] = "Charles Evans"; QA[15][2] = "Bobby Jones"; QA[15][3] = "Walter Hagen"; QA[15][4] = "Ben Hogan"; QA[15][5] = "Looks easy but looks can be decieving."; CorrAnsNum[15] = 1; QA[16][0] = "The first full time tournament pro was?"; QA[16][1] = "Ben Hogan"; QA[16][2] = "Charles Evans"; QA[16][3] = "Bobby Jones"; QA[16][4] = "Walter Hagen"; QA[16][5] = "Don't let fatigue get the best of you, your almost done."; CorrAnsNum[16] = 4; QA[17][0] = "This man turned to golf as therapy for an ankle injury. Who is he?"; QA[17][1] = "Gary Player"; QA[17][2] = "Jack Nicklaus"; QA[17][3] = "Gene Sarazen"; QA[17][4] = "Arnold Palmer"; QA[17][5] = "Can you taste the glory. Keep your head down and your sholders straight. Good game!"; CorrAnsNum[17] = 2; } void initPar() { Par[0] = 3; Par[1] = 4; Par[2] = 4; Par[3] = 3; Par[4] = 5; Par[5] = 5; Par[6] = 5; Par[7] = 4; Par[8] = 4; Par[9] = 5; Par[10] = 3; Par[11] = 4; Par[12] = 4; Par[13] = 4; Par[14] = 5; Par[15] = 4; Par[16] = 4; Par[17] = 4; } public boolean action(Event e, Object o) { if(e.target instanceof Button) { Button bTarg = (Button)e.target; String bStr = bTarg.getLabel(); if(bStr.compareTo("Check Answer!") == 0) { if(tfHole[18] == false) { if(CheckAnswer()) { // If anwser is correct rotate to image of next hole HoleCounter++; tfHole[HoleCounter] = true; // Then Talley score and finally reset questions TalleyScore("correct"); MoveToNextQuestion(); } else { /* If answer is incorrect only increase score do not move to next question or change image. */ TalleyScore("incorrect"); } } else { GameOver(); } } else if(bStr.compareTo(" Begin :-) ") == 0) { HoleCounter++; tfHole[HoleCounter] = true; MoveToNextQuestion(); bTarg.setLabel("Check Answer!"); controls.setStats(HoleScore, TotalScore, Par[CurrQuesNum]); } } repaint(); return true; } boolean CheckAnswer() { GameStarted = true; int CorrAns = CorrAnsNum[CurrQuesNum]; Checkbox UserAns = controls.cpbottom.answer.AnsGroup.getCurrent(); if(UserAns == controls.cpbottom.answer.ans[CorrAns - 1]) { RightWrong = true; return true; } else { RightWrong = false; return false; } } void MoveToNextQuestion() { // Increment the question counter CurrQuesNum++; // Set new question and answer choices controls.setQues(QA[CurrQuesNum][0]); controls.setAns(QA, CurrQuesNum); } void TalleyScore(String corr_incorr) { Try++; if(corr_incorr == "Correct" || corr_incorr == "correct") { if(Par[CurrQuesNum] == 3) TotalScore += Par[CurrQuesNum] + Try - 2; else TotalScore += Par[CurrQuesNum] + Try - 1; Try = 0; HoleScore = 0; controls.setStats(HoleScore, TotalScore, Par[CurrQuesNum + 1]); } else { if(Par[CurrQuesNum] == 3) HoleScore = Par[CurrQuesNum] + Try - 2; else HoleScore = Par[CurrQuesNum] + Try - 1; controls.setStats(HoleScore, TotalScore, Par[CurrQuesNum]); } } void GameOver() { EndOfGame = true; remove(controls); repaint(); } // TriviaGame Paint Handler //-------------------------------------------------------------------------- public void paint(Graphics g) { Color currentcolor; Font currentfont; String finalmessage; // Save current color and font to be put back after message is diplayed. currentcolor = g.getColor(); currentfont = g.getFont(); if(tfHole[HoleCounter]) { g.drawImage(CourseImages[HoleCounter], anchor.x, anchor.y, holewidth, holeheight, grassgreen, this); if(controls.cpbottom.rightpanel.chckans.chDesc.getState()) { g.setColor(grassgreen); g.setFont(new Font("fon1", 1, 10)); g.drawString(QA[CurrQuesNum][5], 20, 20); } } if(RightWrong) { g.setColor(Color.green); g.setFont(new Font("fon1", 1, 14)); g.drawString("Congratulations!", 475, 25); } else { g.setFont(new Font("fon1", 1, 12)); if(Try < 4) { switch (Try) { case 1: g.setColor(Color.red); g.drawString("Sorry Incorrect.", 475, 25); g.drawString("Try Again!", 475, 40); break; case 2: g.setColor(new Color(200, 0, 0)); g.drawString("Maybe you should", 475, 55); g.drawString("let the next", 475, 70); g.drawString("group play through.", 475, 85); break; case 3: g.setColor(new Color(125, 0, 0)); g.drawString("Today just isn't", 475, 95); g.drawString("your day.", 475, 110); break; case 4: g.setColor(Color.orange); g.drawString("You are pretty", 475, 120); g.drawString("sad if you are", 475, 135); g.drawString("still trying to", 475, 150); g.drawString("get the right", 475, 165); g.drawString("answer.", 475, 180); break; } } else { g.setColor(Color.orange); g.drawString("You are pretty", 475, 120); g.drawString("sad if you are", 475, 135); g.drawString("still trying to", 475, 150); g.drawString("get the right", 475, 165); g.drawString("answer.", 475, 180); } } if(EndOfGame) { g.setFont(new Font("fon1", 1, 20)); g.setColor(Color.blue); g.drawImage(CourseImages[0], anchor.x, anchor.y, holewidth, holeheight, grassgreen, this); g.drawString("Congratulations!", 50, 300); g.drawString("You have played the master's", 50, 330); g.drawString("TriviaGame Golf Course", 50, 360); g.drawString("Your final score of " + TotalScore, 50, 390); if(TotalScore == 67) finalmessage = "is the best!"; else if(TotalScore <= 69) finalmessage = "is pretty darn good!"; else if (TotalScore <= 71) finalmessage = "isn't bad!"; else if (TotalScore <= 75) finalmessage = "could be better!"; else if (TotalScore <= 81) finalmessage = "has a lot to be desired!"; else finalmessage = "is just sad!"; g.drawString(finalmessage, 50, 420); } // Restore original color and font g.setColor(currentcolor); g.setFont(currentfont); } // Place additional applet clean up code here. destroy() is called when // when you applet is terminating and being unloaded. //------------------------------------------------------------------------- public void destroy() { } } /* Combines all of the Panels into one 2 x 2 which can then be placed into the BorderLayout layout manager in the main class. */ class ControlPanel extends Panel { Question question = new Question(); CPBottom cpbottom = new CPBottom(); ControlPanel() { GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); setLayout(gridbag); //constraints.weighty = 1; constraints.fill = constraints.BOTH; constraints.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints(question, constraints); add(question); //constraints.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints(cpbottom, constraints); add(cpbottom); } void setQues(String Quest) { question.queslbl.setFont(new Font("Courier", Font.BOLD|Font.ITALIC, 12)); question.queslbl.setText(Quest); } void setAns(String[][] QA, int QAIndex) { cpbottom.answer.setSt(false); for(int i = 1; i <= 4; i++) { cpbottom.answer.ans[i - 1].setLabel(QA[QAIndex][i]); repaint(); } } void setStats(int HScore, int TScore, int Pr) { cpbottom.rightpanel.score.holescore.setText("" + HScore); cpbottom.rightpanel.score.totalscore.setText("" + TScore); cpbottom.rightpanel.score.par.setText("" + Pr); } } class CPBottom extends Panel { Answer answer = new Answer(); RightPanel rightpanel = new RightPanel(); CPBottom() { add(answer); add(rightpanel); } } class RightPanel extends Panel { Score score; ChckAns chckans; RightPanel() { setLayout(new GridLayout(2, 1)); add(score = new Score()); add(chckans = new ChckAns()); } } class Question extends Panel { // Question will be displayed in a label TextArea queslbl; Font quesfont = new Font("TimesRoman", Font.BOLD|Font.ITALIC, 12); // Constructor constucts a label for the question Question() { queslbl = new TextArea("I hope you know your stuff because this ain't no mini golf course.", 3, 100); queslbl.setFont(quesfont); queslbl.setEditable(false); add(queslbl); } } class Answer extends Panel { // Number of possible answers final int numAns = 4; // Radio button group CheckboxGroup AnsGroup; // Array of radio buttons representing 4 possible answers numbered from 0 - 3 Checkbox ans[]; Answer() { ans = new Checkbox[numAns]; AnsGroup = new CheckboxGroup(); setLayout(new GridLayout(4, 1)); add(ans[0] = new Checkbox("Welcome to the ", AnsGroup, false)); add(ans[1] = new Checkbox(" Golf Course ", AnsGroup, false)); add(ans[2] = new Checkbox(" Trivia ", AnsGroup, false)); add(ans[3] = new Checkbox(" Game ", AnsGroup, false)); } void setSt(boolean state) { for(int i = 0; i < numAns; i++) ans[i].setState(state); } } class Score extends Panel { Label holescore, totalscore, par; Font pifont = new Font("pifont", 1, 13); Font infofont = new Font("infofont", 1, 12); Score() { setLayout(new GridLayout(4, 3)); Label pi = new Label("Player Info:"); pi.setFont(pifont); Label hslbl = new Label("Hole Score:"); hslbl.setFont(infofont); Label tslbl = new Label("Total Score:"); tslbl.setFont(infofont); Label prlbl = new Label("Par:"); prlbl.setFont(infofont); add(pi); add(new Label()); add(new Label()); add(new Label()); add(prlbl); add(par = new Label("Let's")); add(new Label()); add(hslbl); add(holescore = new Label("Rock!")); add(new Label()); add(tslbl); add(totalscore = new Label()); } } class ChckAns extends Panel { /* Define button that will actually run all of the back ground processing such as: Checking if answer is correct, talleying score, talleying how, many times they tired a particular question, Rotating to picture of next hole if correct.*/ Button chckansbtn; Checkbox chDesc; ChckAns() { chckansbtn = new Button(" Begin :-) "); chckansbtn.setForeground(Color.white); chckansbtn.setBackground(Color.black); chDesc = new Checkbox("Description"); setLayout(new GridLayout(2, 3)); add(chDesc); add(new Label()); add(chckansbtn); add(new Label()); add(new Label()); add(new Label()); } }