<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1924</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1924" rel="self"/>
  <title>freshnerd.myopenid.com</title>
  <updated>Tue Feb 09 03:55:25 -0800 2010</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1172</id>
    <published>2010-02-09T03:55:25-08:00</published>
    <updated>2010-03-24T03:31:35-07:00</updated>
    <title>[ActionScript] number guessing game 1000 to 9999</title>
    <content type="html">&lt;p&gt;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&lt;/p&gt;

&lt;pre&gt;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JOptionPane;

public class Assignment2 {
	ArrayList&amp;lt;Integer&amp;gt; num;
	
	
	public Assignment2 ( ){
		// fill in code here
		// initialization
		num= new ArrayList&amp;lt;Integer&amp;gt;();
	}

	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, &amp;quot;Think of a number between 1000 and 9999.\n Click OK when you are ready...&amp;quot;, &amp;quot;Let's play a game&amp;quot;, JOptionPane.INFORMATION_MESSAGE);
		int numMatches = 0;
		int myguess = 0;
		
		do {
			myguess = gamer.myGuessIs();
			if (myguess == -1) {
				JOptionPane.showMessageDialog(null, &amp;quot;I don't think your number exists.\n I could be wrong though...&amp;quot;, &amp;quot;Mistake&amp;quot;, JOptionPane.INFORMATION_MESSAGE);
				System.exit(0);
			}
			String userInput = JOptionPane.showInputDialog(&amp;quot;I guess your number is &amp;quot; + myguess + &amp;quot;. How many digits did I guess correctly?&amp;quot;);
			// 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, &amp;quot;Your input is invalid. Please enter a number between 0 and 4&amp;quot;, &amp;quot;Warning&amp;quot;, JOptionPane.WARNING_MESSAGE);
				numMatches = 0;
			}
			// the number of matches must be between 0 and 4
			if (numMatches &amp;lt; 0 || numMatches &amp;gt; 4) {
				JOptionPane.showMessageDialog(null, &amp;quot;Your input is invalid. Please enter a number between 0 and 4&amp;quot;, &amp;quot;Warning&amp;quot;, 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(&amp;quot;Aha, I got it, your number is &amp;quot; + myguess + &amp;quot;.&amp;quot;);
		System.out.println(&amp;quot;I did it in &amp;quot; + gamer.totalNumGuesses() + &amp;quot; turns.&amp;quot;);
	}
}
&lt;/pre&gt;</content>
    <author>
      <name>freshnerd.myopenid.com</name>
      <email>vmetayer@student.umass.edu</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1172-number-guessing-game-1000-to-9999" rel="alternate"/>
  </entry>
</feed>

