This repository has been archived by the owner on Jul 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
executable file
·160 lines (130 loc) · 3.04 KB
/
utils.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
#ifndef UTILS_C_INCLUDED
#define UTILS_C_INCLUDED
#ifdef WINDOWS
#include <windows.h>
#define SLEEP_MS(x) Sleep(x)
#else
#include <unistd.h>
#define SLEEP_MS(x) usleep((x) * 1000)
#endif
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "kbhit.c"
// AKC = Arrow Key Code
#define LEFT_AKC KEY_LEFT
#define RIGHT_AKC KEY_RIGHT
#define UP_AKC KEY_UP
#define DOWN_AKC KEY_DOWN
#define SHOULD_NOT_REACH_HERE (-1)
bool rand_bool() {
return rand() % 2;
}
void print_2d_str_arr(const int rows, const int columns, const char *arr[rows][columns]) {
int i;
for (i = 0; i < rows; i++) {
int j;
for (j = 0; j < columns; j++) {
printf("%s", arr[i][j]);
}
printf("\r\n");
}
}
void print_int_arr(const int size, const int arr[size]) {
int i;
for (i = 0; i < size; i++) {
printf("%d", arr[i]);
if (i != size - 1) printf(", ");
}
}
int replace_2d_int_arr(const int rows, const int columns, int arr[rows][columns], const int key, const int replace) {
int replaceds = 0;
int i;
for (i = 0; i < rows; i++) {
int j;
for (j = 0; j < columns; j++) {
if (arr[i][j] == key) {
arr[i][j] = replace;
replaceds++;
}
}
}
return replaceds;
}
int linear_int_arr_search(const int size, const int arr[size], const int key) {
int i;
for (i = 0; i < size; i++) {
if (arr[i] == key) return i;
}
return -1;
}
void clear_screen() {
#ifdef WINDOWS
system("cls");
#else
system("clear");
#endif
}
void purge_keys() {
while (kbhit()) getch();
}
int find_common_elements_of_int_arrs(
const int res_size, const int arr1_size, const int arr2_size, int res[res_size],
const int arr1[arr1_size], const int arr2[arr2_size]
) {
int rai = 0; // Result Append Index
bool has_space = res_size != 0;
// Set the smaller length array as base
if (arr1_size < arr2_size) {
int i;
for (i = 0; i < arr1_size; i++) {
if (linear_int_arr_search(arr2_size, arr2, arr1[i]) != -1) { // Found a common int
if (has_space) res[rai] = arr1[i];
rai++;
if (rai == res_size) has_space = false;
}
}
} else {
int i;
for (i = 0; i < arr2_size; i++) {
if (linear_int_arr_search(arr1_size, arr1, arr2[i]) != -1) { // Found a common int
if (has_space) res[rai] = arr2[i];
rai++;
if (rai == res_size) has_space = false;
}
}
}
return rai;
}
void insertion_sort_int_arr(int size, int arr[size]) {
int i;
for (i = 1; i < size; i++) {
int j;
for (j = i; arr[j] < arr[j - 1] && j > 0; j--) {
const int t = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = t;
}
}
}
bool shift_left_int_arr(const int size, int arr[size], const int remove_index) {
if (remove_index < 0 || remove_index >= size) return false;
else if (remove_index == size - 1) return true;
bool executed = false;
int i;
for (i = remove_index + 1; i < size; i++) {
const int t = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = t;
if (!executed) executed = true;
}
return executed;
}
void sleep_ms(const long ms) {
SLEEP_MS(ms);
}
void bug_alert(const char *msg) {
printf("Bug: %s", msg);
sleep_ms(1000);
}
#endif // UTILS_C_INCLUDED