C# 需要帮助制作检查语句以确保所有控件都不是空的吗
这是一个井字游戏。我需要帮助做一个检查语句,看看是否所有控件的文本都是非空的,如果是,你有一个平局(如果有人赢了之前的代码,就会发现这一点)。你能用我的代码给我举个好例子吗C# 需要帮助制作检查语句以确保所有控件都不是空的吗,c#,C#,这是一个井字游戏。我需要帮助做一个检查语句,看看是否所有控件的文本都是非空的,如果是,你有一个平局(如果有人赢了之前的代码,就会发现这一点)。你能用我的代码给我举个好例子吗 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; namespace MyGame { publ
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
}
}
return gameOver;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用系统图;
名称空间MyGame
{
公开课成绩1
{
静态私有整数[,]Winners=新整数[,]
{
//主要游戏玩法示例:如果x在0,1,2,则x为赢家
{0,1,2},
{3,4,5},
{6,7,8},
{0,3,6},
{1,4,7},
{2,5,8},
{0,4,8},
{2,4,6},
};
静态公共bool CheckWinner(按钮[]myControls)
{
//bolean声明检查是否有获胜者
bool gameOver=false;
对于(int i=0;i<8;i++)
{
int a=优胜者[i,0];
int b=优胜者[i,1];
int c=优胜者[i,2];
按钮b1=myControls[a],b2=myControls[b],b3=myControls[c];
如果(b1.Text==“”| | b2.Text==“”| | b3.Text==“”)
继续;
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无衬线”,32F,System.Drawing.FontStyle.Italic和System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((System.Byte)(0));
gameOver=true;
xwinnexperform xwinnexperform=新的xwinnexperform();
xWinPerform.ShowDialog();//仅适用于show not ShowDialog方法重载(b1.Text+“是赢家”);为了解决这个问题,我添加了显示最后一个播放器的图像
}
}
返回游戏结束;
}
}
}
看起来所有控件都在myControls
数组中。您可以使用LINQ轻松地查询它们:
if (myControls.All(c => c.Text != "") {
// Draw
}
请注意,您已经有了检查空白的代码,并且在所有情况下,如果您发现了空白,则没有绘制。如果你遍历了所有可能的空格,但没有找到赢家或空空格,那么很明显这是一场平局
static public bool CheckWinner(Button[] myControls)
{
//bolean statement to check for the winner
bool gameOver = false;
bool foundEmpty = 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 == "")
{
foundEmpty = true;
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
}
}
if (!gameOver && !foundEmpty)
{
// must be a draw
gameOver = true;
}
return gameOver;
}
static public bool CheckWinner(按钮[]myControls)
{
//bolean声明检查是否有获胜者
bool gameOver=false;
bool foundEmpty=false;
对于(int i=0;i<8;i++)
{
int a=优胜者[i,0];
int b=优胜者[i,1];
int c=优胜者[i,2];
按钮b1=myControls[a],b2=myControls[b],b3=myControls[c];
如果(b1.Text==“”| | b2.Text==“”| | b3.Text==“”)
{
foundEmpty=true;
继续;
}
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无衬线”,32F,System.Drawing.FontStyle.Italic和System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((System.Byte)(0));
gameOver=true;
xwinnexperform xwinnexperform=新的xwinnexperform();
xWinPerform.ShowDialog();//仅适用于show not ShowDialog方法重载(b1.Text+“是赢家”);为了解决这个问题,我添加了显示最后一个播放器的图像
}
}
如果(!gameOver&&!foundEmpty)
{
//一定是平局
gameOver=true;
}
返回游戏结束;
}
我尝试了此操作,但当仅选择了3个框时会触发事件。如何解决此问题。我是编程新手,所以我还不是很了解它