# Connect Four Games

## Homework Statement

Trying to make the connect four game on C#.

## The Attempt at a Solution

I need to make the horizontal check work which it is not working and I do not know why?
I need to also make the vertical check work for the other player and am not sure why its not working either?

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace C4
{
public partial class Form1 : Form
{

static int[,] board = new int[6, 7];
static Button[] one = new Button[6];
static Button[] two = new Button[6];
static Button[] three = new Button[6];
static Button[] four = new Button[6];
static Button[] five = new Button[6];
static Button[] six = new Button[6];
static Button[] seven = new Button[6];

static int row1 = 0;
static int row2 = 0;
static int row3 = 0;
static int row4 = 0;
static int row5 = 0;
static int row6 = 0;
static int row7 = 0;

int turn = 0;

static int games = 0;

static int player1_wins = 0;

static int player2_wins = 0;

public Form1()
{
InitializeComponent();

}

private void button28_Click(object sender, EventArgs e)
{
init_board();
button28.Enabled = false;

}

private void button16_Click(object sender, EventArgs e)
{

for (int i = 5; i > -1; i--)
{

if (board[i, 0] == 0)
{
if (turn == 0)
{
board[i, 0] = 1;
one[i].BackColor = Color.Red;
turn = 1;
row1++;
}
else
{
board[i, 0] = 2;
one[i].BackColor = Color.Yellow;
turn = 0;
row1++;
}

one[i].Visible = true;
i = -1;
}

}

if (row1 == 6)
{

button16.Enabled = false;
button16.Text = "Full";
}

textBox1.Text = check_winner_v().ToString();
textBox2.Text = check_winner_h().ToString();
}

private void init_board()
{

for (int i = 0; i < 6; i++)
{

for (int j = 0; j < 7; j++)
{

board[i, j] = 0;

}

}

}

private void button18_Click(object sender, EventArgs e)
{
{

for (int i = 5; i > -1; i--)
{

if (board[i, 2] == 0)
{
if (turn == 0)
{
board[i, 2] = 1;
three[i].BackColor = Color.Red;
turn = 1;
row3++;
}
else
{
board[i, 2] = 2;
three[i].BackColor = Color.Yellow;
turn = 0;
row3++;
}

three[i].Visible = true;
i = -1;
}

}

if (row3 == 6)
{

button18.Enabled = false;
button18.Text = "Full";
}

textBox1.Text = check_winner_v().ToString();
textBox2.Text = check_winner_h().ToString();
}
}

private void button19_Click(object sender, EventArgs e)
{
{

for (int i = 5; i > -1; i--)
{

if (board[i, 3] == 0)
{
if (turn == 0)
{
board[i, 3] = 1;
four[i].BackColor = Color.Red;
turn = 1;
row4++;
}
else
{
board[i, 3] = 2;
four[i].BackColor = Color.Yellow;
turn = 0;
row4++;
}

four[i].Visible = true;
i = -1;
}

}

if (row4 == 6)
{

button19.Enabled = false;
button19.Text = "Full";
}

textBox1.Text = check_winner_v().ToString();
textBox2.Text = check_winner_h().ToString();
}
}

private void button20_Click(object sender, EventArgs e)
{

for (int i = 5; i > -1; i--)
{

if (board[i, 4] == 0)
{
if (turn == 0)
{
board[i, 4] = 1;
five[i].BackColor = Color.Red;
turn = 1;
row5++;
}
else
{
board[i, 4] = 2;
five[i].BackColor = Color.Yellow;
turn = 0;
row5++;
}

five[i].Visible = true;
i = -1;
}

}

if (row5 == 6)
{

button20.Enabled = false;
button20.Text = "Full";
}

textBox1.Text = check_winner_v().ToString();
textBox2.Text = check_winner_h().ToString();
}

private void button8_Click(object sender, EventArgs e)
{

for (int i = 5; i > -1; i--)
{

if (board[i, 5] == 0)
{
if (turn == 0)
{
board[i, 5] = 1;
six[i].BackColor = Color.Red;
turn = 1;
row6++;
}
else
{
board[i, 5] = 2;
six[i].BackColor = Color.Yellow;
turn = 0;
row6++;
}

six[i].Visible = true;
i = -1;
}

}

if (row6 == 6)
{

button8.Enabled = false;
button8.Text = "Full";
}

textBox1.Text = check_winner_v().ToString();
textBox2.Text = check_winner_h().ToString();

}

private void button21_Click(object sender, EventArgs e)
{

for (int i = 5; i > -1; i--)
{

if (board[i, 6] == 0)
{
if (turn == 0)
{
board[i, 6] = 1;
seven[i].BackColor = Color.Red;
turn = 1;
row7++;
}
else
{
board[i, 6] = 2;
seven[i].BackColor = Color.Yellow;
turn = 0;
row7++;
}

seven[i].Visible = true;
i = -1;
}

}

if (row7 == 6)
{

button21.Enabled = false;
button21.Text = "Full";
}

textBox1.Text = check_winner_v().ToString();
textBox2.Text = check_winner_h().ToString();

}

private void Form1_Load(object sender, EventArgs e)
{
one[0] = button22;
one[1] = button23;
one[2] = button24;
one[3] = button25;
one[4] = button26;
one[5] = button27;

two[0] = button29;
two[1] = button30;
two[2] = button31;
two[3] = button32;
two[4] = button33;
two[5] = button34;

three[0] = button35;
three[1] = button36;
three[2] = button37;
three[3] = button38;
three[4] = button39;
three[5] = button40;

four[0] = button41;
four[1] = button42;
four[2] = button43;
four[3] = button44;
four[4] = button45;
four[5] = button46;

five[0] = button47;
five[1] = button48;
five[2] = button49;
five[3] = button50;
five[4] = button51;
five[5] = button52;

six[0] = button53;
six[1] = button54;
six[2] = button55;
six[3] = button56;
six[4] = button57;
six[5] = button58;

seven[0] = button59;
seven[1] = button60;
seven[2] = button61;
seven[3] = button62;
seven[4] = button63;
seven[5] = button64;

}

private void button65_Click(object sender, EventArgs e)
{
help.Show();
}

private void button66_Click(object sender, EventArgs e)
{
Form2 rules = new Form2();
rules.Show();
}

private Boolean check_winner_v()
{

Boolean winner = false;

for (int i = 0; i < 7; i++)
{

for (int j = 0; j < 3; j++)
{

if (board[j, 0] == 1 && board[j + 1, 0] == 1 && board[j + 2, 0] == 1 && board[j + 3, 0] == 1)
{

winner = true;

if (board[j, i] == 1)
{
player1_wins = 1;
}

else
{
player2_wins = 2;
}

}

}
}
return winner;

}

private Boolean check_winner_h()
{
Boolean winner2 = false;

{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 4; j++)
{
if (board[0, j] == 1 && board[0, j + 1] == 1 && board[0, j + 2] == 1 && board[0, j + 3] == 1 && board[0, j + 4] == 1)
{
winner2 = true;
}
}
}
return winner2;
}
}

