Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am working on a number guessing game in java. The goal of this game is to guess a 4-digit number according to the game

I am working on a number guessing game in java.

The goal of this game is to guess a 4-digit number according to the game rules. There are a host and a player. The project is designed such that the computer can be either the host or the player. The game works as follows:

1. The host picks a 4-digit number (i.e. any number between 1000 and 9999). Lets call this the base number. In the following example, let's pick 5432 as the base number.

2. The player takes a guess, for example, 1234.

3. The host tells the player how many digits of the guess number match the base number. A match is defined as the same digit in the same position. For example, 1234 and 5432 have 1 match (at position 3); 2345 and 5432 have 0 match even though its the same set of digits, none matches in the exact same position.

4. Based on the number of matches, the player performs some update and takes another guess, say 5533.

5. The host again tells the player the number of matches. This time, 5533 and 5432 have 2 matches.

6. Repeat steps 4 and 5 until the game is over. The game is over when the host tells the player that all 4 digits are matched. Therefore the player has found the base number.

Here is the ArrayGame.java I work on as all code should be written down in this class:

package guessme;

/**

* An Array-based implementation of the Guess-A-Number game

*/

public class ArrayGame {

// stores the next number to guess

private int guess;

public int[] ArrayGame;

// TODO: declare additional data members, such as arrays that store

// prior guesses, eliminated candidates etc.

// NOTE: only primitive type arrays are allowed, such as int[], boolean[] etc.

// You MAY NOT use any Collection type (such as ArrayList) provided by Java.

/********************************************************

* NOTE: you are allowed to add new methods if necessary,

* but DO NOT remove any provided method, otherwise your

* code will fail the JUnit tests!

* Also DO NOT create any new Java files, as they will

* be ignored by the autograder!

*******************************************************/

// ArrayGame constructor method

public ArrayGame() {

// TODO

}

// Resets data members and game state so we can play again

public void reset() {

// TODO

}

// Returns true if n is a prior guess; false otherwise.

public boolean isPriorGuess(int n) {

// TODO

return false;

}

// Returns the number of guesses so far.

public int numGuesses() {

// TODO

return 0;

}

/**

* Returns the number of matches between integers a and b.

* You can assume that both are 4-digits long (i.e. between 1000 and 9999).

* The return value must be between 0 and 4.

* A match is the same digit at the same location. For example:

* 1234 and 4321 have 0 match;

* 1234 and 1114 have 2 matches (1 and 4);

* 1000 and 9000 have 3 matches (three 0's).

*/

public static int numMatches(int a, int b) { // DO NOT remove the static qualifier

// TODO

return 0;

}

/**

* Returns true if the game is over; false otherwise.

* The game is over if the number has been correctly guessed

* or if all candidates have been eliminated.

*/

public boolean isOver() {

// TODO

return false;

}

// Returns the guess number and adds it to the list of prior guesses.

public int getGuess() {

// TODO: add guess to the list of prior guesses.

return guess;

}

/**

* Updates guess based on the number of matches of the previous guess.

* If nmatches is 4, the previous guess is correct and the game is over.

* Check project description for implementation details.

* Returns true if the update has no error; false if all candidates

* have been eliminated (indicating a state of error);

*/

public boolean updateGuess(int nmatches) {

// TODO

return true;

}

// Returns the list of guesses so far as an integer array.

// The size of the array must be the number of prior guesses.

// Returns null if there has been no prior guess

public int[] priorGuesses() {

// TODO

return null;

}

}

Here's the PlayGame.java, this class is designed for you playing as a host and computer as a player.

package guessme;

import javax.swing.JOptionPane;

