これが取引です。ローカルリソース内の画像ととの画像を比較し、配列内で同じである場合は、勝ったユーザーにメッセージを送信しようとしX
てO
いますwinCondition
。
私が見つけた問題はb1.Image
、私に適切な比較を与えていないということです。これを比較しています:
+ b1.Image {System.Drawing.Bitmap} System.Drawing.Image {System.Drawing.Bitmap}
代わりに画像名を比較したい場合。
これらの問題の1つは、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;
}
}
}