Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int loadGame(GameStage stage, int penguins, char *inputBoardFilePath, char *outputBoardFilePath){
- FILE *file;
- file = fopen(inputBoardFilePath, "r");
- if (file == NULL){
- printf("Could not open file: %s\n", inputBoardFilePath);
- return 2;
- }
- int m = 0, n = 0;
- /*while(fscanf(file, "%[^\n]%*c", linia) != EOF) {
- sscanf
- }*/
- fscanf(file, "%d %d", &m, &n);
- if (m <= 0 || n <= 0){
- fclose(file);
- printf("Board size must be positive (m >= 0 && n >= 0)");
- return 2;
- }
- Board *tmpBoard = createEmptyBoard(m, n);
- Player *tmpPlayers[10];
- int tmpPenguinsMs[100], tmpPenguinsNs[100];
- for (int i = 0; i < 100; ++i) {
- tmpPenguinsMs[i] = -1;
- tmpPenguinsNs[i] = -1;
- }
- for (int k = 0; k < 10; ++k) {
- tmpPlayers[k] = NULL;
- }
- //char ***rawBoard = malloc(sizeof(char) * m);
- for (int i = 0; i < m; ++i) {
- //rawBoard[i] = malloc(sizeof(char) * n);
- for (int j = 0; j < n; ++j) {
- //rawBoard[i][j] = malloc(sizeof(char) * 3);
- char fieldStr[3];
- fscanf(file, "%s", fieldStr);
- if (atoi(fieldStr) != 0){
- tmpBoard->fields[i][j]->fieldType = iceFloeFT;
- char c1[2], c2[2];
- c1[0] = fieldStr[0];
- c1[1] = '\0';
- c2[0] = fieldStr[1];
- c2[1] = '\0';
- int fishCount = atoi(c1);
- if (fishCount > 3){
- fclose(file);
- printf("Cannot place %d fish on the field (m = %d, n = %d).", fishCount, i, j);
- return 2;
- }
- tmpBoard->fields[i][j]->fishCount = fishCount;
- int playerInt = atoi(c2);
- if (playerInt > 0){
- if (tmpPlayers[playerInt - 1] == NULL) {
- tmpPlayers[playerInt - 1] = createNewPlayer("Player", playerInt, "iksde", true);
- tmpPlayers[playerInt - 1]->penguinsCount = 0;
- }
- tmpPenguinsMs[(playerInt - 1) * 10 + (tmpPlayers[(playerInt - 1)]->penguinsCount)] = j;
- tmpPenguinsNs[(playerInt - 1) * 10 + (tmpPlayers[(playerInt - 1)]->penguinsCount)] = i;
- tmpPlayers[(playerInt - 1)]->penguinsCount++;
- }
- }
- }
- }
- char line[10000];
- for(int i = 0; (fscanf(file, "%[^\n]%*c", line) != EOF); i++) {
- char playerIdStr[1000];
- fscanf(file, "%s", playerIdStr);
- int playerId;
- fscanf(file, "%d", &playerId);
- fscanf(file, "%d", &tmpPlayers[playerId - 1]->score);
- int tmpIdStrLength = 0;
- for (int i = 0; i < playerIdStr[i]; ++i) {
- tmpIdStrLength++;
- }
- tmpPlayers[playerId - 1]->idStr = malloc(sizeof(char) * (tmpIdStrLength + 1));
- //tmpPlayers[playerId - 1]->id = playerId;
- strcpy(tmpPlayers[playerId - 1]->idStr,playerIdStr);
- }
- fclose(file);
- int tmpPlayersCount = 0;
- for (int i = 0; tmpPlayers[i] != NULL; ++i) {
- tmpPlayersCount++;
- addPenguinsToPlayer(tmpPlayers[i], tmpPlayers[i]->penguinsCount);
- for (int j = 0; j < tmpPlayers[i]->penguinsCount; ++j) {
- // printf("%d", tmpBoard->fields[tmpPenguinsMs[i * 10 + j]][tmpPenguinsNs[i * 10 + j]]->posX == 1);
- tmpPlayers[i]->penguins[j]->field = tmpBoard->fields[tmpPenguinsMs[i * 10 + j]][tmpPenguinsNs[i * 10 + j]];
- }
- }
- Player **tmpPlayersFinal = malloc(sizeof(Player) * tmpPlayersCount);
- /*for (int i = 0; i < tmpPlayersCount; ++i) {
- tmpPlayersFinal[i] = &tmpPlayers[i];
- }*/
- memcpy(tmpPlayersFinal, tmpPlayers, sizeof(Player) * tmpPlayersCount);
- initGame(tmpBoard, tmpPlayersFinal, tmpPlayersCount, stage);
- draw();
- // }else {
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement