拼数字游戏源代码

2022-05-19 13:16:23   第一文档网     [ 字体: ] [ 阅读: ] [ 文档下载 ]
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。下载word有问题请添加QQ:admin处理,感谢您的支持与谅解。点击这里给我发消息

#第一文档网# 导语】以下是®第一文档网的小编为您整理的《拼数字游戏源代码》,欢迎阅读!
源代码,数字,游戏

拼数字游戏源代码如下:

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;

namespace NumberGame {

public partial class MainForm : Form {

public MainForm() {

InitializeComponent(); }

Label[,] arrLbl = new Label[3, 3];//存放Label控件的二维数组 int unRow = 0, unCol = 0;//记录不可见Label的下标 bool playing = false;//是否正在游戏



private void btnPlay_Click_1(object sender, EventArgs e) {

//把九个Label控件装入二维数组中以便控制 arrLbl[0, 0] = label1; arrLbl[0, 1] = label2; arrLbl[0, 2] = label3; arrLbl[1, 0] = label4; arrLbl[1, 1] = label5; arrLbl[1, 2] = label6; arrLbl[2, 0] = label7; arrLbl[2, 1] = label8; arrLbl[2, 2] = label9;

arrLbl[unRow, unCol].Visible = true;//防止2次单击开始游戏 int[] arrNum = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

//将一位数组arrNum中的数字随机排列

Random rm = new Random();//初始化随机函数类 for (int i = 0; i < 8; i++)


{

int rmNum = rm.Next(i, 9);//随机数大于等于i,小于9 int temp = arrNum[i];//交换数组中2个元素的值 arrNum[i] = arrNum[rmNum]; arrNum[rmNum] = temp; }

for (int i = 0; i < 9; i++)

{ //把一维数组的数字依次在二维数组的标签控件显示 arrLbl[i / 3, i % 3].Text = arrNum[i].ToString();

}

int cover = rm.Next(0, 9);//生成一个随机数用于掩盖某个数字

unRow=cover/3;//转化为不可见标签的在二维数组中的行下标 unCol = cover % 3;//转化为列下标

arrLbl[unRow, unCol].Visible = false;//让该标签不可见 playing = true;//设置游戏进行中标记 }

private void label5_Click(object sender, EventArgs e) {

if (!playing)//如果游戏不在进行中 {

return;//退出事件方法的执行 }

int row = ((Label)sender).Top / 80;//计算点中标签的行下标

int col = ((Label)sender).Left / 80;//计算点中标签的列下标

if (Math.Abs(row - unRow)+Math.Abs(col-unCol) == 1)

{ //判断方块是否可以移动,如果可以则交换标签显示的数字 string temp = arrLbl[unRow, unCol].Text;

arrLbl[unRow,unCol].Text=arrLbl[row, col].Text; arrLbl[row, col].Text = temp;

arrLbl[unRow, unCol].Visible = true; arrLbl[row, col].Visible = false;

unRow = row; //设置新的不可见标签下标值 unCol = col;

}

for (int i = 0; i < 9; i++)

{ //判断是否已成功排列数字


if(arrLbl[i/3,i%3].Text!=Convert.ToString(i+ 1)) {

break; }

if (i == 8) {

arrLbl[unRow,unCol].Visible =true;//显示被掩盖数字

playing = false;//设置游戏结束标志 MessageBox.Show("恭喜你通过了游戏", "消息对话框",



} } }

MessageBoxButtons.OK,MessageBoxIcon.Information); } }

本文来源:https://www.dywdw.cn/d6a996fd284ac850ad0242cf.html

推荐阅读