views:

139

answers:

2

Hey guys!

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, does anyone have any idea how this can be achieved?

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);

            }

        }

    }

    private void pictureBoxCounter_Click(object sender, EventArgs e)
    {
        //I need some code here but nothing seems to work :(
    }

I have been spending way to long on this problem (about two weeks to be precise!), and 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!

+1  A: 

You'll need to find the clicked picture box back in the Click event handler. You can use the sender argument for that. Trivially:

    private void pictureBoxCounter_Click(object sender, EventArgs e)
    {
      PictureBox box = sender as PictureBox;
      box.BackColor = System.Drawing.Color.Coral;
    }

But you'll need to implement your game logic as well, which will require you know the box's location on the game board. You could use the PictureBox.Tag property for that. It could store a Point for example:

...
                    pictureBoxCounter[j, i].BackColor = System.Drawing.Color.Transparent;
                    pictureBoxCounter[j, i].Tag = new Point(j, i);
                    panelGame.Controls.Add(pictureBoxCounter[j, i]);
...

    private void pictureBoxCounter_Click(object sender, EventArgs e)
    {
      PictureBox box = sender as PictureBox;
      box.BackColor = System.Drawing.Color.Coral;
      Point pos = (Point)box.Tag;
      int row = pos.X;
      int col = pos.Y;
      //etc...
    }

But you probably want to use a helper class that stores more info than just the row and column number.

Hans Passant
Thank you soo much nobugzYou have made my day!
Exile
+2  A: 

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:

public class Board
{
   public const int Height = 8;
   public const int Width = 8;
   private Counters[Height][] Counters { get; set; }

   public Counter GetCounter(int row, int col)
   {
      return Counters[row][col];
   }
   public void Initialize() { }
   public void ExecuteMove(Counter c) { }
}

public class Counter
{
   public int Row { get; set; }
   public int Column { get; set; }
}

So, a Counter object knows where it is (its Row and Column). A Board object knows about all of the Counter objects, it knows how to find a Counter given 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 the ExecuteMove method.

You can now trivially write a method that prints the board to the console:

public void PrintBoard(Board b)
{
   for (int col = 0; col < board.Width; col ++)
   {
      for (int row = 0, row < board.Height; row++)
      {
         Counter c = board.GetCounter[row][col];
         Console.Write(c == null ? " " : "*");
      }
      Console.WriteLine();
   }
}

and a method to input a move:

public Counter InputMove(Board b)
{
   string s;
   Console.Write("Row: ");
   s = Console.ReadLine();
   int row = Convert.ToInt32(s);
   if (s == "") return null;
   Console.Write("Column: ");
   s = Console.ReadLine();
   if (s == "") return null;
   int column = Convert.ToInt32(s);
   return b.GetCounter(row, column);
}

...and now you have everything you need in order to code and test the ExecuteMove method. 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 PictureBox for each (with its position based on the Row and Column properties), and add it to a Dictionary<PictureBox, Counter> called, say, Counters. You'll also want a Dictionary<Counter, PictureBox> called, say, PictureBoxes. These maps give you the ability to find a Counter given its PictureBox, and vice versa.

Attach this event handler to each PictureBox:

private void PictureBox_Click(object sender, EventArgs e)
{
   PictureBox p = (PictureBox) sender;
   Counter c = Counters[p];
   Board.ExecuteMove(c);
}

(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 its Counter, 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 Visible property to the Counter class and have the Board class's ExecuteMove update it. If, when a Counter becomes invisible, the other Counters in its row move left or right, the ExecuteMove method will have to update their Row and Column, and do something with the now-invisible Counter that 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 their PictureBox based 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.

Robert Rossney
wow thanks for that! Yeah I took the game rules and my project offline after nobugz answered what I asked. (the rules are back online now) I will come back at look over this tomorrow evening as I have a deadline tomorrow I need to meet first! Thanks again Robert Rossney!
Exile