分享好友 天南地北首页 网站导航

求用C语言编写一个简单游戏的代码

网友发布 2023-07-19 00:10 · 头闻号游戏应用

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

char str[10][10]={0};

int n,i=0,j=0,k=1;

main()

{

for(i=0;i<10;i++){

for(j=0;j<10;j++)

str[i][j]='.';

}

srand((unsigned) time(NULL));

i=0,j=0;

char c='A';

str[0][0]=c;

do{

top:

n=rand()%4;

switch(n){

case 0:

if(i!=0){

i--;

break;

}else

goto top;

case 1:

if(i!=9){

i++;

break;

}else

goto top;

case 2:

if(j!=0){

j--;

break;

}else

goto top;

case 3:

if(j!=9){

j++;

break;

}else

goto top;

}

if(str[i][j]!='.'){

if(n==0){

i++;

goto top;

}

else if(n==1){

i--;

goto top;

}

else if(n==2){

j++;

goto top;

}

else{

j--;

goto top;

}

}else{

str[i][j]=++c;

k++;

}

if((str[i-1>0?i-1:1-i][j]!='.')

&&(str[i+1>9?i-1:1+i][j]!='.')

&&(str[i][j-1>0?j-1:1-j]!='.')

&&(str[i][j+1>9?j-1:1+j]!='.'))

goto end;

}while(k<26);

end:

for(i=0;i<10;i++){

for(j=0;j<10;j++)

printf("%c ",str[i][j]);

printf("n");

}

}

我们只需在Form中手动添加一个按钮,其余代码添加。

///

const int N = 3;

Button[,] buttons = new Button[N, N];

private void Form1_Load(object sender, EventArgs e)

{

//产生所有按钮

GenerateAllButtons();

}

private void button1_Click(object sender, EventArgs e)

{

//打乱顺序

Shuffle();

}

void Shuffle()

{

//多次随机交换两个按钮

Random rnd = new Random();

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

{

int a = rnd.Next(N);

int b = rnd.Next(N);

int c = rnd.Next(N);

int d = rnd.Next(N);

Swap(buttons[a, b], buttons[c, d]);

}

}

//生成所有按钮

void GenerateAllButtons()

{

int x0 = 100, y0 = 50, w = 100, d = 100;

for (int r = 0; r < N; r++)

{

for (int c = 0; c < N; c++)

{

int num = r * N + c;

Button btn = new Button();

btn.Text = (num + 1).ToString();

btn.Top = y0 + r * d;

btn.Left = x0 + c * d;

btn.Width = w;

btn.Height = w;

btn.Visible = true;

btn.Tag = r * N + c;//表示它所在的行列位置

//注册事件

btn.Click += new EventHandler(btn_Click);

buttons[r, c] = btn;//放到数组中

this.Controls.Add(btn);//放到界面上

}

}

buttons[N - 1, N - 1].Visible = false;//最后一个不可见

}

//交换两个按钮

void Swap(Button btna, Button btnb)

{

string t = btna.Text;

btna.Text = btnb.Text;

btnb.Text = t;

bool v = btna.Visible;

btna.Visible = btnb.Visible;

btnb.Visible = v;

}

//按钮点击事件处理

void btn_Click(object sender, EventArgs e)

{

Button btn = sender as Button;//当前点中的按钮

Button blank = FindHiddenButton();//空白按钮

//判断是否与空白块相邻,true 则交换

if (IsNeighbor(btn, blank))

{

Swap(btn, blank);

blank.Focus();

}

//判断是否完成

if (ResultIsOk())

{

MessageBox.Show("成功了!");

}

}

//查找要隐藏的按钮

Button FindHiddenButton()

{

for (int r = 0; r < N; r++)

{

for (int c = 0; c < N; c++)

{

if (!buttons[r, c].Visible)

{

return buttons[r, c];

}

}

}

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;

}

//

免责声明:本平台仅供信息发布交流之途,请谨慎判断信息真伪。如遇虚假诈骗信息,请立即举报

举报
反对 0
打赏 0
更多相关文章

收藏

点赞