private void button67_Click(object sender, EventArgs e)
{
label1.Visible = true;
label2.Visible = true;
label3.Visible = true;
label4.Visible = true;

textBox3.Text = games.ToString();
textBox4.Text = player1_wins.ToString();
textBox5.Text = player2_wins.ToString();

textBox3.Visible = true;
textBox4.Visible = true;
textBox5.Visible = true;
textBox6.Visible = true;
}

private void button17_Click_1(object sender, EventArgs e)
{
for (int i = 5; i > -1; i--)
{

if (board[i, 1] == 0)
{
if (turn == 0)
{
board[i, 1] = 1;
two[i].BackColor = Color.Red;
turn = 1;
row2++;
}
else
{
board[i, 1] = 2;
two[i].BackColor = Color.Yellow;
turn = 0;
row2++;
}

two[i].Visible = true;
i = -1;
}

}

if (row2 == 6)
{

button17.Enabled = false;
button17.Text = "Full";
}

textBox1.Text = check_winner_v().ToString();
textBox2.Text = check_winner_h().ToString();
}

}
}

Last edited by a moderator:

jedishrfu
Mentor
This is an unreasonable request. You can't just dump your program on PF and expect anyone to tell you why it doesn't work.

The proper way is to isolate the problem down to a few lines, show the data you got and why you think it isn't working. Often just by doing this you will discover the source of the problem for yourself.

