User Tools

Site Tools


guess_my_rule

This is an old revision of the document!


Guess My Rule

Guess My Rule games are games in which one person thinks up a secret rule, and then gives clues by stating some examples of some that follow the “rule,” and examples of something that doesn't follow the “rule.” Next, rule-guesser(s) try to discover the rule by trying predict whether their own examples follow the rule or not. After the rule-maker answers a few questions from the rule-guesser(s), the rule guesser(s) try to guess the rule.

For example, The secret rule might be that it is an integer that is a prime number. The rule-maker Starts with “2 follows my rule, but 4 does not”. The rule-guesser than asks “Does 3 follow the rule?” and is told “3 follows my rule” Or “15 follows the rule, right?” which the rule-make will respond with “No, 15 does not follow the rule”. The guesser will eventually win by guessing the rule or lose by giving up.

In our computer version, the user asks about a certain int or String, and the computer answers whether it follows the secret rule or not. After 10 clues are given, the user has to pass a test by answering “yes” or “no” to certain int or Strings.

To be fair, the secret rule cannot involve any random behavior.

Guess My Integer Rule

This is best used with the BlueJ IDE where you can make an instance of a class without writing a main method.

/**
 * Make up your own rule
 * 
 * @author Chris Thiel, OFMCap
 *
 */
public class GuessMyIntegerRule 
{
	private String secretRule = "It must be prime";
	public boolean DoesThisIntFollowMyRule(int n) 
	{
		for(int i = 2; i < n; i++)
			if (n % i == 0)
				return false;
		return true;
	}
	
	public String IGiveUp()
	{
		return secretRule;
	}	
}

Now Try this:

  1. Change the code so it the number must me even
  2. Change the code so it must be odd
  3. Make up your own rule and try it out with your lab partner

Guess My String Rule

This is best using the Eclipse IDE

import java.util.Scanner;

public class GuessMyStringRule {
	private String secretRule, hint;
	private String[] correctGuess;
	
	public GuessMyStringRule()
	{
		secretRule = "The String needed to contain a lower case s";
		hint = "Ben follows the rule, but bend does not";
		
	}
	public static void main( String[] args ) {
		GuessMyStringRule rule = new GuessMyStringRule();
		Scanner kb = new Scanner(System.in);
		System.out.println("Guess My Rule\nType a string and I will repond with true or false");
		int attempt = 10;
		int correct = 0;
		String[] correctGuess = new String[10];
		System.out.println( rule.getHint() );
		while ( attempt > 0 )
		{
			System.out.print (correct + " correct, " + attempt + " attempts left\n" + (11-attempt) + ": ");
			String guess = kb.nextLine();
			
			if (rule.isCorrect(guess))
			{
				correct++;
				for (String g:correctGuess)
					if (guess.equals(g))
						correct--;
				correctGuess[10-attempt] = guess;
				System.out.print("TRUE! ");
			} else {
				System.out.print("False. ");
			}
			attempt--;
		}
		System.out.println("Did you get the idea? The rule was " + rule.getSecretRule() );
		
		

	}
	private String getHint() {
		return hint;
	}
	private String getSecretRule() {
		return secretRule;
	}
	public boolean isCorrect(String guess) {
		if (guess.indexOf('s') < 0)
			return false;
		
		return true;
	}
	

}
guess_my_rule.1659545788.txt.gz · Last modified: 2022/08/03 12:56 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki