C#+Winform实现成语接龙(一)单机离线版本

提示:本文字重点请解算法,相关代码,控伟问题自己查询.NET API文档,如果你完全不会Windows开发,请从VB6开始学习,这是我的VB6专栏

界面设计与算法

我们在生活中玩成础龙,就需要先发起一个成语,然后接龙,使新的成语的第一个字和旧成语最后一个字一样。游戏有限定时间,如果长时间卡顿,游戏就结束了,并且不能使用 已经用过的成语°现在,我们要将它用计算机实现这个程序。在此之前,需要设计一个界面,使用户方便,我们可以利用一个ListBox来储存成语,一个TextBox来输入成语,ComboBox来设定游戏时间,Label来显示分数和剩余时间,界面如图所示。控件名称是红字

现在我们通过界面和月户使用习惯来设计算法。这是程序运行大致过程

0读取用户先设置时间,

1用户要先输入一个成语当开始成语,选择时间,然后开始游戏。精入的成语就自动添加到列表框里面。

2之后就正式送入游戏,Label的提示语就要自动换成“请接龙”,按钮文字变为接龙,之后开始计时,让Ibltime显示设定的时间,每一秒都使Ibltime减少1。

3用户输入成语,接下来蔓判断用户输入的成语是否符合标准,也就是把新旧成语对比。如果符合条件,就把时间复位井且添加列表框,并使Ibiscore加一分,不满足则在Ibltip里提 示用户。

4如果某一次时间到了,就结束游戏,如果用户不想玩了,就单击复但,恢复初始状态。

这样,我们了解了整个算法顺序,接下来就要根据要求设计代码了.我们需要用代码逐一解决5个问题口

问题0可以说根本不艰问题,本来和问题1一起解决,但是因为问题1有些特别所以就单列

问题1似比较简单,只需要定义变量存储数据就行,给解决之后的问题提供敬据,不过实际上里面有点黑思 第二个间题前半部分也是^艮简单,只需要控制控件属性,代码如下。

后半部分就需要利用Wnfonn的Timer控件。这个控件在这里不做讲解,请自行学习。

,学不会直接看下方代码

第三个间题是最重要的,因为这个牵扯到一个判断,需要判断用户是不翳入第一个成语,因为这里设定成语和接龙用的是同一个按钮很容易想到,用户设定成语时列表是空白的,但是开始接龙之后就有了文字。据此就能判断顶果是第-个就执行问题1,不是就接着执行问龄判断直援用Length,现在需要对比成岛对比本次输入的和上次输入 的苹好入的好获取,那么上次的怎么沉这里就需要提前准备,在第-次设定成语的时候把成语看作上次的,赋值到湖变量,之后开始接龙的毗肴成新的成语赋值到newt 和上次的比、对比完成之后立到销毁上次的,把这次的赋值到上次的,再接收新成语,之后以此类推。而判断就需要后一个字和新成语第一个字时,对比是否一样。这里需要利用string类的一个函数方法,是,之后欢判转不是敛字,因为数字在字符串里也是文字,之后判断是不是四字成岛用, 判断这些是为了不让某些人蒙混过关,最后加分就简单了,复位直接设置属性,千万不要偷懒用Initializecomponet()以个是专门用初始化Winform设计器的,并不是某些人认为的初始化窗体。

代码

这是逻辑代码,也就是双击窗体之后的代码

using System;
using System.Windows.Forms;

namespace ChengyuJielong
{
    public partial class frmCyjl : Form
    {
        public int settime;
        public string oldt;
        public frmCyjl()
        {
            InitializeComponent();
        }

