I need some help with this. For my class I have to make a game called hexo, basically

"Hexo is a game loosely based on the television game show Lingo. Rather than trying to guess 5 letter words (as in Lingo) the Hexo player will have sixteen tries to guess randomly generated 5 digit hexadecimal numbers.

The hexadecimal ‘secret numbers’ will not have any duplicated digits.

Each time the user enters a guess:
• The number of the guess will be displayed.
• The number of matching digits will be displayed.
• The number of matches in the correct position will be displayed.

If the user guesses the number; the ‘secret number’ will be displayed, a winning message will be displayed, the number of guesses will be displayed and the game will end.

If the user does not guess the number by the sixteenth try, a losing message will be displayed, the ‘secret number’ will be displayed, and the game will end.

For each guess, the user’s entry will be checked for valid hexadecimal digits and for having a length of 5 characters.

Error handling will be simple:
• If non-hexadecimal characters are detected, the user will get a message, but the guess will be processed normally and will count as a try.
• If the guess is longer than 5 characters, the user will get a message, the guess will be truncated, processed normally and will count as a try
• If the guess is shorter than 5 characters, the user will get a message, the guess will have an appropriate number of blank spaces added, be processed normally and will count as a try
"

I've Figured out most of it. I have a class that generates the random hex fine (using a method called create();) but Im coding in my other class that handles all the UI stuff and i cant figure out how to
-Have the scanner count how many times the user has inputted a guess
-How to only have the program run 16 times
- How to display the guess #, number of correct digits, and how many are in the correct position

Heres My Code:

import java.util.Scanner;

/**
 * HexoGame Class
 * 
 * @author Allan Ntaate
 * @version 1.0
 */
public class HexoGame
{
/**
 * 
 * 
 * @param  
 * @return      
 */
public void runHexo()
{
 final int WORDLENGTH = 5;
 HexGen obj = new HexGen();
 Scanner scan = new Scanner(System.in);
 String secretHex = obj.create();
 int turn = 0;
 final int MAX = 16;
 String guess = new String();
//   int a = secretHex.charAt(0);
//   int b = secretHex.charAt(1);
//   int c = secretHex.charAt(2);
//   int d = secretHex.charAt(3);
//   int e = secretHex.charAt(4);
//  try
//  {
// //  int aa = guess.charAt(0);
// //  int bb = guess.charAt(1);
// //  int cc = guess.charAt(2);
// //  int dd = guess.charAt(3);
// //  int ee = guess.charAt(4);
// 
// }
// 
// catch(Exception f)
// {
  int g = 0;
 System.out.println("Welcome to the game of Hexo");
 System.out.println("The computer will generate a random hexadecimal number.");
 System.out.println("Each of the five places will be unique.");
 System.out.println("The object of the game is to guess the number within 16 tries.");
 do
 {
 System.out.print("Please make a guess. > ");
 guess = scan.nextLine();

 int addX = WORDLENGTH - guess.length();
if ( guess.length() < WORDLENGTH)
{

for (int i = 0; i < addX ; i ++)
 {
     System.out.println("The number has five places");
     System.out.println("You'll never get it if your guess has less than five characters.");
     System.out.println("Your guess has been 'filled in' with X's.");
     System.out.println("Hex numbers have the digits 0 - 9 and the letters A - F,the character X in your guess will never match.");
     guess += "X";

for (int j = 0; j < guess.length(); j++)
g = guess.charAt(j);
for (int k = 0; k < secretHex.length(); k++)

if (g == secretHex.charAt(k))
{
    System.out.println( guess + " guess number 1 has " + g + " matching characters with " + k + " in the correct position.");
    
}
else
System.out.println(guess +" guess 1 has 0 matching characters with 0 in the correct position");
 }
 
}


else if  (guess.length() == WORDLENGTH)
{

for (int j = 0; j < guess.length(); j++)
g = guess.charAt(j);
for (int k = 0; k < secretHex.length(); k++)

if (g == secretHex.charAt(k))

    System.out.println( guess + " guess number 1 has " + g + " matching characters with " + k + " in the correct position.");
    
else
System.out.println(guess +" guess 1 has 0 matching characters with 0 in the correct position");
}



else if (guess.length() > WORDLENGTH)
{
 System.out.println("The number has only five places.");
 System.out.println("The first five characters of your guess will be used.");
 guess = guess.substring(0, 5);
 for (int j = 0; j < guess.length(); j++)
g = guess.charAt(j);
for (int k = 0; k < secretHex.length(); k++)

if (g == secretHex.charAt(k))

    System.out.println( guess + " guess number 1 has " + g + " matching characters with" + k + " in the correct position.");
    else
    System.out.println(guess +" guess 1 has 0 matching characters with 0 in the correct position"); 
}
}
while (!guess.equalsIgnoreCase(secretHex));
}
}

Recommended Answers

All 2 Replies

To simply have a counter on the number of times the scanner takes the next line, you just need,

count++

after line 51 with a count variable initialised to 0 at the top of your class. To print this out just used a system print with this count variable after your large if statement/while. To clean up your code a little more you could use,

System.out.println("The number has only five places.\nThe first five characters of your guess will be used.");

where the \n starts a new line.

Also since you are doing the same thins between lines 84 - 93 and 103 - 111, I would suggest placin this in a method and calling it from within the if statement rather than repeating the same coded function.

Yes, a simple counter would do the trick, and then to ensure the program only loops x times, just add the 'counter < x' condition to your do-while loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.