Click here to Skip to main content
15,938,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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:
C#
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:
C#
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;
   }
 
Share this answer
 
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 ...
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900