简体   繁体   中英

C program to simulate a submarine game

I am trying to make a battleship simulation game in C. What I am trying to do here is to get the starting and end point on the submarine on the board. For that I use a function. Now the problem is that the program crashes after getting 2 points.

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;
}

That is on my source file. The other function I use is to get a coordinate (like A9) and turn it into a number between 0 and 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;
}

These are my structs:

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
};

and this is my main program:

#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;
}

and this is 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

You are not allocating the subelements of list . Remember that Submarine is typecasted from typedef struct Submarine_s* Submarine; . You allocated the main list, but did not allocate the member before you dereferenced it here list[i]->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));

you should have allocated list[i] first as sizeof(submarine_s) before you dereferenced it.

footnote: anyway, I don't agree with using the typedef that hid the pointer without any indication that the new type is a pointer. I would prefer if you typecasted it as SubmarinePtr instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM