I’m trying to learn C# (so go easy on me!) and as part of a coursework I have been asked to create a game called strikeout.
The game rules are very simple. It is simply a case of knocking one counter into another, with the aim of leaving one counter left on the screen.
I am having trouble simply moving one counter into another. I have created a number of picture boxes using a loop (to make up my board), and created an event handler using the loop. How can I use the single even handler to move all of my counters?
{
(...)
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
int x = j * (75);
int y = i * (75);
pictureBoxCounter[j, i] = new PictureBox();
pictureBoxCounter[j, i].Location = new System.Drawing.Point(x, y);
pictureBoxCounter[j, i].Size = new System.Drawing.Size(75, 75);
pictureBoxCounter[j, i].BackColor = System.Drawing.Color.Transparent;
panelGame.Controls.Add(pictureBoxCounter[j, i]);
this.pictureBoxCounter[j, i].Click += new System.EventHandler(this.pictureBoxCounter_Click);
}
}
} // end of some function
private void pictureBoxCounter_Click(object sender, EventArgs e)
{
//I need some code here but nothing seems to work :(
}
I’ve spent way to long on this problem. Even asked my tutor to help. Instead of helping he managed to break most of my code. So after fixing the problems he caused I am now back with a compiling program!
(When running the program you will need to enter player info to enable the start game button.)
If I can help with any other information don’t hesitate to ask!
This programming problem appears intended to teach you about separation of concerns. If it isn’t, it should be. (It is now!)
You have two problems, not one. (Actually, you have more than two, but you have two big ones.) One problem is: How do I create a set of objects representing the counters in this game, and what rules do those objects follow? The other problem is: How do I represent the counters on the screen?
If your solution to the first problem is to create a bunch of
PictureBoxes, you’re going down a rabbit hole that it’s going to be tough to get back out of. You should solve the first problem first, and then the second problem.Here’s a rough sketch (very rough, because I don’t know the rules of this game) of an object model that addresses the first problem:
So, a
Counterobject knows where it is (itsRowandColumn). ABoardobject knows about all of theCounterobjects, it knows how to find aCountergiven its row and column, and it knows how to execute a move when a counter gets clicked on. (I don’t know anything about the rules of the game, but they’re going to live in theExecuteMovemethod.You can now trivially write a method that prints the board to the console:
and a method to input a move:
…and now you have everything you need in order to code and test the
ExecuteMovemethod. Get that working. I’ll wait.You done? Good. There are a couple of problems that you probably ran into that I haven’t addressed. For instance, you probably discovered that when you move a
Counter, you have to update both the board’s array and the Counter itself. And you also probably discovered that you have to come up with some way of keeping track of what happens when a Counter is destroyed. Aren’t you glad you weren’t screwing around with mouse clicks and UI elements too?Now for part two. Let’s make a UI that knows how to talk to the Board. In your form, loop through the counters in the board, create a
PictureBoxfor each (with its position based on theRowandColumnproperties), and add it to aDictionary<PictureBox, Counter>called, say,Counters. You’ll also want aDictionary<Counter, PictureBox>called, say,PictureBoxes. These maps give you the ability to find aCountergiven itsPictureBox, and vice versa.Attach this event handler to each
PictureBox:(This, by the way, is the answer to your original question.)
Well, that’s all well and good: you can click on a
PictureBox, find itsCounter, and execute the move. But how do you update the visual state of the game in the UI? That depends, a lot, on what the actual rules of the game are.For instance, if clicking on a counter makes it disappear, you might add a
Visibleproperty to theCounterclass and have theBoardclass’sExecuteMoveupdate it. If, when aCounterbecomes invisible, the otherCounters in its row move left or right, theExecuteMovemethod will have to update theirRowandColumn, and do something with the now-invisibleCounterthat was at that position.But you already worked all of this out back when you tested all of the game logic, right? All you need to implement in your UI is an equivalent method to the one you built for printing it to the console. Only this one iterates through all of the
Counters in the board, and updates theirPictureBoxbased on their state.There are a lot of subtleties that I’m glossing over here, mostly because a) I don’t fully understand your problem and b) I’m not actually trying to do your homework for you.