所以这是交易,我正在尝试比较X
和O
的本地资源中的图片,如果它们在winCondition
数组中相同,则发送消息他们赢了的用户。
我发现的问题是b1.Image
没有给我正确的比较。它正在比较这个:
+ b1.Image {System.Drawing.Bitmap} System.Drawing.Image {System.Drawing.Bitmap}
当我想要它来比较图像名称时。
其中一个问题是turnNumber = 5
,当我没有时,它表示我赢了。我相信这是由于b1.Image problem
。一旦我点击另一个方格,它就会再次检查胜利。
我希望在游戏完成后禁用按钮,但我不知道如何执行此操作。
它会如此简单:
foreach (Button btnEnabled in buttonArray)
{
btnEnabled.Enabled = false;
}
以下是代码,再次感谢您的帮助。几天来我一直在努力。
namespace BGreinAssignment2
{
public partial class frmTicTacToe : Form
{
//Global variables
private bool player1Turn = false;
private bool player2Turn = true;
private int[,] winCondition =
{
{0,1,2}, //Horizontal top
{3,4,5}, //Horizontal Middle
{6,7,8}, //Horizontal Bottom
{0,3,6}, //Vertical Left
{1,4,7}, //Vertical Middle
{2,5,8}, //Vertical Right
{0,4,8}, //Diagonal Top Left to Bottom Right or Vice-Versa
{2,4,6} //Diagonal Top Right to Bottom Left or Vice-Versa
};
private Button[] buttonArray;
private int turnNumber = 0;
public frmTicTacToe()
{
InitializeComponent();
}
//Creates the button array for checks and sets X to go first.
private void frmTicTacToe_Load(object sender, EventArgs e)
{
//Creates button array for checking if image is there/check for beginning of game
buttonArray = new Button[9] {btnTopLeft, btnTopMid, btnTopRight, btnMidLeft, btnMid, btnMidRight, btnBotLeft, btnBotMid, btnBotRight};
//Sets player 1 to go first to satisfy the "X always goes first"
player1Turn = true;
player2Turn = false;
}
/// <summary>
/// Checks the buttons if the images don't create a win condition through the winCheck method,
/// displays message box to user with "Draw!" break is included so it doesn't say "Draw!" for each button it checks.
/// </summary>
private void drawCheck()
{
foreach (Button checkDraw in buttonArray)
{
if (checkDraw.Image != null)
{
MessageBox.Show("Draw!");
break;
}
}
}
/// <summary>
/// Checks the win condition to see if the images are the same. If they are, it will show a message box with the winner.
/// </summary>
/// <param name="btnChecks">Creates an array to check the button images</param>
/// <returns>If there is a winner, returns true and shows message box</returns>
private bool winCheck(Button[] btnChecks)
{
bool win = false;
for (int i = 0; i < 8; i++)
{
int a = winCondition[i, 0], b = winCondition[i, 1], c = winCondition[i, 2];
Button b1 = btnChecks[a], b2 = btnChecks[b], b3 = btnChecks[c];
if (b1.Image == null || b2.Image == null || b3.Image == null)
{
continue;
}
if (b1.Image == b2.Image && b2.Image == b3.Image)
{
win = true;
MessageBox.Show("Game over. " + b1.Image + " Wins!");
}
}
return win;
}
//If player chooses top left square
private void btnTopLeft_Click(object sender, EventArgs e)
{
if (btnTopLeft.Image == null)
{
if (player1Turn == true)
{
if (turnNumber == 0)
{
btnTopLeft.Image = BGreinAssignment2.Properties.Resources.tic_tac_toe_X;
player1Turn = false;
player2Turn = true;
turnNumber++;
}
else
{
btnTopLeft.Image = BGreinAssignment2.Properties.Resources.tic_tac_toe_X;
player1Turn = false;
player2Turn = true;
turnNumber++;
if (turnNumber >= 5)
{
winCheck(buttonArray);
}
if (turnNumber == 9)
{
drawCheck();
}
}
}
else
{
btnTopLeft.Image = BGreinAssignment2.Properties.Resources.tic_tac_toe_O;
player1Turn = true;
player2Turn = false;
turnNumber++;
if (turnNumber <= 5)
{
winCheck(buttonArray);
}
if (turnNumber == 9)
{
drawCheck();
}
}
}
else
{
MessageBox.Show("This space has already been selected");
}
}
//Excluded rest of code (just button clicks, repeats of same for 9 squares)
/// <summary>
/// Resets the game for the player.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPlayAgain_Click(object sender, EventArgs e)
{
//For each button, set image to null then reset turn counter and set turn to player 1.
foreach (Button btnSpaces in buttonArray)
{
btnSpaces.Image = null;
}
turnNumber = 0;
player1Turn = true;
player2Turn = false;
}
}
}
答案 0 :(得分:1)
正如您所发现的,直接比较Image
不是答案。我没有测试过这个,但我怀疑是因为每个Image都有不同的属性,尽管是从同一个资源加载的。例如,如果一个按钮只有20像素的正方形,而另一个按钮是50,则Height
和Width
属性会有所不同。有关其他一些属性,请参阅the documentation。
正如@ John3136所说,更好的处理方法是将模型从绘图中分离出来。这可以像在每个按钮上使用Tag
属性来存储“O”或“X”一样简单。除了你要比较b1.Tag == b2.Tag && b2.Tag == b3.Tag
之外,你现在检查的一切都和现在一样。一种更好但更复杂的方法是使用一系列代表游戏板的东西。与任何编程问题一样,有许多可能的方法 - char[9]
将是最简单的,尽管string[]
或bool?[]
可以工作,创建枚举并存储其数组,或者任何上述数组的数组。 (最终还是List<>
而不是数组)。在任何一种情况下,您都必须在更新图像的同时更新模型,但是您正在进行更简单的比较以测试胜利。
关于禁用按钮你完全正确 - 但这里有一个问题。如果有人点击其他玩家已经点击的按钮会发生什么?当然,您可以提醒用户,但它仍然看起来就像一个有效的选择。为什么不在点击它后立即禁用它?
我也会更进一步,给你一个“//Excluded rest of code (just button clicks, repeats of same for 9 squares)
”行的提示。如果你要重复相同的代码超过两次(有些人会说不止一次),你最好制作一个函数,它可以将它作为一个参数区分开来。例如,你可以有
//If player chooses top left square
private void btnTopLeft_Click(object sender, EventArgs e)
{
DoClick(btnTopLeft);
}
//If player chooses top center square
private void ptnTopCenter_CLick(object sender, EventArgs e)
{
DoClick(btnTopCenter);
}
private void DoClick(Button button)
{
// Manipulate button appropriately
}
这没有做任何你不知道怎么做的事情 - 你已经在调用其他函数,并在参数中传递按钮(参见winCheck()
)。这只是一种思考方式,它将减少您需要编写的代码量,从而减少修复任何给定错误所需的位置。