samedi 19 février 2022

C place n elements randomly throughout array map

I am quite new at c, so I am still learning. I am trying to make a colonization type game where there are two teams which have soldiers randomly placed on a map, each soldier fires a missile somewhere on the map randomly. I am having trouble placing the soldiers though. The issue is that instead of displaying the correct amount of soldiers in the right locations, they always end up filling a row instead. For example I would want teams(2,3,4,4) to return something like:

A 0 0 0
0 A B 0
B 0 0 0
0 0 0 B

But instead am getting:

65 65 65 65
0 0 0 0
66 66 66 66
66 66 66 66

I am unsure if I can make this show up as A and B, so 65 and 66 is still ok, but there are too many. Here is some snippets of my code (I am printing the array a lot to see if it messes up somewhere)

Code to populate soldiers:

//declare the arrays
   InArray = calloc(row * col, sizeof(char));
    OutArray;

void teams(int teamAsoldiers, int teamBsoldiers, int mapRows, int mapCols){
   srand(time(0));
   const char teams[2] = {'A', 'B'};
   int maxTeamSoldiers = teamAsoldiers + teamBsoldiers;

   //Place team soldiers
   for(int j = 0; j < 2; j++){
      for(int i = 0; i < maxTeamSoldiers; i++){
         int location = ((rand() % (mapRows * mapCols)) );
        //read the elements from the file into the array
        ReadFromBinFile(sFileName,&OutArray,&nElements);
         //binary file stuff
         if(OutArray[location] == 0){ //if current is empty
            InArray[location] = teams[j];
           //write the array into the file
            WriteToBinFile(sFileName,InArray,mapSize);
            PrintArray(InArray,row,col);
            printf("-------------------\n");
         }else{
            i--;
         }
      }
   }
}

Code for reading/writing to binary file:

//-----------------------------------------
// Write the content of an array into a bin file
//-----------------------------------------
int WriteToBinFile(char * sFileName, char * bArray, int nSize)
{
    FILE * pFile = NULL;
    if ((pFile = fopen(sFileName,"wb+"))==NULL)
    {
        printf("Error in creating the file:%s\n",sFileName);
        return -1;
    }
    else
        {
            fwrite(bArray,sizeof(char),nSize,pFile);

            fclose(pFile);

            return 0;

        }   
}

//-----------------------------------------
// Read the content of a bin file into an array
//-----------------------------------------
int ReadFromBinFile(char * sFileName, char ** bArray, int * nSize)
{
        
        
        FILE * pFile = NULL;
        if ((pFile = fopen(sFileName,"rb+"))==NULL)
        {
                printf("Error in opening the file:%s\n",sFileName);
                return -1;
        }
        else
                {
            //calculate the size of the file
      
            //position the file pointer to the end of the file
            fseek(pFile,0,SEEK_END);
            //get the size of the file in bytes
                *nSize = ftell(pFile);

            //allocate the space for the array
            *bArray = (char *)malloc(*nSize*sizeof(char));
            fseek(pFile,0,SEEK_SET);

            //second solution reading all at once (fast)
                        fread(*bArray,sizeof(char),*nSize,pFile);

                        fclose(pFile);

                        return 0;

                }
}




Aucun commentaire:

Enregistrer un commentaire