C战舰计划malloc内存分配和放置船只

时间:2014-11-26 03:33:41

标签: c arrays malloc

我在战舰计划中为网格分配内存时遇到问题。虽然我不必创建整个游戏(只是设置),但我并不完全熟悉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");
    }
}

1 个答案:

答案 0 :(得分:0)

你做错了。

int board[SIZE][SIZE];

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

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

这不是您想要做的,除非您想要创建3D战舰地图:*board将是一个2D数组,因此board将是3D。无论如何,这是静态和动态分配之间的错误混合:您不需要malloc

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

  • 您网格的每个“单元格”都应具有状态(例如,免费/占用)
  • 生成2个随机索引ij,两者都是&lt; SIZE
  • 确保cell[i][j]是免费的,然后随机生成一个方向并确保连续的单元格是免费的
  • 成功时,将单元格标记为OCCUPIED,否则重复处理(在其他方向或从其他单元格)

另外,关于代码:

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