C#實現(xiàn)簡單的井字游戲?qū)嵗?/h1>
更新時間:2015年06月15日 10:57:18 作者:小卒過河
這篇文章主要介紹了C#實現(xiàn)簡單的井字游戲,以一個完整實例分析了C#實現(xiàn)井字游戲的方法,需要的朋友可以參考下
本文實例講述了C#實現(xiàn)簡單的井字游戲。分享給大家供大家參考。具體如下:
/*
* Created using: SharpDevelop
* Created by: Tony Misner
* Date: 1/2/2007
* Time: 2:34 PM
*
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace TicTacToe
{
/// <summary>
/// This is a basic one player versus computer game of TicTacToe
/// </summary>
public partial class frmMain
{
string playerTurn = "0";
string playerSymbol = "X";
string computerSymbol = "O";
int playCounter = 0;
[STAThread]
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
public frmMain()
{
InitializeComponent();
}
void Label1Click(object sender, System.EventArgs e)
{
bool playerDone = false;
string labelText = label1.Text;
if (playerClick(labelText) == true)
{
label1.Text = playerSymbol;
playerDone = true;
}
else
{
return ;
}
if (checkWin() == true)
{
resetGame();
}
else
{
computerGo();
if (checkWin() == true)
{
resetGame();
}
}
}
void Label2Click(object sender, System.EventArgs e)
{
bool playerDone = false;
string labelText = label2.Text;
if (playerClick(labelText) == true)
{
label2.Text = playerSymbol;
playerDone = true;
}
else
{
return ;
}
if (checkWin() == true)
{
resetGame();
}
else
{
computerGo();
if (checkWin() == true)
{
resetGame();
}
}
}
void Label3Click(object sender, System.EventArgs e)
{
bool playerDone = false;
string labelText = label3.Text;
if (playerClick(labelText) == true)
{
label3.Text = playerSymbol;
playerDone = true;
}
else
{
return ;
}
if (checkWin() == true)
{
resetGame();
}
else
{
computerGo();
if (checkWin() == true)
{
resetGame();
}
}
}
void Label4Click(object sender, System.EventArgs e)
{
bool playerDone = false;
string labelText = label4.Text;
if (playerClick(labelText) == true)
{
label4.Text = playerSymbol;
playerDone = true;
}
else
{
return ;
}
if (checkWin() == true)
{
resetGame();
}
else
{
computerGo();
if (checkWin() == true)
{
resetGame();
}
}
}
void Label5Click(object sender, System.EventArgs e)
{
bool playerDone = false;
string labelText = label5.Text;
if (playerClick(labelText) == true)
{
label5.Text = playerSymbol;
playerDone = true;
}
else
{
return ;
}
if (checkWin() == true)
{
resetGame();
}
else
{
computerGo();
if (checkWin() == true)
{
resetGame();
}
}
}
void Label6Click(object sender, System.EventArgs e)
{
bool playerDone = false;
string labelText = label6.Text;
if (playerClick(labelText) == true)
{
label6.Text = playerSymbol;
playerDone = true;
}
else
{
return ;
}
if (checkWin() == true)
{
resetGame();
}
else
{
computerGo();
if (checkWin() == true)
{
resetGame();
}
}
}
void Label7Click(object sender, System.EventArgs e)
{
bool playerDone = false;
string labelText = label7.Text;
if (playerClick(labelText) == true)
{
label7.Text = playerSymbol;
playerDone = true;
}
else
{
return ;
}
if (checkWin() == true)
{
resetGame();
}
else
{
computerGo();
if (checkWin() == true)
{
resetGame();
}
}
}
void Label8Click(object sender, System.EventArgs e)
{
bool playerDone = false;
string labelText = label8.Text;
if (playerClick(labelText) == true)
{
label8.Text = playerSymbol;
playerDone = true;
}
else
{
return ;
}
if (checkWin() == true)
{
resetGame();
}
else
{
computerGo();
if (checkWin() == true)
{
resetGame();
}
}
}
void Label9Click(object sender, System.EventArgs e)
{
bool playerDone = false;
string labelText = label9.Text;
if (playerClick(labelText) == true)
{
label9.Text = playerSymbol;
playerDone = true;
}
else
{
return ;
}
if (checkWin() == true)
{
resetGame();
}
else
{
computerGo();
if (checkWin() == true)
{
resetGame();
}
}
}
bool playerClick(string labelText)
{
if (playerTurn == "1" && labelText == "" && playCounter < 4)
{
playerTurn = "2";
lblTurn.Text = "Player 2 Turn";
playCounter++;
return true;
} else if (playCounter == 4)
{
toolStripTotal.Text = ((Convert.ToInt32(toolStripTotal.Text)) + 1).ToString();
toolStripDraw.Text = ((Convert.ToInt32(toolStripDraw.Text)) + 1).ToString();
MessageBox.Show("Draw","Game Over", MessageBoxButtons.OK, MessageBoxIcon.Stop);
resetGame();
}
return false;
}
bool checkWin()
{
bool win = false;
if (label1.Text == label2.Text && label2.Text == label3.Text && label1.Text != "")
{
win = true;
}
else if (label4.Text == label5.Text && label5.Text == label6.Text && label4.Text != "")
{
win = true;
}
else if (label7.Text == label8.Text && label8.Text == label9.Text && label7.Text != "")
{
win = true;
}
else if (label1.Text == label4.Text && label4.Text == label7.Text && label1.Text != "")
{
win = true;
}
else if (label2.Text == label5.Text && label5.Text == label8.Text && label2.Text != "")
{
win = true;
}
else if (label3.Text == label6.Text && label6.Text == label9.Text && label3.Text != "")
{
win = true;
}
else if (label1.Text == label5.Text && label5.Text == label9.Text && label1.Text != "")
{
win = true;
}
else if (label3.Text == label5.Text && label5.Text == label7.Text && label3.Text != "")
{
win = true;
}
if (win == true)
{
toolStripTotal.Text = ((Convert.ToInt32(toolStripTotal.Text)) + 1).ToString();
if (playerTurn == "1")
{
toolStripLost.Text = ((Convert.ToInt32(toolStripLost.Text)) + 1).ToString();
MessageBox.Show("Player 2 has won!","Game Over", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return win = true;
}
else
{
toolStripWon.Text = ((Convert.ToInt32(toolStripWon.Text)) + 1).ToString();
MessageBox.Show("Player 1 has won!","Game Over", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return win = true;
}
}
return win;
}
void resetGame()
{
label1.Text = "";
label2.Text = "";
label3.Text = "";
label4.Text = "";
label5.Text = "";
label6.Text = "";
label7.Text = "";
label8.Text = "";
label9.Text = "";
playerTurn = "1";
playCounter = 0;
lblTurn.Text = "Player 1 Turn";
}
void computerGo()
{
bool computerDone = false;
computerDone = computerGoForWin();
if (computerDone == false)
{
computerDone = computerGoForBlock();
if (computerDone == false)
{
computerDone = computerGoRandom();
}
}
playerTurn = "1";
lblTurn.Text = "Player 1 Turn";
}
bool computerGoForWin()
{
bool computerDone = false;
if (label1.Text == computerSymbol && label2.Text == computerSymbol && label3.Text == "")
{
label3.Text = computerSymbol;
return computerDone = true;
}
else if (label1.Text == computerSymbol && label3.Text == computerSymbol && label2.Text == "")
{
label2.Text = computerSymbol;
return computerDone = true;
}
else if (label2.Text == computerSymbol && label3.Text == computerSymbol && label1.Text == "")
{
label1.Text = computerSymbol;
return computerDone = true;
}
else if (label4.Text == computerSymbol && label5.Text == computerSymbol && label6.Text == "")
{
label6.Text = computerSymbol;
return computerDone = true;
}
else if (label4.Text == computerSymbol && label6.Text == computerSymbol && label5.Text == "")
{
label5.Text = computerSymbol;
return computerDone = true;
}
else if (label5.Text == computerSymbol && label6.Text == computerSymbol && label4.Text == "")
{
label4.Text = computerSymbol;
return computerDone = true;
}
else if (label7.Text == computerSymbol && label8.Text == computerSymbol && label9.Text == "")
{
label9.Text = computerSymbol;
return computerDone = true;
}
else if (label7.Text == computerSymbol && label9.Text == computerSymbol && label8.Text == "")
{
label8.Text = computerSymbol;
return computerDone = true;
}
else if (label8.Text == computerSymbol && label9.Text == computerSymbol && label7.Text == "")
{
label7.Text = computerSymbol;
return computerDone = true;
}
else if (label1.Text == computerSymbol && label4.Text == computerSymbol && label7.Text == "")
{
label7.Text = computerSymbol;
return computerDone = true;
}
else if (label1.Text == computerSymbol && label7.Text == computerSymbol && label4.Text == "")
{
label4.Text = computerSymbol;
return computerDone = true;
}
else if (label4.Text == computerSymbol && label7.Text == computerSymbol && label1.Text == "")
{
label1.Text = computerSymbol;
return computerDone = true;
}
else if (label2.Text == computerSymbol && label5.Text == computerSymbol && label8.Text == "")
{
label8.Text = computerSymbol;
return computerDone = true;
}
else if (label2.Text == computerSymbol && label8.Text == computerSymbol && label5.Text == "")
{
label5.Text = computerSymbol;
return computerDone = true;
}
else if (label5.Text == computerSymbol && label8.Text == computerSymbol && label2.Text == "")
{
label2.Text = computerSymbol;
return computerDone = true;
}
else if (label3.Text == computerSymbol && label6.Text == computerSymbol && label9.Text == "")
{
label9.Text = computerSymbol;
return computerDone = true;
}
else if (label3.Text == computerSymbol && label9.Text == computerSymbol && label6.Text == "")
{
label6.Text = computerSymbol;
return computerDone = true;
}
else if (label6.Text == computerSymbol && label9.Text == computerSymbol && label3.Text == "")
{
label3.Text = computerSymbol;
return computerDone = true;
}
else if (label1.Text == computerSymbol && label5.Text == computerSymbol && label9.Text == "")
{
label9.Text = computerSymbol;
return computerDone = true;
}
else if (label5.Text == computerSymbol && label9.Text == computerSymbol && label1.Text == "")
{
label1.Text = computerSymbol;
return computerDone = true;
}
else if (label1.Text == computerSymbol && label9.Text == computerSymbol && label5.Text == "")
{
label5.Text = computerSymbol;
return computerDone = true;
}
else if (label3.Text == computerSymbol && label5.Text == computerSymbol && label7.Text == "")
{
label7.Text = computerSymbol;
return computerDone = true;
}
else if (label3.Text == computerSymbol && label7.Text == computerSymbol && label5.Text == "")
{
label5.Text = computerSymbol;
return computerDone = true;
}
else if (label5.Text == computerSymbol && label7.Text == computerSymbol && label3.Text == "")
{
label3.Text = computerSymbol;
return computerDone = true;
}
return computerDone = false;
}
bool computerGoForBlock()
{
bool computerDone = false;
if (label1.Text == playerSymbol && label2.Text == playerSymbol && label3.Text == "")
{
label3.Text = computerSymbol;
return computerDone = true;
}
else if (label1.Text == playerSymbol && label3.Text == playerSymbol && label2.Text == "")
{
label2.Text = computerSymbol;
return computerDone = true;
}
else if (label2.Text == playerSymbol && label3.Text == playerSymbol && label1.Text == "")
{
label1.Text = computerSymbol;
return computerDone = true;
}
else if (label4.Text == playerSymbol && label5.Text == playerSymbol && label6.Text == "")
{
label6.Text = computerSymbol;
return computerDone = true;
}
else if (label4.Text == playerSymbol && label6.Text == playerSymbol && label5.Text == "")
{
label5.Text = computerSymbol;
return computerDone = true;
}
else if (label5.Text == playerSymbol && label6.Text == playerSymbol && label4.Text == "")
{
label4.Text = computerSymbol;
return computerDone = true;
}
else if (label7.Text == playerSymbol && label8.Text == playerSymbol && label9.Text == "")
{
label9.Text = computerSymbol;
return computerDone = true;
}
else if (label7.Text == playerSymbol && label9.Text == playerSymbol && label8.Text == "")
{
label8.Text = computerSymbol;
return computerDone = true;
}
else if (label8.Text == playerSymbol && label9.Text == playerSymbol && label7.Text == "")
{
label7.Text = computerSymbol;
return computerDone = true;
}
else if (label1.Text == playerSymbol && label4.Text == playerSymbol && label7.Text == "")
{
label7.Text = computerSymbol;
return computerDone = true;
}
else if (label1.Text == playerSymbol && label7.Text == playerSymbol && label4.Text == "")
{
label4.Text = computerSymbol;
return computerDone = true;
}
else if (label4.Text == playerSymbol && label7.Text == playerSymbol && label1.Text == "")
{
label1.Text = computerSymbol;
return computerDone = true;
}
else if (label2.Text == playerSymbol && label5.Text == playerSymbol && label8.Text == "")
{
label8.Text = computerSymbol;
return computerDone = true;
}
else if (label2.Text == playerSymbol && label8.Text == playerSymbol && label5.Text == "")
{
label5.Text = computerSymbol;
return computerDone = true;
}
else if (label5.Text == playerSymbol && label8.Text == playerSymbol && label2.Text == "")
{
label2.Text = computerSymbol;
return computerDone = true;
}
else if (label3.Text == playerSymbol && label6.Text == playerSymbol && label9.Text == "")
{
label9.Text = computerSymbol;
return computerDone = true;
}
else if (label3.Text == playerSymbol && label9.Text == playerSymbol && label6.Text == "")
{
label6.Text = computerSymbol;
return computerDone = true;
}
else if (label6.Text == playerSymbol && label9.Text == playerSymbol && label3.Text == "")
{
label3.Text = computerSymbol;
return computerDone = true;
}
else if (label1.Text == playerSymbol && label5.Text == playerSymbol && label9.Text == "")
{
label9.Text = computerSymbol;
return computerDone = true;
}
else if (label5.Text == playerSymbol && label9.Text == playerSymbol && label1.Text == "")
{
label1.Text = computerSymbol;
return computerDone = true;
}
else if (label1.Text == playerSymbol && label9.Text == playerSymbol && label5.Text == "")
{
label5.Text = computerSymbol;
return computerDone = true;
}
else if (label3.Text == playerSymbol && label5.Text == playerSymbol && label7.Text == "")
{
label7.Text = computerSymbol;
return computerDone = true;
}
else if (label3.Text == playerSymbol && label7.Text == playerSymbol && label5.Text == "")
{
label5.Text = computerSymbol;
return computerDone = true;
}
else if (label5.Text == playerSymbol && label7.Text == playerSymbol && label3.Text == "")
{
label3.Text = computerSymbol;
return computerDone = true;
}
return computerDone = false;
}
bool computerGoRandom()
{
bool computerDone = false;
Random random = new Random();
do
{
int position = random.Next(1,10);
switch(position)
{
case 1:
if (label1.Text == "")
{
label1.Text = computerSymbol;
return computerDone = true;
}
break;
case 2:
if (label2.Text == "")
{
label2.Text = computerSymbol;
return computerDone = true;
}
break;
case 3:
if (label3.Text == "")
{
label3.Text = computerSymbol;
return computerDone = true;
}
break;
case 4:
if (label4.Text == "")
{
label4.Text = computerSymbol;
return computerDone = true;
}
break;
case 5:
if (label5.Text == "")
{
label5.Text = computerSymbol;
return computerDone = true;
}
break;
case 6:
if (label6.Text == "")
{
label6.Text = computerSymbol;
return computerDone = true;
}
break;
case 7:
if (label7.Text == "")
{
label7.Text = computerSymbol;
return computerDone = true;
}
break;
case 8:
if (label8.Text == "")
{
label8.Text = computerSymbol;
return computerDone = true;
}
break;
case 9:
if (label9.Text == "")
{
label9.Text = computerSymbol;
return computerDone = true;
}
break;
}
}while (computerDone == false);
return computerDone = false;
}
void BtnExitClick(object sender, System.EventArgs e)
{
if (MessageBox.Show("Are you sure you want to exit?","Exit?",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
{
Application.Exit();
}
}
void ExitToolStripMenuItemClick(object sender, System.EventArgs e)
{
BtnExitClick(sender,e);
}
void BtnNewGameClick(object sender, System.EventArgs e)
{
if (MessageBox.Show("Are you sure you want to restart?","Restart?",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
{
toolStripTotal.Text = ((Convert.ToInt32(toolStripTotal.Text)) + 1).ToString();
toolStripDraw.Text = ((Convert.ToInt32(toolStripDraw.Text)) + 1).ToString();
resetGame();
}
}
void NewGameToolStripMenuItemClick(object sender, System.EventArgs e)
{
BtnNewGameClick(sender, e);
}
void AboutToolStripMenuItemClick(object sender, System.EventArgs e)
{
frmAbout about = new frmAbout();
about.ShowDialog();
}
}
}
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
-
如何:對Windows 窗體控件進(jìn)行線程安全調(diào)用
使用多線程提高 Windows 窗體應(yīng)用程序的性能時,必須注意以線程安全方式調(diào)用控件。
2007-03-03
-
C#實現(xiàn)復(fù)制文件夾中文件到另一個文件夾的方法
這篇文章主要介紹了C#實現(xiàn)復(fù)制文件夾中文件到另一個文件夾的方法,實例分析了C#實現(xiàn)文件夾的查找、判斷及文件復(fù)制相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下 2015-07-07
最新評論
本文實例講述了C#實現(xiàn)簡單的井字游戲。分享給大家供大家參考。具體如下:
/* * Created using: SharpDevelop * Created by: Tony Misner * Date: 1/2/2007 * Time: 2:34 PM * */ using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace TicTacToe { /// <summary> /// This is a basic one player versus computer game of TicTacToe /// </summary> public partial class frmMain { string playerTurn = "0"; string playerSymbol = "X"; string computerSymbol = "O"; int playCounter = 0; [STAThread] public static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmMain()); } public frmMain() { InitializeComponent(); } void Label1Click(object sender, System.EventArgs e) { bool playerDone = false; string labelText = label1.Text; if (playerClick(labelText) == true) { label1.Text = playerSymbol; playerDone = true; } else { return ; } if (checkWin() == true) { resetGame(); } else { computerGo(); if (checkWin() == true) { resetGame(); } } } void Label2Click(object sender, System.EventArgs e) { bool playerDone = false; string labelText = label2.Text; if (playerClick(labelText) == true) { label2.Text = playerSymbol; playerDone = true; } else { return ; } if (checkWin() == true) { resetGame(); } else { computerGo(); if (checkWin() == true) { resetGame(); } } } void Label3Click(object sender, System.EventArgs e) { bool playerDone = false; string labelText = label3.Text; if (playerClick(labelText) == true) { label3.Text = playerSymbol; playerDone = true; } else { return ; } if (checkWin() == true) { resetGame(); } else { computerGo(); if (checkWin() == true) { resetGame(); } } } void Label4Click(object sender, System.EventArgs e) { bool playerDone = false; string labelText = label4.Text; if (playerClick(labelText) == true) { label4.Text = playerSymbol; playerDone = true; } else { return ; } if (checkWin() == true) { resetGame(); } else { computerGo(); if (checkWin() == true) { resetGame(); } } } void Label5Click(object sender, System.EventArgs e) { bool playerDone = false; string labelText = label5.Text; if (playerClick(labelText) == true) { label5.Text = playerSymbol; playerDone = true; } else { return ; } if (checkWin() == true) { resetGame(); } else { computerGo(); if (checkWin() == true) { resetGame(); } } } void Label6Click(object sender, System.EventArgs e) { bool playerDone = false; string labelText = label6.Text; if (playerClick(labelText) == true) { label6.Text = playerSymbol; playerDone = true; } else { return ; } if (checkWin() == true) { resetGame(); } else { computerGo(); if (checkWin() == true) { resetGame(); } } } void Label7Click(object sender, System.EventArgs e) { bool playerDone = false; string labelText = label7.Text; if (playerClick(labelText) == true) { label7.Text = playerSymbol; playerDone = true; } else { return ; } if (checkWin() == true) { resetGame(); } else { computerGo(); if (checkWin() == true) { resetGame(); } } } void Label8Click(object sender, System.EventArgs e) { bool playerDone = false; string labelText = label8.Text; if (playerClick(labelText) == true) { label8.Text = playerSymbol; playerDone = true; } else { return ; } if (checkWin() == true) { resetGame(); } else { computerGo(); if (checkWin() == true) { resetGame(); } } } void Label9Click(object sender, System.EventArgs e) { bool playerDone = false; string labelText = label9.Text; if (playerClick(labelText) == true) { label9.Text = playerSymbol; playerDone = true; } else { return ; } if (checkWin() == true) { resetGame(); } else { computerGo(); if (checkWin() == true) { resetGame(); } } } bool playerClick(string labelText) { if (playerTurn == "1" && labelText == "" && playCounter < 4) { playerTurn = "2"; lblTurn.Text = "Player 2 Turn"; playCounter++; return true; } else if (playCounter == 4) { toolStripTotal.Text = ((Convert.ToInt32(toolStripTotal.Text)) + 1).ToString(); toolStripDraw.Text = ((Convert.ToInt32(toolStripDraw.Text)) + 1).ToString(); MessageBox.Show("Draw","Game Over", MessageBoxButtons.OK, MessageBoxIcon.Stop); resetGame(); } return false; } bool checkWin() { bool win = false; if (label1.Text == label2.Text && label2.Text == label3.Text && label1.Text != "") { win = true; } else if (label4.Text == label5.Text && label5.Text == label6.Text && label4.Text != "") { win = true; } else if (label7.Text == label8.Text && label8.Text == label9.Text && label7.Text != "") { win = true; } else if (label1.Text == label4.Text && label4.Text == label7.Text && label1.Text != "") { win = true; } else if (label2.Text == label5.Text && label5.Text == label8.Text && label2.Text != "") { win = true; } else if (label3.Text == label6.Text && label6.Text == label9.Text && label3.Text != "") { win = true; } else if (label1.Text == label5.Text && label5.Text == label9.Text && label1.Text != "") { win = true; } else if (label3.Text == label5.Text && label5.Text == label7.Text && label3.Text != "") { win = true; } if (win == true) { toolStripTotal.Text = ((Convert.ToInt32(toolStripTotal.Text)) + 1).ToString(); if (playerTurn == "1") { toolStripLost.Text = ((Convert.ToInt32(toolStripLost.Text)) + 1).ToString(); MessageBox.Show("Player 2 has won!","Game Over", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return win = true; } else { toolStripWon.Text = ((Convert.ToInt32(toolStripWon.Text)) + 1).ToString(); MessageBox.Show("Player 1 has won!","Game Over", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return win = true; } } return win; } void resetGame() { label1.Text = ""; label2.Text = ""; label3.Text = ""; label4.Text = ""; label5.Text = ""; label6.Text = ""; label7.Text = ""; label8.Text = ""; label9.Text = ""; playerTurn = "1"; playCounter = 0; lblTurn.Text = "Player 1 Turn"; } void computerGo() { bool computerDone = false; computerDone = computerGoForWin(); if (computerDone == false) { computerDone = computerGoForBlock(); if (computerDone == false) { computerDone = computerGoRandom(); } } playerTurn = "1"; lblTurn.Text = "Player 1 Turn"; } bool computerGoForWin() { bool computerDone = false; if (label1.Text == computerSymbol && label2.Text == computerSymbol && label3.Text == "") { label3.Text = computerSymbol; return computerDone = true; } else if (label1.Text == computerSymbol && label3.Text == computerSymbol && label2.Text == "") { label2.Text = computerSymbol; return computerDone = true; } else if (label2.Text == computerSymbol && label3.Text == computerSymbol && label1.Text == "") { label1.Text = computerSymbol; return computerDone = true; } else if (label4.Text == computerSymbol && label5.Text == computerSymbol && label6.Text == "") { label6.Text = computerSymbol; return computerDone = true; } else if (label4.Text == computerSymbol && label6.Text == computerSymbol && label5.Text == "") { label5.Text = computerSymbol; return computerDone = true; } else if (label5.Text == computerSymbol && label6.Text == computerSymbol && label4.Text == "") { label4.Text = computerSymbol; return computerDone = true; } else if (label7.Text == computerSymbol && label8.Text == computerSymbol && label9.Text == "") { label9.Text = computerSymbol; return computerDone = true; } else if (label7.Text == computerSymbol && label9.Text == computerSymbol && label8.Text == "") { label8.Text = computerSymbol; return computerDone = true; } else if (label8.Text == computerSymbol && label9.Text == computerSymbol && label7.Text == "") { label7.Text = computerSymbol; return computerDone = true; } else if (label1.Text == computerSymbol && label4.Text == computerSymbol && label7.Text == "") { label7.Text = computerSymbol; return computerDone = true; } else if (label1.Text == computerSymbol && label7.Text == computerSymbol && label4.Text == "") { label4.Text = computerSymbol; return computerDone = true; } else if (label4.Text == computerSymbol && label7.Text == computerSymbol && label1.Text == "") { label1.Text = computerSymbol; return computerDone = true; } else if (label2.Text == computerSymbol && label5.Text == computerSymbol && label8.Text == "") { label8.Text = computerSymbol; return computerDone = true; } else if (label2.Text == computerSymbol && label8.Text == computerSymbol && label5.Text == "") { label5.Text = computerSymbol; return computerDone = true; } else if (label5.Text == computerSymbol && label8.Text == computerSymbol && label2.Text == "") { label2.Text = computerSymbol; return computerDone = true; } else if (label3.Text == computerSymbol && label6.Text == computerSymbol && label9.Text == "") { label9.Text = computerSymbol; return computerDone = true; } else if (label3.Text == computerSymbol && label9.Text == computerSymbol && label6.Text == "") { label6.Text = computerSymbol; return computerDone = true; } else if (label6.Text == computerSymbol && label9.Text == computerSymbol && label3.Text == "") { label3.Text = computerSymbol; return computerDone = true; } else if (label1.Text == computerSymbol && label5.Text == computerSymbol && label9.Text == "") { label9.Text = computerSymbol; return computerDone = true; } else if (label5.Text == computerSymbol && label9.Text == computerSymbol && label1.Text == "") { label1.Text = computerSymbol; return computerDone = true; } else if (label1.Text == computerSymbol && label9.Text == computerSymbol && label5.Text == "") { label5.Text = computerSymbol; return computerDone = true; } else if (label3.Text == computerSymbol && label5.Text == computerSymbol && label7.Text == "") { label7.Text = computerSymbol; return computerDone = true; } else if (label3.Text == computerSymbol && label7.Text == computerSymbol && label5.Text == "") { label5.Text = computerSymbol; return computerDone = true; } else if (label5.Text == computerSymbol && label7.Text == computerSymbol && label3.Text == "") { label3.Text = computerSymbol; return computerDone = true; } return computerDone = false; } bool computerGoForBlock() { bool computerDone = false; if (label1.Text == playerSymbol && label2.Text == playerSymbol && label3.Text == "") { label3.Text = computerSymbol; return computerDone = true; } else if (label1.Text == playerSymbol && label3.Text == playerSymbol && label2.Text == "") { label2.Text = computerSymbol; return computerDone = true; } else if (label2.Text == playerSymbol && label3.Text == playerSymbol && label1.Text == "") { label1.Text = computerSymbol; return computerDone = true; } else if (label4.Text == playerSymbol && label5.Text == playerSymbol && label6.Text == "") { label6.Text = computerSymbol; return computerDone = true; } else if (label4.Text == playerSymbol && label6.Text == playerSymbol && label5.Text == "") { label5.Text = computerSymbol; return computerDone = true; } else if (label5.Text == playerSymbol && label6.Text == playerSymbol && label4.Text == "") { label4.Text = computerSymbol; return computerDone = true; } else if (label7.Text == playerSymbol && label8.Text == playerSymbol && label9.Text == "") { label9.Text = computerSymbol; return computerDone = true; } else if (label7.Text == playerSymbol && label9.Text == playerSymbol && label8.Text == "") { label8.Text = computerSymbol; return computerDone = true; } else if (label8.Text == playerSymbol && label9.Text == playerSymbol && label7.Text == "") { label7.Text = computerSymbol; return computerDone = true; } else if (label1.Text == playerSymbol && label4.Text == playerSymbol && label7.Text == "") { label7.Text = computerSymbol; return computerDone = true; } else if (label1.Text == playerSymbol && label7.Text == playerSymbol && label4.Text == "") { label4.Text = computerSymbol; return computerDone = true; } else if (label4.Text == playerSymbol && label7.Text == playerSymbol && label1.Text == "") { label1.Text = computerSymbol; return computerDone = true; } else if (label2.Text == playerSymbol && label5.Text == playerSymbol && label8.Text == "") { label8.Text = computerSymbol; return computerDone = true; } else if (label2.Text == playerSymbol && label8.Text == playerSymbol && label5.Text == "") { label5.Text = computerSymbol; return computerDone = true; } else if (label5.Text == playerSymbol && label8.Text == playerSymbol && label2.Text == "") { label2.Text = computerSymbol; return computerDone = true; } else if (label3.Text == playerSymbol && label6.Text == playerSymbol && label9.Text == "") { label9.Text = computerSymbol; return computerDone = true; } else if (label3.Text == playerSymbol && label9.Text == playerSymbol && label6.Text == "") { label6.Text = computerSymbol; return computerDone = true; } else if (label6.Text == playerSymbol && label9.Text == playerSymbol && label3.Text == "") { label3.Text = computerSymbol; return computerDone = true; } else if (label1.Text == playerSymbol && label5.Text == playerSymbol && label9.Text == "") { label9.Text = computerSymbol; return computerDone = true; } else if (label5.Text == playerSymbol && label9.Text == playerSymbol && label1.Text == "") { label1.Text = computerSymbol; return computerDone = true; } else if (label1.Text == playerSymbol && label9.Text == playerSymbol && label5.Text == "") { label5.Text = computerSymbol; return computerDone = true; } else if (label3.Text == playerSymbol && label5.Text == playerSymbol && label7.Text == "") { label7.Text = computerSymbol; return computerDone = true; } else if (label3.Text == playerSymbol && label7.Text == playerSymbol && label5.Text == "") { label5.Text = computerSymbol; return computerDone = true; } else if (label5.Text == playerSymbol && label7.Text == playerSymbol && label3.Text == "") { label3.Text = computerSymbol; return computerDone = true; } return computerDone = false; } bool computerGoRandom() { bool computerDone = false; Random random = new Random(); do { int position = random.Next(1,10); switch(position) { case 1: if (label1.Text == "") { label1.Text = computerSymbol; return computerDone = true; } break; case 2: if (label2.Text == "") { label2.Text = computerSymbol; return computerDone = true; } break; case 3: if (label3.Text == "") { label3.Text = computerSymbol; return computerDone = true; } break; case 4: if (label4.Text == "") { label4.Text = computerSymbol; return computerDone = true; } break; case 5: if (label5.Text == "") { label5.Text = computerSymbol; return computerDone = true; } break; case 6: if (label6.Text == "") { label6.Text = computerSymbol; return computerDone = true; } break; case 7: if (label7.Text == "") { label7.Text = computerSymbol; return computerDone = true; } break; case 8: if (label8.Text == "") { label8.Text = computerSymbol; return computerDone = true; } break; case 9: if (label9.Text == "") { label9.Text = computerSymbol; return computerDone = true; } break; } }while (computerDone == false); return computerDone = false; } void BtnExitClick(object sender, System.EventArgs e) { if (MessageBox.Show("Are you sure you want to exit?","Exit?",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes) { Application.Exit(); } } void ExitToolStripMenuItemClick(object sender, System.EventArgs e) { BtnExitClick(sender,e); } void BtnNewGameClick(object sender, System.EventArgs e) { if (MessageBox.Show("Are you sure you want to restart?","Restart?",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes) { toolStripTotal.Text = ((Convert.ToInt32(toolStripTotal.Text)) + 1).ToString(); toolStripDraw.Text = ((Convert.ToInt32(toolStripDraw.Text)) + 1).ToString(); resetGame(); } } void NewGameToolStripMenuItemClick(object sender, System.EventArgs e) { BtnNewGameClick(sender, e); } void AboutToolStripMenuItemClick(object sender, System.EventArgs e) { frmAbout about = new frmAbout(); about.ShowDialog(); } } }
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
如何:對Windows 窗體控件進(jìn)行線程安全調(diào)用
使用多線程提高 Windows 窗體應(yīng)用程序的性能時,必須注意以線程安全方式調(diào)用控件。2007-03-03C#實現(xiàn)復(fù)制文件夾中文件到另一個文件夾的方法
這篇文章主要介紹了C#實現(xiàn)復(fù)制文件夾中文件到另一個文件夾的方法,實例分析了C#實現(xiàn)文件夾的查找、判斷及文件復(fù)制相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07