        private void frmCyjl_Load(object sender, EventArgs e)
        {
            comboBox1.Text = "10";
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)//回车的ASCII
            {
                button1.Focus();//设置焦点,方便用户使用
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            lbltip.Text = "";
            settime = Convert.ToInt32(comboBox1.Text);//读取时间
            int score = Convert.ToInt32(lblscore.Text);//读取分数
            comboBox1.Enabled = false;//锁定时间框
            string newt = textBox1.Text;//读取成语
            if (listBox1.Items.Count == 0)//判断是不是第一次输入
            {
                lbltime.Text = settime.ToString();//赋值时间
                listBox1.Items.Add(newt);//添加成语到列表
                oldt = newt;//添加之后把新的成语变成旧的成语
                newt = "";//销毁新成语
                label1.Text = "请接龙";//以下四行修改属性,进入游戏状态
                textBox1.Text = "";
                button1.Text = "接龙";
                timer1.Enabled = true;//开始计时
            }
            else
            {
                if (listBox1.FindStringExact(newt) == -1)//判断成语是否用过
                {
                    if (newt.Length != 4)//判断成语是不是四字词语
                    {
                        lbltip.Text = "目前仅支持四字词语";
                    }
                    else
                    {
                        if (oldt.Substring(oldt.Length - 1, 1) == newt.Substring(0, 1))//判断接龙是否正确
                        {
                            lbltime.Text = settime.ToString();//复位时间
                            timer1.Enabled = true;//重新计时
                            listBox1.Items.Add(newt);
                            oldt = newt;
                            newt = "";
                            score = score + 1;//分数+1
                            lblscore.Text = score.ToString();//赋值新分数
                        }
                        else
                        {
                            lbltip.Text = "请正确接龙";
                        }
                    }
                }
                else
                {
                    lbltip.Text = "成语已经用过了";
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (lbltime.Text != "0")//判断是否时间结束
            {
                int lasttime = Convert.ToInt32(lbltime.Text);
                lasttime = lasttime - 1;
                lbltime.Text = lasttime.ToString();
            }
            else
            {
                lbltip.Text = "游戏结束";//如果剩余0秒代表时间到了,停止游戏
                button1.Enabled = false;//设置按钮不能输入
            }
        }

        private void button2_Click(object sender, EventArgs e)//复位游戏
        {
            listBox1.Items.Clear();//清理列表框
            textBox1.Text = "";//请客文本框
            comboBox1.Enabled = true;//激活组合框
            timer1.Enabled = false;//停止计时
            lbltime.Text = settime.ToString () ;//复位时间
            lblscore.Text = "0";//分数归零
        }
    }
}

这是界面代码,不想拖拽的直接复制粘贴,记得修改名称空间

namespace ChengyuJielong
{
    partial class frmCyjl
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.lbltip = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.lblscore = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.lbltime = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(49, 273);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "开始游戏";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(288, 273);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "复位";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // listBox1
            // 
            this.listBox1.FormattingEnabled = true;
            this.listBox1.ItemHeight = 12;
            this.listBox1.Location = new System.Drawing.Point(49, 48);
            this.listBox1.Name = "listBox1";
            this.listBox1.ScrollAlwaysVisible = true;
            this.listBox1.Size = new System.Drawing.Size(75, 172);
            this.listBox1.TabIndex = 2;
            // 
            // timer1
            // 
            this.timer1.Interval = 1000;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(288, 48);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 21);
            this.textBox1.TabIndex = 3;
            this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(196, 51);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(53, 12);
            this.label1.TabIndex = 4;
            this.label1.Text = "开接成语";
            // 
            // lbltip
            // 
            this.lbltip.AutoSize = true;
            this.lbltip.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lbltip.Location = new System.Drawing.Point(160, 241);
            this.lbltip.Name = "lbltip";
            this.lbltip.Size = new System.Drawing.Size(55, 16);
            this.lbltip.TabIndex = 5;
            this.lbltip.Text = "提示:";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold);
            this.label3.Location = new System.Drawing.Point(195, 138);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(41, 16);
            this.label3.TabIndex = 6;
            this.label3.Text = "分数";
            // 
            // lblscore
            // 
            this.lblscore.AutoSize = true;
            this.lblscore.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold);
            this.lblscore.Location = new System.Drawing.Point(283, 138);
            this.lblscore.Name = "lblscore";
            this.lblscore.Size = new System.Drawing.Size(16, 16);
            this.lblscore.TabIndex = 7;
            this.lblscore.Text = "0";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label5.Location = new System.Drawing.Point(197, 189);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(41, 16);
            this.label5.TabIndex = 8;
            this.label5.Text = "时间";
            // 
            // lbltime
            // 
            this.lbltime.AutoSize = true;
            this.lbltime.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold);
            this.lbltime.Location = new System.Drawing.Point(285, 189);
            this.lbltime.Name = "lbltime";
            this.lbltime.Size = new System.Drawing.Size(25, 16);
            this.lbltime.TabIndex = 9;
            this.lbltime.Text = "10";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(196, 94);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(53, 12);
            this.label2.TabIndex = 10;
            this.label2.Text = "预设时间";
            // 
            // comboBox1
            // 
            this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.ImeMode = System.Windows.Forms.ImeMode.Off;
            this.comboBox1.Items.AddRange(new object[] {
            "10",
            "20",
            "30"});
            this.comboBox1.Location = new System.Drawing.Point(288, 91);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(100, 20);
            this.comboBox1.TabIndex = 11;
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            // 
            // frmCyjl
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(449, 328);
            this.Controls.Add(this.comboBox1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.lbltime);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.lblscore);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.lbltip);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Name = "frmCyjl";
            this.Text = "成语接龙";
            this.Load += new System.EventHandler(this.frmCyjl_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label lbltip;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label lblscore;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label lbltime;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.ComboBox comboBox1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值