2在一排C#程序游戏 [英] 2 in a row c# program game

查看:153
本文介绍了2在一排C#程序游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是游戏板的只是给你它的样子(这个委员会将扩大到7x6)

的想法

我想要做的是检测一个胜利者时,2种颜色是一排类似的游戏conmect四考虑到对角的组合了。但我想用蛮力枚举与出要做到这一点..

这在code的推移我已经我没有要求的解决方案我只是需要一个有效的算法有点帮助的计划背后

 命名空间WindowsFormsApplication1
{
    公共部分类Form1中:形态
    {

        私人按钮[] btns;
        私人按钮[] btns2;

        公共Form1中()
        {
            的InitializeComponent();

            btns =新按钮[] {按钮2,按钮3};
            btns2 =新按钮[] {将Button4,button5};


        }

        私人无效Form1_Load的(对象发件人,EventArgs的)
        {

            的foreach(在btns VAR BTN)
            {
                btn.Enabled = FALSE;
                btn.BackColor = Color.LightCyan;
            }

            的foreach(在btns2 VAR BTN)
            {
                btn.Enabled = FALSE;
                btn.BackColor = Color.LightCyan;
            }
        }
        公众诠释状态;
        INT CC = 0;
        私人无效的button1_Click(对象发件人,EventArgs的)
        {
            的foreach(在btns VAR BTN)
            {
                  {
                    如果(!btn.Enabled)
                    {
                        btn.Enabled = TRUE;

                        如果(CC == 0)
                        {
                            CC = 1;
                            btn.BackColor = Color.Red;
                        }
                        其他
                        {
                            CC = 0;
                            btn.BackColor = Color.Yellow;
                        }
                        返回;
                    }

                }
            }
        }

        私人无效button6_Click(对象发件人,EventArgs的)
        {
            的foreach(在btns2 VAR BTN)
            {
                如果(!btn.Enabled)
                {
                    btn.Enabled = TRUE;

                    如果(CC == 0)
                    {
                        CC = 1;
                        btn.BackColor = Color.Red;
                    }
                    其他
                    {
                        CC = 0;
                        btn.BackColor = Color.Yellow;

                    }

                    返回;
                }
            }
        }
    }
}
 

解决方案

首先,对于效率和理智,我会保持一个二维数组在我局的状态。

二,检测赢得状态,因为你开始游戏了( presumably 的)空板,你只能得到的的双赢状态时,一个按钮改变状态。而如果按钮改变状态让您进入一个双赢的状态,则必须参与该赢状态按钮(即它必须是你行的一部分)。

所以......你不需要暴力破解整个董事会。你只需要确定这只是改变状态的按钮是线的一部分。换句话说,只看上面的按钮,下面,向左和向右(也许对角线,你的问题是不明确的,如果你提供对角线),看看他们是否对视了一眼,你改了相同的颜色。如果他们中的任何一个,那么这是一个双赢的状态。这是用一个二维数组会让你的生活变得更加容易。如果在按钮( X 的)发生了变化,那么你只需要检查( X-1 ),( X + 1 的),( X Y-1 )和( X Y + 1 的),(也许对角线),并确保做,当然适当的边界检查。

扩展这一至3,4或在连续多不困难得多,除了需要记住你可能在一排的中间,而不是一个端或其他。

未优化伪$ C $,连续下2(请注意,我已经切换到指南针点,避免了左,上右,等等,因为我觉得它变得有点笨拙):

  //细胞是,去年的变化,它有一个X和Y属性和颜色属性的单元格
//板是一个成员变量,细胞的2D阵列。注[0,0]是董事会的左上角(NW)一角。
// boardHeight和boardWidth与电路板的尺寸成员变量
董事会//板[boardWidth-1,boardHeight-1]是右下(SE)一角
//一场胜利返回true,否则为false
功能checkWin(小区)返回布尔{
    //检查西
    如果(cell.x> 0安培;&放大器;电路板[cell.x  -  1,cell.y]。颜色== cell.color)
        返回true;
    //检查西北
    如果(cell.x大于0&安培;&安培; cell.y大于0&安培;&安培;板[cell.x-1,cell.y-1]。颜色== cell.color)
        返回true;
    //检查北
    如果(cell.y大于0&安培;&安培;板[cell.x,cell.y-1]。颜色== cell.color)
        返回true;
    //检查东北
    如果(cell.y大于0&安培;&安培; cell.x&其中; boardWidth&安培;&安培;板[cell.x + 1,cell.y-1]。颜色== cell.color)
        返回true;
    //检查其他方向是留给作为练习读者,希望你明白了吧
    返回false;
}
 

如果你正在做超过2,我会考虑一个递归函数来匹配的单元格数目算到左,右,上,下​​,和diagnoals

  // k是细胞在连续数为胜利
功能checkWin(小区)返回布尔{
    //检查东/西
    诠释计数= checkWest(小区);
    如果(计数> K)
         返回true;
    数+ = checkEast(小区);
    如果(计数> K)
         返回true;
    //检查NW / SE
    数= checkNW(小区);
    如果(计数> K)
         返回true;
    数+ = checkSE(小区);
    如果(计数> K)
         返回true;
    //等,检查N / S和NE / SW
    返回false;
}

