SHARE
TWEET

Untitled

a guest May 25th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int loadGame(GameStage stage, int penguins, char *inputBoardFilePath, char *outputBoardFilePath){
  2.  
  3.     FILE *file;
  4.     file = fopen(inputBoardFilePath, "r");
  5.  
  6.     if (file == NULL){
  7.         printf("Could not open file: %s\n", inputBoardFilePath);
  8.         return 2;
  9.     }
  10.  
  11.     int m = 0, n = 0;
  12.     /*while(fscanf(file, "%[^\n]%*c", linia) != EOF) {
  13.         sscanf
  14.     }*/
  15.  
  16.     fscanf(file, "%d %d", &m, &n);
  17.  
  18.     if (m <= 0 || n <= 0){
  19.         fclose(file);
  20.         printf("Board size must be positive (m >= 0 && n >= 0)");
  21.         return 2;
  22.     }
  23.  
  24.     Board *tmpBoard = createEmptyBoard(m, n);
  25.     Player *tmpPlayers[10];
  26.     int tmpPenguinsMs[100], tmpPenguinsNs[100];
  27.  
  28.     for (int i = 0; i < 100; ++i) {
  29.         tmpPenguinsMs[i] = -1;
  30.         tmpPenguinsNs[i] = -1;
  31.     }
  32.  
  33.     for (int k = 0; k < 10; ++k) {
  34.         tmpPlayers[k] = NULL;
  35.     }
  36.     //char ***rawBoard = malloc(sizeof(char) * m);
  37.  
  38.     for (int i = 0; i < m; ++i) {
  39.         //rawBoard[i] = malloc(sizeof(char) * n);
  40.         for (int j = 0; j < n; ++j) {
  41.             //rawBoard[i][j] = malloc(sizeof(char) * 3);
  42.             char fieldStr[3];
  43.             fscanf(file, "%s", fieldStr);
  44.  
  45.             if (atoi(fieldStr) != 0){
  46.                 tmpBoard->fields[i][j]->fieldType = iceFloeFT;
  47.                 char c1[2], c2[2];
  48.                 c1[0] = fieldStr[0];
  49.                 c1[1] = '\0';
  50.  
  51.                 c2[0] = fieldStr[1];
  52.                 c2[1] = '\0';
  53.  
  54.                 int fishCount = atoi(c1);
  55.                 if (fishCount > 3){
  56.                     fclose(file);
  57.                     printf("Cannot place %d fish on the field (m = %d, n = %d).",  fishCount, i, j);
  58.                     return 2;
  59.                 }
  60.                 tmpBoard->fields[i][j]->fishCount = fishCount;
  61.  
  62.                 int playerInt = atoi(c2);
  63.  
  64.                 if (playerInt > 0){
  65.                     if (tmpPlayers[playerInt - 1] == NULL) {
  66.                         tmpPlayers[playerInt - 1] = createNewPlayer("Player", playerInt, "iksde", true);
  67.                         tmpPlayers[playerInt - 1]->penguinsCount = 0;
  68.                     }
  69.                     tmpPenguinsMs[(playerInt - 1) * 10 + (tmpPlayers[(playerInt - 1)]->penguinsCount)] = j;
  70.                     tmpPenguinsNs[(playerInt - 1) * 10 + (tmpPlayers[(playerInt - 1)]->penguinsCount)] = i;
  71.                     tmpPlayers[(playerInt - 1)]->penguinsCount++;
  72.                 }
  73.             }
  74.         }
  75.     }
  76.  
  77.  
  78.     char line[10000];
  79.     for(int i = 0; (fscanf(file, "%[^\n]%*c", line) != EOF); i++) {
  80.         char playerIdStr[1000];
  81.         fscanf(file, "%s", playerIdStr);
  82.         int playerId;
  83.         fscanf(file, "%d", &playerId);
  84.         fscanf(file, "%d", &tmpPlayers[playerId - 1]->score);
  85.  
  86.         int tmpIdStrLength = 0;
  87.         for (int i = 0; i < playerIdStr[i]; ++i) {
  88.             tmpIdStrLength++;
  89.         }
  90.         tmpPlayers[playerId - 1]->idStr = malloc(sizeof(char) * (tmpIdStrLength + 1));
  91.         //tmpPlayers[playerId - 1]->id = playerId;
  92.         strcpy(tmpPlayers[playerId - 1]->idStr,playerIdStr);
  93.     }
  94.  
  95.     fclose(file);
  96.  
  97.     int tmpPlayersCount = 0;
  98.     for (int i = 0; tmpPlayers[i] != NULL; ++i) {
  99.         tmpPlayersCount++;
  100.         addPenguinsToPlayer(tmpPlayers[i], tmpPlayers[i]->penguinsCount);
  101.         for (int j = 0; j < tmpPlayers[i]->penguinsCount; ++j) {
  102.            // printf("%d", tmpBoard->fields[tmpPenguinsMs[i * 10 + j]][tmpPenguinsNs[i * 10 + j]]->posX == 1);
  103.             tmpPlayers[i]->penguins[j]->field = tmpBoard->fields[tmpPenguinsMs[i * 10 + j]][tmpPenguinsNs[i * 10 + j]];
  104.         }
  105.     }
  106.  
  107.     Player **tmpPlayersFinal = malloc(sizeof(Player) * tmpPlayersCount);
  108.     /*for (int i = 0; i < tmpPlayersCount; ++i) {
  109.         tmpPlayersFinal[i] = &tmpPlayers[i];
  110.     }*/
  111.     memcpy(tmpPlayersFinal, tmpPlayers, sizeof(Player) * tmpPlayersCount);
  112.  
  113.     initGame(tmpBoard, tmpPlayersFinal, tmpPlayersCount, stage);
  114.     draw();
  115.  
  116.    // }else {
  117.  
  118.    // }
  119. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top