-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs.c
167 lines (134 loc) · 3.58 KB
/
logs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* Author: Brett Stevens
File name: logs.c
Purpose: handles logs, saving/printing
Last modified: 23/10/19
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "logs.h"
/* Function: addLog
Purpose: to add a new log struct to the linkedlist; is called in UI.c
*/
void addLog(char player, char** board, int moveCount, int i, int j, LinkedList* list)
{
log* newLog;
newLog = (log*)malloc(sizeof(log));
newLog->turn = moveCount;
newLog->player = player;
newLog->locX = j;
newLog->locY = i;
insertLast(list, newLog); /*inserts struct newLog into linkedlist*/
return;
}
/* Function: displaySettings
Purpose: to display current settings
*/
void displaySettings(int *m, int *n, int *k)
{
printf("\nSettings:\n\tM: %d\n\tN: %d\n\tK: %d\n", *m, *n, *k);
return;
}
/* Function: viewLogs
Purpose: to iterate through the linked list and display settings and logs of each game to screen; is called from UI.c
*/
void viewLogs(int *m, int *n, int *k, LinkedList* list)
{
int gameCount = 1;
log* currLog;
int turn;
char player;
int x;
int y;
LinkedListNode *currNode;
currNode = list->head;
displaySettings(m, n, k);
while(currNode != NULL)
{
currLog = (log*)(currNode->data);
turn = currLog->turn;
player = currLog->player;
x = currLog->locX;
y = currLog->locY;
if((turn == 1) && (gameCount > 1))
{
printf("\n\t...\n\nGAME %d:", gameCount);
gameCount++;
}
else if(turn == 1)
{
printf("\nGAME %d:", gameCount);
gameCount++;
}
printf("\n\tTurn: %d\n\tPlayer: %c\n\tLocation: %d, %d\n", turn, player, x, y);
currNode = currNode->next;
}
return;
}
/* Function: saveLogs
Purpose: to iterate through the linked list and save settings and logs of each game to file; is called from UI.c
*/
void saveLogs(int *m, int *n, int *k, LinkedList* list)
{
FILE *outFile;
char* filename = (char*)malloc(26 * sizeof(char));
int hours, mins, month, day;
int gameCount = 1;
LinkedListNode* currNode;
getTime(&hours, &mins, &month, &day);
sprintf(filename, "MNK_%d-%d-%d_%d-%d_%d-%d.log", *m, *n, *k, hours, mins, day, month);
outFile = fopen(filename, "w");
fprintf(outFile, "Settings:\n\tM: %d\n\tN: %d\n\tK: %d\n", *m, *n, *k);
currNode = list->head;
while(currNode != NULL)
{
fprintLog(currNode->data, outFile, &gameCount);
currNode = currNode->next;
}
printf("\nSaved to %s....\n", filename);
fclose(outFile);
free(filename);
return;
}
/* Function: getTime
Purpose: function used to get current date and time for output file
*/
void getTime(int *hours, int *mins, int *month, int *day)
{
time_t now;
struct tm *local;
time(&now);
local = localtime(&now);
*hours = local->tm_hour;
*mins = local->tm_min;
*month = local->tm_mon + 1;
*day = local->tm_mday;
return;
}
/* Function: fprintLog
Purpose: to iterate through the linked list and print settings and logs of each game to file
*/
void fprintLog(void* data, FILE* filename, int* gameCount)
{
log* currLog = (log*)data;
int turn;
char player;
int x;
int y;
turn = currLog->turn;
player = currLog->player;
x = currLog->locX;
y = currLog->locY;
if((turn == 1) && (*gameCount > 1))
{
fprintf(filename, "\n\t...\n\nGAME %d:", *gameCount);
(*gameCount)++;
}
else if(turn == 1)
{
fprintf(filename, "\nGAME %d:", *gameCount);
(*gameCount)++;
}
fprintf(filename, "\n\tTurn: %d\n\tPlayer: %c\n\tLocation: %d, %d\n", turn, player, x, y);
return;
}