所以我正在创建一个游戏。它有一个 5 x 5 的棋盘,上面写满了字符 a、b 和 c。我需要创建一个函数,如果板检测到彼此相邻的相同字母,它就会消失,并且清空的单元格将替换为一组新的字母(a、b、c)。有点像糖果粉碎游戏。我还需要显示游戏结束前的移动次数。这是我目前所处的位置
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX 10
//creates board and fills in the letters randomly
int board()
{
char grid[MAX][MAX];
char letter[3] = {'a', 'b', 'c'};
int i,j,row,col;
printf("Please enter your grid size: ");
scanf("%d %d", &row, &col);
if(row < 10 && col < 10){
for(i=0; i < MAX; i++){
for(j=0; j < MAX; j++){
grid[i][j] = letter[rand()%3];
}
}
for(i=0; i < MAX; i++){
for(j=0; j < MAX; j++){
printf("%c ", grid[i][j]);
}
printf("\n");
}
}
else{
printf("Board is too big\n");
board();
}
return 0;
}
//the count doesn't quite do what I need it to
int moveCount()
{
char s;
printf("Press s to start: ");
scanf("%c", &s);
if(s == 's' || s == 'S'){
int count;
int max = 10;
for(count=1; count < max; count++)
if(count == max){
-printf("No more moves can be made");
}
else{
printf("Number of moves made: %d\n", count);
}
}
else{
printf("That is not s\n");
moveCount();
}
}
//Trying to check to make sure that n board is always atleast three cells
int inputCheck(){
int n, m;
if(n == 3 || n > 3 && m == 1 || m > 1){
moveCount();
}
}
int main()
{
board();
inputCheck();
}
实现检查相邻单元格是否相同然后删除它们的功能的最佳方法是什么。我会想象做类似 if(myArray[0][0] == 'a' && myArray[0][1] == 'a'{do Something} 的事情...但我不知道这是否是最好的方式或者我将如何循环它。另外,如何正确实现显示移动的计数? 我意识到这段代码有很多缺陷,但我很新,所以请放轻松。感谢您提供任何帮助或插入正确的方向。
最佳答案
这里有一个严重的错误:
int n, m;
if(n == 3 || n > 3 && m == 1 || m > 1){
n
和m
未初始化时使用。
你需要#include <stdlib.h>
对于 rand()
关于c - 更新我的阵列棋盘游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24831132/