c#字符串学习之祖玛游戏~

众所周知呢,c#是微软推出的一门面向对象的语言,学习他的人非常的多,有多少呢?很多很多,哈哈哈

学习呢,当然要有一个目标,或者觉得有趣才会去主动的,心甘情愿的去学,哈哈哈

这里呢,给大家分享一个以前学习c#时候的一个小小的项目,c#祖玛游戏,十分简单,别吐槽奥,哈哈哈

代码来了~

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    /// <summary>
    /// 程序入口
    /// </summary>
    class Program//只包含ABCDE五个字符,遇到三个相同的自动消除,当字符等于null时游戏结束
    {
        static void Main(string[] args)
        {
            int index = 0;          //插入的序号
            string c_insert = "";      //插入的字符
            string game_str = "ABDCBBDDAEECBDC";
            Console.WriteLine("****************************************************\n" +
                              "*                    游戏开始                      *\n" +
                              "****************************************************");
            while (!end(game_str))
            {
                Console.WriteLine("需要插入的字符串:" + game_str);
                Console.WriteLine("输入要插入的序号(0~" + game_str.Length + ")和插入的字符");
                if (in_error(ref index, ref c_insert, game_str))
                {
                    Console.WriteLine("输入有误,下一轮");
                }
                else
                {
                    game_str = game_str.Insert(index, c_insert);
                    if (game_str.Length > 2)
                    {
                        check(ref game_str);
                    }
                }
            }
            Console.WriteLine("游戏结束");
        }

        #region 检查是否消除,可以则消除
        /// <summary>
        /// 检查是否消除,可以则消除
        /// </summary>
        /// <param name="game_str"></param>
        public static void check(ref string game_str)
        {
            for (int i = 0; i < game_str.Length - 2; i++)
            {
                if (game_str[i].Equals(game_str[i + 1]) && game_str[i].Equals(game_str[i + 2]))
                {
                    for (int j = 0; j < 3; j++)
                    {
                        game_str = game_str.Remove(i, 1);
                    }
                    if(game_str.Length > 2)
                    {
                        check(ref game_str);
                    }
                }
            }
        }
        #endregion 检查是否消除,可以则消除

        #region 判断是否结束游戏
        /// <summary>
        /// 判断是否结束游戏
        /// </summary>
        /// <returns></returns>
        private static bool end(string game_str)
        {
            if (game_str.Equals(""))
                return true;
            else
                return false;
        }
        #endregion 判断是否结束游戏

        #region 判断输入是否错误
        /// <summary>
        /// 判断输入是否错误
        /// </summary>
        /// <param name="index">插入的序号</param>
        /// <param name="c_insert">插入的字符</param>
        /// <param name="game_str">被插入的字符串</param>
        /// <returns></returns>
        private static bool in_error(ref int index, ref string c_insert, string game_str)
        {
            try
            {
                index = Convert.ToInt32(Console.ReadLine());
            }
            catch(Exception e)
            {
                return true;
            }
            c_insert = Console.ReadLine();
            if (index > game_str.Length || index < 0 || c_insert[0] < 'A' || c_insert[0] > 'E' || c_insert.Length != 1)
            {
                return true;
            }
            return false;
        }
        #endregion 判断输入是否错误
    }
}

重点提示一下!!!!

字符串类型变量是不可变的!

大家要自己研究下这句话哦~

大家直接创建一个项目,复制代码运训即可,学习娱乐两不误~

什么??!你不想新建项目,不想复制粘贴?

好吧,你赢了,在这直接下载项目把。。。

c#字符祖玛游戏-C#代码类资源-CSDN下载

祝大家学习愉快,心想事成~

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java拾荒者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值