public class PlayGame {

// Computer as the player of the game

public static void main(String[] args) {

ArrayGame gamer = new ArrayGame();

JOptionPane.showMessageDialog(null, "Think of a number between 1000 and 9999. Click OK when you are ready...",

"Let's play a game", JOptionPane.INFORMATION_MESSAGE);

int guess=0, nmatches=0;

while(!gamer.isOver()) {

// take guess

guess = gamer.getGuess();

String userInput = JOptionPane.showInputDialog("I guess your number is " + guess + ". How many digits are matched?");

if (userInput == null)

System.exit(0);

try {

nmatches = Integer.parseInt(userInput.trim());

}

catch(Exception exception) {

JOptionPane.showMessageDialog(null, "Invalid. Please enter a number between 0 and 4", "Warning",

JOptionPane.WARNING_MESSAGE);

continue;

}

// the number of matches must be between 0 and 4

if (nmatches < 0 || nmatches > 4) {

JOptionPane.showMessageDialog(null, "Invalid. Please enter a number between 0 and 4", "Warning",

JOptionPane.WARNING_MESSAGE);

continue;

}

// update based on user input

if(gamer.updateGuess(nmatches)==false) {

JOptionPane.showMessageDialog(null, "Something is wrong. I don't think your number exists...", "Mistake",

JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

}

System.out.println("I got it. Your number was "+guess+".");

System.out.println("I did it in "+gamer.numGuesses()+" rounds. Here is the list of my guesses:");

for(int g : gamer.priorGuesses()) {

System.out.print(g+" ");

}

System.out.println("");

}

}

Here is HostGame.java, designed for you to play as a player and computer as a host.

package guessme;

import java.util.Random;

import java.util.Scanner;

public class HostGame {

// Computer as the host of the game

public static void main(String[] args) {

Random rnd = new Random();

Scanner conIn = new Scanner(System.in);

int groundtruth = rnd.nextInt(9000)+1000;

System.out.println("I have thought of a number for you to guess. Let's play!");

int nguesses = 0;

while(true) {

// take guess

System.out.print("What's your guess? Input a 4-digit integer: ");

int guess;

try {

guess = conIn.nextInt();

} catch(Exception e) {

conIn.nextLine();

System.out.println("Invalid input!");

continue;

}

if(guess<1000 || guess>9999) {

System.out.println("Your number is out of range!");

continue;

}

nguesses ++;

int nmatches = ArrayGame.numMatches(guess, groundtruth);

if(nmatches==4) {

System.out.print("You have won! ");

break;

}

else {

System.out.println("Almost there. Number of matches: "+nmatches);

}

}

System.out.println("The number I had was "+groundtruth);

System.out.println("You got it in "+nguesses+" rounds.");

conIn.close();

}

}

And here is a set of JUnit test I use for this game:

package guessme;

import static org.junit.Assert.*;

import org.junit.Before;

import org.junit.Test;

public class ArrayGamePublicTest {

private ArrayGame gamerA, gamerB;

@Before

public void before() {

gamerA = new ArrayGame();

gamerB = new ArrayGame();

}

@Test(timeout = 1000)

public void testNumMatchesTrivial() {

assertEquals("0 match", 0,

ArrayGame.numMatches(1234, 4321));

assertEquals("0 match", 0,

ArrayGame.numMatches(1234, 2341));

}

@Test(timeout = 1000)

public void testNumMatches() {

assertEquals("1 matches", 1,

ArrayGame.numMatches(5678, 7777));

assertEquals("1 match", 1,

ArrayGame.numMatches(9000, 3210));

assertEquals("2 matches", 2,

ArrayGame.numMatches(1234, 1114));

assertEquals("2 matches", 2,

ArrayGame.numMatches(5005, 6006));

assertEquals("3 matches", 3,

ArrayGame.numMatches(1000, 9000));

assertEquals("4 matches", 4,

ArrayGame.numMatches(1234, 1234));

}

@Test(timeout = 1000)

public void testReset() {

gamerA.reset();

assertEquals("numGuesses after reset", 0,

gamerA.numGuesses());

assertFalse("isOver after reset", gamerA.isOver());

assertEquals("priorGuesses after reset", null,

gamerA.priorGuesses());

}

@Test(timeout = 1000)

public void testFirstGuess() {

gamerA.reset();

assertEquals("test first guess", 1000, gamerA.getGuess());

}

@Test(timeout = 1000)

public void testIsOver() {

gamerA.reset();

gamerB.reset();

assertFalse("gameB not over yet", gamerB.isOver());

assertEquals("gameB 1st guess", 1000, gamerB.getGuess());

gamerB.updateGuess(4);

assertTrue("gameB is over", gamerB.isOver());

assertFalse("gameA not over yet", gamerA.isOver());

}

@Test(timeout = 1000)

public void testIsPriorGuess() {

gamerA.reset();

int g1 = gamerA.getGuess();

assertTrue("is prior guess", gamerA.isPriorGuess(g1));

assertFalse("not prior guess", gamerA.isPriorGuess(9999));

gamerA.updateGuess(0);

int g2 = gamerA.getGuess();

assertTrue("is prior guess", gamerA.isPriorGuess(g2));

assertFalse("not prior guess", gamerA.isPriorGuess(9999));

}

@Test(timeout = 1000)

public void testNumGuesses() {

gamerB.reset();

assertEquals("number of guesses shold be 0", 0, gamerB.numGuesses());

assertEquals("number of guesses shold be 0", 0, gamerB.numGuesses());

gamerB.getGuess();

assertEquals("number of guesses shold be 1", 1, gamerB.numGuesses());

assertEquals("number of guesses shold be 1", 1, gamerB.numGuesses());

gamerB.updateGuess(0);

gamerB.getGuess();

assertEquals("number of guesses shold be 2", 2, gamerB.numGuesses());

assertEquals("number of guesses shold be 2", 2, gamerB.numGuesses());

gamerB.updateGuess(4);

gamerB.getGuess();

assertEquals("number of guesses shold be 3", 3, gamerB.numGuesses());

assertEquals("number of guesses shold be 3", 3, gamerB.numGuesses());

}

@Test(timeout = 1000)

public void testUpdateGuessTrivial() {

gamerB.reset();

// ground truth number is 1000

assertEquals("gamerB first guess", 1000, gamerB.getGuess());

assertTrue("gamerB first update", gamerB.updateGuess(4));

assertTrue("gamerB game over", gamerB.isOver());

}

@Test(timeout = 1000)

public void testUpdateGuess() {

gamerA.reset();

// ground truth number is 3242

assertEquals("gamerA first guess", 1000, gamerA.getGuess());

assertTrue("gamerA first update", gamerA.updateGuess(0));

assertEquals("gamerA second guess", 2111, gamerA.getGuess());

assertTrue("gamerA second update", gamerA.updateGuess(0));

assertEquals("gamerA third guess", 3222, gamerA.getGuess());

assertTrue("gamerA third update", gamerA.updateGuess(3));

assertEquals("gamerA fourth guess", 3223, gamerA.getGuess());

assertTrue("gamerA fourth update", gamerA.updateGuess(2));

assertEquals("gamerA fifth guess", 3232, gamerA.getGuess());

assertTrue("gamerA fifth update", gamerA.updateGuess(3));

assertEquals("gamerA sixth guess", 3242, gamerA.getGuess());

assertTrue("gamerA sixth update", gamerA.updateGuess(4));

assertTrue("gamerA game over", gamerA.isOver());

}

@Test(timeout = 1000)

public void testUpdateGuessError() {

gamerA.reset();

gamerA.getGuess(); // should be 1000

gamerA.updateGuess(3);

gamerA.getGuess(); // should be 1001

assertFalse("state of error", gamerA.updateGuess(1)); // this number does not exist

// updateGuess should return false

gamerA.reset();

gamerA.getGuess(); // should be 1000

gamerA.updateGuess(3);

gamerA.getGuess(); // should be 1001

gamerA.updateGuess(2);

gamerA.getGuess(); // should be 1010

assertFalse("state of error", gamerA.updateGuess(1)); // this number does not exist

// updateGuess should return false

}

@Test(timeout = 1000)

public void testPriorGuesses() {

gamerB.reset();

assertEquals("test prior guesses", null, gamerB.priorGuesses());

int g1 = gamerB.getGuess();

assertArrayEquals("test prior guesses", new int[]{g1},

gamerB.priorGuesses());

gamerB.updateGuess(1);

int g2 = gamerB.getGuess();

gamerB.updateGuess(2);

int g3 = gamerB.getGuess();

gamerB.updateGuess(3);

int g4 = gamerB.getGuess();

assertArrayEquals("test prior guesses", new int[]{g1, g2, g3, g4},

gamerB.priorGuesses());

gamerB.updateGuess(4);

assertArrayEquals("test prior guesses", new int[]{g1, g2, g3, g4},

gamerB.priorGuesses());

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Upgrade Migration And Transformation Tips And Techniques

Authors: Edward Whalen ,Jim Czuprynski

1st Edition

0071846050, 978-0071846059

More Books

Students also viewed these Databases questions

Question

5. How can I help others in the network achieve their goals?

Answered: 1 week ago