Skip to content

Instantly share code, notes, and snippets.

@dzt
Created March 5, 2020 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dzt/234671338f36d260e76189567b6f39cd to your computer and use it in GitHub Desktop.
Save dzt/234671338f36d260e76189567b6f39cd to your computer and use it in GitHub Desktop.
HW2 CS2303
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
typedef struct Marker {
int x;
int y;
} Marker;
typedef struct Table {
Marker m; // Marker location
int r; // Rows
int c; // Columns
int arr[]; // Matrix
} Table;
Table *init(int rows, int columns) {
Table* t = malloc(sizeof *t + sizeof(int[rows][columns]));
t->r = rows;
t->c = columns;
int (*array_2D)[t->r] = (int(*)[t->r]) t->arr;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
array_2D[r][c] = 0;
}
}
return t;
}
bool validateCell(Table *t, Marker m) {
int (*array_2D)[t->r] = (int(*)[t->r]) t->arr;
for (int r = 0; r < t->r; r++) {
for (int c = 0; c < t->c; c++) {
if ((m.x == r) && (m.y == c)) {
return true;
}
}
}
return false;
}
void printTable(Table *t) {
int (*array_2D)[t->r] = (int(*)[t->r]) t->arr;
for (int r = 0; r < t->r; r++) {
for (int c = 0; c < t->c; c++) {
if (array_2D[r][c] == 1) {
printf("\033[1;31m");
}
printf("%d ", array_2D[r][c]);
printf("\033[0m");
}
printf("\n");
}
}
void placeMarker(Table *t, Marker m) {
int (*array_2D)[t->r] = (int(*)[t->r]) t->arr;
if (validateCell(t, m)) {
for (int r = 0; r < t->r; r++) {
for (int c = 0; c < t->c; c++) {
array_2D[r][c] = 0;
if ((r == m.x) && (c == m.y)) {
array_2D[r][c] = 1;
t->m = m;
printf("Marker Placed: [%d, %d]\n", m.x, m.y);
}
}
}
} else {
printf("Could not place marker, location is out of bounds.\n");
}
}
void moveMarker(Table *t, int steps) {
for (int i = 0; i < steps; i++) {
// Validation Checks
Marker up;
up.x = t->m.x - 1;
up.y = t->m.y;
Marker down;
down.x = t->m.x + 1;
down.y = t->m.y;
Marker left;
left.x = t->m.x;
left.y = t->m.y - 1;
Marker right;
right.x = t->m.x;
right.y = t->m.y + 1;
bool upCheck = validateCell(t, up);
bool downCheck = validateCell(t, down);
bool leftCheck = validateCell(t, left);
bool rightCheck = validateCell(t, right);
// Randomly Select a move
bool selection = false;
while (!selection) {
int random = rand() % (4 + 1 - 1) + 1;
printf("Random Number: %d\n", random);
switch(random) {
case 1:
if (upCheck) {
selection = true;
placeMarker(t, up);
}
break;
case 2:
if (downCheck) {
selection = true;
placeMarker(t, down);
}
break;
case 3:
if (leftCheck) {
selection = true;
placeMarker(t, left);
}
break;
case 4:
if (rightCheck) {
selection = true;
placeMarker(t, right);
}
break;
}
}
}
}
int main(int argc, char* argv[]) {
Table *test = init(20, 20); // Generate a 20x20 Table
Marker m1;
m1.x = 1;
m1.y = 5;
placeMarker(test, m1); // Place a marker at [0, 3]
printTable(test);
printf("Randomly Moving the Marker 5 times\n");
moveMarker(test, 5); // Move the marker in 10 cells randomly
printTable(test);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment