Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1.  
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5.  
  6. public class TriviaGame
  7. {
  8. public static void main(String args[]) throws IOException
  9. {
  10. // Constants
  11. final int NUM_QUESTIONS = 10;
  12. final int NUM_PLAYERS = 2;
  13.  
  14. // Variables
  15. int playerTurn = 1; // The current player
  16. int questionNum; // The current question number
  17. int playerAnswer; // The player's chosen answer
  18. int player1points = 0; // Player 1's points
  19. int player2points = 0; // Player 2's points
  20.  
  21. // Create an array of Player objects for player #1 and player #2.
  22. Player[] players = new Player[NUM_PLAYERS];
  23. for (int i = 0; i < NUM_PLAYERS; i++)
  24. {
  25. players[i] = new Player(i+1);
  26. }
  27.  
  28. // Create an array to hold Question objects.
  29. Question[] questions = new [NUM_QUESTIONS];
  30.  
  31. // Initialize the array with data.
  32. initQuestions(questions);
  33.  
  34. // Play the game.
  35. for (int i = 0; i < NUM_QUESTIONS; i++)
  36. {
  37. // Display the question.
  38. TriviaGame.displayQuestion(qArray[i], playerTurn);
  39.  
  40. // Get the player's answer.
  41. players[playerTurn - 1].chooseAnswer();
  42.  
  43. // See if the correct answer was chosen.
  44. if (qArray[i].getCorrectAnswerNumber() == players[playerTurn - 1].getCurrentAnswer())
  45. {
  46. players[playerTurn -1].incrementPoints();
  47. }
  48.  
  49. // See if the the player chose the wrong answer.
  50. // do nothing
  51.  
  52. // Switch players for the next iteration.
  53. if (playerTurn == 1)
  54. playerTurn = 2;
  55. else
  56. playerTurn = 1;
  57. }
  58.  
  59. // Show the game results.
  60. showGameResults(players);
  61. }
  62.  
  63. /**
  64. * The initQuestions method uses the contents of the trivia.txt file to
  65. * populate the qArray parameter with Question objects.
  66. */
  67. public static void initQuestions(Question qArray[]) throws IOException
  68. {
  69. // Open the trivia.txt file.
  70. File file = new File("trivia.txt");
  71. Scanner inputFile = new Scanner(file);
  72.  
  73. // Populate the qArray with data from the file.
  74. for (int i = 0; i < qArray.length; i++)
  75. {
  76. // Create a Question object in the array.
  77. qArray[i] = new Question();
  78.  
  79. // Get the question text from the file.
  80. qArray[i].setQuestion(inputFile.nextLine());
  81.  
  82. // Get the possible answers.
  83. for (int j = 1; j <= 4; j++)
  84. {
  85. qArray[i].setPossibleAnswer(inputFile.nextLine(), j);
  86. }
  87.  
  88. // Get the correct answer.
  89. qArray[i].setCorrectAnswerNumber(Integer.parseInt(inputFile.nextLine()));
  90. }
  91. }
  92.  
  93. public static void displayQuestion(Question q, int playerNum)
  94. {
  95. // Display the player number.
  96. System.out.println("Question for player #" + playerNum);
  97. System.out.println("------------------------");
  98.  
  99. // Display the question.
  100. System.out.println(q.getQuestionText());
  101. for (int i = 1; i <= 4; i++)
  102. {
  103. System.out.println(i + ". " + q.getPossibleAnswer(i));
  104. }
  105. }
  106.  
  107. public static void showGameResults(Player[] players)
  108. {
  109. // Display the stats.
  110. System.out.println("Game Over!");
  111. System.out.println("---------------------");
  112. System.out.println("Player 1's points: " + players[0].getPoints());
  113. System.out.println("Player 2's points: " + players[1].getPoints());
  114.  
  115. // Declare the winner.
  116. if (players[0].getPoints() > players[1].getPoints())
  117. System.out.println("Player 1 wins!");
  118. else if (players[1].getPoints() > players[0].getPoints())
  119. System.out.println("Player 2 wins!");
  120. else
  121. System.out.println("It's a TIE!");
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement