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

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

(0)

相关推荐

  • C#面向对象编程之猜拳游戏实现方法

    本文实例讲述了C#面向对象编程之猜拳游戏实现方法.分享给大家供大家参考.具体实现方法如下: 1.需求 现在要制作一个游戏,玩家与计算机进行猜拳游戏,玩家出拳,计算机出拳,计算机自动判断输赢. 2.需求分析 根据需求,来分析一下对象,可分析出:玩家对象(Player).计算机对象(Computer).裁判对象(Judge). 玩家出拳由用户控制,使用数字代表:1石头.2剪子.3布 计算机出拳由计算机随机产生 裁判根据玩家与计算机的出拳情况进行判断输赢. 3.类对象的实现 ①.玩家类示例代码: 复制

  • C#十五子游戏编写代码

    本文实例为大家分享了C#十五子游戏的具体代码,供大家参考,具体内容如下 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

  • 基于C#实现俄罗斯方块游戏

    最近在看C#,写了一个很水的方块游戏练手. 代码: namespace game { class Square { public Square() { state = 0; positionY = 0; positionX = 0; } public Square(int InitShapeCnt, int InitState) { state = InitState; positionY = 0; positionX = 0; InitShape(InitShapeCnt); } public

  • C#实现洗牌游戏实例

    棋牌类游戏是目前比较火的游戏之一.今天本文就以实例形式实现洗牌游戏.本文实例所采用的算法是:遍历每个位置上的牌,然后与随机位置上的牌交换. 运行结果如下图所示: 对于牌来讲,2个关键的因素是面值和类型(如红桃.梅花等). 代码如下: public class Card { private string mianzhi; private string leixin; public Card(string m, string l) { mianzhi = m; leixin = l; } publi

  • C#利用控件拖拽技术制作拼图游戏

    主要实现的功能: 1.程序附带多张拼图随机拼图. 2.可手动添加拼图. 3.游戏成功判断. 4.30秒超时判断. Puzzle.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms

  • C#实现的算24点游戏算法实例分析

    本文实例讲述了C#实现的算24点游戏算法.分享给大家供大家参考.具体如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Calc24Points { public class Cell { public enum Type { Number, Signal } public int Number; public ch

  • C#实现的24点游戏实例详解

    本文实例分析了C#实现的24点游戏.分享给大家供大家参考.具体如下: 1. 24点游戏规则及算法 规则:给出4个自然数,找出能够求出24的四则运算式,要求数字不能重复使用 分析: 本算法是一种暴力求解法: 给出任意两个数字,可以进行6种四则运算,求出最多6个值.以数字a和b为例,有: 加(a+b).减(a-b).被减(b-a).乘以(a*b).除以(a/b)和除(b/a) abcd共计四个数,如果顺序固定,则有5种计算顺序(★代表上面6种四则运算中的一种): ((a★b)★c)★d.(a★b)★

  • C#拼图游戏编写代码(2)

    前言:在C#拼图游戏编写代码程序设计 之 C#实现<拼图游戏>(上),上传了各模块代码,而在本文中将详细剖析原理,使读者更容易理解并学习,程序有诸多问题,欢迎指出,共同学习成长! 正文: 拼图是一个非常经典的游戏,基本每个人都知道他的玩法,他的开始,运行,结束.那么,当我们想要做拼图的时候如何入手呢?答案是:从现实出发,去描述需求(尽量描述为文档),当我们拥有了全面的需求,就能够提供可靠的策略,从而在代码中实现,最终成为作品! (一)需求: (这个需求书写较为潦草,为广大小白定制,按照最最最普

  • C#拼图游戏编写代码

    本文设计了C#拼图游戏程序,供大家参考,具体内容如下 功能描述: 1.用户自定义上传图片 2.游戏难度选择:简单(3*3).一般(5*5).困难(9*9)三个级别 3.纪录完成步数 模块: 1.拼图类 2.配置类 3.游戏菜单窗口 4.游戏运行窗口 代码文件VS2013版本: 下载链接: 拼图游戏 --------------------------------------------------我叫分割线---------------------------------------------

  • C#在Unity游戏开发中进行多线程编程的方法

    在这之前,有很多人在质疑Unity支不支持多线程,事实上Unity是支持多线程的.而提到多线程就要提到Unity非常常用的协程,然而协程并非真正的多线程.协程其实是等某个操作完成之后再执行后面的代码,或者说是控制代码在特定的时机执行.而多线程在Unity渲染和复杂逻辑运算时可以高效的使用多核CPU,帮助程序可以更高效的运行.本篇主要介绍在Unity中如何使用多线程. 首先引入C#中使用多线程的类库 using System.Threading; 创建线程实例的四种方式 一.线程执行无参方法 构造

随机推荐