-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.c
132 lines (128 loc) · 3.49 KB
/
settings.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
#include "settings.h"
#include "board.h"
/* #include "error.h" COMBINED INTO COMMONLIB */
#include <stdio.h>
#include <stdlib.h>
#include "commonlib.h"
/*
Author: Jonathan Wright 19779085
Purpose: This is a C file relating to settings files.
Date: 7/10/2019, 11:45pm
*/
/*
IMPORTS: char* filename (file to open)
EXPORTS: struct board - tictactoe board.
PURPOSE: read settings from file, validate it using validators and return NULL if invalid.
DATE: 7/10/2019, 11:45pm
*/
board* readSettings(char* filename)
{
FILE* file;
int width;
int height;
int tiles;
int nRead;
board* board;
board = NULL;
file = fopen(filename, "r");
if (file == NULL)
{
printf("Error Occurred Opening file '%s'\n", filename);
}
else
{
/* Read From File */
nRead = fscanf(file, "M=%d\n", &width);
if (invalidRead(nRead) == FALSE)
{
nRead = fscanf(file, "N=%d\n", &height);
if (invalidRead(nRead) == FALSE)
{
nRead = fscanf(file, "K=%d\n", &tiles);
if (invalidRead(nRead) == FALSE)
{
board = createBoard(width,height,tiles); /* BOARD SUCCESSFULLY CREATED */
}
else
{ /* ERROR WITH TILE ROW VALUE */
errorPrint("Error Occured Reading Tile Row Count Value.");
}
}
else
{ /* ERROR WITH HEIGHT VALUE */
errorPrint("Error Occured Reading Height Value.");
}
}
else
{ /* ERROR WITH WIDTH VALUE */
errorPrint("Error Occured Reading Width Value.");
}
/* READ COMPLETE CLEANUP TIME */
if (ferror(file))
{
perror("Error occured during read, discarding board!");
free(board); /* FREE OLD BOARD. */
board = NULL; /* Discard Board. */
}
fclose(file);
}
return board; /* Will Return Null if Its invalid. */
}
/*
IMPORTS: int nRead (num items read or EOF constant)
EXPORTS: validity (is read okay?)
PURPOSE: check input is valid.
DATE: 7/10/2019, 11:47pm
*/
int invalidRead(int nRead)
{
int validity = FALSE;
if (nRead > 1 || nRead < 1 || nRead == EOF)
{
validity = TRUE;
}
return validity;
}
/*
IMPORTS: board gameBoard (Board to read settings of.)
EXPORTS: None
PURPOSE: To display the settings of the gameboard to user.
DATE: 8/10/2019 3:52am
*/
void displaySettings(board gameBoard)
{
printf("Width: %d\nHeight: %d\nX needed in row to win: %d\n", gameBoard.width,
gameBoard.height, gameBoard.tiles);
}
/*
IMPORTS: board* gameBoard (Changing M N K values of board.)
EXPORTS: None
Purpose: Editor Complimation that lets you edit board.
DATE: 13/10/2019 2:18am
*/
void editor(board* gameBoard)
{
int m;
int n;
int k;
char bufferWaster[5];
int validity = FALSE;
while (validity == FALSE)
{
printf("Enter New M,N,K Values in the format M,N,K\n");
scanf("%d,%d,%d",&m,&n,&k);
validity = boardValidity(m,n,k);
if (validity == TRUE)
{
printf("Updated Values\n");
}
else
{
/*errorPrint("Invalid choice! The options are 1 to 5."); Error printed by boardValidity */
fgets(bufferWaster,5,stdin); /* Waste Buffer */
}
}
gameBoard->width = m;
gameBoard->height = n;
gameBoard->tiles = k;
}