Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9147025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:53:16+00:00 2026-06-17T10:53:16+00:00

I wondering if someone could help me, I have a game where the computer

  • 0

I wondering if someone could help me, I have a game where the computer generates a random 4 digit code, each digit ranging from 0-5. Then the user guesses this code and the computer returns an array that i print out to a string. The array should contain a 6 for every number in the guess that is the correct number in the correct place, a 7 for each number in the guess that is a correct number but wrong spot for it, and finally a 5 for any completely incorrect numbers.

Example,, if the code is: 0143
and the user’s guess is: 0451
the array should be: 6775

This is because the 0 is completely correct, the 1 and 4 are in the code, but were guessed in the wrong spot, and the 5 is completely incorrect. Also each digit is in a separate part of the arrays.

This is what I have so far:

for (int i = 0; i < 4; i++) { 
    if (combo[i] == guess[i]) { 
        Pegs[peg] = 6; 
        peg++; 
    } 
} 

for (int i = 0; i < 4; i++) { 
    for (int x = 0; x < 4; x++) { 
        if (guess[i] == combo[x] && guess[i] != combo[i]) { 
            Pegs[peg] = 7; 
            peg++; 
        } 
    } 
} 

for (int i = 0; i < 4; i++) { 
    if (Pegs[i] == 0) { 
        Pegs[i] = 5; 
    } 
} 

The array guess stores the users guess, the array combo is the correct code, Pegs[] is where the compared array is stored, and the int peg just says where to store the value in the array. The problem with this is it doesn’t always return the array correctly to what it to do.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:53:18+00:00Added an answer on June 17, 2026 at 10:53 am

    With the other methods, you will get into troubles if you have a scenario where

    int[] combot = new int[] {0, 1, 1, 3};
    int[] guess  = new int[] {0, 4, 5, 1};
    

    as you’ll get an incorrect [6, 7, 7, 5] instead of the correct [6, 7, 5, 5] because you’ll count your last guess 1 in double.

    By using two flags array, one for the exact matches and one for the misplaced guess a single flag array, you can achieve much better and accurate results :

    ** Edited ** I reverted it back because I have found that for combo = [0, 1, 1, 3] and guess = [3, 0, 1, 5], it was giving an incorrect response.

    public int[] computeGuess(int[] combo, int[] guess) {
        int[] result = new int[4];
    
        Arrays.fill(result, 5);  // incorrect values for all!
    
        boolean[] exactMatches = new boolean[4];  // all initially set to false
        boolean[] misplaced    = new boolean[4];
    
        for (int i = 0; i < 4; i++) {
            if (combo[i] == guess[i]) {
                exactMatches[i] = true;
                misplaced[i] = false;  // make sure we don't use this as misplaced
            } else {
                for (int j = 0; j < 4; j++) {
                    if (i != j && combo[i] == guess[j] && !exactMatches[j] && !misplaced[j]) {
                        misplaced[j] = true;
                        break;
                    }
                }   
            }
        }
    
        int i = 0;
        for (boolean b : exactMatches) {
            if (b) {
                result[i++] = 6;
            }
        }
        for (boolean b : misplaced) {
            if (b) {
                result[i++] = 7;
            }
        }
    
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering if someone could help me. I have next to no knowledge with Ajax,
I was wondering if someone could help me with this. I have defined my
Im wondering if someone could help me. I have hundreds of people uploading images
I was wondering if someone could help me. I have started using version control
I was wondering if someone could help me with a simple question? i have
I was wondering if someone could help me with the performance of this code
I was wondering if someone could help me. Suppose I have some classes as
I have reached a problem and was wondering if someone could help me please.
I have an SQL problem i was wondering if someone could help me out.
Im wondering if someone could help me .... I have 2 images, and i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.