11 Replies - 3146 Views - Last Post: 05 March 2013 - 08:07 AM Rate Topic: -----

#1 eli14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-March 13

Help with mastermind game

Posted 02 March 2013 - 10:31 AM

Hey everyone

I have been working on the mastermind game in c# but currently am stuck. Currently my program works but it is having difficulty spotting when the user has entered the correct colour (in my code number) and position. Below is a statement i use to try and find if the user has entered a correct guess

It seems to think all guesses are a correct Black pin guess after you have mad ea correct black pin guess once.
  int[] GuessArray = new int[N];

         for(int i=0; i < N; i++)
         {
             Console.Write("Please enter your guess: ");
             GuessArray[i] = Convert.ToInt16(Console.ReadLine());

             if (GuessArray[i] == numbers[i] && numbers.Contains(GuessArray[i]))
             {
                 guessblack = true;
                 

             }
             else if (numbers.Contains(GuessArray[i]))
             {
                 guesswhite = true;
                 
             }
             else
             {
                 wrong = true;
                 
             }


             //Show if a guess was correct or not

             if (guessblack == true)
             {
                 Console.WriteLine("=======================");
                 Console.WriteLine("Black Pin");
                 Console.WriteLine("======================");

             }
             else if (guesswhite == true)
             {
                 Console.WriteLine("=======================");
                 Console.WriteLine("White Pin");
                 Console.WriteLine("======================");
             }
             else
             {
                 Console.WriteLine("=======================");
                 Console.WriteLine("Incorrect");
                 Console.WriteLine("======================");
             }

         }
        }



this is how i create the scret code

//Create the secret code
            Random random = new Random();

            int[] numbers = new int[N];

            Console.WriteLine();

            for (int i = 0; i < numbers.Length; i++)
            {
                numbers[i] = random.Next(1, M);
                
                Console.WriteLine(numbers[i]);
                
            }



I hope someone can help me.
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Help with mastermind game

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Help with mastermind game

Posted 02 March 2013 - 11:25 AM

you need to reset the value of your guesbBlack and guesswhite variables at the top of your for loop, otherwise once they get set to true they will stay true.

This post has been edited by Nakor: 02 March 2013 - 11:26 AM

Was This Post Helpful? 0
  • +
  • -

#3 eli14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-March 13

Re: Help with mastermind game

Posted 02 March 2013 - 01:24 PM

hey thanks alot that worked

could you tell me why this is not working for calculating if the user has won the game.

int correctguesses;
correctguesses = 0;

            

             if (guessblack == true)
             {
                 Console.WriteLine("=======================");
                 Console.WriteLine("Black Pin");
                 Console.WriteLine("======================");
                 correctguesses = correctguesses + 1;


             }
             else if (guesswhite == true)
             {
                 Console.WriteLine("=======================");
                 Console.WriteLine("White Pin");
                 Console.WriteLine("======================");
             }
             else
             {
                 Console.WriteLine("=======================");
                 Console.WriteLine("Incorrect");
                 Console.WriteLine("======================");
             }

             if (correctguesses == 4)
             {
                 Console.WriteLine("YOU HAVE WONT THE GAME");
                 Console.ReadLine();
             }



the correct guesses doesn't show up, is there a better way of establishing weather they have won the game or not?

thanks in advance
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Help with mastermind game

Posted 02 March 2013 - 05:39 PM

you're resetting your correctguesses variable to 0 each time. You need to initialize it to 0 outside of the loop.

This post has been edited by Nakor: 02 March 2013 - 05:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 eli14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-March 13

Re: Help with mastermind game

Posted 03 March 2013 - 09:52 AM

Thanks once again that was very helpful.
One final thing i would like to ask, I am trying to save the users previous guesses and display them to the user after each set of guesses, but i am struggling with the logic. This is what i have so far
//History of users guesses
                    Console.WriteLine("Guess History");

                    //for loop to get the generate the number of guesses
                   for (int c = 1; c < (N + 1); c++)
                        {
                            Console.Write(" " + c + " ");
                            
                            
                        }


                    //Make it look pretty
                    Console.Write(" | B  W ");
                    Console.WriteLine();
                    Console.WriteLine("------------------");

                    int[] TempGuesses = new int[N];


                    //For loop to loop through and display the users preivous guesses.
                    for (int i =0; i < N; i++)
                    {

                        TempGuesses[i] = HistoryGuesses[i];
                        Console.Write(" " + HistoryGuesses[i] + " ");
                        
                    }

                    //Outputs the users gueses along with the correct colours and positions.
                    Console.Write(" | " + CorrectPositions + "  " + CorrectColours);
                    Console.WriteLine();
                    Console.WriteLine();



