gpt4 book ai didi

C Battleship程序malloc内存分配和放置船只

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

我在为 Battleship 程序中的网格分配内存时遇到问题。虽然我不必创建整个游戏(只是设置),但我对 malloc 并不十分熟悉,所以我一直难以在我的代码中实现它。基本上,我不知道该怎么做。有什么建议吗?

另一个问题是我需要一个函数来随机生成这两个部分的位置,一艘航母(5 个单位长)和一艘战列舰(4 个单位长)而不让它们重叠。我不确定如何调用数组或显示片段。

输出应该是这样的:

battleship grid with 2 ships

到目前为止,这是我的代码:

/*
HEADER:
Author: Laura Kent
Date: 11/23/2014
Purpose: In this code, the user plays a simple game of battle ship on a 10x10 board, in which both hidden pieces must be sunk within a certain number of moves.
It is the coder's job to make sure the locations of each piece are random and do not over-lap.
The game must be explained beforehand and set to one of the three difficulties that the user selected.
After each update, the board display must be updated. */


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

#define SIZE 10;

void menu(void);
void dispBoard(int board[][SIZE]);

int main()
{
headerinfo();
menu();

char lvl[50];

int board[SIZE][SIZE];
int line, column, count=0, attempt;

/*Another void function is used to print out the main menu which then loops back in the main function so the user can choose other options.*/

while(1)
{
printf("\tSelect your difficulty( easy, normal, hard):");
gets(lvl);

if(strncmp(lvl,"easy",4)==0)
{
attempt = 30;
}

else if (strncmp(lvl,"normal",6)==0)
{
attempt = 25;
}
else if (strncmp(lvl,"hard",4)==0)
{
attempt = 20;
}
else
{
printf("Invalid input!/n");
}

*board = (int *)malloc(SIZE * SIZE * sizeof(int));

for (line=0 ; line < SIZE ; line++ )
{
for(column=0 ; column < SIZE ; column++ )
{
*(board + line*SIZE + column) = ++count;
}
}
dispBoard(board);
}
return 0;
}

/*This function justs prints out the coder's header info through a void function.*/

void headerinfo (void)
{
printf ("********************************\nc\nAuthor: Laura Kent lek0073\nCSCE 1030\n********************************\n\n");
}

/*This function prints out the main menu for the game, which is a intro message and the instructions for the player. The difficulty attempts are also mentioned.*/

void menu(void)
{

printf("\t\t\t\t\t\t\tWellcome to battleship!\n\tThe objective of this game if for you, the player to sink both of the hidden vessels by guessing their locations on a 10x10 board.\n\tThe two ships are an aircraft carrier (A) that is 5 spaces long and a battleship (B) that is 4 spaces long.\n\tThe location of theses vessels are random so either can be found in a row or column. It is up to the player to guess a square where they might be.\n");
printf("\tIf the player's guess is a miss, that spot will be marked with an '0' but if it is a hit then a '1' will appear, otherwise all squares will be blank.\n");
printf("\tLastly, each difficulty has a certain amount of attempts: easy (30 attempts), normal (25 attempts), hard (20 attemps).\n\n");

}

void dispBoard(int board[][SIZE])
{
int line, column;

printf("\t1 \t2 \t3 \t4 \t5 \t6 \t7 \t8 \t9 \t10");
printf("\n");

for (line='A'; line <= 'J'; line++ ){
printf("%c",line);
for(column=0; column < SIZE; column++ ){

if(board[line][column]==-1){
printf("\t!");

}else if(board[line][column]==0){
printf("\t*");
}else if(board[line][column]==1){
printf("\tX");
}
}
printf("\n");
}
}

最佳答案

你做错了。

int board[SIZE][SIZE];

这里您已经有了静态分配的网格。

*board = (int *)malloc(SIZE * SIZE * sizeof(int));

这不是你想要做的,除非你想创建一个 3D 战舰 map :*board 将是一个 2D 数组,所以 board 将是 3D。无论如何,这是静态和动态分配之间的糟糕混合:您不需要 malloc

关于生成随机位置,这里有一个建议:

  • 网格的每个“单元格”都应该有一个状态(例如空闲/占用)
  • 生成2个随机索引ij,都是<SIZE
  • 确保 cell[i][j] 是免费的然后随机生成一个方向并确保连续的单元格是免费的
  • 成功时,将单元标记为已占用,否则重复过程(在其他方向或从其他单元)

另外,关于代码:

  • void dispBoard(int board[][SIZE]) 应该是 void dispBoard(int board[SIZE][SIZE]) 以保持一致性
  • line 声明为 int 但您将其用作 char

关于C Battleship程序malloc内存分配和放置船只,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27140885/

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