uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

commit 63846965b28446791d315166bc6534df1a2c15cd
parent 8aa0f3347c23281e104bb1531d5787b24ef5f814
Author: Christos Margiolis <christos@margiolis.net>
Date:   Thu, 16 Jan 2020 17:57:15 +0200

fixed makefiles and pending minesweeper bug fix

Diffstat:
Massignment-1.4-functions/hanoi-tower/Makefile | 18+++++++++---------
Massignment-1.4-functions/menu/Makefile | 18+++++++++---------
Massignment-1.4-functions/sine-cos-taylor/Makefile | 18+++++++++---------
Massignment-1.5-arrays-pointers-files/combinations/Makefile | 20++++++++++----------
Aassignment-1.5-arrays-pointers-files/combinations/include/arrhandler.h | 28++++++++++++++++++++++++++++
Massignment-1.5-arrays-pointers-files/combinations/include/combinations.h | 17-----------------
Aassignment-1.5-arrays-pointers-files/combinations/src/arrhandler.c | 162+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Massignment-1.5-arrays-pointers-files/combinations/src/combinations.c | 158++-----------------------------------------------------------------------------
Massignment-1.5-arrays-pointers-files/combinations/src/main.c | 8+++++---
Aassignment-1.5-arrays-pointers-files/combinations/txt/data.txt | 46++++++++++++++++++++++++++++++++++++++++++++++
Rassignment-1.5-arrays-pointers-files/combinations/data.txt -> assignment-1.5-arrays-pointers-files/combinations/txt/output.txt | 0
Massignment-1.5-arrays-pointers-files/fcombinations/Makefile | 18+++++++++---------
Massignment-1.5-arrays-pointers-files/minesweeper/Makefile | 18+++++++++---------
Massignment-1.5-arrays-pointers-files/minesweeper/mnsout.txt | 112++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
Massignment-1.5-arrays-pointers-files/minesweeper/src/minesweeper.c | 69++++++++++++++++++++++++++++++++-------------------------------------
15 files changed, 414 insertions(+), 296 deletions(-)

diff --git a/assignment-1.4-functions/hanoi-tower/Makefile b/assignment-1.4-functions/hanoi-tower/Makefile @@ -1,8 +1,8 @@ -EXEC = ht +TARGET = ht SRC_DIR = src OBJ_DIR = obj -EXEC_DIR = bin +BIN_DIR = bin SRC = $(wildcard $(SRC_DIR)/*.c) OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) @@ -18,19 +18,19 @@ LDLIBS += -lm -lncurses .PHONY: all clean -all: $(EXEC) +all: $(TARGET) -$(EXEC): $(OBJ) - $(MKDIR_P) $(EXEC_DIR) +$(TARGET): $(OBJ) + $(MKDIR_P) $(BIN_DIR) $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ - $(MOVE) $(EXEC) $(EXEC_DIR) + $(MOVE) $(TARGET) $(BIN_DIR) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(MKDIR_P) $(OBJ_DIR) $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ run: - ./$(EXEC_DIR)/$(EXEC) + ./$(BIN_DIR)/$(TARGET) clean: - $(RM) $(OBJ) $(EXEC_DIR)/$(EXEC)- \ No newline at end of file + $(RM) $(OBJ) $(BIN_DIR)/$(TARGET)+ \ No newline at end of file diff --git a/assignment-1.4-functions/menu/Makefile b/assignment-1.4-functions/menu/Makefile @@ -1,8 +1,8 @@ -EXEC = menu +TARGET = menu SRC_DIR = src OBJ_DIR = obj -EXEC_DIR = bin +BIN_DIR = bin SRC = $(wildcard $(SRC_DIR)/*.c) OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) @@ -18,19 +18,19 @@ LDLIBS += -lm -lncurses .PHONY: all clean -all: $(EXEC) +all: $(TARGET) -$(EXEC): $(OBJ) - $(MKDIR_P) $(EXEC_DIR) +$(TARGET): $(OBJ) + $(MKDIR_P) $(BIN_DIR) $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ - $(MOVE) $(EXEC) $(EXEC_DIR) + $(MOVE) $(TARGET) $(BIN_DIR) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(MKDIR_P) $(OBJ_DIR) $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ run: - ./$(EXEC_DIR)/$(EXEC) + ./$(BIN_DIR)/$(TARGET) clean: - $(RM) $(OBJ) $(EXEC_DIR)/$(EXEC)- \ No newline at end of file + $(RM) $(OBJ) $(BIN_DIR)/$(TARGET)+ \ No newline at end of file diff --git a/assignment-1.4-functions/sine-cos-taylor/Makefile b/assignment-1.4-functions/sine-cos-taylor/Makefile @@ -1,8 +1,8 @@ -EXEC = sct +TARGET = sct SRC_DIR = src OBJ_DIR = obj -EXEC_DIR = bin +BIN_DIR = bin SRC = $(wildcard $(SRC_DIR)/*.c) OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) @@ -18,19 +18,19 @@ LDLIBS += -lm -lncurses .PHONY: all clean -all: $(EXEC) +all: $(TARGET) -$(EXEC): $(OBJ) - $(MKDIR_P) $(EXEC_DIR) +$(TARGET): $(OBJ) + $(MKDIR_P) $(BIN_DIR) $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ - $(MOVE) $(EXEC) $(EXEC_DIR) + $(MOVE) $(TARGET) $(BIN_DIR) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(MKDIR_P) $(OBJ_DIR) $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ run: - ./$(EXEC_DIR)/$(EXEC) + ./$(BIN_DIR)/$(TARGET) clean: - $(RM) $(OBJ) $(EXEC_DIR)/$(EXEC)- \ No newline at end of file + $(RM) $(OBJ) $(BIN_DIR)/$(TARGET)+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/combinations/Makefile b/assignment-1.5-arrays-pointers-files/combinations/Makefile @@ -1,8 +1,8 @@ -EXEC = combs +TARGET = combs SRC_DIR = src OBJ_DIR = obj -EXEC_DIR = bin +BIN_DIR = bin SRC = $(wildcard $(SRC_DIR)/*.c) OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) @@ -14,23 +14,23 @@ CC = gcc CPPFLAGS += -Iinclude CFLAGS += -Wall LDFLAGS += -Llib -LDLIBS += -lm -lncurses +LDLIBS += -lm .PHONY: all clean -all: $(EXEC) +all: $(TARGET) -$(EXEC): $(OBJ) - $(MKDIR_P) $(EXEC_DIR) +$(TARGET): $(OBJ) + $(MKDIR_P) $(BIN_DIR) $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ - $(MOVE) $(EXEC) $(EXEC_DIR) + $(MOVE) $(TARGET) $(BIN_DIR) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(MKDIR_P) $(OBJ_DIR) $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ run: - ./$(EXEC_DIR)/$(EXEC) + ./$(BIN_DIR)/$(TARGET) clean: - $(RM) $(OBJ) $(EXEC_DIR)/$(EXEC)- \ No newline at end of file + $(RM) $(OBJ) $(BIN_DIR)/$(TARGET)+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/combinations/include/arrhandler.h b/assignment-1.5-arrays-pointers-files/combinations/include/arrhandler.h @@ -0,0 +1,27 @@ +#ifndef ARRHANDLER_H +#define ARRHANDLER_H + +#include <stdbool.h> + +int *fill_array(int); +bool exists_in_array(int *, int, int); + +void quicksort(int *, int, int); +int partition(int *, int, int); +void swap(int *, int *); +void printarray(int *, int); + +void combinations(int *, int, int, int, int); +int even_calc(int *); +bool belongs_x(int, int, int); +int sum_calc(int *); +bool belongs_y(int, int, int); +int sum_comb_calc(); + +void print(); +int not_printed(); +int not_first_condition(); +int not_second_condition_only(); +int frequency(); + +#endif+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/combinations/include/combinations.h b/assignment-1.5-arrays-pointers-files/combinations/include/combinations.h @@ -1,31 +1,14 @@ #ifndef COMBINATIONS_H #define COMBINATIONS_H -#include <stdbool.h> - int get_n(); int get_k(); -int *fill_array(int); -bool exists_in_array(int *, int, int); -int *sort(int *); void x_pair(int *, int *); void y_pair(int *, int *); -void combinations(int *, int, int, int, int); -int even_calc(int *); -bool belongs_x(int, int, int); -int sum_calc(int *); -bool belongs_y(int, int, int); void print_combs(int *); int combinations_count(int); int factorial(int); -int sum_comb_calc(); - -void print(); -int not_printed(); -int not_first_condition(); -int not_second_condition_only(); -int frequency(); #endif \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/combinations/src/arrhandler.c b/assignment-1.5-arrays-pointers-files/combinations/src/arrhandler.c @@ -0,0 +1,161 @@ +#include <stdlib.h> +#include <stdbool.h> +#include "arrhandler.h" +#include "ccolors.h" + +#define COMBSN 6 + + +int *fill_array(int N) +{ + int *arr, num, i = 0; + + arr = (int *)malloc(N * sizeof(int)); + + if (arr == NULL) + { + set_color(BOLD_RED); + printf("Error! Not enough memory, exiting...\n"); + set_color(STANDARD); + exit(EXIT_FAILURE); + } + else + { + do + { + printf("arr[%d]: ", i); + scanf("%d", &num); + + if (num >= 1 && num <= 49) + { + if (i == 0) { *(arr + i) = num; i++; } + else + { + if (!exists_in_array(arr, N, num)) { *(arr + i) = num; i++; } + else printf("Give a different number.\n"); + } + } + else printf("Give a number in [1, 49].\n"); + } while (i < N); + } + + return arr; +} + + +bool exists_in_array(int *arr, int N, int num) +{ + int *arrEnd = arr + (N - 1); + while (arr <= arrEnd && *arr != num) arr++; + return (arr <= arrEnd) ? true : false; +} + + +void quicksort(int *arr, int low, int high) +{ + if (low < high) + { + int partIndex = partition(arr, low, high); + quicksort(arr, low, partIndex - 1); + quicksort(arr, partIndex + 1, high); + } +} + + +int partition(int *arr, int low, int high) +{ + int pivot = *(arr + high); + int i = (low - 1), j; + + for (j = low; j <= high - 1; j++) + if (*(arr + j) < pivot) + swap(arr + ++i, arr + j); + + swap(arr + (i + 1), arr + high); + return (i + 1); +} + + +void swap(int *a, int *b) +{ + int temp = *a; + *a = *b; + *b = temp; +} + + +void printarray(int *arr, int N) +{ + for (int i = 0; i < N; i++) + printf("arr[%d] = %d\n", i, *(arr + i)); +} + +int even_calc(int *arr) +{ + +} + + +bool belongs_x(int numEven, int x1, int x2) +{ + +} + + +int sum_calc(int *arr) +{ + +} + + +bool belongs_y(int sumNums, int y1, int y2) +{ + +} + + +void print_combs(int *arr) +{ + int i; + + for (i = 0; i < COMBSN; i++) + printf("%d ", *(arr + i)); + printf("\n"); +} + + +void print(int N) +{ + +} + + + +int sum_comb_calc() +{ + +} + + +int not_printed() +{ + +} + + +int not_first_condition() +{ + +} + + +int not_second_condition_only() +{ + +} + + +int frequency() +{ + +}+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/combinations/src/combinations.c b/assignment-1.5-arrays-pointers-files/combinations/src/combinations.c @@ -3,7 +3,6 @@ #include <stdbool.h> #include <string.h> #include "combinations.h" -#include "ccolors.h" #define COMBSN 6 @@ -22,77 +21,6 @@ int get_n() } -int get_k(int N) -{ - int K; - - do - { - printf("K: "); - scanf("%d", &K); - } while (K > N || K < 0); - - return K; -} - - -int *fill_array(int N) -{ - int *arr, num, i = 0; - - arr = (int *)malloc(N * sizeof(int)); - - if (arr == NULL) - { - set_color(BOLD_RED); - printf("Error! Not enough memory, exiting...\n"); - set_color(STANDARD); - exit(EXIT_FAILURE); - } - else - { - do - { - printf("arr[%d]: ", i); - scanf("%d", &num); - - if (num >= 1 && num <= 49) - { - if (i == 0) { *(arr + i) = num; i++; } - else - { - if (!exists_in_array(arr, N, num)) { *(arr + i) = num; i++; } - else printf("Give a different number.\n"); - } - } - else printf("Give a number in [1, 49].\n"); - } while (i < N); - } - - return arr; -} - - -bool exists_in_array(int *arr, int N, int num) -{ - int *arrEnd = arr + N - 1; - - while (arr <= arrEnd && *arr != num) - arr++; - - if (arr <= arrEnd) - return true; - else - return false; -} - - -int *sort(int *arr) -{ - -} - - void x_pair(int *x1, int *x2) { do @@ -121,11 +49,11 @@ void combinations(int *arr, int x1, int x2, int y1, int y2) { int i, j, k, l, m, n; - for (i=0; i < COMBSN-5; i++) + for (i = 0; i < COMBSN-5; i++) for (j = i+1; j < COMBSN-4; j++) for (k = j+1; k < COMBSN-3; k++) for (l = k+1; l < COMBSN-2; l++) - for (m= l+1; m < COMBSN-1; m++) + for (m = l+1; m < COMBSN-1; m++) for (n = m+1; n < COMBSN; n++) { printf("%d %d %d %d %d %d", *(arr + i), *(arr + j), *(arr + k), *(arr + l), *(arr + m), *(arr + n)); @@ -134,93 +62,15 @@ void combinations(int *arr, int x1, int x2, int y1, int y2) } -int even_calc(int *arr) -{ - -} - - -bool belongs_x(int numEven, int x1, int x2) -{ - -} - - -int sum_calc(int *arr) -{ - -} - - -bool belongs_y(int sumNums, int y1, int y2) -{ - -} - - -void print_combs(int *arr) -{ - int i; - - for (i = 0; i < COMBSN; i++) - printf("%d ", *(arr + i)); - printf("\n"); -} - - -void print(int N) -{ - -} - - - int combinations_count(int N) { - int numCombinations; - - numCombinations = factorial(N) / (factorial(COMBSN) * factorial(N - COMBSN)); - - return numCombinations; + return factorial(N) / (factorial(COMBSN) * factorial(N - COMBSN)); } int factorial(int num) { int i, fac; - - for (i = 1, fac = 1; i <= num; i++) - fac *= i; - + for (i = 1, fac = 1; i <= num; i++) fac *= i; return fac; -} - - -int sum_comb_calc() -{ - -} - - -int not_printed() -{ - -} - - -int not_first_condition() -{ - -} - - -int not_second_condition_only() -{ - -} - - -int frequency() -{ - } \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/combinations/src/main.c b/assignment-1.5-arrays-pointers-files/combinations/src/main.c @@ -1,6 +1,7 @@ #include <stdio.h> #include <stdlib.h> #include "combinations.h" +#include "arrhandler.h" int main(int argc, char **argv) @@ -8,16 +9,17 @@ int main(int argc, char **argv) int N, K; int *arr; int x1, x2, y1, y2; - char *fileName; N = get_n(); - K = get_k(N); arr = fill_array(N); - + quicksort(arr, 0, N-1); + //printarray(arr, N); + x_pair(&x1, &x2); y_pair(&y1, &y2); combinations(arr, x1, x2, y1, y2); + free(arr); return 0; diff --git a/assignment-1.5-arrays-pointers-files/combinations/txt/data.txt b/assignment-1.5-arrays-pointers-files/combinations/txt/data.txt @@ -0,0 +1,45 @@ +40 +20 +44 +6 +5 +7 +8 +9 +10 +13 +11 +12 +26 +24 +22 +3 +33 +32 +31 +29 +28 +27 +23 +48 +47 +46 +45 +43 +42 +41 +40 +39 +38 +37 +36 +35 +34 +2 +4 +1 +49 +2 +5 +22 +190+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/combinations/data.txt b/assignment-1.5-arrays-pointers-files/combinations/txt/output.txt diff --git a/assignment-1.5-arrays-pointers-files/fcombinations/Makefile b/assignment-1.5-arrays-pointers-files/fcombinations/Makefile @@ -1,8 +1,8 @@ -EXEC = fcombs +TARGET = fcombs SRC_DIR = src OBJ_DIR = obj -EXEC_DIR = bin +BIN_DIR = bin SRC = $(wildcard $(SRC_DIR)/*.c) OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) @@ -18,19 +18,19 @@ LDLIBS += -lm -lncurses .PHONY: all clean -all: $(EXEC) +all: $(TARGET) -$(EXEC): $(OBJ) - $(MKDIR_P) $(EXEC_DIR) +$(TARGET): $(OBJ) + $(MKDIR_P) $(BIN_DIR) $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ - $(MOVE) $(EXEC) $(EXEC_DIR) + $(MOVE) $(TARGET) $(BIN_DIR) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(MKDIR_P) $(OBJ_DIR) $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ run: - ./$(EXEC_DIR)/$(EXEC) + ./$(BIN_DIR)/$(TARGET) clean: - $(RM) $(OBJ) $(EXEC_DIR)/$(EXEC)- \ No newline at end of file + $(RM) $(OBJ) $(BIN_DIR)/$(TARGET)+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/Makefile b/assignment-1.5-arrays-pointers-files/minesweeper/Makefile @@ -1,8 +1,8 @@ -EXEC = minesweeper +TARGET = minesweeper SRC_DIR = src OBJ_DIR = obj -EXEC_DIR = bin +BIN_DIR = bin SRC = $(wildcard $(SRC_DIR)/*.c) OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) @@ -18,19 +18,19 @@ LDLIBS += -lm -lncurses .PHONY: all clean -all: $(EXEC) +all: $(TARGET) -$(EXEC): $(OBJ) - $(MKDIR_P) $(EXEC_DIR) +$(TARGET): $(OBJ) + $(MKDIR_P) $(BIN_DIR) $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ - $(MOVE) $(EXEC) $(EXEC_DIR) + $(MOVE) $(TARGET) $(BIN_DIR) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(MKDIR_P) $(OBJ_DIR) $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ run: - ./$(EXEC_DIR)/$(EXEC) + ./$(BIN_DIR)/$(TARGET) clean: - $(RM) $(OBJ) $(EXEC_DIR)/$(EXEC)- \ No newline at end of file + $(RM) $(OBJ) $(BIN_DIR)/$(TARGET)+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/mnsout.txt b/assignment-1.5-arrays-pointers-files/minesweeper/mnsout.txt @@ -2,33 +2,85 @@ Mine hit at position (1, 2) Board overview -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - * - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - * - - - - - - - - - - - - - - - - * - -- - - - - - - - - - - - * - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - * - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - * - - - - - - - - - - -- - - - - - - - - - * - - - - - - - - - - - - - - - - - - - -- - - - - - - - * - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - * - - - - - - - - - - - - - - - - -- - - - - - - * - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - * - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - * - - - - -* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - * - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - * -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * - +- - - - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - * - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - * - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - * * - - - - - - * - - - - - - - - - - - - - - - - +- - - - - - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - - - - * - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - * - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - +- - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - * - - - - - - - * - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/src/minesweeper.c b/assignment-1.5-arrays-pointers-files/minesweeper/src/minesweeper.c @@ -82,12 +82,12 @@ int set_width(WINDOW *menuWin, int xMax) do { - mvwprintw(menuWin, 1, 1, "Width: "); + mvwprintw(menuWin, 1, 1, "Width (Max = %d): ", xMax-12); wrefresh(menuWin); scanw("%d", &WIDTH); - mvwprintw(menuWin, 1, strlen("Width: ") + 1, "%d", WIDTH); + mvwprintw(menuWin, 1, strlen("Width (Max = XXX): ") + 1, "%d", WIDTH); wrefresh(menuWin); - } while (WIDTH < 5 || WIDTH > 114 /*WIDTH > xMax*/); // fix limits + } while (WIDTH < 5 || WIDTH > xMax - 12); return WIDTH; } @@ -99,12 +99,12 @@ int set_height(WINDOW *menuWin, int yMax) do { - mvwprintw(menuWin, 2, 1, "Height: "); + mvwprintw(menuWin, 2, 1, "Height (Max = %d): ", yMax-12); wrefresh(menuWin); scanw("%d", &HEIGHT); - mvwprintw(menuWin, 2, strlen("Height: ") + 1, "%d", HEIGHT); + mvwprintw(menuWin, 2, strlen("Height (Max = YYY): ") + 1, "%d", HEIGHT); wrefresh(menuWin); - } while (HEIGHT < 5 || HEIGHT > 41 /*HEIGHT > yMax*/); // fix limits + } while (HEIGHT < 5 || HEIGHT > yMax - 12); return HEIGHT; } @@ -116,12 +116,12 @@ int set_nmines(WINDOW *menuWin, int DIMENSIONS) do { - mvwprintw(menuWin, 3, 1, "Mines: "); + mvwprintw(menuWin, 3, 1, "Mines (Max = %d): ", DIMENSIONS-600); // -500 so the player has a chance to win wrefresh(menuWin); scanw("%d", &NMINES); - mvwprintw(menuWin, 3, strlen("Mines: ") + 1, "%d", NMINES); + mvwprintw(menuWin, 3, strlen("Mines (Max = MMMM): ") + 1, "%d", NMINES); wrefresh(menuWin); - } while (NMINES < 1 || NMINES > DIMENSIONS); + } while (NMINES < 1 || NMINES > DIMENSIONS-500); return NMINES; } @@ -148,7 +148,7 @@ void init_dispboard(WINDOW *gameWin, int WIDTH, int HEIGHT) int i; char **dispboard = (char **)malloc(WIDTH * sizeof(char *)); for (i = 0; i < WIDTH; i++) - *(dispboard + i) = (char *)malloc(HEIGHT); + dispboard[i] = (char *)malloc(HEIGHT); if (dispboard == NULL) { @@ -171,7 +171,7 @@ void fill_dispboard(char **dispboard, int WIDTH, int HEIGHT) for (i = 0; i < WIDTH; i++) for (j = 0; j < HEIGHT; j++) - *(*(dispboard + i) + j) = HIDDEN; + dispboard[i][j] = HIDDEN; } @@ -180,7 +180,7 @@ void init_mineboard(WINDOW *gameWin, int WIDTH, int HEIGHT, int NMINES) int i; char **mineboard = (char **)malloc(WIDTH * sizeof(char *)); for (i = 0; i < WIDTH; i++) - *(mineboard + i) = (char *)malloc(HEIGHT); + mineboard[i] = (char *)malloc(HEIGHT); if (mineboard == NULL) { @@ -193,7 +193,6 @@ void init_mineboard(WINDOW *gameWin, int WIDTH, int HEIGHT, int NMINES) place_mines(mineboard, WIDTH, HEIGHT, NMINES); add_adj(mineboard, WIDTH, HEIGHT); - print(gameWin, mineboard, WIDTH, HEIGHT); filewrite(mineboard, WIDTH, HEIGHT); } @@ -212,7 +211,7 @@ void place_mines(char **mineboard, int WIDTH, int HEIGHT, int NMINES) { wRand = rand() % WIDTH; hRand = rand() % HEIGHT; - *(*(mineboard + wRand) + hRand) = MINE; + mineboard[wRand][hRand] = MINE; } } @@ -224,25 +223,18 @@ void add_adj(char **mineboard, int WIDTH, int HEIGHT) for (i = 0; i < WIDTH; i++) for (j = 0; j < HEIGHT; j++) if (!is_mine(mineboard, i, j)) - *(*(mineboard + i) + j) = adj_mines(mineboard, i, j, WIDTH, HEIGHT) + '0'; + mineboard[i][j] = adj_mines(mineboard, i, j, WIDTH, HEIGHT) + '0'; } bool is_mine(char **mineboard, int row, int col) { - if (*(*(mineboard + row) + col) == MINE) - return true; - else - return false; + return (mineboard[row][col] == MINE) ? true : false; } - bool outof_bounds(int row, int col, int WIDTH, int HEIGHT) { - if (row < 0 || row > WIDTH || col < 0 || col > HEIGHT) - return true; - else - return false; + return (row < 0 || row > WIDTH || col < 0 || col > HEIGHT) ? true : false; } @@ -250,14 +242,14 @@ int8_t adj_mines(char **mineboard, int row, int col, int WIDTH, int HEIGHT) { int8_t numAdj = 0; - if (!outof_bounds(row, col - 1, WIDTH, HEIGHT) && *(*(mineboard + row) + (col - 1)) == MINE) numAdj++; // North - if (!outof_bounds(row, col + 1, WIDTH, HEIGHT) && *(*(mineboard + row) + (col + 1)) == MINE) numAdj++; // South - if (!outof_bounds(row + 1, col, WIDTH, HEIGHT) && *(*(mineboard + (row + 1)) + col) == MINE) numAdj++; // East - if (!outof_bounds(row - 1, col, WIDTH, HEIGHT) && *(*(mineboard + (row - 1)) + col) == MINE) numAdj++; // West - if (!outof_bounds(row + 1, col - 1, WIDTH, HEIGHT) && *(*(mineboard + (row + 1)) + (col - 1)) == MINE) numAdj++; // North-East - if (!outof_bounds(row - 1, col - 1, WIDTH, HEIGHT) && *(*(mineboard + (row - 1)) + (col - 1)) == MINE) numAdj++; // North-West - if (!outof_bounds(row + 1, col + 1, WIDTH, HEIGHT) && *(*(mineboard + (row + 1)) + (col + 1)) == MINE) numAdj++; // South-East - if (!outof_bounds(row - 1, col + 1, WIDTH, HEIGHT) && *(*(mineboard + (row - 1)) + (col + 1)) == MINE) numAdj++; // South-West + if (!outof_bounds(row, col - 1, WIDTH, HEIGHT) && mineboard[row][col-1] == MINE) numAdj++; // North + if (!outof_bounds(row, col + 1, WIDTH, HEIGHT) && mineboard[row][col+1] == MINE) numAdj++; // South + if (!outof_bounds(row + 1, col, WIDTH, HEIGHT) && mineboard[row+1][col] == MINE) numAdj++; // East + if (!outof_bounds(row - 1, col, WIDTH, HEIGHT) && mineboard[row-1][col] == MINE) numAdj++; // West + if (!outof_bounds(row + 1, col - 1, WIDTH, HEIGHT) && mineboard[row+1][col-1] == MINE) numAdj++; // North-East + if (!outof_bounds(row - 1, col - 1, WIDTH, HEIGHT) && mineboard[row-1][col-1] == MINE) numAdj++; // North-West + if (!outof_bounds(row + 1, col + 1, WIDTH, HEIGHT) && mineboard[row+1][col+1] == MINE) numAdj++; // South-East + if (!outof_bounds(row - 1, col + 1, WIDTH, HEIGHT) && mineboard[row-1][col+1] == MINE) numAdj++; // South-West return numAdj; } @@ -269,8 +261,8 @@ void fill_spaces(char **mineboard, int WIDTH, int HEIGHT, int NMINES) for (i = 0; i < WIDTH; i++) for (j = 0; j < HEIGHT; j++) - if ((*(*mineboard + i) + j) != MINE) - *(*(mineboard + i) + j) = '-'; + if (mineboard[i][j] != MINE) + mineboard[i][j] = '-'; } @@ -282,7 +274,7 @@ void print(WINDOW *gameWin, char **mineboard, int WIDTH, int HEIGHT) { for (j = 0; j < HEIGHT; j++) { - mvwaddch(gameWin, j + 1, i + 1, *(*(mineboard + i) + j)); + mvwaddch(gameWin, j + 1, i + 1, mineboard[i][j]); wrefresh(gameWin); } } @@ -302,15 +294,18 @@ void filewrite(char **mineboard, int WIDTH, int HEIGHT) else { fprintf(mnsOut, "Mine hit at position (%d, %d)\n\n", 1, 2); // add actual position - fprintf(mnsOut, "Board overview\n\n"); + for (i = 0; i < WIDTH; i++) { for (j = 0; j < HEIGHT; j++) - fprintf(mnsOut, "%c ", *(*(mineboard + i) + j)); + fprintf(mnsOut, "%c ", mineboard[i][j]); fprintf(mnsOut, "\n"); } + mvprintw(1, 1, "Session written to file"); refresh(); } + + fclose(mnsOut); } \ No newline at end of file