首页 » 技术分享 » 3*3 数字拼图(去除无解的随机组合)

3*3 数字拼图(去除无解的随机组合)

 

3*3拼图 9个数字随机生成位置 从中随机取一个数字作为隐藏的数字,满足条件显示。

Winform窗体创建 9个Label。

代码详情:

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 Pingtu
{
    public partial class 拼图 : Form
    {
        public 拼图()
        {
            InitializeComponent();
        }
        Label[,] LabelList = new Label[3,3];

        int[] numList = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int unRow = 0; int unCol = 0;
        bool playing = false;
        int targetNum=0; //初始化隐藏数字
        private void button1_Click(object sender, EventArgs e)
        {
            CreatenNum();         
        }


        //随机排列 9个数字 找到有解得排列拼图
        void isCreateNumAgain()
        {
            int NumCount = 0;
            int cnt = 0;
            for (int i = 0; i < 8; i++)
            {
                for (int j = i + 1; j < 9; j++)
                {
                    if (numList[i] > numList[j]&&numList[j] != targetNum && numList[i]!=targetNum)
                    {
                        cnt++;
                    }
                }
            }
            if (cnt % 2 != 0)
            {
                NumCount++;
                RandomNum();              
            }
            else
            {
                for (int i = 0; i < 9; i++)
                {
                    LabelList[i / 3, i % 3].Text = numList[i].ToString();
                    if (numList[i].ToString() == targetNum.ToString())
                    {
                        unRow = i / 3;
                        unCol = i % 3;
                        LabelList[unRow, unCol].Visible = false;
                    }
                }

            }
        }

        void RandomNum()
        {
            Random rd = new Random();
            for (int i = 0; i < 9; i++)
            {
                int num = rd.Next(i, 9);
                int temp = numList[i];
                numList[i] = numList[num];
                numList[num] = temp;
            }
           targetNum = rd.Next(1, 9);
           isCreateNumAgain();
        }

        void CreatenNum()
        {
            button1.Visible = false;
            playing = true;
            Label[] LB = new Label[9] { label1, label2, label3, label4, label5, label6, label7, label8, label9 };
            for (int i = 0; i < 9; i++)
            {
                LabelList[i / 3, i % 3] = LB[i];
            }
            RandomNum();        
        }


        private void label1_Click(object sender, EventArgs e)
        {

            if (!playing)
                return;

            int row = ((Label)sender).Top / 160;
            int col = ((Label)sender).Left / 160;
            if (Math.Abs(row - unRow) + Math.Abs(col - unCol) == 1)
            {
                string temp = LabelList[row, col].Text.ToString();
                LabelList[row, col].Text = LabelList[unRow, unCol].Text;
                LabelList[unRow, unCol].Text = temp;
                LabelList[row, col].Visible = false;
                LabelList[unRow, unCol].Visible = true;
                unRow = row;
                unCol = col;
            }
            for (int i = 0; i < 9; i++)
            {
                if (LabelList[i / 3, i % 3].Text != (i + 1).ToString())
                    break;
                if (i == 8)
                {
                    LabelList[unRow, unCol].Visible = true;
                    playing = false;
                    button1.Visible = true;
                }
            }
        }
    }
}

初始界面

点击按钮 游戏界面:

转载自原文链接, 如需删除请联系管理员。

原文链接:3*3 数字拼图(去除无解的随机组合),转载请注明来源!

0