Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am very new to programming and have very little experience. I am currently writing a code in C# to play connect 4. I cant seem to write code to recognise a winner.
Plus it is very messy

The only code I have so far is

C#
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 WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
       // bool Red = true;  // sets the bool red to equal true
       // bool there_is_a_winner = false;
        bool turn = true;
        int turn_count = 0;

        public Form1()
        {
            InitializeComponent();

        }
        // I added the menu-strip event, to be able to exit the game cleanly
        private void exitGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();// This is the code to Exit game
        }
        // This event holds all 42 buttons under one name.
        private void Button_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;// object is vague/generic so initiated it to button b.

            if (turn)  // if turn is true change back colour to Blue
                b.BackColor = Color.Blue;
            else
                b.BackColor = Color.Red; // if false change back colour to Red

            turn=!turn;  // turn is true then it becomes not true, false

           b.Enabled = false;  // This is to stop the button from executing the next colour
        }



   <pre>
Posted
Comments
Dennis_E 29-Apr-14 7:10am    
You need to know the positions in order to determine a winner. You could put the buttons into a 2-dimensional array, for instance. Then you can loop over the lines to check if 4 adjacent buttons have the same color.

1 solution

Since only the last move can determine the winner, you only have to check its neighbors:
count the number of adjacent buttons with the same color.
C#
private const int RowCount = 6, ColumnCount = 7;
private Button[,] grid = new Button[RowCount, ColumnCount];
//You need to put the buttons in this grid somewhere in your code...

public bool Won(int row, int column) {
    return DownAdjacent(row, column) + 1 >= 4
        || LeftUpAdjacent(row, column) + RightDownAdjacent(row, column) + 1 >= 4
        || LeftDownAdjacent(row, column) + RightUpAdjacent(row, column) + 1 >= 4;
}

private int DownAdjacent(int row, int column) {
    Color color = grid[row, column].BackColor;
    int count = 0;
    for (int r = row - 1; r >= 0; --r, ++count) {
	if (grid[r, column].BackColor != color) break;
    }
    return count;
}

private int LeftUpAdjacent(int row, int column) {
    Color color = grid[row, column].BackColor;
    int count = 0;
    for (int r = row + 1, c = column - 1; r < RowCount && c >= 0; ++r, --c, ++count) {
	if (grid[r, c].BackColor != color) break;
    }
    return count;
}

private int RightDownAdjacent(int row, int column) {
    Color color = grid[row, column].BackColor;
    int count = 0;
    for (int r = row - 1, c = column + 1; r >= 0 && c < ColumnCount; --r, ++c, ++count) {
	if (grid[r, c].BackColor != color) break;
    }
    return count;
}

private int RightUpAdjacent(int row, int column) {
    Color color = grid[row, column].BackColor;
    int count = 0;
    for (int r = row + 1, c = column + 1; r < RowCount && c < ColumnCount; ++r, ++c, ++count) {
	if (grid[r, c].BackColor != color) break;
    }
    return count;
}

private int LeftDownAdjacent(int row, int column) {
    Color color = grid[row, column].BackColor;
    int count = 0;
    for (int r = row - 1, c = column - 1; r >= 0 && c >= 0; --r, --c, ++count) {
	if (grid[r, c].BackColor != color) break;
    }
    return count;
}
 
Share this answer
 
Comments
Dennis_E 29-Apr-14 9:01am    
And I forgot LeftAdjacent and RightAdjacent...

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