C#实现简单的井字游戏实例
本文实例讲述了C#实现简单的井字游戏。分享给大家供大家参考。具体如下:
/*
*Createdusing:SharpDevelop
*Createdby:TonyMisner
*Date:1/2/2007
*Time:2:34PM
*
*/
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Drawing;
usingSystem.Windows.Forms;
namespaceTicTacToe
{
///<summary>
///ThisisabasiconeplayerversuscomputergameofTicTacToe
///</summary>
publicpartialclassfrmMain
{
stringplayerTurn="0";
stringplayerSymbol="X";
stringcomputerSymbol="O";
intplayCounter=0;
[STAThread]
publicstaticvoidMain(string[]args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(newfrmMain());
}
publicfrmMain()
{
InitializeComponent();
}
voidLabel1Click(objectsender,System.EventArgse)
{
boolplayerDone=false;
stringlabelText=label1.Text;
if(playerClick(labelText)==true)
{
label1.Text=playerSymbol;
playerDone=true;
}
else
{
return;
}
if(checkWin()==true)
{
resetGame();
}
else
{
computerGo();
if(checkWin()==true)
{
resetGame();
}
}
}
voidLabel2Click(objectsender,System.EventArgse)
{
boolplayerDone=false;
stringlabelText=label2.Text;
if(playerClick(labelText)==true)
{
label2.Text=playerSymbol;
playerDone=true;
}
else
{
return;
}
if(checkWin()==true)
{
resetGame();
}
else
{
computerGo();
if(checkWin()==true)
{
resetGame();
}
}
}
voidLabel3Click(objectsender,System.EventArgse)
{
boolplayerDone=false;
stringlabelText=label3.Text;
if(playerClick(labelText)==true)
{
label3.Text=playerSymbol;
playerDone=true;
}
else
{
return;
}
if(checkWin()==true)
{
resetGame();
}
else
{
computerGo();
if(checkWin()==true)
{
resetGame();
}
}
}
voidLabel4Click(objectsender,System.EventArgse)
{
boolplayerDone=false;
stringlabelText=label4.Text;
if(playerClick(labelText)==true)
{
label4.Text=playerSymbol;
playerDone=true;
}
else
{
return;
}
if(checkWin()==true)
{
resetGame();
}
else
{
computerGo();
if(checkWin()==true)
{
resetGame();
}
}
}
voidLabel5Click(objectsender,System.EventArgse)
{
boolplayerDone=false;
stringlabelText=label5.Text;
if(playerClick(labelText)==true)
{
label5.Text=playerSymbol;
playerDone=true;
}
else
{
return;
}
if(checkWin()==true)
{
resetGame();
}
else
{
computerGo();
if(checkWin()==true)
{
resetGame();
}
}
}
voidLabel6Click(objectsender,System.EventArgse)
{
boolplayerDone=false;
stringlabelText=label6.Text;
if(playerClick(labelText)==true)
{
label6.Text=playerSymbol;
playerDone=true;
}
else
{
return;
}
if(checkWin()==true)
{
resetGame();
}
else
{
computerGo();
if(checkWin()==true)
{
resetGame();
}
}
}
voidLabel7Click(objectsender,System.EventArgse)
{
boolplayerDone=false;
stringlabelText=label7.Text;
if(playerClick(labelText)==true)
{
label7.Text=playerSymbol;
playerDone=true;
}
else
{
return;
}
if(checkWin()==true)
{
resetGame();
}
else
{
computerGo();
if(checkWin()==true)
{
resetGame();
}
}
}
voidLabel8Click(objectsender,System.EventArgse)
{
boolplayerDone=false;
stringlabelText=label8.Text;
if(playerClick(labelText)==true)
{
label8.Text=playerSymbol;
playerDone=true;
}
else
{
return;
}
if(checkWin()==true)
{
resetGame();
}
else
{
computerGo();
if(checkWin()==true)
{
resetGame();
}
}
}
voidLabel9Click(objectsender,System.EventArgse)
{
boolplayerDone=false;
stringlabelText=label9.Text;
if(playerClick(labelText)==true)
{
label9.Text=playerSymbol;
playerDone=true;
}
else
{
return;
}
if(checkWin()==true)
{
resetGame();
}
else
{
computerGo();
if(checkWin()==true)
{
resetGame();
}
}
}
boolplayerClick(stringlabelText)
{
if(playerTurn=="1"&&labelText==""&&playCounter<4)
{
playerTurn="2";
lblTurn.Text="Player2Turn";
playCounter++;
returntrue;
}elseif(playCounter==4)
{
toolStripTotal.Text=((Convert.ToInt32(toolStripTotal.Text))+1).ToString();
toolStripDraw.Text=((Convert.ToInt32(toolStripDraw.Text))+1).ToString();
MessageBox.Show("Draw","GameOver",MessageBoxButtons.OK,MessageBoxIcon.Stop);
resetGame();
}
returnfalse;
}
boolcheckWin()
{
boolwin=false;
if(label1.Text==label2.Text&&label2.Text==label3.Text&&label1.Text!="")
{
win=true;
}
elseif(label4.Text==label5.Text&&label5.Text==label6.Text&&label4.Text!="")
{
win=true;
}
elseif(label7.Text==label8.Text&&label8.Text==label9.Text&&label7.Text!="")
{
win=true;
}
elseif(label1.Text==label4.Text&&label4.Text==label7.Text&&label1.Text!="")
{
win=true;
}
elseif(label2.Text==label5.Text&&label5.Text==label8.Text&&label2.Text!="")
{
win=true;
}
elseif(label3.Text==label6.Text&&label6.Text==label9.Text&&label3.Text!="")
{
win=true;
}
elseif(label1.Text==label5.Text&&label5.Text==label9.Text&&label1.Text!="")
{
win=true;
}
elseif(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("Player2haswon!","GameOver",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
returnwin=true;
}
else
{
toolStripWon.Text=((Convert.ToInt32(toolStripWon.Text))+1).ToString();
MessageBox.Show("Player1haswon!","GameOver",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
returnwin=true;
}
}
returnwin;
}
voidresetGame()
{
label1.Text="";
label2.Text="";
label3.Text="";
label4.Text="";
label5.Text="";
label6.Text="";
label7.Text="";
label8.Text="";
label9.Text="";
playerTurn="1";
playCounter=0;
lblTurn.Text="Player1Turn";
}
voidcomputerGo()
{
boolcomputerDone=false;
computerDone=computerGoForWin();
if(computerDone==false)
{
computerDone=computerGoForBlock();
if(computerDone==false)
{
computerDone=computerGoRandom();
}
}
playerTurn="1";
lblTurn.Text="Player1Turn";
}
boolcomputerGoForWin()
{
boolcomputerDone=false;
if(label1.Text==computerSymbol&&label2.Text==computerSymbol&&label3.Text=="")
{
label3.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label1.Text==computerSymbol&&label3.Text==computerSymbol&&label2.Text=="")
{
label2.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label2.Text==computerSymbol&&label3.Text==computerSymbol&&label1.Text=="")
{
label1.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label4.Text==computerSymbol&&label5.Text==computerSymbol&&label6.Text=="")
{
label6.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label4.Text==computerSymbol&&label6.Text==computerSymbol&&label5.Text=="")
{
label5.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label5.Text==computerSymbol&&label6.Text==computerSymbol&&label4.Text=="")
{
label4.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label7.Text==computerSymbol&&label8.Text==computerSymbol&&label9.Text=="")
{
label9.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label7.Text==computerSymbol&&label9.Text==computerSymbol&&label8.Text=="")
{
label8.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label8.Text==computerSymbol&&label9.Text==computerSymbol&&label7.Text=="")
{
label7.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label1.Text==computerSymbol&&label4.Text==computerSymbol&&label7.Text=="")
{
label7.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label1.Text==computerSymbol&&label7.Text==computerSymbol&&label4.Text=="")
{
label4.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label4.Text==computerSymbol&&label7.Text==computerSymbol&&label1.Text=="")
{
label1.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label2.Text==computerSymbol&&label5.Text==computerSymbol&&label8.Text=="")
{
label8.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label2.Text==computerSymbol&&label8.Text==computerSymbol&&label5.Text=="")
{
label5.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label5.Text==computerSymbol&&label8.Text==computerSymbol&&label2.Text=="")
{
label2.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label3.Text==computerSymbol&&label6.Text==computerSymbol&&label9.Text=="")
{
label9.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label3.Text==computerSymbol&&label9.Text==computerSymbol&&label6.Text=="")
{
label6.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label6.Text==computerSymbol&&label9.Text==computerSymbol&&label3.Text=="")
{
label3.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label1.Text==computerSymbol&&label5.Text==computerSymbol&&label9.Text=="")
{
label9.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label5.Text==computerSymbol&&label9.Text==computerSymbol&&label1.Text=="")
{
label1.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label1.Text==computerSymbol&&label9.Text==computerSymbol&&label5.Text=="")
{
label5.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label3.Text==computerSymbol&&label5.Text==computerSymbol&&label7.Text=="")
{
label7.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label3.Text==computerSymbol&&label7.Text==computerSymbol&&label5.Text=="")
{
label5.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label5.Text==computerSymbol&&label7.Text==computerSymbol&&label3.Text=="")
{
label3.Text=computerSymbol;
returncomputerDone=true;
}
returncomputerDone=false;
}
boolcomputerGoForBlock()
{
boolcomputerDone=false;
if(label1.Text==playerSymbol&&label2.Text==playerSymbol&&label3.Text=="")
{
label3.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label1.Text==playerSymbol&&label3.Text==playerSymbol&&label2.Text=="")
{
label2.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label2.Text==playerSymbol&&label3.Text==playerSymbol&&label1.Text=="")
{
label1.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label4.Text==playerSymbol&&label5.Text==playerSymbol&&label6.Text=="")
{
label6.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label4.Text==playerSymbol&&label6.Text==playerSymbol&&label5.Text=="")
{
label5.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label5.Text==playerSymbol&&label6.Text==playerSymbol&&label4.Text=="")
{
label4.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label7.Text==playerSymbol&&label8.Text==playerSymbol&&label9.Text=="")
{
label9.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label7.Text==playerSymbol&&label9.Text==playerSymbol&&label8.Text=="")
{
label8.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label8.Text==playerSymbol&&label9.Text==playerSymbol&&label7.Text=="")
{
label7.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label1.Text==playerSymbol&&label4.Text==playerSymbol&&label7.Text=="")
{
label7.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label1.Text==playerSymbol&&label7.Text==playerSymbol&&label4.Text=="")
{
label4.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label4.Text==playerSymbol&&label7.Text==playerSymbol&&label1.Text=="")
{
label1.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label2.Text==playerSymbol&&label5.Text==playerSymbol&&label8.Text=="")
{
label8.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label2.Text==playerSymbol&&label8.Text==playerSymbol&&label5.Text=="")
{
label5.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label5.Text==playerSymbol&&label8.Text==playerSymbol&&label2.Text=="")
{
label2.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label3.Text==playerSymbol&&label6.Text==playerSymbol&&label9.Text=="")
{
label9.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label3.Text==playerSymbol&&label9.Text==playerSymbol&&label6.Text=="")
{
label6.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label6.Text==playerSymbol&&label9.Text==playerSymbol&&label3.Text=="")
{
label3.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label1.Text==playerSymbol&&label5.Text==playerSymbol&&label9.Text=="")
{
label9.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label5.Text==playerSymbol&&label9.Text==playerSymbol&&label1.Text=="")
{
label1.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label1.Text==playerSymbol&&label9.Text==playerSymbol&&label5.Text=="")
{
label5.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label3.Text==playerSymbol&&label5.Text==playerSymbol&&label7.Text=="")
{
label7.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label3.Text==playerSymbol&&label7.Text==playerSymbol&&label5.Text=="")
{
label5.Text=computerSymbol;
returncomputerDone=true;
}
elseif(label5.Text==playerSymbol&&label7.Text==playerSymbol&&label3.Text=="")
{
label3.Text=computerSymbol;
returncomputerDone=true;
}
returncomputerDone=false;
}
boolcomputerGoRandom()
{
boolcomputerDone=false;
Randomrandom=newRandom();
do
{
intposition=random.Next(1,10);
switch(position)
{
case1:
if(label1.Text=="")
{
label1.Text=computerSymbol;
returncomputerDone=true;
}
break;
case2:
if(label2.Text=="")
{
label2.Text=computerSymbol;
returncomputerDone=true;
}
break;
case3:
if(label3.Text=="")
{
label3.Text=computerSymbol;
returncomputerDone=true;
}
break;
case4:
if(label4.Text=="")
{
label4.Text=computerSymbol;
returncomputerDone=true;
}
break;
case5:
if(label5.Text=="")
{
label5.Text=computerSymbol;
returncomputerDone=true;
}
break;
case6:
if(label6.Text=="")
{
label6.Text=computerSymbol;
returncomputerDone=true;
}
break;
case7:
if(label7.Text=="")
{
label7.Text=computerSymbol;
returncomputerDone=true;
}
break;
case8:
if(label8.Text=="")
{
label8.Text=computerSymbol;
returncomputerDone=true;
}
break;
case9:
if(label9.Text=="")
{
label9.Text=computerSymbol;
returncomputerDone=true;
}
break;
}
}while(computerDone==false);
returncomputerDone=false;
}
voidBtnExitClick(objectsender,System.EventArgse)
{
if(MessageBox.Show("Areyousureyouwanttoexit?","Exit?",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
{
Application.Exit();
}
}
voidExitToolStripMenuItemClick(objectsender,System.EventArgse)
{
BtnExitClick(sender,e);
}
voidBtnNewGameClick(objectsender,System.EventArgse)
{
if(MessageBox.Show("Areyousureyouwanttorestart?","Restart?",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
{
toolStripTotal.Text=((Convert.ToInt32(toolStripTotal.Text))+1).ToString();
toolStripDraw.Text=((Convert.ToInt32(toolStripDraw.Text))+1).ToString();
resetGame();
}
}
voidNewGameToolStripMenuItemClick(objectsender,System.EventArgse)
{
BtnNewGameClick(sender,e);
}
voidAboutToolStripMenuItemClick(objectsender,System.EventArgse)
{
frmAboutabout=newfrmAbout();
about.ShowDialog();
}
}
}
希望本文所述对大家的C#程序设计有所帮助。