Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to fix my counter which increments each time a certain # is selected?
    primarykey
    data
    text
    <p>I am working on a guessing game assignment. The computer randomizes 3 letters from R/B/G/Y and the user tries to guess the correct order and position of the combination. A counter is incremented to give hints, ie. count how many letters and/or positions the user gets right with each guess until they narrow it down.</p> <p>This is my method that randomizes the computer's combo:</p> <pre><code>public static void newGame(){ // Randomize colour choice (# between 1 and 4) col1 = (int)(Math.random()*4)+1; col2 = (int)(Math.random()*4)+1; col3 = (int)(Math.random()*4)+1; switch ((int)col1){ case 1: ans1 = ("R"); break; case 2: ans1 = ("G"); break; case 3: ans1 = ("B"); break; case 4: ans1 = ("Y"); break; } switch ((int)col2){ case 1: ans2 = ("R"); break; case 2: ans2 = ("G"); break; case 3: ans2 = ("B"); break; case 4: ans2 = ("Y"); break; } switch ((int)col3){ case 1: ans3 = ("R"); break; case 2: ans3 = ("G"); break; case 3: ans3 = ("B"); break; case 4: ans3 = ("Y"); break; } colchoice = ans1 + " " + ans2 + " " + ans3; }//end newGame method </code></pre> <p>These are my counter methods:</p> <pre><code>public static void checkColoursCorrect(){ inputnumber(guess1,guess2,guess3); // Determine how many user inputted letters equal the randomized ones correctc = 0; if (uguess1 == col1 || uguess1 == col2 || uguess1 == col3){ correctc++; } if (uguess2 == col1 || uguess2 == col2 || uguess2 == col3){ correctc++; } if (uguess3 == col1 || uguess3 == col2 || uguess3 == col3){ correctc++; } }//end checkColoursCorrect method public static void checkPositionsCorrect(){ inputnumber(guess1,guess2,guess3); correctp = 0; if (uguess1 == col1){ correctp++; } if (uguess2 == col2){ correctp++; } if (uguess3 == col3){ correctp++; } if (uguess1 == col1 &amp;&amp; uguess2 == col2 &amp;&amp; uguess3 == col3){ System.out.println("User wins! Colour was " + colchoice); } }//end checkPositionsCorrect method </code></pre> <p>I think that, since the combo is randomized, each time the counter methods call col1/col2/col3, a NEW combo is used rather than the one initialized in the first place? If anyone could point me in the direction on how to fix this it would be appreciated.</p> <p>Input number method:</p> <pre><code>public static void inputnumber(String guess1, String guess2, String guess3){ // Convert user's FIRST guess to a number if ("R".equals(guess1)){ uguess1 = 1; } if ("G".equals(guess1)){ uguess1 = 2; } if ("B".equals(guess1)){ uguess1 = 3; } if ("Y".equals(guess1)){ uguess1 = 4; } // Convert user's SECOND guess to a number if ("R".equals(guess2)){ uguess2 = 1; } if ("G".equals(guess2)){ uguess2 = 2; } if ("B".equals(guess2)){ uguess2 = 3; } if ("Y".equals(guess2)){ uguess2 = 4; } // Convert user's FINAL guess to a number if ("R".equals(guess3)){ uguess3 = 1; } if ("G".equals(guess3)){ uguess3 = 2; } if ("B".equals(guess3)){ uguess3 = 3; } if ("Y".equals(guess3)){ uguess3 = 4; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload