gpt4 book ai didi

c - 在二维数组中的随机位置弹出一个数字

转载 作者:太空宇宙 更新时间:2023-11-04 03:46:18 24 4
gpt4 key购买 nike

我目前正在进行一个项目,编写一个 C 程序来克隆流行的 iPhone 游戏“2048”。对这款游戏不熟悉的可以访问here检查出来。

我将简要谈谈我是如何解决这个问题的:

  1. 生成一个只有两个“2”的表格。

  2. 用户开始播放。

  3. 该程序将在二维数组中为“0”的随机位置弹出一个“2”。

  4. 检查是否还有更多 Action 。如果没有,游戏结束。

我目前写的程序结构是:

  1. 生成一个二维数组。

  2. 编写了 4 个函数来移动数组中的数字。 (上移、下移、左移、右移)。

  3. 编写了 4 个函数来计算移动后的数字。 (AddUp、AddDown、AddLeft、AddRight)。

  4. 添加后再次调用移动函数重新排列表格。

  5. 打印矩阵并等待用户下一步 Action 。

这是我目前拥有的部分代码:

代码有点长。

现在我想不出在每次移动后随机在零 block 上弹出“2”的方法。感谢您的帮助。

我也愿意接受对我现有代码的建议。如果您需要完整的代码,我也可以给您。

#include<stdio.h>
#include<stdlib.h>

/* The main matrix */
int a[4][4];

/* <- Function declarations begin -> */
void InitMat(int a[4][4]);
void PrintMat(int a[4][4]);
void MoveUp(int a[4][4]);
void MoveLeft(int a[4][4]);
void MoveRight(int a[4][4]);
void MoveDown(int a[4][4]);
void AddUp(int a[4][4]);
void AddDown(int a[4][4]);
void AddLeft(int a[4][4]);
void AddRight(int a[4][4]);

/* -> Function declarations end <- */


/* <- Main function begin -> */
int main(void)
{
int input;
/* Initialize the starting matrix */
InitMat(a);
/* Display the main matrix */
PrintMat(a);

while ((input = getchar()) != 'q')
{
switch (input)
{
case 'w':
MoveUp(a);
AddUp(a);
MoveUp(a);
PrintMat(a);
break;
case 'a':
MoveLeft(a);
AddLeft(a);
MoveLeft(a);
PrintMat(a);
break;
case 'd':
MoveRight(a);
AddRight(a);
MoveRight(a);
PrintMat(a);
break;
case 's':
MoveDown(a);
AddDown(a);
MoveDown(a);
PrintMat(a);
break;
}
}


return 0;
}
/* -> Main function end <- */

/* <- Function definitions begin -> */

/* Function1: initializing the main matrix */
void InitMat(int a[4][4])
{
int i, j;

for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
a[i][j] = 0;
}
}
a[1][2] = a[2][2] = 2;
a[0][0] = 2;
a[3][0] = 2;
a[3][3] = 2;
}

/* Function2: Print the main matrix */
void PrintMat(int a[4][4])
{
printf("+-------+-------+-------+-------+\n");
printf("| %d | %d | %d | %d |\n", a[0][0], a[0][1], a[0][2], a[0][3]);
printf("+-------+-------+-------+-------+\n");
printf("| %d | %d | %d | %d |\n", a[1][0], a[1][1], a[1][2], a[1][3]);
printf("+-------+-------+-------+-------+\n");
printf("| %d | %d | %d | %d |\n", a[2][0], a[2][1], a[2][2], a[2][3]);
printf("+-------+-------+-------+-------+\n");
printf("| %d | %d | %d | %d |\n", a[3][0], a[3][1], a[3][2], a[3][3]);
printf("+-------+-------+-------+-------+\n");
}

/* Function3: Move upwards */
void MoveUp(int a[4][4])
{
int j;
/* i: rows; j: columns */
for (j = 0; j < 4; j++)
{
if (a[0][j] == 0)
{
if (a[1][j] == 0)
{
if (a[2][j] == 0)
{
if (a[3][j] == 0)
{
}
else
{
a[0][j] = a[3][j];
a[3][j] = 0;
}
}
else
{
a[0][j] = a[2][j];
a[2][j] = 0;
}
}
else
{
a[0][j] = a[1][j];
a[1][j] = 0;
}
}
else
{
a[0][j] = a[0][j];
}

if (a[1][j] == 0)
{
if (a[2][j] == 0)
{
if (a[3][j] == 0)
{
}
else
{
a[1][j] = a[3][j];
a[3][j] = 0;
}
}
else
{
a[1][j] = a[2][j];
a[2][j] = 0;
}
}
else
{
a[1][j] = a[1][j];
}

if (a[2][j] == 0)
{
if (a[3][j] == 0)
{
}
else
{
a[2][j] = a[3][j];
a[3][j] = 0;
}
}
else
{
a[2][j] = a[2][j];
}
}
}

/* Function7: addition after moving up */
void AddUp(int a[4][4])
{
int i, j;
/* i: rows; j: columns */
for (j = 0; j < 4; j++)
{
for (i = 0; i < 4; i++)
{
if (a[i][j] == a[i + 1][j])
{
a[i][j] += a[i + 1][j];
a[i + 1][j] = 0;
}
else
{
}
}
}

最佳答案

您可以使用函数 rand() 生成一个介于 0 和 maxint 值之间的随机值。

你必须做这样的事情:

#define MAXRANDOMVALUE 3

/.../ //functions and everything

void poprandomtwo() {

int i,j; //your indexs
i=(rand ())%(MAXRANDOMVALUE+1));
j=(rand ())%(MAXRANDOMVALUE+1));
/../ //here comes your job ;)
}

int main {

srand(time(NULL)); //do this one time at the beginning of your code to set the seed
/.../
return 0;
}

希望对你有帮助

关于c - 在二维数组中的随机位置弹出一个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24173470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com