Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java Student in Need of Help! [MasterMind game]

807588May 4 2009 — edited May 6 2009
/*
  Colors:
Red
White
Black
Blue
Yellow
Blue
Green

4 slots
*/
import java.util.*;

public class MasterMind
{
    @SuppressWarnings("unchecked")
	public static void main (String[] args)
	{
        Object strColor1, strColor2, strColor3, strColor4;
        String strUserColor1, strUserColor2, strUserColor3, strUserColor4;
        int intTurns = 1;
        int intTurnsRemaining;

		ArrayList colors = new ArrayList();
		colors.add ("Red");
		colors.add ("White");
		colors.add ("Black");
		colors.add ("Blue");
		colors.add ("Yellow");
		colors.add ("Green");

		Random generator = new Random();
		strColor1 = colors.get(generator.nextInt(6));
		strColor2 = colors.get(generator.nextInt(6));
		strColor3 = colors.get(generator.nextInt(6));
		strColor4 = colors.get(generator.nextInt(6));

        System.out.println ("Kevin Saul - CIS-129-01 Java Project 3");
        System.out.println ("Java MasterMind\n");
        System.out.println ("Colors to Choose from:" + colors +"\n");
		
		Scanner scan = new Scanner (System.in);
        System.out.println ("\nPick 4 colors (Format: Color Color Color Color)");
        strUserColor1 = scan.next();
        strUserColor2 = scan.next();
        strUserColor3 = scan.next();
        strUserColor4 = scan.next();
        System.out.println ("User Choices:" + strUserColor1 + strUserColor2 + strUserColor3 + strUserColor4);

        do {
            if (strUserColor1.equals(strColor1) || strUserColor2.equals(strColor2) || strUserColor3.equals(strColor3) || strUserColor4.equals(strColor4))
            {
                if (strUserColor1.equals(strColor1) && strUserColor2.equals(strColor2) && strUserColor3.equals(strColor3) && strUserColor4.equals(strColor4))
                {
                    System.out.println ("CONGRATULATIONS! YOU GUESSED ALL THE COLORS!");
                    intTurns = 11;
                }

                else if (strUserColor1 != strColor1)
                {
                    intTurns++;
                    System.out.println (strUserColor1 + " (Slot1) is incorrect guess. Try Again");
                    strUserColor1 = scan.next();
                }
                else if(strUserColor2 != strColor2)
                {
                    intTurns++;
                    System.out.println (strUserColor2 + " (Slot2) is incorrect guess. Try Again");
                    strUserColor2 = scan.next();
                }
                else if(strUserColor3 != strColor3)
                {
                    intTurns++;
                    System.out.println (strUserColor3 + " (Slot3) is incorrect guess. Try Again");
                    strUserColor3 = scan.next();
                }
                else if(strUserColor4 != strColor4)
                {
                    intTurns++;
                    System.out.println (strUserColor4 + " (Slot4) is incorrect guess. Try Again");
                    strUserColor4 = scan.next();
                }
                
            } else {                
                    intTurnsRemaining = (10 - intTurns);
                    System.out.println ("Unfortunately, you didn't guess any of the colors.");
                    System.out.println ("Turns Left: "+intTurnsRemaining);
                    System.out.println ("\nPick 4 colors (Format: Color Color Color Color)");
                    strUserColor1 = scan.next();
                    strUserColor2 = scan.next();
                    strUserColor3 = scan.next();
                    strUserColor4 = scan.next();
                    intTurns++;
                }
            }        
        while (intTurns < 10);
        System.out.println ("Computers Choices: " + strColor1 + strColor2 + strColor3 + strColor4);
    }
}
I know my code is sloppy, but its what is working so far. I am new to Java with not much coding background.
Anywho, my problems:
- Even is guess #1 is right, it says its wrong
- How do I make it each guess (or slot) be focused on before moving on to another slot (for re-guesses)
- Make the code not case sensitive. (I already tried (strUserColor#.equalsIgnoreCase(strColor#) but doesnt seem to work [Debug errors])

thanks in advance, loving the language so far but hitting some bumps
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 3 2009
Added on May 4 2009
17 comments
1,964 views