如果else语句需要帮助

时间:2010-05-29 14:43:35

标签: c#

我正在尝试做一个其他声明,告诉用户游戏以平局结束(tic tac toe game)。我得到它在哪里工作,如果玩,并有一个赢家,它将显示另一种形式通过if声明宣布获胜者但我无法弄清楚它的抽奖部分。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace MyGame
{
    public class Result1
    {


        static private int[,] Winners = new int[,]
                   {
                       // main gameplay Ex: if x is on 0,1,2 x is the winner
                        {0,1,2},
                        {3,4,5},
                        {6,7,8},
                        {0,3,6},
                        {1,4,7},
                        {2,5,8},
                        {0,4,8},
                        {2,4,6},
                   };


        static public bool CheckWinner(Button[] myControls)
        {
            //bolean statement to check for the winner 
            bool gameOver = false;
            for (int i = 0; i < 8; i++)
            {
                int a = Winners[i, 0];
                int b = Winners[i, 1];
                int c = Winners[i, 2];

                Button b1 = myControls[a], b2 = myControls[b], b3 = myControls[c];
                if (b1.Text == "" || b2.Text == "" || b3.Text == "")
                    continue;

                if (b1.Text == b2.Text && b2.Text == b3.Text)
                {

                    b1.BackColor = b2.BackColor = b3.BackColor = Color.LightCoral;
                    b1.Font = b2.Font = b3.Font = new System.Drawing.Font("Microsoft Sans Serif", 32F, System.Drawing.FontStyle.Italic & System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
                    gameOver = true;
                    xWinnerForm xWinnerForm = new xWinnerForm();
                    xWinnerForm.ShowDialog(); //only works with show not showDialog method gets overloaded  (b1.Text + " is the Winner"); to get around this I added and image showing the last player


                }
                 //else statement here for draw what code would I put in?



            }


                   return gameOver;

        }



    }

}

1 个答案:

答案 0 :(得分:2)

当所有空间都被填满且没有获胜者时,游戏以平局结束。

您的检查不应该在检查每个获胜行的for循环中,而应该在for循环后进行单独检查。此时,检查所有控件'Text是否为非空白,如果是,则表示您有抽奖(如果有人赢了之前的代码就会发现)。