Actually, there's a trick in here, but I thought I might tackle it later.. since I'm going to provide what my code would look like, I'll just put it along =)
The trick is, the typdef struct is having unsigned short int for "alive and fired"... but my .STATE file ( the file I read to feed my Statefile struct ) is having "alive and fired" as "true" and "false". Why am I using it this way? it's just a requirement ~.~.
my header:
Code:
typedef struct {
unsigned short int xMap;
unsigned short int yMap;
unsigned short int xPosition1; /*X position of player1 tank*/
unsigned short int yPosition1; /*Y position of player1 tank*/
unsigned short int alive1; /*1 if my tank is still alive, 0 if I've
been destroyed*/
unsigned short int fired1; /*1 if my tank has fired, 0 otherwise*/
unsigned short int xPosition2; /*X position of player2 tank*/
unsigned short int yPosition2; /*Y position of player2 tank*/
unsigned short int alive2; /*1 if their tank is still alive, 0 if I've
been destroyed*/
unsigned short int fired2; /*1 if their tank has fired, 0 otherwise*/
unsigned short int Ammo;
unsigned short int Fuel;
char* Move;
unsigned short int shellVelocity;
} tankState;
//Defining the members of struct for the State file
struct Statefile{
char* tank1Fired;
char* tank2Fired;
char* tank1Alive;
char* tank2Alive;
unsigned short int mapDimensionsX;
unsigned short int mapDimensionsY;
unsigned short int tank1PositionX;
unsigned short int tank1PositionY;
unsigned short int tank2PositionX;
unsigned short int tank2PositionY;
unsigned short int shellVelocity;
unsigned short int initialAmmo;
unsigned short int initialFuel;
}State;
struct Statefile *s =&State; // assigning a pointer to the struct
//Defining the members of struct for the Turn file
struct Turnfile{
char* firing;
unsigned short int targetX;
unsigned short int targetY;
char* movement;
} Turn;
struct Turnfile *p = &Turn; // assigning a pointer to the struct
char* extractFileName(const char filePath[])
{
char fileName[200];
size_t fileNameLength;
int i, j;
for (i = strlen(filePath); filePath[i] != '\\'; i--)
if (i == 0) return 0;
i++;
fileNameLength = strlen(filePath);
for (j=0 ; filePath[i] != '.' && j < fileNameLength; j++, i++)
fileName[j] = filePath[i];
fileName[j] = '\0';
return fileName;
//note, this function FAILS if a SPACE was present in the filePath
}
tankState get_State( unsigned short int xMap, unsigned short int yMap, unsigned short int shellVelocity, unsigned short int Ammo,
unsigned short int Fuel, char* Move,unsigned short int xPosition1, unsigned short int yPosition1,
unsigned short int xPosition2, unsigned short int yPosition2, unsigned short int fired1,
unsigned short int fired2, unsigned short int alive1, unsigned short int alive2)
{
tankState t;
t.xPosition1 = xPosition1 ;
t.yPosition1 = yPosition1;
t.xPosition2 = xPosition2 ;
t.yPosition2 = yPosition2;
t.alive1 = alive1;
t.fired1 = fired1;
t.alive2 = alive2;
t.fired2 = fired2;
t.Ammo = Ammo;
t.Fuel = Fuel;
t.xMap = xMap;
t.yMap = yMap;
t.Move = Move;
return t;
}
tankState tank_move(tankState xpos1, tankState ypos1,tankState xpos2,tankState ypos2,tankState alive1,
tankState alive2,tankState fired1, tankState fired2,tankState Fuel,tankState xMap,
tankState yMap,tankState Move)
{
tankState t;
t.xPosition1 = xpos1.xPosition1 ;
t.yPosition1 = ypos2.yPosition1;
t.xPosition2 = xpos2.xPosition2 ;
t.yPosition2 = ypos2.yPosition2;
t.alive1 = alive1.alive1;
t.fired1 = fired1.fired1;
t.alive2 = alive2.alive2;
t.fired2 = fired2.fired2;
t.Fuel = Fuel.Fuel;
t.xMap = xMap.xMap;
t.yMap = yMap.yMap;
t.Move = Move.Move;
if(Fuel.Fuel >0 && alive1.alive1==1 && alive2.alive2==1 && fired1.fired1==0 && fired2.fired2==1)
{
for (; xpos1.xPosition1 < xMap.xMap && ypos1.yPosition1 < yMap.yMap ;){
if ( xpos1.xPosition1 - xpos2.xPosition2 <-11 )
{ xpos1.xPosition1 = xpos1.xPosition1 +1;
Move.Move = "right";}
else if ( xpos1.xPosition1 - xpos2.xPosition2 >11 )
{ xpos1.xPosition1 = xpos1.xPosition1 -1;
Move.Move = "left";}
else if ( -11 < (xpos1.xPosition1 - xpos2.xPosition2) <11 && xpos1.xPosition1 > xpos2.xPosition2)
{xpos1.xPosition1 = xpos1.xPosition1 +1;
Move.Move = "right";}
else if ( -11 < (xpos1.xPosition1 - xpos2.xPosition2) <11 && xpos1.xPosition1 < xpos2.xPosition2)
{ xpos1.xPosition1 = xpos1.xPosition1 -1;
Move.Move = "left";}
else if ( ypos1.yPosition1 - ypos2.yPosition2 <-16 )
{ypos1.yPosition1 = ypos1.yPosition1 +1;
Move.Move ="up";}
else if ( ypos1.yPosition1 - ypos2.yPosition2 >16 )
{ ypos1.yPosition1 = ypos1.yPosition1 -1;
Move.Move = "down";}
else if (-16< (ypos1.yPosition1 - ypos2.yPosition2) < 16 && ypos1.yPosition1 > ypos2.yPosition2)
{ypos1.yPosition1 = ypos1.yPosition1 +1;
Move.Move = "up";}
else if(-16< (ypos1.yPosition1 - ypos2.yPosition2) < 16 && ypos1.yPosition1 < ypos2.yPosition2)
{ypos1.yPosition1 = ypos1.yPosition1 -1;
Move.Move = "down";}
Fuel.Fuel = Fuel.Fuel -1;
break;
}
}
return t;
}
my source:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cstring>
#include <windows.h>
#include <math.h>
void main(int argc, char* argv[])
{
int i;
for (i=0 ; i<argc; i++)
{
//printf("player %d \nName of file: %s\n", i, argv[i]); // printing my player number
}
extractFileName(argv[0]); // extracting the name of file
//Creating the Read/Write Files.
FILE *outfile, *infile;
char buffer1 [30]; // for STATE file
char buffer2 [30]; // for TEMP file
char buffer3 [30]; // for TURN file
int t=1;
//Using the format given to name the state file
sprintf(buffer1, "p%dt%03d.state", i, t);
infile = fopen (buffer1,"r");
fscanf(infile, "%*s %d %d\n", &, &tankState.yMap.yMap);
fscanf(infile, "%*s %d\n", &s->shellVelocity);
fscanf(infile, "%*s %d\n", &s->initialAmmo);
fscanf(infile, "%*s %d\n", &s->initialFuel);
fscanf(infile, "%*s %d %d\n", &s->tank1PositionX, &s->tank1PositionY);
fscanf(infile, "%*s %d %d\n", &s->tank2PositionX, &s->tank2PositionY);
fscanf(infile, "%*s %s\n", &s->tank1Fired);
fscanf(infile, "%*s %s\n", &s->tank2Fired);
fscanf(infile, "%*s %s\n", &s->tank1Alive);
fscanf(infile, "%*s %s\n", &s->tank2Alive);
sprintf(buffer2, "p%dt%03d.tmp", i, t);
outfile = fopen (buffer2,"w");
fprintf(outfile, "firing \t%s\n", p->firing);
fprintf(outfile, "target \t%d %d\n", p->targetX, p->targetY);
fprintf(outfile, "movment \t%s\n", p->movement);
fclose(outfile);
rename ("p%dt%03d.tmp", "p%dt%03d.turn"); // renaming the file to .turn
sprintf(buffer3, "p%dt%03d.turn", i, t);
get_State(s->mapDimensionsX, s->mapDimensionsY,s->shellVelocity, s->initialAmmo,
s->initialFuel, p->movement,s->tank1PositionX, s->tank1PositionY, s->tank2PositionX, s->tank2PositionY,
(unsigned short)s->tank1Fired, (unsigned short)s->tank2Fired, (unsigned short)s->tank1Alive, (unsigned short)s->tank2Alive);
tank_move();
}