C#实现简单的井字游戏实例

 更新时间:2020年6月25日 11:28  点击:2163

本文实例讲述了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#程序设计有所帮助。

[!--infotagslink--]

相关文章

  • C#实现简单的登录界面

    我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
  • 浅谈C# 字段和属性

    这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C# List 排序各种用法与比较

    这篇文章主要介绍了C# List 排序各种用法与比较的相关资料,需要的朋友可以参考下...2020-06-25
  • C#实现简单的Http请求实例

    这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
  • C#中new的几种用法详解

    本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • 使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • 经典实例讲解C#递归算法

    这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
  • C#和JavaScript实现交互的方法

    最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • C#变量命名规则小结

    本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
  • 轻松学习C#的基础入门

    轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
  • c#中(&&,||)与(&,|)的区别详解

    这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • c#自带缓存使用方法 c#移除清理缓存

    这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25