Do you have access to a debugger where you can step through your code and verify things are working as you expected?

This is an unreasonable request. You can't just dump your program on PF and expect anyone to tell you why it doesn't work.

The proper way is to isolate the problem down to a few lines, show the data you got and why you think it isn't working. Often just by doing this you will discover the source of the problem for yourself.

Do you have access to a debugger where you can step through your code and verify things are working as you expected?
I did not mean to just dump it that was not my intention what so ever. Still trying to figure this out. I will fix it when I get the chance.

jedishrfu
Mentor
Its not the dumping action as much as the notion of :"I don't know whats wrong with my code here is my 1000 line program." Sometimes a programmer needs the whole pie to figure it out.

The issue is you need to isolate it down as much as you can. You need to describe the user actions you took to replicate the problem such that if I ran the same set of steps then I should see the same problem.

I'm trying to show you how a programmer will handle this issue as we do have these kinds of problems with our code all the time.

Do you have a debugger that you can use on your code?

Alternatively you can create a kind of audit trail using print statements everywhere to see what is going on. The Print statement is your friend and is the oldest debugging tool around.

Its not the dumping action as much as the notion of :"I don't know whats wrong with my code here is my 1000 line program." Sometimes a programmer needs the whole pie to figure it out.

The issue is you need to isolate it down as much as you can. You need to describe the user actions you took to replicate the problem such that if I ran the same set of steps then I should see the same problem.

I'm trying to show you how a programmer will handle this issue as we do have these kinds of problems with our code all the time.

Do you have a debugger that you can use on your code?

Alternatively you can create a kind of audit trail using print statements everywhere to see what is going on. The Print statement is your friend and is the oldest debugging tool around.
Shoot sorry I'll fix it. And quite sure I don't have have a debugging tool.

collinsmark
Homework Helper
Gold Member
And quite sure I don't have have a debugging tool.
I see that you've invoked System.Windows.Forms (one of your "using" statements). That tells me that you are probably using Microsoft Visual Studio to compile the program. Visual Studio has a built in debugger. (This is true even for the free version.)

Set a breakpoint (by clicking in the left margin) in your code in a place where you want to start checking stuff. You can step through your code using F10 and F11 (Step Over and Step Into). If you want to then let the code run automatically until the next breakpoint, click the "Continue" button.

When the code is halted, a debug window should open up allowing you to inspect local variables and also other variables on your "watch" list.

Mark44
Mentor

## Homework Statement

Trying to make the connect four game on C#.

## The Attempt at a Solution

I need to make the horizontal check work which it is not working and I do not know why?
I need to also make the vertical check work for the other player and am not sure why its not working either?
In addition to what jedishrfu said about dumping a long chunk of code on us with little explanation of what is wrong, is there some reason you posted two copied of what appears to be the same code?

In addition to what jedishrfu said about dumping a long chunk of code on us with little explanation of what is wrong, is there some reason you posted two copied of what appears to be the same code?
That was an accident.

I see that you've invoked System.Windows.Forms (one of your "using" statements). That tells me that you are probably using Microsoft Visual Studio to compile the program. Visual Studio has a built in debugger. (This is true even for the free version.)

Set a breakpoint (by clicking in the left margin) in your code in a place where you want to start checking stuff. You can step through your code using F10 and F11 (Step Over and Step Into). If you want to then let the code run automatically until the next breakpoint, click the "Continue" button.

When the code is halted, a debug window should open up allowing you to inspect local variables and also other variables on your "watch" list.
I will do that when I can.

jedishrfu
Mentor