close

亂數不重複  

====================================================================================================================

※數字推盤遊戲_進度:

這次我們想利用C Sharp完成的程式設計是「數字推盤遊戲」。

這個遊戲的目標是希望藉由“推移”的方式,讓推盤上的亂數能按次序整齊的排列。換句話說就是要遊戲者移動板上的方塊,讓所有的方塊順著數字的次序排列。而現在我們首要的工作就是透過程式設計軟體C Sharp的協助,近一步呈現遊戲所需的亂數方塊。你現在所看到的是一個規格4x4(16個方塊)的推盤,而且上頭的方塊也已經能夠出現介於0 ~ 15之間的亂數。隨著點擊按鈕butont1,我們可以獲得更多亂數不重覆的4x4(16個方塊)推盤。

====================================================================================================================

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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
int[] myarray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
Button[,] buttons = new Button[9, 9];
Random rand = new Random();
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
// int x;
int width, height;



// Instantiating all the buttons in the array
for (int j = 1; j < 5; j++)
{
for (int i = 1; i < 5; i++)
{
width = this.Size.Width;
height = this.Size.Height;
buttons[i, j] = new Button();
buttons[i, j].Location = new Point(i * 50, j * 50);
//x = (i + 1) * (j + 1);
// x = myarray[(i - 1) * 4 + j - 1];

width = 50;
height = 50;
buttons[i, j].Size = new Size(width, height);
// buttons[i, j].Text = x.ToString();
this.Controls.Add(buttons[i, j]);
}
}

}

private void button1_Click(object sender, EventArgs e)
{
int a, b, y;
//int width, height;
//int[] myarray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
//Button[,] buttons = new Button[9, 9];
// Instantiating all the buttons in the array

for (int i = 15; i > 1; i--)
{

a = rand.Next(0, i);
b = myarray[a];
myarray[a] = myarray[i];
myarray[i] = b;

}



for (int j = 1; j < 5; j++)
{
for (int i = 1; i < 5; i++)
{
// buttons[1,1].Text="";
//width = this.Size.Width;
//height = this.Size.Height;

//buttons[i, j].Location = new Point(i * 50, j * 50);
y = myarray[(i - 1) * 4 + j - 1];
// width = 50;
// height = 50;
//buttons[i, j].Size = new Size(width, height);
buttons[i, j].Text = y.ToString();
this.Controls.Add(buttons[i, j]);
}

}
}
}
}

 

arrow
arrow
    全站熱搜

    鄭莠蓉 發表在 痞客邦 留言(1) 人氣()