last day (14 days later) » 

5:55 PM
0
A: How to determine 5 labels in a row of the same color in a C# array?

Brian RogersYou actually don't need recursion to solve this; just a couple of nested loops. Since you said you wanted to start just by checking for horizontal wins, I'll start there. I'll use the term "stone" to represent a space on the board that has been clicked to turn it black or white. If there are n...

 
I get an error that board does not exist in the current context. Don't I need to pass it as an argument in CheckForWinner?
 
If your board is not a member of the class where the method resides, then yes, you would need to pass the board to the method. I've updated my answer.
 
It is not. Could you please show that in an edit? I get errors with the variables you used after passing the array.
 
I updated my answer to show passing the board into the method. What variables are you having trouble with? Everything else is local to the method.
 
No errors just not working. Shouldn't the property be BackColor instead of Color when checking for background color of the label? Also, why did yo need to create a separate array to check for an answer instead of using the array of labels?
 
5:55 PM
The code above is checking the BackColor of the labels. The BackColor property of a label is of type Color, which is an enum. So if you want to make a variable that holds the BackColor (like I did for startColor), you have to declare that variable to be of type Color. Regarding the board, I did not create a separate array here; I am just using a parameter called board. When you call the method, you would pass in the existing board. The parameter is passed by reference, so it is the same board, not a copy.
 
Ahhhh I see. I tried inserting a messagebox.show before win=false and return startColor but it did not pop up when 5 pieces were in a row. Something must be wrong.
 
OK, perhaps it would help if I updated my answer to show how to call the method. I was not intending that you put the MessageBox.Show in this method, but instead that you would check the return value and use that to decide whether to show the message box.
 
Sure that would be helpful.
 
OK, I updated my answer. Hope that makes sense.
 
So I am still not getting the messagebox to pop up. Please see my edited code above.
 
5:55 PM
The problem is you've now got two click handlers. The first one, which I assume existed before, doesn't call the new method to check for a winner. That is why you never see the message. The second handler it looks like you added from my answer. I was trying to show that you would put that code inside your click handler, not add another one.
 
Am I able to simply combine those two? Should one statement come before the other or it doesn't matter?
 
Yes, you can combine them. Probably you would want to do the check for the winner before you do any other logic, e.g. to determine who has the next turn, but that is up to you to decide. It is your program.
 
Ok. I have one problem though with CheckForWinner. I have it in my Form1.cs and it says CheckForWinner does not exist in the current context. How do I deal with that?
 
If you've put CheckForWinner into a separate class (which is a different context from your form) then you'll need to make the method public, and call it by using the reference to that class, e.g. gamePlay.CheckForWinner().
 
Still no go. Can you please check my edit above?
Hi
Did you get a chance to check my edit?
 
5:57 PM
In your gamePlay class, make the CheckForWinner method public. It looks like it is currently marked private. Remove gamePlay.CheckForWinner() from the method signature of the your click handler in your form. That is invalid syntax and won't work. It should be private void labelClick (object sender, EventArgs e) as you had it before.
 
it is public
public Color? CheckForWinner(Label[,] board, int r, int c)
Still the same error though.
 
OK, Inside your click handler method use Color? winner = gamePlay.CheckForWinner(board, r, c);
 
An object reference is required for the nonstatic field, method, or property
gomokuproject.gameplay.checkforwinner
 
Sorry, you have to use the instance of gameplay so,Color? winner = obj.CheckForWinner(board, r, c);
 
(system.windows.forms.label[,],int,int)
 
6:01 PM
your instance of the gamePlay class is called obj right?
 
Great it works, but it say you win every time I click the mouse
 
That is because you added the MessageBox.Show inside the CheckForWinner method. Remove that.
 
Now it doesn't work again.
 
What do you mean by doesn't work? What happens?
 
nothing pops up when there are 5 labels in a row
 
6:06 PM
Does the code show in your question accurately reflect the current state of your code?
 
yes
I cant send you my code here if you want
'var'
 
Have you tried stepping through the code in the debugger to see what happens?
 
I mean there are no bugs
it simply can't tell when there are five labels in a row of the same color
if (board[i, j].BackColor == Color.Black && board[i + 1, j].BackColor == Color.Black && board[i + 2, j].BackColor == Color.Black &&
// board[i + 3, j].BackColor == Color.Black && board[i + 4, j].BackColor == Color.Black || board[i, j].BackColor == Color.White &&
// board[i + 1, j].BackColor == Color.White && board[i + 2, j].BackColor == Color.White &&
// board[i + 3, j].BackColor == Color.White && board[i + 4, j].BackColor == Color.White)
// {
// MessageBox.Show("You Win!");
this is how I was checking it before
 
This might be a stupid question, but when you are testing the program for 5 in a row, are the 5 in a row horizontal?
Here is my complete code; it works for me. You can try putting this code into a brand new project and see if it works for you.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

const int W = 19;
const int H = 19;
const int WinLength = 5;
Button[,] board = new Button[H, W];
Color nextTurn = Color.White;

private void Form1_Load(object sender, EventArgs e)
{
for (int r = 0; r < H; r++)
{
for (int c = 0; c < W; c++)
{
Button b = new Button();
board[r, c] = b;
b.Width = 25;
b.Height = 25;
b.Top = r * 25 + 25;
b.Left = c * 25 + 25;
b.TabIndex = r * H + c;
b.BackColor = Color.Transparent;
b.Click += board_Click;
Sorry about the indenting-- it didn't come through correctly.
 
6:24 PM
yes right now I am just trying to check horizontal
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GomokuProject
{
public partial class Form1 : Form
{
int labelCount = 0;
int iteration = 0;
public Label[,] board = new Label[19,19];
const int WinLength = 5;
const int BoardWidth = 19;
const int BoardHeight = 19;
gamePlay obj = new gamePlay();

public Form1()
{
InitializeComponent();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

namespace GomokuProject
{

class gamePlay
{
const int WinLength = 5;
const int BoardWidth = 19;
const int BoardHeight = 19;

public Color? CheckForWinner(Label[,] board, int r, int c)
{
Color startColor = board[r, c].BackColor;
for (int c1 = c - WinLength + 1; c1 <= c; c1++)
{
if (c1 >= 0 && c1 < BoardWidth && board[r, c1].BackColor == startColor)
This is my complete code. DO you see any mistakes?
 
6:35 PM
I found the problem
Remember before when you asked me about combining the two click handlers and you asked what order the statements should come in? I told you wrong-- you need to set the color of the current move before checking for the winner
It should look like this:
private void labelClick(object sender, EventArgs e)
{

Label x = (Label)sender;

if (x.BackColor == Color.Transparent)
{
if (iteration % 2 == 0)
{
x.BackColor = Color.Black;
}
else
{
x.BackColor = Color.White;
}
iteration++;
}
else
{

}

for (int r = 0; r < BoardHeight; r++)
{
for (int c = 0; c < BoardWidth; c++)
{
if (board[r, c] == x)
{
Color? winner = obj.CheckForWinner(board, r, c);
if (winner == Color.Black)
{
MessageBox.Show("Black is the winner!");
}
else if (winner == Color.White)
{
MessageBox.Show("White is the winner!");
If you make that change it works
 
Perfect!!!!
Thank you so much!!!!
 
Phew! I'm glad it finally is working!
 
Same! You're awesome!
 

last day (14 days later) »