0

I'm working on a connect 4 game and I've got the following part done:

I've made the board which is able to display tiles of the board in 3 colors (white, red and yellow). I've also written some code to display a box around the column on which the user is hovering such that he can see where he is placing a dish.

I've also created the code which allows a dish to be added to the board. Both of these processes use the following code:

//Calculate the size of a single cell
int cellX = this.Size.Width / this.Columns;

//calculate the cell which was clicked
int nColumn = (int)Math.Floor((Double)x / cellX);

x is the value of MouseEventArgs.X of the pannel on which this is called. For drawing a boundry box this code works perfectly but for dropping a dish it doesn't. sometimes it drops 1 left of where I want it sometimes one right.

Here is the code for both events:

//draws the bounding box around the column for a given x value. 
public void drawBoundingBox(int x)
{
    //Calculate the size of a single cell
    int cellX = this.Size.Width / this.Columns;

    //calculate the cell which was clicked
    int nColumn = (int)Math.Floor((Double)x / cellX);

    if (nColumn != this.lastColumn)
    {
        this.Refresh();

        Graphics g = CreateGraphics();
        Pen pRed = new Pen(Color.Red, 3);

        if (nColumn < this.Columns)
        {
            Rectangle rect = new Rectangle(new Point(nColumn * cellX, 0),
                                           new Size(cellX, this.Size.Height));

            g.DrawRectangle(pRed, rect);
        }
    }
    this.lastColumn = nColumn;
}

public Boolean move(int mousePosition) {
    int cellX = this.Size.Width / this.Columns;

    int nColumn = (int)Math.Floor((Double)mousePosition / cellX);

    return board.Move(nColumn);
}

Here is the code for board.move():

public bool Move(int x)
{
    bool found = false;
    for (int i = Rows - 1; i >= 0 && !found; i--)
    {
        Console.WriteLine("X equals: " + x + " i equals: " + i);
        if (States[i,x] == 0) {
            found = true;
            States[i, x] = (byte)(((moves++) % 2) + 1);
        }
    }
    return found;
}

Here is a gif showing what I mean: enter image description here

To me it seams like a rounding error but it's wierd to me that the bounding box works well with the mouse but the clicking action doesn't...

BRHSM
  • 854
  • 3
  • 13
  • 48
  • 1
    `this.Size` includes the non-client area like borders. You mention a panel, but it looks like you are using the dimensions of the form. Do favor the paint event instead of the CreateGraphics function. – LarsTech Jan 06 '21 at 15:23
  • you also have a leak here: `int cellX = this.Size.Width / this.Columns;` the value is rounded to the closest int although it might be a double, so you miss some important pixels here. – Eldshe Jan 06 '21 at 15:25
  • @LarsTech Sorry for the late response, According to [this](https://stackoverflow.com/a/8201484/4454435) you can use `MouseEventArgs.X` to get the position of the mouse relative to the control, this is what I'm using. The code above is called in the class of the panel so `this.Size` refers to the size of the panel, not the form. Sorry if this wasn't clear. Also can you explain what you mean with your last statement, it's not entirely clear to me? – BRHSM Jan 08 '21 at 09:22
  • If the panel is on the form, `this` refers to the form. While the panel's Mouse events do provide the relative coordinates of the control, you are calculating against `this.Size`, which is the form's dimensions. For the CreateGraphics issue, see [How to draw shapes in panels of windows form?](https://stackoverflow.com/a/8312568/719186) for an example. – LarsTech Jan 08 '21 at 14:13

0 Answers0