功能checkWest(小区)返回INT {
    //基本情况,检查的界限!
    如果(cell.x == 0)
        返回0;
    //碱的情况下,下一个到这一个不匹配的细胞
    如果(板[cell.x-1,cell.y]。颜色!= cell.color)
        返回0;
    //递归,请在该行的下一个单元格
    返回1 + checkWest(板[cell.x-1,cell.y]);
 }
 

here is the game board just to give you an idea of how it looks like (this board will be expanded to a 7x6)

what i want to do is detect a winner when 2 colors are in a row similar to the game "conmect four" taking into account diagonal combos too. BUT i want to do this with out using brute-force enumeration..

this the code that goes behind the program i have made I'm not asking for solution i just need a bit of help on an effective algorithm

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        private Button[] btns;
        private Button[] btns2;

        public Form1()
        {
            InitializeComponent();

            btns = new Button[] { button2, button3 };
            btns2 = new Button[] { button4, button5 };


        }

        private void Form1_Load(object sender, EventArgs e)
        {

            foreach (var btn in btns)
            {
                btn.Enabled = false;
                btn.BackColor = Color.LightCyan;
            }

            foreach (var btn in btns2)
            {
                btn.Enabled = false;
                btn.BackColor = Color.LightCyan;
            }
        }
        public int state;
        int cc = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            foreach (var btn in btns)
            {
                  {
                    if (!btn.Enabled)
                    {
                        btn.Enabled = true;

                        if (cc == 0)
                        {
                            cc = 1;
                            btn.BackColor = Color.Red;
                        }
                        else
                        {
                            cc = 0;
                            btn.BackColor = Color.Yellow;
                        }
                        return;
                    }

                }
            }       
        }

        private void button6_Click(object sender, EventArgs e)
        {
            foreach (var btn in btns2)
            {
                if (!btn.Enabled)
                {
                    btn.Enabled = true;

                    if (cc == 0)
                    {
                        cc = 1;
                        btn.BackColor = Color.Red;
                    }
                    else
                    {
                        cc = 0;
                        btn.BackColor = Color.Yellow;

                    }

                    return;
                }
            }
        }
    }
}

解决方案

First, for efficiency and sanity, I'd keep the state of my board in a 2D array.

Second, for detecting win states, given that you start the game with a (presumably) empty board, you can only get into a win state when a button changes state. And if the button changing state puts you into a win state, then that button must be involved in that win state (i.e. it must be part of you line).

So...you don't need to brute-force the whole board. You only need to determine if the button that just changed state is part of a line. In other words, look only at the buttons to above, below, to the left and to the right (and maybe diagonal, your question wasn't clear if you included diagonals) to see if they are the same color as the one you changed. If any one of them is, then this is a win state. This is where using a 2D array will make you life much easier. If the button at (x, y) is changed, then you only need to check (x-1, y), (x+1, y), (x, y-1) and (x, y+1), (and maybe diagonals) making sure to do appropriate boundary checks, of course.

Extending this to 3, 4 or more in a row isn't much more difficult, except you need to remember you might be in the middle of a row rather than one end or the other.

Unoptimized Pseudo Code for 2 in a row (note, I've switched to compass points to avoid up-left, up-right, etc because I feel it gets a bit unwieldy):

// cell is the cell that last changes, it has an x and y property and a color property
// board is a member variable, a 2D array of cells. Note [0,0] is the upper-left (NW) corner of the board.
// boardHeight and boardWidth are member variable with the dimensions of the board
// board[boardWidth-1, boardHeight-1] is the lower-right (SE) corner of the board
// returns true for a win, false otherwise
function checkWin(cell) returns bool {
    // check west
    if (cell.x > 0 && board[cell.x - 1, cell.y].color == cell.color)
        return true;
    // check northwest
    if (cell.x > 0 && cell.y > 0 && board[cell.x-1, cell.y-1].color == cell.color)
        return true;
    // check north
    if (cell.y > 0 && board[cell.x, cell.y-1].color == cell.color)
        return true;
    // check northeast
    if (cell.y > 0 && cell.x < boardWidth && board[cell.x+1, cell.y-1].color == cell.color)
        return true;
    // checking the other directions is left as an exercise for the reader, hopefully you get the point
    return false;
}

If you are doing more than 2, I'd think about a recursive function to count the number of matching cells to the left, right, up, down, and diagnoals

// k is the number of cells in a row for a win
function checkWin(cell) returns bool {
    // check west / east
    int count = checkWest(cell);
    if (count > k)
         return true;
    count += checkEast(cell);
    if (count > k)
         return true;
    // check nw / se
    count = checkNW(cell);
    if (count > k)
         return true;
    count += checkSE(cell);
    if (count > k)
         return true;
    // and so on, checking N/S and NE/SW
    return false;
}

function checkWest(cell) returns int {
    // base case, check the boundaries!
    if (cell.x == 0)
        return 0;
    // base case, the cell next to this one doesn't match
    if (board[cell.x-1,cell.y].color != cell.color)
        return 0;
    // recursion, check the next cell in the line
    return 1 + checkWest(board[cell.x-1,cell.y]);
 }

这篇关于2在一排C#程序游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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