【问题标题】:C program to simulate a submarine game模拟潜艇游戏的C程序
【发布时间】:2017-10-28 07:07:54
【问题描述】:

我正在尝试用 C 语言制作战舰模拟游戏。我在这里尝试做的是在板上获取潜艇的起点和终点。为此,我使用了一个函数。现在的问题是程序在获得2分后崩溃。

int getsub(char* filename, Submarine* list)
{
    int subcount=0;
    int i=0;
    char* token;
    char line[100];
    FILE *fptr=fopen(filename,"r");
    if(fptr==NULL)
    {
        printf("no file found");
        exit(-1);
    }
    while(fgets(line,100,fptr))
    {
        if(isalpha(line[0])|| isalnum(line[0]))
        {
            ++subcount;
        }
    }

    list=malloc(sizeof(Submarine)*subcount);
    rewind(fptr);
    while(fgets(line,100,fptr)){
    if(isalpha(line[0]))
    {
        list[i]->start=malloc(sizeof(Point));
        token=strtok(line,"-");
        list[i]->start=TranslateCoordinate(token);
        list[i]->end=malloc(sizeof(Point));
        token=strtok(NULL,"-");
        list[i]->end=TranslateCoordinate(token);
        i++;
    }
    }
    return subcount;
}

这是我的源文件。我使用的另一个函数是获取坐标(如 A9)并将其转换为 0 到 9 之间的数字

Point TranslateCoordinate(char* c)
{
    Point newpoint;
    newpoint=malloc(sizeof(Point));
    int row=c[0];
    int column;
    newpoint->x=row-'A';
    if(c[1]== 1 && c[2]==0)
    {
        column=9;
        newpoint->y=column;
        return newpoint;
    }
    column=c[1];
    newpoint->y=column-'1';;
    return newpoint;
}

这些是我的结构:

struct Point_s
{
    int x;
    int y;
};

struct Submarine_s
{
    Point start;
    Point end;
    int Life;
};

struct SubmarinePart_s
{
    Submarine* Sub;
    int IsHit; // 0 = not hit, <1 = exploded
};

这是我的主程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "header.h"
#define BoardSize 10

int main(int argc, char *argv[])
{
    if (argc < 3){
        printf("Error not enough files");
        exit(-1);
    }
    char* P1setup=argv[2];
    char* P2setup=argv[3];
    Submarine* P1list;
    Submarine* P2list;
    getsub(P1setup,P1list);

    char Board1[BoardSize][BoardSize];
    char Board2[BoardSize][BoardSize];
    int x,y;
    for(x=0;x<BoardSize;x++){
        for(y=0;y<BoardSize;y++){
            Board1[x][y]='0';
        }
    }
    for(x=0;x<BoardSize;x++){
        for(y=0;y<BoardSize;y++){
            Board2[x][y]='0';
        }
    }
    return 1;
}

这是header.h

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <stdio.h>
#include <string.h>
typedef struct Point_s* Point;
typedef struct Submarine_s* Submarine;
typedef struct SubmarinePart_s* SubmarinePart;
Point TranslateCoordinate(char* c);
int getsub(char* filename,Submarine* list);
void printpoint(Point p);
void printsub(Submarine s);

#endif // HEADER_H_INCLUDED

【问题讨论】:

  • 看看int getsub(char* filename,Submarine* list),你在别处有typedef struct Submarine_s* Submarine;,要知道你打算有多少间接级别并不容易。请不要typedef指针。
  • 确实令人困惑 ;)
  • 请拿出调试器。
  • 当我尝试调试时,我得到的只是 malloc 行中的错误list[i]-&gt;start=malloc(sizeof(Point)); 女巫我真的不明白它可能有什么问题
  • ...编译后启用所有警告。

标签: c module modularity


【解决方案1】:

您没有分配list 的子元素。请记住,Submarine 是从 typedef struct Submarine_s* Submarine; 类型转换而来的。您分配了主列表,但在此处取消引用之前没有分配成员list[i]-&gt;start

int getsub(char* filename, Submarine* list)
...

list=malloc(sizeof(Submarine)*subcount);
rewind(fptr);
while(fgets(line,100,fptr)){
if(isalpha(line[0]))
{
    list[i]->start=malloc(sizeof(Point));

在取消引用之前,您应该首先将 list[i] 分配为 sizeof(submarine_s)。

脚注:无论如何,我不同意在没有任何迹象表明新类型是指针的情况下使用隐藏指针的 typedef。如果你把它改成SubmarinePtr,我会更喜欢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-20
    • 2014-11-14
    • 2015-09-28
    • 2019-05-09
    • 2010-11-28
    • 2018-01-18
    • 1970-01-01
    • 2013-11-30
    相关资源
    最近更新 更多