如何让Connect 4重新获得胜利者 [英] How do I get Connect 4 to recorgnise a winner

查看:85
本文介绍了如何让Connect 4重新获得胜利者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程很新,而且经验很少。我目前正在用C#编写代码来玩连接4.我似乎无法编写代码来识别胜利者。

加上它非常凌乱



到目前为止我唯一的代码是



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

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>

推荐答案

因为只有最后一步才能确定获胜者,你只需检查它的邻居:

计算相同颜色的相邻按钮的数量。

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.
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;
}


这篇关于如何让Connect 4重新获得胜利者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