Results 1 to 33 of 33

Thread: Matermind using c#

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Exclamation Matermind using c#

    I'm currently doing my project on mastermind game...The thing is i dont know how to go abt writing this program...And not just that...I tried using drag-and-drop for the pegs but i dont really like the outcome of it...Any suggestion?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Matermind using c#

    Ask a specific question if you are having a specific issue. Tell us what you're trying to achieve, how you're trying to achieve it (including code if appropriate) and what's wrong with the outcome.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Matermind using c#

    We must at least attempt to cater for the mentally handicapped.
    I don't live here any more.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Matermind using c#

    Should you really be attempting a 'mastermind' game then?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Unhappy Re: Matermind using c#

    mastermind is my given topic...I have no choice but to do it...

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Matermind using c#

    Ok...This is one of the code that i get for the mastermind game...I've look at it and try to understand it but cant...It looks simple but i'm not sure...


    Code:
    Private float CalculateFromMastermindBoard()
    {
    // cycle through all of the current rows on the board and calculate a fitness score
    // based upon how each of the guessing row's peg score matches against the Genome
    // as if the Genome were the current solution
    float fFitnessScore = 0.0f;
    for (int i = 0; i < TheBoard.CurrentRow; i++)
    {
    // calculate a score using the Board's calculate function against the next guessed row 
    int[] result = TheBoard.CalcScore(GetIntArray(TheArray), i);
    // compare the actual peg score of the next guessed row to the peg score of the Genome against that same row
    int numCorrectInRow = CompareToScore(i, result); 
    // Normalize the fitness score a little bit (not really necessary, since the AutoPlayer takes the highest fitness score in 
    // final generation)
    fFitnessScore += ((float)numCorrectInRow)/4.0f;
    }
    // this function goes through each peg score of the current peg row and compares it to the 
    // peg score that was calculated against the same row and returns the # of pegs that match against
    // it (including blank pegs)
    private int CompareToScore(int nCompareRow, int[] testpegs)
    {
    int nScoreMatch = 0;
    for (int i = 0; i < 4; i++)
    {
    int nPeg = TheScore.GetPeg(i, nCompareRow);
    if (testpegs[i] == nPeg)
    {
    nScoreMatch++;
    }
    }
    return nScoreMatch;
    }
    fFitnessScore += .02f; 
    // give it some value greater than 0
    return fFitnessScore;
    }
    public override float CalculateFitness()
    {
    CurrentFitness = CalculateFromMastermindBoard();
    return CurrentFitness;
    }

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Matermind using c#

    here is another of the code...

    Code:
    public void Draw(Graphics g)
    {
    // cycle through the Grid Matrix and place the
    // indicated colored peg at the indicated location
    for (int i = 0; i < Grid.GetLength(0); i++)
    {
    for (int j = 0; j < Grid.GetLength(1); j++)
    { 
    // a Grid value of 0 indicates an empty slot
    // draw an empty hole 
    if (Grid[i,j] == 0)
    { 
    Rectangle r = new Rectangle(PegHole.Left, PegHole.Top, PegHole.Width, PegHole.Height);
    r.Offset(i* (PegHole.Width + SPACING), j* (PegHole.Height + SPACING));
    r.Offset(MARGIN, MARGIN);
    g.DrawEllipse(Pens.Black, r);
    g.FillEllipse(Brushes.Black, r);
    }
    else
    {
    // there is a peg here, draw the colored peg
    Rectangle r = new Rectangle(Peg.Left, Peg.Top, Peg.Width, Peg.Height);
    r.Offset(i* (PegHole.Width + SPACING), j* (PegHole.Height + SPACING));
    r.Offset(MARGIN, MARGIN);
    Brush aBrush = Brushes.Black;
    // This routine retrieves the brush matching
    // the integer at the grid point
    aBrush = GetBrush(Grid[i,j]);
    // draw the colored peg with the determined brush 
    Pen aPen = new Pen(aBrush,1);
    g.FillEllipse(aBrush, r); 
    g.DrawEllipse(Pens.Black, r);
    aPen.Dispose();
    }
    }
    }
    // draw the score column 
    TheScore.Draw(g);
    // draw the color peg choice panel 
    ThePanel.Draw(g);
    }
    
    Listing 1.0 - Drawing the entire board of Mastermind
    
    The last interesting code to look at in this project is the scoring algorithm.  The score is determined by first figuring out the locations that the player matched exactly and marking them.  Then the white pegs are determined by going through each peg in the solution row and checking it against all the pegs of the player row. Again, arrays are used extensively here:
    
    public int CalcScore()
    {
    int nExact = 0;
    int nThere = 0;
    int nCount = 0;
    int[] places = new int[4]{-1, -1, -1, -1};
    int[] places2 = new int[4]{-1, -1, -1, -1}; 
    // match exact rows first, this is easier
    for (int i = 0; i < 4; i++)
    {
    if (GuessingRow[i] == Grid[i, CurrentRow])
    {
    nExact++;
    TheScore.AddBlackPeg(CurrentRow, nCount);
    nCount++;
    places[i] = 1;
    places2[i] = 1;
    }
    }
    // check the special case where the player may have solved the puzzle
    if (nExact == 4)
    {
    return nExact; 
    }
    // now check for all the white pegs, a bit trickier
    for (int i = 0; i < 4; i++)
    {
    for (int j = 0; j < 4; j++)
    {
    // make sure the positions aren't the same and the pegs in the different positions
    // both in the solution and the player row haven't already been determined
    if ((i != j) && (places[i] != 1) && (places2[j] != 1))
    {
    // if the GuessedRow color matches the Grid Color at a different position
    // We have a white peg
    if (GuessingRow[i] == Grid[j, CurrentRow])
    {
    nThere++;
    TheScore.AddWhitePeg(CurrentRow, nCount);
    nCount++;
    places[i] = 1;
    places2[j] = 1;
    j = 5; // force a break, (I know, you can also use break;)
    }
    }
    }
    }
    return nExact; // return the # of exact (black pegs) that matched
    }

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Matermind using c#

    What are we supposed to do with that? Like I said, if you have a specific issue then ask a specific question. This is your homework so it's not for us to do it for you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Matermind using c#

    Look...my problem is that i dun understand the code above...I just need someone to explain the code so that i can DO MY HOMEWORK...

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Matermind using c#

    I would think the idea is that you write the code yourself, not just get someone else's and use that. If you understand what functionality your game needs then you should be able to make an attempt at implementing that functionality in code yourself. If you do that then we can help you with issues you encounter along the way.

    The idea with homework is that you answer the question yourself, not get someone else's answer and have someone explain it to you so you can pass it off as your own. That seems an awful lot like cheating to me.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Matermind using c#

    i dont even know where to begin...How am i suppose to know how to write...and i dont even know how the game works

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Matermind using c#

    Quote Originally Posted by redladie
    i dont even know where to begin...How am i suppose to know how to write...and i dont even know how the game works
    Exactly my point. How are you supposed to implement a game in C# if you don't even know how the game works? That means your first step is to find out how the game works. That's got nothing to do with C#. I'm quite sure that you can find out on the Web or from a friend how the game works first, then you can start thinking about how it might be implemented in code.

    What do you think a professional developer does? Goes and finds working code and then tries to understand it, or gathers the requirements of the project first and then considers how to implement functionality that meets those requirements? The same is true of any project in any field. You don't start with a working example and then work backwards to a spec. You start with a specification and if you don't have at least a rough spec then you shouldn't even be considering writing any code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Matermind using c#

    ok...now i understand that part...I've done drawing a flow chart to help me understand better...

    My next question is...Mastermind game need the player to select a row of colors in order to guess the hidden code... what can i use to help the player select the color....

    I've tried using drag and drop where i drag a picture from the picture box and drag it to the label...But, i find it difficult to drag the picture when the label size is limited...

    What i have in mind is to have a few buttons line up...When i press a button, a picture will appear...Is that possible?

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Matermind using c#

    It's possible. You can display an Image in a PictureBox, make a PictureBox that already contains an Image appear by setting its Viible property to True, or you can use GDI+ to draw the Image directly onto a control.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Matermind using c#

    ok...i'm assuming your viible is visible...But i dun get one thing...How does it help me to get wat i want... Like when i click the button, the picture appear?

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Matermind using c#

    p.s i already try doing wat u suggest...just need things to clarified

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Matermind using c#

    wat if i need more then i picture to appear in the same picture box but using other buttons? How can i achieve tat?

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Matermind using c#

    If you want to display an image in a PictureBox when a Button is clicked then you handle the Click event of the Button, create an Image object and assign it to the Image property of the PictureBox. If you want to display a different image in the same PictureBox when a different Button is clicked then you handle the Click event of that Button, create a different Image object and assign it to the Image property of the same PictureBox.

    In your case it would be logical to include your images as resources, which you would add to your application on the Resources tab of the project properties. You would then access them in via properties of the Properties.Resources object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Matermind using c#

    sori but can u give me an example of how to write...

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Matermind using c#

    Are you saying that you don't know how to handle the Click event of a Button? If that's the case then I suggest that you find a beginner's tutorial and work your way through it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Matermind using c#

    i know how to do those click thing but wat i dun know is that...for every button click, i have to set to the same picture box... Is tat even posible... i dun get wat u mean...

  22. #22
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Matermind using c#

    If you know how to set the Image property of a PictureBox when one Button is clicked then you know how to do the same thing for multiple Buttons. You just do the same thing multiple times.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  23. #23
    Lively Member mikelynch's Avatar
    Join Date
    May 2006
    Location
    Goombungee Qld
    Posts
    83

    Re: Matermind using c#

    Bitmap myimage = new Bitmap("c:\\image.jpg");
    pictureBox1.Image = (Image)myimage;

  24. #24
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Matermind using c#

    you could do that or you could have the picture in a container control and assign its reference to the picture box on the button click. Only do this if there are just a few different pictures to choose from.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Matermind using c#

    I tried using the Bitmap myimage = new Bitmap ("green.jpg");
    pictureBox1.Image = (Image)myimage;
    But i got error. They mention something about the parameter not valid.

  26. #26
    Lively Member mikelynch's Avatar
    Join Date
    May 2006
    Location
    Goombungee Qld
    Posts
    83

    Re: Matermind using c#

    I don't think you get it. In this case green.jpg needs to be a file you have . and you need to include the path of where it is you have stored this file on your drive.

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Matermind using c#

    i already import it to my resource file. Which i think is already inside my programme

  28. #28
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Matermind using c#

    Then maybe you should be using the container control.

    i don't know how to do this in c#, but it shouldn't be too much different than this basic equivalent:
    form1.picture1.Picture = ImageList1.ListImages(1).Picture
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  29. #29

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Matermind using c#

    Sori...But i never heard of container control before

  30. #30
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Matermind using c#

    the actual name of the control is "image list".
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  31. #31
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Matermind using c#

    ok i experimented with the problem in c# myself. I got it to display from an imagelist control into the picture box like this:
    pictureBox1.Image = imageList1.Images[1];
    HOWEVER there is one issue: The control will set every picture inside it to the same size. If you have multiple sizes you need more than one control.
    You can add to the control images in run or design time, your choice, and it can load them from a resource file.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  32. #32

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Matermind using c#

    Thanks people...jmcilhinney...I finally understand wat u were trying to tell me...

  33. #33

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Matermind using c#

    I finally get how to have afew pictures to appear in one picturebox using afew buttons... How do i get the programme to shift to the next picturebox when picture already selected for the first picturebox...I know that i have to use loop statement...but i dont know how to go about writing it...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width