3fea25dd69004e274488c8ef1d4b4569

I have to create a program that guesses a number from 1000 to 9999 using an ArrayList and possibly an Iterator. When the user choses a number say 5432, the computer tries to guess it. say the computer spits out 1234, the user then says there is 1 match, the 3, but doesnt state the position. How do I go about coding this, hints would be appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JOptionPane;

public class Assignment2 {
	ArrayList<Integer> num;
	
	
	public Assignment2 ( ){
		// fill in code here
		// initialization
		num= new ArrayList<Integer>();
	}

	public int myGuessIs() {
		// fill in code here

	}
	
	public int totalNumGuesses() {
		// fill in code here
		// this should return the total number of guesses taken

	}
 
	public void updateMyGuess(int nmatches) {
		// fill in code here
		// update the guess based on the number of matches claimed by the user

	}
	
	// fill in code here (optional)
	// feel free to add more methods as needed
	
	
	
	// you shouldn't need to change the main function
	public static void main(String[] args) {

		Assignment2 gamer = new Assignment2( );
  
		JOptionPane.showMessageDialog(null, "Think of a number between 1000 and 9999.\n Click OK when you are ready...", "Let's play a game", JOptionPane.INFORMATION_MESSAGE);
		int numMatches = 0;
		int myguess = 0;
		
		do {
			myguess = gamer.myGuessIs();
			if (myguess == -1) {
				JOptionPane.showMessageDialog(null, "I don't think your number exists.\n I could be wrong though...", "Mistake", JOptionPane.INFORMATION_MESSAGE);
				System.exit(0);
			}
			String userInput = JOptionPane.showInputDialog("I guess your number is " + myguess + ". How many digits did I guess correctly?");
			// quit if the user input nothing (such as pressed ESC)
			if (userInput == null)
				System.exit(0);
			// parse user input, pop up a warning message if the input is invalid
			try {
				numMatches = Integer.parseInt(userInput.trim());
			}
			catch(Exception exception) {
				JOptionPane.showMessageDialog(null, "Your input is invalid. Please enter a number between 0 and 4", "Warning", JOptionPane.WARNING_MESSAGE);
				numMatches = 0;
			}
			// the number of matches must be between 0 and 4
			if (numMatches < 0 || numMatches > 4) {
				JOptionPane.showMessageDialog(null, "Your input is invalid. Please enter a number between 0 and 4", "Warning", JOptionPane.WARNING_MESSAGE);
				numMatches = 0;
			}
			if (numMatches == 4)
				break;
			// update based on user input
			gamer.updateMyGuess(numMatches);
			
		} while (true);
		
		// the game ends when the user says all 4 digits are correct
		System.out.println("Aha, I got it, your number is " + myguess + ".");
		System.out.println("I did it in " + gamer.totalNumGuesses() + " turns.");
	}
}

Refactorings

No refactoring yet !

F9a9ba6663645458aa8630157ed5e71e

Ants

February 10, 2010, February 10, 2010 07:07, permalink

No rating. Login to rate!

Looks like one of your classmates has an interesting approach to the problem:

http://forums.sun.com/thread.jspa?threadID=5427365

I suggest reading the wikipedia article about the game of "Mastermind" and then expand the algorithm to 10 colors, and also only having knowledge of exact color and position matches, but not the color matches.

294dae001922842c385346c4f08f650e

alex

March 24, 2010, March 24, 2010 03:31, permalink

No rating. Login to rate!

good site

1
1000 to 9999

Your refactoring





Format Copy from initial code

or Cancel