簡體   English   中英

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

[英]C Battleship program malloc memory allocation and placing ships

我在戰艦計划中無法為網格分配內存。 雖然我不必創建整個游戲(只是設置),但我對malloc並不十分熟悉,因此我很難在代碼中實現它。 基本上,我不知道該怎么做。 有什么建議么?

另一個問題是我需要一個函數來隨機生成兩個零件的位置,即一艘航母(長5個單位)和一艘戰艦(長4個單位),並且不要讓它們重疊。 我不確定如何調用數組或顯示片段。

輸出應如下所示:

2艘戰艦網格

到目前為止,這是我的代碼:

/*
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戰艦地圖: *board將是2D數組,因此board將是3D。 無論如何,這是靜態分配與動態分配之間的不良結合:您不需要malloc

關於生成隨機位置,這是一個建議:

  • 網格的每個“單元”都應有一個狀態(例如,“免費” /“已占用”)
  • 生成2個隨機索引ij ,均< SIZE
  • 確保cell[i][j]空閑,然后隨機生成方向並確保連續的cell空閑
  • 成功后,將單元格標記為“已占用”,否則重復此過程(從另一個方向或從另一個單元格進行)

另外,關於代碼:

  • void dispBoard(int board[][SIZE])應該為void dispBoard(int board[SIZE][SIZE])以保持一致性
  • line被聲明為int但您將其用作char

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM