-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_aux.c
130 lines (118 loc) · 3.41 KB
/
main_aux.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
/** @file main_aux.c
* @brief main_aux source file.
*
* This module contains auxiliary functions to be used in other modules.
*
* @author Itay Keren (itaykeren)
* @author Rotem Bar (rotembar)
*
*/
/* -- Includes -- */
#include "main_aux.h"
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
/* -- Global Variables -- */
extern int blockRows;
extern int blockCols;
extern int markErrors;
extern char mode;
extern bool gameOver;
/* Simulates deletion of an element in given position in an int pointer. */
void delFromArr(int position, int numbersLeft, int* availableNumbers) {
int i = 0;
int j;
int* tmpArray = malloc(numbersLeft * sizeof(int));
if (tmpArray == NULL) {
memory_error("Memory allocation failed\n");
exit(0);
}
for ( j = 0; j < numbersLeft + 1; j++ ){
if (j != position) {
tmpArray[i] = availableNumbers[j];
i++;
}
}
for ( j = 0; j < numbersLeft; j++ ){
availableNumbers[j] = tmpArray[j];
}
free(tmpArray);
}
/* Prints error message */
void memory_error(char* func){
printf("Error: %s has failed\n", func);
}
/* returns true if s can be converted to int, else returns false */
bool is_integer(char* s){
char * t;
for (t = s; *t != '\0'; t++) {
if (*t == '.' || isalpha(*t)) {
return false;
}
}
return true;
}
/* returns an int representing corresponding starting block column index that matches given column */
int get_block_col_index(int column){
int initialCol;
int blockNumberCols = 1 + ((column - 1) / blockCols);
initialCol = blockCols * (blockNumberCols - 1);
return initialCol;
}
/* returns an int representing corresponding starting block row index that matches given row */
int get_block_row_index(int row){
int initialRow;
int blockNumberRows = 1 + ((row - 1) / blockRows);
initialRow = blockRows * (blockNumberRows - 1);
return initialRow;
}
/* returns true if index is between 1-N (including), else returns false */
bool valid_board_index(int index, int N){
if (index < 1 || index > N ) {
return false;
}
return true;
}
/* Returns true if index is between 0-N (including), else returns false */
bool valid_set_value(int val, int N){
if (val < 0 || val > N ) {
return false;
}
return true;
}
/* Returns an array representing next empty unassigned cell position */
int* get_next_play(cell** board) {
int i, j, N;
int* auxArray = (int*) calloc(2, sizeof(int));
if (auxArray == NULL) {
memory_error("Memory allocation failed\n");
exit(0);
}
auxArray[0] = -1;
auxArray[1] = -1;
N = blockRows * blockCols;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (board[i][j].number == 0) {
auxArray[0] = i;
auxArray[1] = j;
return auxArray;
}
}
}
return auxArray;
}
/* generates an int pointer to represent an int array containg values from 1-maxVal */
int* generate_int_array(int maxVal) {
int* array;
int i;
array = malloc(maxVal * sizeof(int));
if (array == NULL) {
memory_error("Memory allocation failed\n");
exit(0);
}
for (i = 1; i < maxVal + 1; i++){
array[i-1] = i;
}
return array;
}