HistoryGuesses is just a copy of the array of original guesses. I can get it show the current guesses but not sure how to save each round of guesses so I can print it out to the user after each set of guesses. Any help would be appreciated.

Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Help with mastermind game

Posted 03 March 2013 - 10:08 AM

Step away the keyboard for awhile, and think about the steps.

Imagine you are doing a pencil and paper version of the game.
What would each step of progress through the game be?
Write down those instructions.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3738
  • View blog
  • Posts: 13,067
  • Joined: 12-December 12

Re: Help with mastermind game

Posted 03 March 2013 - 10:12 AM

You could use a multi-dimensional array to store previous (historic) details. MSDN on arrays

Alternatively, you could investigate storing details in a text file, Configuration.AppSettings or even a database.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3738
  • View blog
  • Posts: 13,067
  • Joined: 12-December 12

Re: Help with mastermind game

Posted 03 March 2013 - 10:19 AM

AdamSpeight2008 's advice is better though. In particular, a multi-dimensional array is messy because you don't (as yet) know how many rows you might want to store.
Was This Post Helpful? 0
  • +
  • -

#9 eli14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-March 13

Re: Help with mastermind game

Posted 03 March 2013 - 10:49 AM

I assume i would need to implement a queue but im not familiar with how to do that really.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3738
  • View blog
  • Posts: 13,067
  • Joined: 12-December 12

Re: Help with mastermind game

Posted 03 March 2013 - 11:03 AM

Questions to consider:

Is your current array of a fixed size? How many guesses might they make (for a particular game?) that you need to store?
How many previous sets of guesses do you want to keep? If you only want their most recent guesses (for the previous game) then you might just create another array previousGuesses[] and copy historyGuesses[] into it before starting a new game.

BTW Do you really need to copy the values to tempGuesses[]? You might rename this to previousGuesses[], as it seems to be performing the function I mentioned in my previous paragraph.

BTWW It is more likely to be a Stack than a Queue :). That is, when the user requests the history they are more likely to want to start with the most recent game. But if they might want to retrieve game 2, or game 5, then neither seems appropriate.

This post has been edited by andrewsw: 03 March 2013 - 11:05 AM

Was This Post Helpful? 0
  • +
  • -

#11 eli14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-March 13

Re: Help with mastermind game

Posted 04 March 2013 - 01:59 PM

okay thanks for the reply guys, i have been requested to use a queue so i have to use that instead of a stact. I have this code which currently does not work can any tell me why;

This code is in the main playgame() method


                int currentRow = 0;

                Console.Write("| B W\n");

                for (int y = 0; y < 4; y++)
                    Console.Write("--");



                Console.Write("------\n");
                add(CorrectPositions);
                add(4 - CorrectPositions); //add white
                currentRow++;
                publicRows = currentRow;
                print_queue();




and this code is the code for the queue

 public static int back = -1;   // newest entry added here
        public static int[] data = new int[100];
        public static int publicPos;
        public static int publicRows;

        static void add(int i)
        {
            back++;
            data[back] = i;
        }

        static int size()
        {
            return (back + 1);
        }

        static void print_queue()
        {
            int len = size(), x = 0, i = 0, j = 0;
            while (x < publicRows)
            {
                for (i = 0; i < (publicPos + 2); i++)
                {
                    if (i == publicPos)
                        Console.Write("| ");
                    Console.Write(data[i + j] + " ");
                }
                Console.WriteLine();
                j = j + (publicPos + 2);
                x++;
            }
        }





why does this not show me the guesses of the user in a queue

any help would be appreciated.
Thanks
Was This Post Helpful? 0
  • +
  • -

#12 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: Help with mastermind game

Posted 05 March 2013 - 08:07 AM

If you have to keep an unknown number of answers to an unknown number of puzzles, why not use a dictionary? Store the puzzle as the key, and a List of the answers.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1