C#实现简单的井字游戏实例
程序员文章站
2022-10-15 15:42:53
本文实例讲述了c#实现简单的井字游戏。分享给大家供大家参考。具体如下:
/*
* created using: sharpdevelop
* create...
本文实例讲述了c#实现简单的井字游戏。分享给大家供大家参考。具体如下:
/* * 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#程序设计有所帮助。