Question
The purpose of this assignment is to use classes and class methods. The application will play a variation of the Fermi game. In the game
The purpose of this assignment is to use classes and class methods. The application will play a variation of the Fermi game. In the game the user (TestHarness) tries to guess the order of three unique numbers between 1 and 9 inclusive. Prompt the game accepts input of three integer numbers between 1 and 9 (inclusive) (guess method). The application will respond with the words Nano, Pico, and Fermi in a String array (String[]). Nano will appear whenever an incorrect number was entered. Pico will appear whenever a correct number is entered but it is in the wrong position. Fermi will appear whenever a correct number is entered in the correct position. The goal is to guess all the three numbers in the correct positions. The application response will be a String array containing "Fermi Fermi Fermi" in that case. Generate three random integers between 1 and 9 inclusive. The random numbers must be unique. For example, 2 5 7 is valid but 2 5 5 is not. The random number generator doesn't guarantee unique numbers. You must generate another random number if a duplicate is generated. Accept three numbers (guess) provided by the user. The numbers must be between 1 and 9 inclusive. Compare the guess entered to the three random numbers. If the first number the provided matches the first random number, assign the word "Fermi" first in the response. If the first number the user entered matches the second or third random numbers, assign the word "Pico" first in the response. Assign the word "Nano" first in the response if the first number the user entered doesn't match any of the three random numbers. If the second number the provided matches the second random number, assign the word "Fermi" second in the response. If the second number the provided matches the first or third random numbers, assign the word "Pico" second in the response. Assign the word "Nano" second in the response if the second number the user entered doesn't match any of the three random numbers. If the third number provided matches the third random number, assign the word "Fermi" third in the response. If the third number provided matches the first or second random numbers, assign the word "Pico" third in the response. Assign the word "Nano" third in the response if the third number the user entered doesn't match any of the three random numbers. Keep track of whether the user won. The UML defines a method isWinner on the FermiGame class. Allow the user to provide another guess again after each application feedback of Nano, Pico, Fermi. The design uses the TestHarness to call the FermiGame class you implement. The UML defines the methods and signatures for the FermiGame class. Your implementation will need to provide the game logic in the guess method, the random number initialization, and keep track of whether the user has won. Review the TestHarness implementation to see how it calls the FermiGame methods.
package week08; import java.util.Random; // SKL need to put your name in this file class FermiGame { FermiGame(){ // SKL need to call the other constructor with the defaults } FermiGame(String s1, String s2, String s3){ ValidateParameters(s1, s2, s3); } public void ValidateParameters(String respond1, String respond2, String respond3){ // SKL check to see if any of the parameters are null and use defaults if they are. // Check to see that they are all unique if(respond1.toUpperCase() != m_fermi){ m_fermi = respond1; } if(respond2.toUpperCase() != m_pico) { m_pico = respond2; } if(respond3.toUpperCase() != m_nano) { m_nano = respond3; } } public void newGame() { // SKL need to reset the gameWon variable generateRandomIntergers(); } private void generateRandomIntergers() { Random _r = new Random(); randomNumber[0] = _r.nextInt(9) + 1; int num = _r.nextInt(9) + 1; while (randomNumber[0] == num) { num = _r.nextInt(9) + 1; } randomNumber[1] = num; num = _r.nextInt(9) + 1; while (randomNumber[0] == num || randomNumber[1] == num) { num = _r.nextInt(9) + 1; } randomNumber[2] = num; } public String[] guess(int num1, int num2, int num3) { boolean valid = validateGuess(num1, num2, num3); // SKL this test won't put a value in each string position // remember fermi means a match at the exact position, pico means the // number is in the list but wrong position and nano means the number is not in the list if(valid) { String match = testMatch(randomNumber[0], num1); String match2 = testMatch(randomNumber[1], num1); String match3 = testMatch(randomNumber[2], num1); if(match == "Match") { results[0] = m_fermi; } else if(match2 == "Match") { results[0] = m_pico; } else if(match3 == "Match") { results[0] = m_pico; } else { results[0] = m_nano; } String match1 = testMatch(randomNumber[0], num2); String match2 = testMatch(randomNumber[1], num2); String match3 = testMatch(randomNumber[2], num2); if(match1 == "Match") { results[1] = m_pico; } else if(match2 == "Match") { results[1] = m_fermi; } else if(match3 == "Match") { results[1] = m_pico; } else { results[1] = m_nano; } String match = testMatch(randomNumber[0], num3); String match2 = testMatch(randomNumber[1], num3); String match3 = testMatch(randomNumber[2], num3); if(match == "Match") { results[2] = m_pico; } else if(match2 == "Match") { results[2] = m_pico; } else if(match3 == "Match") { results[2] = m_fermi; } else { results[2] = m_nano; } return results; } else { throw new Exception(); } return results; } private String testMatch(int rNum, int gNum) { // SKL this needs to return m_fermi, m_pico or m_nano dependin on whether the guess matches, is in the list or no match. // this will not generate the correct result when executing. if (rNum == gNum) { return "Match"; } else { return "No Match"; } } private boolean validateGuess(int g1, int g2, int g3) { // SKL this is supposed to throw an InvalidArgumentException if any of the values are not between MIN and MAX if(g1 <= Min || g1 >= MAX || g2 <= Min || g2 >= MAX || g3 <= Min || g3 >= MAX) { return false; } else { return true; } } public boolean isWinner() { // SKL this should only return m_wonGame. The if statement belongs in the guess method if(results[0] == m_fermi && results[1] == m_fermi && results[2] == m_fermi) { return m_wonGame = true; } else { return m_wonGame = false; } } int[] randomNumber = new int[3]; String[] results = new String[3]; private String m_fermi = "FERMI"; private String m_nano = "NANO"; private String m_pico = "PICO"; private boolean m_wonGame = false; private int MAX = 10; private int Min = 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started