- #1
Veronica_Oles
- 142
- 3
Homework Statement
Trying to make the connect four game on C#.
Homework Equations
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)
{
AboutBox1 help = new AboutBox1();
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: