开发者

Basic Guessing Game Mechanics in C#

开发者 https://www.devze.com 2023-04-13 02:26 出处:网络
I\'ve had quite a bit of experience with programming (three semesters teaching VBasic, C++, and Java), and now I\'m in college and I\'m taking a C# class, which is quite boring (the teacher knows less

I've had quite a bit of experience with programming (three semesters teaching VBasic, C++, and Java), and now I'm in college and I'm taking a C# class, which is quite boring (the teacher knows less than I do).

Anyways, for one of our exercises, we're creating a number guessing/lottery game. It works kind of like this:

  • User inputs three integers from 1-4 and clicks submit (I have them storing into an array)
  • Program generates three numbers from 1-4 (also in an array)
  • Function that checks matching runs and checks the two arrays
    • If all three match in order (i.e. 1,2,3 = 1,2,3 and NOT 1,2,3 = 1,3,2), matching = 4
    • If all three match NOT in order, matching = 3
    • If only two match, matching = 2
      • I want to make sure that only one match counts as one (i.e. [1,1,2][1,2,3] only gives one match to the user.
    • If only one matches, matching = 1
    • If no matches, matching stays at 0 (it's instantiated at submit_click)

I've got all of the code and GUI working except for the matching logic. I know I could do it with a LARGE amount of if statements, and I know cases would probably work, but I'm not as experienced with cases.

I'm not expecting my 'homework' to be done here, but I just want to know what method would be most effective to get this to correctly work (if it's easier to exclude the one match per item, then that's fine), and to possibly see some working code.

Thanks!

EDIT

I apologize if I come across as arrogant, I didn't mean to come across as a know-it-all (I definitely do not).

I have NOT taught classes, I've just taken classes from a teacher who's primarily a programming in and I'm at a community college and my professor isn't primarily a programming teacher.

I didn't take time to write a ton of if statements because I know that it would just get shot down as ineffective. I currently don't have the resources to test the answers, but as soon as I can I'开发者_StackOverflowll check them out and post back.

Again, I apologize for coming across as rude and arrogant, and I appreciate your answers more than you know.

Thanks again!


You can use a loop to achieve this functionality. I've used a list simply for ease of use, performing remove operations and the like. Something like this should work:

public static int getNumberOfMatches(List<int> userGuesses, List<int> machineGuesses) {
    // Determine list equality.
    bool matchedAll = true;
    for (int i = 0; i < userGuesses.Count; i++) {
        if (userGuesses[i] != machineGuesses[i]) {
            matchedAll = false;
            break;
        }
    }

    // The lists were equal; return numberOfGuesses + 1 [which equals 4 in this case].
    if (matchedAll) {
        return userGuesses.Count + 1;
    }

    // Remove all matches from machineGuesses.
    foreach (int userGuess in userGuesses) {
        if (machineGuesses.Contains(userGuess)) {
            machineGuesses.Remove(userGuess);
        }
    }

    // Determine number of matches made.
    return userGuesses.Count - machineGuesses.Count;
}


I think for the first case, for all matches in order you would scan the arrays together and maybe increment a counter. Since you mentioned you know c++, this would be

  int userGuesses[3];
  int randomGen[3];

  int matches = 0;
  for(int i=0; i < 3; i++) if(userGuesses[i] == randoGen[i]) matches++;

  if(matches == 3) //set highest score here.
  if(matches == 2) // next score for ordered matches etc.

For the not-in-order case, you will need to lookup the generated array for each user guess to see if it has that value.

0

精彩评论

暂无评论...
验证码 换一张
取 消