|
-
Jun 12th, 2007, 02:26 AM
#1
Thread Starter
Junior Member
-
Jun 12th, 2007, 04:22 AM
#2
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.
-
Jun 12th, 2007, 01:32 PM
#3
Re: Matermind using c#
We must at least attempt to cater for the mentally handicapped.
I don't live here any more.
-
Jun 12th, 2007, 02:23 PM
#4
Re: Matermind using c#
Should you really be attempting a 'mastermind' game then?
-
Jun 12th, 2007, 07:37 PM
#5
Thread Starter
Junior Member
Re: Matermind using c#
mastermind is my given topic...I have no choice but to do it...
-
Jun 12th, 2007, 07:42 PM
#6
Thread Starter
Junior Member
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;
}
-
Jun 12th, 2007, 07:43 PM
#7
Thread Starter
Junior Member
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
}
-
Jun 12th, 2007, 09:11 PM
#8
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.
-
Jun 13th, 2007, 02:03 AM
#9
Thread Starter
Junior Member
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...
-
Jun 13th, 2007, 02:11 AM
#10
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.
-
Jun 13th, 2007, 02:21 AM
#11
Thread Starter
Junior Member
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
-
Jun 13th, 2007, 02:35 AM
#12
Re: Matermind using c#
 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.
-
Jun 13th, 2007, 02:54 AM
#13
Thread Starter
Junior Member
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?
-
Jun 13th, 2007, 03:27 AM
#14
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.
-
Jun 17th, 2007, 09:02 PM
#15
Thread Starter
Junior Member
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?
-
Jun 17th, 2007, 09:04 PM
#16
Thread Starter
Junior Member
Re: Matermind using c#
p.s i already try doing wat u suggest...just need things to clarified
-
Jun 17th, 2007, 09:11 PM
#17
Thread Starter
Junior Member
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?
-
Jun 17th, 2007, 09:53 PM
#18
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.
-
Jun 17th, 2007, 10:16 PM
#19
Thread Starter
Junior Member
Re: Matermind using c#
sori but can u give me an example of how to write...
-
Jun 17th, 2007, 10:39 PM
#20
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.
-
Jun 17th, 2007, 10:55 PM
#21
Thread Starter
Junior Member
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...
-
Jun 17th, 2007, 10:57 PM
#22
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.
-
Jun 17th, 2007, 11:30 PM
#23
Lively Member
Re: Matermind using c#
Bitmap myimage = new Bitmap("c:\\image.jpg");
pictureBox1.Image = (Image)myimage;
-
Jun 18th, 2007, 01:52 AM
#24
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.
-
Jun 19th, 2007, 10:04 PM
#25
Thread Starter
Junior Member
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.
-
Jun 19th, 2007, 10:19 PM
#26
Lively Member
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.
-
Jun 19th, 2007, 10:20 PM
#27
Thread Starter
Junior Member
Re: Matermind using c#
i already import it to my resource file. Which i think is already inside my programme
-
Jun 19th, 2007, 11:43 PM
#28
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
-
Jun 19th, 2007, 11:45 PM
#29
Thread Starter
Junior Member
Re: Matermind using c#
Sori...But i never heard of container control before
-
Jun 20th, 2007, 12:10 AM
#30
Re: Matermind using c#
the actual name of the control is "image list".
-
Jun 20th, 2007, 12:29 AM
#31
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.
-
Jun 25th, 2007, 08:03 PM
#32
Thread Starter
Junior Member
Re: Matermind using c#
Thanks people...jmcilhinney...I finally understand wat u were trying to tell me...
-
Jun 25th, 2007, 08:07 PM
#33
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|