我试图在c#中创建一个基本的noughts并越过游戏。 到目前为止,我已经使用了按钮来制作网格,当按下该按钮时,文本会根据哪个玩家的转动而改变。 我坚持的部分是检查是否有任何一名球员获胜,我已经写过,但它似乎没有做任何事情。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="testo.css"/>
<title></title>
</head>
<body>
<h1>Test</h1>
<div class="chart">
<canvas id="lineChart" width="600" height="600"></canvas></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.bundle.min.js"></script>
<script type="text/javascript" src=script.js></script>
</body>
</html>
(Application.Restart();只是检查它是否有效的临时方法。) 这只是我必须作为胜利条件的8个陈述中的一个,我知道哪里出错了?
更新
private void Form1_Load(object sender, EventArgs e)
{
if (button1.Text == "X" && button5.Text == "X" && button9.Text == "X")
{
MessageBox.Show("Player", player_turntxt.Text + " wins");
Application.Restart();
}
}
private void button1_Click(object sender, EventArgs e)
{
int Player_Turn = Convert.ToInt32(player_turntxt.Text);
if (Player_Turn == 1)
{
button1.Text = "X";
player_turntxt.Text = "2";
button1.Enabled = false;
return;
}
else
{
button1.Text = "O";
player_turntxt.Text = "1";
button1.Enabled = false;
return;
}
答案 0 :(得分:1)
您应该创建一种方法来检查是否有任何玩家获胜,并在“按钮点击”的每个人的末尾运行它。
我建议您从按钮点击中删除“返回”。它们有时会引入意外的行为,比如在执行调用方法的代码行之前结束函数,以检查某人是否赢了。
这样的事情:
private void button1_Click(object sender, EventArgs e)
{
int Player_Turn = Convert.ToInt32(player_turntxt.Text);
if (Player_Turn == 1)
{
button1.Text = "X";
player_turntxt.Text = "2";
button1.Enabled = false;
}
else
{
button1.Text = "O";
player_turntxt.Text = "1";
button1.Enabled = false;
}
CheckIfSomeoneHasWon();
}
让我们讨论一下CheckIfSomeoneHasWon的实现。我认为有几种方法可以做到这一点。我们可以使用按钮的值创建一个二维数组,并迭代它。事实上,我会这样做。你的方式也有效,但我们必须写很多东西。这就是我想出的:
static readonly string _player1_symbol = "X";
static readonly string _player2_symbol = "O";
static void CheckIfSomeoneHasWon()
{
string[,] userChoices = BuildUserChoices();
string winner = CheckWhoWon(userChoices);
if (winner != null)
{
// Somebody won! Display message and start over
}
}
private static string CheckWhoWon(string[,] values)
{
// Horizontal checks
for (int i = 0; i < 3; i++)
{
if (values[i, 0] == values[i, 1] && values[i, 1] == values[i, 2])
{
return (values[i, 0] == _player1_symbol) ? "player 1" : "player 2";
}
}
// Vertical checks
for (int i = 0; i < 3; i++)
{
if (values[0, i] == values[1, i] && values[1,i] == values[2,i])
{
return (values[i, 0] == _player1_symbol) ? "player 1" : "player 2";
}
}
// Diagonal checks
if (values[0, 0] == values[1, 1] && values[1, 1] == values[2, 2])
{
return (values[0, 0] == _player1_symbol) ? "player 1" : "player 2";
}
if (values[0, 2] == values[1, 1] && values[1, 1] == values[2, 0])
{
return (values[1, 1] == _player1_symbol) ? "player 1" : "player 2";
}
// No one has won yet
return null;
}
private static string[,] BuildUserChoices()
{
var values = new string[3, 3];
values[0, 0] = button1.Text;
values[0, 1] = button2.Text;
values[0, 2] = button3.Text;
// and so on...
// If a button has not been click, they must have a unique text, like a number
return values;
}
答案 1 :(得分:0)
你需要在每回合后检查玩家是否获胜。现在,在加载表单后,您将检查一次。