Click here to Skip to main content
14,603,886 members
Rate this:
Please Sign up or sign in to vote.
See more:
0


i work in visual studio on dots & boxes game, i make an array of dots in 4 rows and 4 column, and i draw lines between each two points, now i want when player1 click on one line, specific line change color to red, player2 click on other line, it change color to black.


What I have tried:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    Pen mypen = new Pen(Color.Blue, 2);

    e.Graphics.FillRectangle(Brushes.Green, 0, 0, 250, 250);
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            e.Graphics.FillEllipse(Brushes.Black, 32 + 48 * j, 32 + 48 * i, 10, 10);
        }
    }
    for (int i = 0; i <= 3; i++)
    {
        for (int j = 0; j <= 3; j++)
        {

            e.Graphics.DrawLine(mypen, 37 + 48 * j, 37 + 48 * i, 37 + 48 * j, 37 + 48 * (i + 1));
            e.Graphics.DrawLine(mypen, 37 + 48 * j, 37 + 48 * i, 37 + 48 * (j + 1), 37 + 48 * i);

        }
    }
}
Posted
Updated 5-Jun-19 23:29pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

First off, you need to change the ways you are doing things.
You need to store the "current status" of the game, and an enum is a good start:
public enum PlayLineStatus
   {
   Unclicked,
   PlayerOne,
   PlayerTwo,
   }
Then create an array (or other collection) of those to represent the current position of the game. Each time a player clicks on a line, you find with array element it is associated with, and check it's status: if it's "Unclicked" then he gets it and you update the array appropriately. If it isn't, he can't click there, so ignore it.

In your Paint handler, you loop through the array, drawing each line in it's appropriate colour according to the status:
Color toPaint;
switch (statusArrayElement)
   {
   default: thrown new ArgumentException("Unknown play status: " + statusArrayElement.ToString());
   case PlayLineStatus.Unclicked:
      toPaint = Color.Green;
      break;
   case PlayLineStatus.PlayerOne:
      toPaint = Color.Red;
      break;
   case PlayLineStatus.PlayerTwo:
      toPaint = Color.Black;
      break;
   }
   
Comments
phil.o 6-Jun-19 5:29am
   
OriginalGriff wrote:
thrown new ArgumentException("Unknown play status: " + statusArrayElement.ToString());
Predictive coding? :)
OriginalGriff 6-Jun-19 5:37am
   
Defensive!
I've lost track of the number of times an option has been added to an enum and not every usage gets updated... :laugh:
lameeszam 6-Jun-19 7:24am
   
I'm a beginner, is it possible to clarify more?
OriginalGriff 6-Jun-19 7:31am
   
Which bit? :laugh:

What part(s) do you understand? Not trying to be rude here - I only get exactly what you type, so I have no idea of your skill level or abilities ...
Rate this:
Please Sign up or sign in to vote.

Solution 2

When the click occurs you need to identify which line needs to be repainted in a different colour, and mark that somehow. You then call Invalidate to force a repaint of the panel (see Control.Update Method (System.Windows.Forms) | Microsoft Docs[^]). The repaint code should check which line to repaint in the different colour.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100