gpt4 book ai didi

c - 如何扫描网格以仅替换某个字符?

转载 作者:行者123 更新时间:2023-11-30 16:09:38 24 4
gpt4 key购买 nike

我想要一个在将所有 CSHIPS ('.') 更改为 REVEALEDCSHIPS ('C') 并显示它们后结束我的程序的函数。
问题是所有 SEA 方 block 也都变成了 REVEALEDCSHIPS,但当然应该保留 SEA。
SEA 的值也定义为“.”,如果这有区别的话。
在下面找到我的代码环境。

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

#define MAXGRIDSIZE 15
#define SEA '.'
#define PSHIP 'P'
#define SUNKENPSHIP 'B'
#define CSHIP '.'
#define BOMB '*'
#define SUNKENCSHIP 'V'
#define REVEALEDCSHIP 'C'
unsigned char grid[MAXGRIDSIZE][MAXGRIDSIZE];
int fleetSize;
void initGrid()
{
int x, y;
for(y=0; y<MAXGRIDSIZE; y++)
{
for(x=0; x<MAXGRIDSIZE; x++)
{
grid[y][x] = SEA;
}
}
}
void printGrid()
{
int x, y;
printf("\n");
for(y=0; y<MAXGRIDSIZE; y++)
{
printf("%2d", y);
for(x=0; x<MAXGRIDSIZE; x++)
{
printf("%3c", grid[y][x]);
}
printf("\n");
}
printf("%2c", ' ');
for(x=0; x<MAXGRIDSIZE; x++)
{
printf("%3d", x);
}
printf("\n");
}
void placePShip()
{
int x,y;
printf("\nEnter Ship location: x , y: ");
scanf("%d , %d", &x, &y);
if (grid[y][x] == SEA)
{
grid[y][x] = PSHIP;
}
}
void placeCShip(){
srand(time(NULL));
while ( 1 )
{
int a=rand()%15 ;
int b=rand()%15 ;
if (grid[a][b] == SEA)
{
grid[a][b] = CSHIP;
break ;
}
}
}
void placeShips()
{
int i;
printf("\nEnter fleet size : ");
scanf("%d", &fleetSize);
if(fleetSize>122)
{
printf("\nThat size is too large to fit in grid, select a smaller number");
placeShips();
return;
}
for(i=0;i<fleetSize;i++)
{
placePShip();
placeCShip();
printGrid();
}
}
void quit()
{
int x,y;
for(y=0;y<MAXGRIDSIZE;y++)
{
if(grid[y][x] == CSHIP){grid[y][x] = REVEALEDCSHIP;}
for(x=0;x<MAXGRIDSIZE;x++)
{
if(grid[y][x] == CSHIP)
{
grid[y][x] = REVEALEDCSHIP;
}
}
}
printGrid();
printf("\n>>GG, here are all remaining ships");
}
int main()
{
initGrid();
printGrid();
placeShips();
quit();
}

最佳答案

您需要对 SEA 和 CSHIP 使用不同的值;
因为对相同的值使用不同的名称/同义词/宏没有帮助。
任何与 CSHIP 相同的内容(被定义为 '.')也将与 SEA 相同(也被定义为 '.'),因此代码将做出以下 react : SEA和CSHIP就像CSHIP一样,即将SEA变成REVEALEDCSHIP。

#define SEA '.'
/* this one gets shown to the player directly */

#define CSHIP 'H'
/* this one needs to be kept secret from the player during game, see below */

为了使它们在游戏过程中对玩家来说看起来相同(在 quit() 中显示它们之前),请更改 printGrid() 函数以显示它们相同,但知道差异:

void printGrid()
{
int x, y;
printf("\n");
for(y=0; y<MAXGRIDSIZE; y++)
{
printf("%2d", y);
for(x=0; x<MAXGRIDSIZE; x++)
{
if(CSHIP == grid[y][x]) /* do not show like this */
{ printf("%3c", SEA); /* camouflage instead */
} else
{ printf("%3c", grid[y][x]); /* others directly */
}
}
printf("\n");
}
printf("%2c", ' ');
for(x=0; x<MAXGRIDSIZE; x++)
{
printf("%3d", x);
}
printf("\n");
}

然后你的 quit() 函数应该可以工作,这里只是一些清理工作:

void quit()
{
/* always init, just for safety */
int x=0;
int y=0;

for(y=0;y<MAXGRIDSIZE;y++)
{
/* unneeded, and did use previously non-initialised x
if(grid[y][x] == CSHIP){grid[y][x] = REVEALEDCSHIP;}
*/
for(x=0;x<MAXGRIDSIZE;x++)
{
if(grid[y][x] == CSHIP)
{
grid[y][x] = REVEALEDCSHIP;
}
}
}
printGrid();
printf("\n>>GG, here are all remaining ships.\n"); /* newline, to ensure output */
}

关于c - 如何扫描网格以仅替换某个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59096863/

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