这周的周总结

    这周主要还是在学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;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        const int N = 4;
        Button[,] buttons = new Button[N, N];   //创建N X N大小的按钮数组

        private void Form1_Load(object sender, EventArgs e)    //初始化
        {
            GenerateAllButtons();
        }
        void swap(Button btn1, Button btn2) //交换两个按钮 实际上只需要交换两个属性(显示的标号Text,是否可见Visible)即可
        {
            string str1 = btn1.Text;
            btn1.Text = btn2.Text;
            btn2.Text = str1;
            bool a = btn1.Visible;
            btn1.Visible = btn2.Visible;
            btn2.Visible = a;
        }
        private void button1_Click_1(object sender, EventArgs e)
        {

            for (int i = 0; i < 1000; i++)  //随机交换两个按钮 1000次实现洗牌
            {
                Random rnd = new Random();
                int a, b, c, d;
                a = rnd.Next(N);          //产生四个不大于N的随机数
                b = rnd.Next(N);
                c = rnd.Next(N);
                d = rnd.Next(N);
                swap(buttons[a, b], buttons[c, d]);
            }
        }

        Button FindHiddenButton()        //找到那个没显示的按钮
        {
            int i, j;
            for (i = 0; i < N; i++)
                for (j = 0; j < N; j++)
                    if (buttons[i, j].Visible == false)
                        return buttons[i, j];
            return null;
        }


        bool IsNeighbor(Button btnA, Button btnB) // 判断两个按钮是否相邻
        {
            int a = (int)btnA.Tag;
            int b = (int)btnB.Tag;
            int r1 = a / N, c1 = a % N;
            int r2 = b / N, c2 = b % N;

            if (r1 == r2 && (c1 == c2 - 1 || c1 == c2 + 1) //相邻条件
                || c1 == c2 && (r1 == r2 - 1 || r1 == r2 + 1))
                return true;
            return false;
        }


        bool ResultIsOk()     //判断是否完成
        {
            for (int r = 0; r < N; r++)
                for (int c = 0; c < N; c++)
                {
                    if (buttons[r, c].Text != (r * N + c + 1).ToString())
                    {
                        return false;
                    }
                }
            return true;
        }

        void btn_Click(object sender, EventArgs e)  //点击某个按钮
        {
            Button btn = sender as Button; //当前点中的按钮
            Button blank = FindHiddenButton();
            if (IsNeighbor(btn, blank))
            {
                swap(btn, blank);
               blank.Focus();    //duang 给"移动"的格子加特效
            }

            //判断是否完成了
            if (ResultIsOk())
            {
                MessageBox.Show("You Win");   //弹出消息窗
            }
        }

        void GenerateAllButtons()             //生成所有按钮
        {
            int x0 = 100, y0 = 10, wid1 = 50, wid2 = 45;
            for (int i = 0; i < N; i++)
                for (int j = 0; j < N; j++)
                {
                    Button btn = new Button();

                    btn.Top = y0 + i * wid1;
                    btn.Left = x0 + j * wid1;
                    btn.Width = wid2;
                    btn.Height = wid2;
                    btn.Visible = true;
                    btn.Text = (i * N + j + 1).ToString();    //初始化按钮所有属性
                    btn.Tag = i * N + j;           //保存行号 列号

                    btn.Click += new EventHandler(btn_Click);

                    buttons[i, j] = btn;       //放到数组上
                    this.Controls.Add(btn);   //加到界面上
                }
            buttons[N - 1, N - 1].Visible = false;
        }




    }
}

    很多都是按照教学来的。然后还学了很多基础知识比如c#中的类,字段,属性和索引,类的继承和修饰符等等,都是些很基础的知识,离做游戏应该还要好久。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值