[英]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. 我正在尝试用C语言制作一艘战舰模拟游戏。我在这里要做的是在板上获得潜水艇的起点和终点。 For that I use a function. 为此,我使用一个函数。 Now the problem is that the program crashes after getting 2 points. 现在的问题是程序在获得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;
}
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 我使用的另一个功能是获取坐标(如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;
}
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
: 这是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
. 您没有分配list
的子元素。 Remember that Submarine
is typecasted from typedef struct Submarine_s* Submarine;
请记住, Submarine
是从typedef struct Submarine_s* Submarine;
类型转换的typedef struct Submarine_s* Submarine;
. 。 You allocated the main list, but did not allocate the member before you dereferenced it here list[i]->start
您分配了主列表,但在此处取消引用之前没有分配成员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. 您应该先取消分配list [i]作为sizeof(submarine_s),然后再取消引用它。
footnote: anyway, I don't agree with using the typedef that hid the pointer without any indication that the new type is a pointer. 脚注:无论如何,我不同意使用隐藏指针的typedef而没有任何迹象表明新类型是指针。 I would prefer if you typecasted it as SubmarinePtr
instead. 如果您将其转换为SubmarinePtr
则希望使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.