Quantcast

Maximum PC

It is currently Fri Nov 28, 2014 1:09 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 19 posts ] 
Author Message
 Post subject: help with random code generator
PostPosted: Sun Oct 03, 2010 1:50 pm 
8086
8086

Joined: Thu Jul 30, 2009 7:49 am
Posts: 74
I have been trying to create a mastermind game in c#. I have created a random color code generator, that never chooses purple. I can't see why, so any help would be appreciated.
Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace Mastermind
{
    class Mastermind
    {
        private static colors[] code, guess;
        private static bool correct;

        public static void Main(String[] arguments)
        {
            //irrelevant commented out code
            for (int i = 0; i < 3; i++)
            {
                CodeGen();
                System.Threading.Thread.Sleep(360);
                System.Console.WriteLine((int)code[0] + " " + (int)code[1] + " " + (int)code[2] + " " + (int)code[3]);
            }
            System.Threading.Thread.Sleep(2000);
        }

        private static void CodeGen()
        {
            code = new colors[4];
            double onedivsix = 1.0 / 6.0, num;
            Random thing = new Random();
            for (int i = 0; i < 4; i++)
            {
            top:
                num = thing.NextDouble();
                if (num < onedivsix)
                {
                    if (Array.IndexOf(code, colors.Purple) != -1)
                    {
                        goto top;
                    }
                    else
                    {
                        code[i] = colors.Purple;
                    }
                }
                else if (num < (2 * onedivsix))
                {
                    if (Array.IndexOf(code, colors.Orange) != -1)
                    {
                        goto top;
                    }
                    else
                    {
                        code[i] = colors.Orange;
                    }
                }
                else if (num < (3 * onedivsix))
                {
                    if (Array.IndexOf(code, colors.White) != -1)
                    {
                        goto top;
                    }
                    else
                    {
                        code[i] = colors.White;
                    }
                }
                else if (num < (4 * onedivsix))
                {
                    if (Array.IndexOf(code, colors.Red) != -1)
                    {
                        goto top;
                    }
                    else
                    {
                        code[i] = colors.Red;
                    }
                }
                else if (num < (5 * onedivsix))
                {
                    if (Array.IndexOf(code, colors.Blue) != -1)
                    {
                        goto top;
                    }
                    else
                    {
                        code[i] = colors.Blue;
                    }
                }
                else //if (num <= 1)
                {
                    if (Array.IndexOf(code, colors.Yellow) != -1)
                    {
                        goto top;
                    }
                    else
                    {
                        code[i] = colors.Yellow;
                    }
                }
            }
        }
     
        //several irrelevant, half finished methods
    enum colors
    {
        Purple, Orange, White, Red, Blue, Yellow
    }
}


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Sun Oct 03, 2010 4:56 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
It looks like you're making things quite a bit harder than they need to be...

Code:
CL-USER> (defvar colors (make-array 6 :initial-contents '(Purple Orange White Red Blue Yellow)))
COLORS
CL-USER> (defun random-color ()
           (aref colors (random 6)))
RANDOM-COLOR
CL-USER> (dotimes (i 8)
           (print (random-color)))

WHITE
YELLOW
ORANGE
WHITE
BLUE
BLUE
PURPLE
WHITE


Just curious - why do you need to create separate threads for this app?


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Mon Oct 04, 2010 4:03 pm 
8086
8086

Joined: Thu Jul 30, 2009 7:49 am
Posts: 74
Sorry, I'm a beginner, and my code probably isn't the best. I used the thread.sleep method to pause execution, because that's what I was taught (I know it's sloppy). Your code example doesn't make sense to me, the syntax doesn't seem to be c#.


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Mon Oct 04, 2010 6:23 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
freak720 wrote:
Sorry, I'm a beginner, and my code probably isn't the best. I used the thread.sleep method to pause execution, because that's what I was taught (I know it's sloppy).

Why are you pausing execution? You realize that the computer will block (ie wait) for user input when you ask for it, right?

freak720 wrote:
Your code example doesn't make sense to me, the syntax doesn't seem to be c#.

It's not C#. I figured that it was close enough.
Code:
colors = make-array of length 6 with initial-contents {Purple Orange White Red Blue Yellow}  //global var
function random-color ()
  return colors[random(6)]


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Mon Oct 04, 2010 9:24 pm 
8086
8086

Joined: Thu Jul 30, 2009 7:49 am
Posts: 74
The current main function is temporary, I only need it to test the codegen function. I am pausing execution both as an extra (probably excessive) precaution while debugging, as quick repetition of the random function can result in it repeatedly returning the same number, and to give me time to read the output without having to fire up the command line. Part of the reason for the mess of code is that I don't want any repeats in the pattern. I changed the code to use a function of the Random class I must have missed back when I wrote the code.

the new codegen function:

Code:
        private static void CodeGen()
        {
            code = new colors[4];
            int num;
            Random thing = new Random();
            for (int i = 0; i < 4; i++)
            {
            top:
                //pick a int between 0 (inclusive) and 6 (exclusive)
                num = thing.Next(0,6);
                if (num == 0)
                {
                    if (Array.IndexOf(code, colors.Purple) != -1)  // Possible mistake around Here?
                    {                                              //
                        goto top;                                  //
                    }                                              //
                    else
                    {
                        code[i] = colors.Purple;
                    }
                }
                else if (num == 1)
                {
                    if (Array.IndexOf(code, colors.Orange) != -1)
                    {
                        goto top;
                    }
                    else
                    {
                        code[i] = colors.Orange;
                    }
                }
                else if (num == 2)
                {
                    if (Array.IndexOf(code, colors.White) != -1)
                    {
                        goto top;
                    }
                    else
                    {
                        code[i] = colors.White;
                    }
                }
                else if (num == 3)
                {
                    if (Array.IndexOf(code, colors.Red) != -1)
                    {
                        goto top;
                    }
                    else
                    {
                        code[i] = colors.Red;
                    }
                }
                else if (num == 4)
                {
                    if (Array.IndexOf(code, colors.Blue) != -1)
                    {
                        goto top;
                    }
                    else
                    {
                        code[i] = colors.Blue;
                    }
                }
                else //if (num == 5)
                {
                    if (Array.IndexOf(code, colors.Yellow) != -1)
                    {
                        goto top;
                    }
                    else
                    {
                        code[i] = colors.Yellow;
                    }
                }
            }
        }


It still won't choose purple.

is the if block marked near the top of the function the culprit? I can't see anything wrong with it, but how can I be sure? I suppose it is worth mentioning that I am using Microsoft Visual C# Express.

EDIT: Slept on it, realized I should have used a switch statement. I'll work on it after school. Will also clean up the loop to get rid of the goto statements.


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Tue Oct 05, 2010 4:25 pm 
8086
8086

Joined: Thu Jul 30, 2009 7:49 am
Posts: 74
I figured it out.
Code:
Array.IndexOf(code, colors.Purple) == -1
always returns true. It turns out, when I initialize the colors array, each element is set to purple, not null. Therefore, until the array is filled, it will not set an element to purple (it turns out the type int, which enumerations are based on, is non-nullable) , at which point it exits the for loop. Working code below.

Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace Mastermind
{
    class Mastermind
    {
        private static colors[] code, guess;
        private static bool correct;

        public static void Main(String[] arguments)
        {
            for (int i = 0; i < 8; i++)
            {
                CodeGen();
                System.Threading.Thread.Sleep(360);
                System.Console.WriteLine((int)code[0] + " " + (int)code[1] + " " + (int)code[2] + " " + (int)code[3]);
            }
            System.Threading.Thread.Sleep(2000);
        }

        private static void CodeGen()
        {
            code = new colors[4];
            int num;
            Random thing = new Random();
            for (int i = 0; i < 4;)
            {
                //pick a int between 0 (inclusive) and 6 (exclusive)
                num = thing.Next(0,6);
                switch (num)
                {
                    case 0:
                        if (Array.IndexOf(code, colors.Purple) == -1)
                        {
                            code[i] = colors.Purple;
                            i++;
                        }
                        break;
                    case 1:
                        if (Array.IndexOf(code, colors.Orange) == -1)
                        {
                            code[i] = colors.Orange;
                            i++;
                        }
                        break;
                    case 2:
                        if (Array.IndexOf(code, colors.White) == -1)
                        {
                            code[i] = colors.White;
                            i++;
                        }
                        break;
                    case 3:
                        if (Array.IndexOf(code, colors.Red) == -1)
                        {
                            code[i] = colors.Red;
                            i++;
                        }
                        break;
                    case 4:
                        if (Array.IndexOf(code, colors.Blue) == -1)
                        {
                            code[i] = colors.Blue;
                            i++;
                        }
                        break;
                    case 5:
                        if (Array.IndexOf(code, colors.Yellow) == -1)
                        {
                            code[i] = colors.Yellow;
                            i++;
                        }
                        break;
                }
            }
        }
    }
   
    enum colors
    {
        Purple = 1, Orange, White, Red, Blue, Yellow
    }
}


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Wed Oct 06, 2010 3:34 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Pardon my French, but that is some fugly code... you wouldn't want me for a TA! =)

1) For starters, you should put the i++ in the for loop conditional (ie for (int i = 0; i < 4; i++) { case ... }

2) Why do you keep doing this?
if (Array.IndexOf(code, colors.Purple) == -1)

3) "<snip> random function can result in it repeatedly returning the same number <snip>"
This should occur occasionally. Why do you think that your thread change the behavior of the random number generator?

4) "Part of the reason for the mess of code is that I don't want any repeats in the pattern."
Repeats in what pattern? Is codeGen supposed to return four different colors?

5) In terms of the design, you should create two methods: codeGen and colorGen. codeGen calls colorGen 4 times... or randomColor. See below.

Code:
CL-USER> (defvar colors (make-array 6 :initial-contents '(Purple Orange White Red Blue Yellow)))  ;;an array of colors
COLORS
CL-USER> (defun random-color ()
           (aref colors (random 6)))      ;;returns a random color from the colors array
RANDOM-COLOR
CL-USER> (defun code-gen ()
      (let ((code (make-array 4)))    ;;create an array of length 4...
        (dotimes (i 4)                      ;;do 4 iterations...
          (setf (aref code i) (random-color)))   ;;fill it with random colors...
        code))  ;;return the array
CODE-GEN
CL-USER> (code-gen)                     ;;testing the code...
#(WHITE WHITE BLUE WHITE)
CL-USER> (code-gen)
#(RED RED YELLOW BLUE)
CL-USER> (code-gen)
#(WHITE YELLOW ORANGE WHITE)
CL-USER> (code-gen)
#(BLUE BLUE PURPLE WHITE)
CL-USER> (code-gen)
#(ORANGE WHITE ORANGE WHITE)
CL-USER> (code-gen)
#(ORANGE WHITE RED WHITE)
CL-USER> (code-gen)
#(WHITE BLUE RED ORANGE)
CL-USER>


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Wed Oct 06, 2010 4:24 pm 
8086
8086

Joined: Thu Jul 30, 2009 7:49 am
Posts: 74
Gadget wrote:
Pardon my French, but that is some fugly code... you wouldn't want me for a TA! =)

This is a personal project, my school is to small to have a CS class. :(

Gadget wrote:
4) "Part of the reason for the mess of code is that I don't want any repeats in the pattern."
Repeats in what pattern? Is codeGen supposed to return four different colors?

Yes. codeGen is supposed to return 4 different colors, no repeats.

Gadget wrote:
2) Why do you keep doing this?
if (Array.IndexOf(code, colors.Purple) == -1)

It will set the element in code[] and increment i only if the chosen color is not already in the code[]. Array.IndexOf returns -1 if the second augment is not found in the array, otherwise, it returns the index of the first occurrence.

Gadget wrote:
1) For starters, you should put the i++ in the for loop conditional (ie for (int i = 0; i < 4; i++) { case ... }

I don't see how I can without either
a) putting back in the goto statements, or
b) altering the behavior of the code.


Gadget wrote:
3) "<snip> random function can result in it repeatedly returning the same number <snip>"
This should occur occasionally. Why do you think that your thread change the behavior of the random number generator?

Yes, occasionally a random function should return the previous result again, but if you call the function to quickly, it will return the same value until you give it a break. quick, repeated calls could result in it returning
Code:
6,6,6,6,6,6,6...

That code is just me being excessively cautious while debugging. It will be removed.



Gadget wrote:
5) In terms of the design, you should create two methods: codeGen and colorGen. codeGen calls colorGen 4 times... or randomColor. See below.

Couldn't hurt. At this point I'm just happy it works, although I am open to suggestions for improving it. Thanks for your help!


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Thu Oct 07, 2010 2:44 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
freak720 wrote:
Gadget wrote:
4) "Part of the reason for the mess of code is that I don't want any repeats in the pattern."
Repeats in what pattern? Is codeGen supposed to return four different colors?

Yes. codeGen is supposed to return 4 different colors, no repeats.

Gotcha. That clears up a lot of the confusion with some of your code. I'm going to suggest that you take a different, and again, simpler approach.

Code:
pseudo-code...
function code-gen
code = array of length 4
colors = list of possible colors
color = null, just a temp variable

for i from 0 to 3
  color = random color from the colors list
  code[i] = color
  remove color from the colors list
return code

;;Actual working code...
(defun code-gen ()
  (let ((code (make-array 4))
   (colors '(Purple Orange White Red Blue Yellow))
   (color nil))
    (dotimes (i 4)
      (setf color (nth (random (length colors)) colors))
      (setf (aref code i) color)
      (setf colors (remove color colors)))
    code))

;;some test runs...
CL-USER> (code-gen)
#(WHITE ORANGE YELLOW BLUE)
CL-USER> (code-gen)
#(RED WHITE ORANGE YELLOW)
CL-USER> (code-gen)
#(WHITE BLUE PURPLE RED)
CL-USER> (code-gen)
#(ORANGE YELLOW BLUE WHITE)
CL-USER>


Normally, I'd write the code in a more lispy style, but I think this probably translates better for you.


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Thu Oct 07, 2010 2:53 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
freak720 wrote:
Gadget wrote:
3) "<snip> random function can result in it repeatedly returning the same number <snip>"
This should occur occasionally. Why do you think that your thread change the behavior of the random number generator?

Yes, occasionally a random function should return the previous result again, but if you call the function to quickly, it will return the same value until you give it a break. quick, repeated calls could result in it returning
Code:
6,6,6,6,6,6,6...

No. If the RNG that you're calling is implemented correctly, it will return a random value regardless of how little or much time passes between calls.

Are you confusing initially seeding a RNG with the current time for repeated calls to the RNG? In several languages, it is common to seed the RNG with the current time, however, you only seed the RNG with the time once, then make calls to something like a next() function.


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Wed Oct 13, 2010 2:24 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
How is the project coming along? What's next?

I wouldn't creating a Common Lisp port along side your C# version... =)


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Wed Oct 13, 2010 1:55 pm 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
Haha I may try this in Perl. Can't imagine it would be too hard :P


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Thu Oct 14, 2010 12:22 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Dwood15 wrote:
Haha I may try this in Perl. Can't imagine it would be too hard :P

Don't worry... I have faith in you finding a way! =P


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Mon Oct 18, 2010 10:51 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Did we lose freak720? This was an interesting thread for a little while there.

Dwood15, if you know how to play Mastermind, why don't you go ahead and define another module of code and we'll get the ball rolling again. Any part will be fine, we'll just work on it in a bottom-up style for now.


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Wed Oct 27, 2010 7:49 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Come on Dwood, don't let this thread die... Let's do this app!


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Thu Oct 28, 2010 4:50 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
I have never played mastermind... basic premise of the game? I'd be willing to take part if we can all agree that C# is the way to go :)


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Mon Nov 01, 2010 1:30 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
CrashTECH wrote:
I have never played mastermind... basic premise of the game?

I don't really know the premise. I glanced at the Wikipedia article a while back when I was busy, but I didn't have time to read or understand much about it. It's a board game, and you try and break the opponents code (or something).

CrashTECH wrote:
I'd be willing to take part if we can all agree that C# is the way to go :)

When Dwood made the Perl comment, I started thinking that it would be interesting to see how a few different language implementations compared to each other. To my eye, the game appears to be about the right level of complexity for this sort of thing: not trivial, not too complex, not huge, not a full-time job. I suggested the bottom-up approach so that people could start suggesting functionality without having to invest a lot of time in the design up front. I was also curious to see how the languages might adapt to changes using a bottom-up approach.

Anyways, I'm pretty booked until December. If someone hasn't suggested another bit of code to write by that time, I might post something to get the ball rolling again.


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Thu Nov 04, 2010 5:43 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Interesting. I will have to look into it. I might try to do something with XNA. It would be a good excuse to work on a game on the framework.


Top
  Profile  
 
 Post subject: Re: help with random code generator
PostPosted: Thu Sep 08, 2011 3:37 pm 
8086
8086

Joined: Thu Sep 08, 2011 3:32 pm
Posts: 1
private Color[] colors = { Color.Red, Color.Yellow, Color.Blue, Color.Green,
Color.DarkOrange, Color.Black, Color.Gray, Color.Purple };

Random rnd = new Random();
for (int x = 0; x < litir.Length; x++)
{
pictureBox1.BackColor = colors[rnd.Next(x)];
pictureBox2.BackColor = colors[rnd.Next(x)];
pictureBox3.BackColor = colors[rnd.Next(x)];
pictureBox4.BackColor = colors[rnd.Next(x)];
}
row1[0] = pictureBox1;
row1[1] = pictureBox2;
row1[2] = pictureBox3;
row1[3] = pictureBox4;
----
and pictureBox5-44 are filled with the guesses
and so on ... ;)

in this version the same color can come up more than once. That´s more fun.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.