From 7380d1a0a708f869c7926ae323123b3e49feadcd Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 23 Feb 2021 09:07:12 +0100 Subject: moved voerbak to own subfolder with makefile --- .gitignore | 3 +- api/game/voerbak | Bin 16624 -> 0 bytes api/game/voerbak.c | 109 ----------------------------------------- api/game/voerbak_connector.py | 2 +- console/input | 14 ------ console/main.py | 90 ---------------------------------- console/test.py | 24 --------- console/v2.py | 105 ---------------------------------------- voerbak/input | 14 ++++++ voerbak/main.py | 90 ++++++++++++++++++++++++++++++++++ voerbak/makefile | 21 ++++++++ voerbak/test.py | 24 +++++++++ voerbak/v2.py | 105 ++++++++++++++++++++++++++++++++++++++++ voerbak/voerbak.c | 110 ++++++++++++++++++++++++++++++++++++++++++ 14 files changed, 367 insertions(+), 344 deletions(-) delete mode 100755 api/game/voerbak delete mode 100644 api/game/voerbak.c delete mode 100644 console/input delete mode 100644 console/main.py delete mode 100644 console/test.py delete mode 100644 console/v2.py create mode 100644 voerbak/input create mode 100644 voerbak/main.py create mode 100644 voerbak/makefile create mode 100644 voerbak/test.py create mode 100644 voerbak/v2.py create mode 100644 voerbak/voerbak.c diff --git a/.gitignore b/.gitignore index 67119f3..673452f 100644 --- a/.gitignore +++ b/.gitignore @@ -31,5 +31,6 @@ node_modules/ .next/ # voerbak -console/voerbak +voerbak/voerbak +voerbak/*.o diff --git a/api/game/voerbak b/api/game/voerbak deleted file mode 100755 index 595a610..0000000 Binary files a/api/game/voerbak and /dev/null differ diff --git a/api/game/voerbak.c b/api/game/voerbak.c deleted file mode 100644 index 0161726..0000000 --- a/api/game/voerbak.c +++ /dev/null @@ -1,109 +0,0 @@ -#include -#include -#include -#include - -void printBoard(int board[], int width, int height) { - for (int i = 0; i < width * height; i++) - printf("%d", board[i]); - printf("\n"); - fflush(stdout); -} - -int recursiveSolve(int board[], int width, int height, int pos, int checkFor, int direction, int d_index, int currentLength) { - int newPos = pos + direction; - if (newPos > width * height - 1 || newPos < 0) return currentLength; - int row = pos % width; - if (row == width && d_index >= 1 && d_index <= 3) return currentLength; - if (row == 0 && d_index >= 5 && d_index <= 7) return currentLength; - if (board[newPos] != checkFor) return currentLength; - return recursiveSolve(board, width, height, newPos, checkFor, direction, d_index, currentLength + 1); -} - -bool checkWin(int board[], int width, int height, int pos) { - int directions[8] = { - width, // north - width + 1, // northeast - 1, // east - -width + 1, // southeast - -width, // south - -width -1, // southwest - -1, // west - width -1 // northwest - }; - - int values[8]; - for (int i = 0; i < 8; i++) - values[i] = recursiveSolve(board, width, height, pos, board[pos], directions[i], i, 0); - - int joinedValues[4] = { - values[0] + values[4], - values[1] + values[5], - values[2] + values[6], - values[3] + values[7] - }; - - bool won = false; - for (int i = 0; i < 4; i++) { - if (joinedValues[i] >= 3) { - won = won || true; - - int start_pos = pos + directions[i+0] * values[i+0]; - int end_pos = pos + directions[i+4] * values[i+4]; - printf("w:%d-%d\n", start_pos, end_pos); - fflush(stdout); - } - } - - return won; -} - -bool boardFull(int board[], int width, int height) { - for (int i = 0; i < width * height; i++) - if (board[i] == 0) return false; - return true; -} - -bool dropFisje(int board[], int width, int height, int column, int disc) { - for (int row = 0; row < height; row++) { - int pos = column + row * width; - if (board[pos] == 0) { - board[pos] = disc; - bool won = checkWin(board, width, height, pos); - return true; // success - } - } - printf("e:full\n"); - fflush(stdout); - return false; // unsuccessful drop on board full -} - -int main() { - int width, height; - scanf("%d %d", &width, &height); - - int board[width * height]; - memset(board, 0, sizeof board); - - bool player_1 = true; - int move = 0; - while (scanf("%d", &move) == 1) { - if (move == 0) break; - if (move < 1 || move > width) continue; - - bool dropSuccess = dropFisje(board, width, height, move - 1, player_1 + 1); - - player_1 = player_1 ^ dropSuccess; // only flip turns on successful drop - printf("m:%s\n", player_1 ? "true" : "false"); - fflush(stdout); - - if (boardFull(board, width, height)) { - printf("d:full\n"); - fflush(stdout); - } - - printBoard(board, width, height); - } - - return 0; -} diff --git a/api/game/voerbak_connector.py b/api/game/voerbak_connector.py index 6c08553..f16a016 100644 --- a/api/game/voerbak_connector.py +++ b/api/game/voerbak_connector.py @@ -8,7 +8,7 @@ DISC_A = Fore.RED + DISC_SHAPE + Fore.RESET DISC_B = Fore.BLUE + DISC_SHAPE + Fore.RESET EMPTY = Fore.LIGHTBLACK_EX + "_" + Fore.RESET -VOERBAK_LOCATION = os.path.dirname(__file__) + "/voerbak" +VOERBAK_LOCATION = os.path.join(os.getcwd(), "voerbak/", "voerbak") if os.name == "nt": VOERBAK_LOCATION += ".exe" class bord: diff --git a/console/input b/console/input deleted file mode 100644 index 1243fe0..0000000 --- a/console/input +++ /dev/null @@ -1,14 +0,0 @@ -7 6 -4 -3 -3 -2 -1 -2 -2 -7 -1 -7 -1 -7 -1 diff --git a/console/main.py b/console/main.py deleted file mode 100644 index 433f130..0000000 --- a/console/main.py +++ /dev/null @@ -1,90 +0,0 @@ -from colorama import Fore -import os - -DISC_SHAPE = "o" -DISC_A = Fore.RED + DISC_SHAPE + Fore.RESET -DISC_B = Fore.BLUE + DISC_SHAPE + Fore.RESET -EMPTY = Fore.LIGHTBLACK_EX + "_" + Fore.RESET - -class bord: - def __init__(self, w, h): - self.width = w - self.height = h - self.board = [[EMPTY for x in range(self.width)] for u in range(self.height)] - - def print(self): - print("\n".join([" ".join(self.board[y]) for y in range(len(self.board) -1, -1, -1)])) - - def outside_board(self, coords): - return coords[0] < 0 or \ - coords[1] < 0 or \ - coords[0] > self.height - 1 or \ - coords[1] > self.width - 1 - - def recursive_solve(self, coords, check_for, direction, current_length): - new_position = ( - coords[0] + direction[0], - coords[1] + direction[1] - ) - if self.outside_board(new_position) or self.board[new_position[0]][new_position[1]] != check_for: - return current_length - else: - return self.recursive_solve(new_position, check_for, direction, current_length + 1) - - def check_win(self, coords): - directions = [ - ( 1, 0), - ( 1, 1), - ( 0, 1), - (-1, 1), - (-1, 0), - (-1, -1), - ( 0, -1), - ( 1, -1) - ] - values = list() - for direction in directions: - values.append(self.recursive_solve(coords, self.board[coords[0]][coords[1]], direction, 0)) - joined_directions = [ - values[0] + values[4], - values[1] + values[5], - values[2] + values[6], - values[3] + values[7] - ] - won = any(i >= 3 for i in joined_directions) - if won: - for i, value in enumerate(joined_directions): - if value >= 3: - start_pos = ( - coords[0] + directions[i][0] * values[i], - coords[1] + directions[i][1] * values[i], - ) - end_pos = ( - coords[0] + directions[i+4][0] * values[i+4], - coords[1] + directions[i+4][1] * values[i+4], - ) - print(start_pos, end_pos) - - return won - - def drop_fisje(self, column, disc): - for row, value in enumerate(self.board): - if self.board[row][column] == EMPTY: - self.board[row][column] = disc - won = self.check_win((row, column)) - print(won) - return - -def main(): - disc_a = True - gert = bord(11, 8) - while True: - gert.print() - column = int(input("column?: ")) - 1 - os.system("clear") - gert.drop_fisje(column, DISC_A if disc_a else DISC_B) - disc_a = not disc_a - -if __name__ == "__main__": - main() - diff --git a/console/test.py b/console/test.py deleted file mode 100644 index 8cc1db9..0000000 --- a/console/test.py +++ /dev/null @@ -1,24 +0,0 @@ -import subprocess - -w = 7 -h = 6 -column = 3 - -process = subprocess.Popen(["./voerbak"], - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=None) - -process.stdin.write(bytearray(f"{w} {h}\n", "utf-8")) -process.stdin.flush() - -process.stdin.write(bytearray(f"{column}\n", "utf-8")) -process.stdin.flush() - -# process.stdin.write(b"0\n") -# process.stdin.flush() -# for c in iter(lambda: process.stdout.read(1), b''): -# sys.stdout.write(c) - -print(process.stdout.readlines(5)) - diff --git a/console/v2.py b/console/v2.py deleted file mode 100644 index 69e61aa..0000000 --- a/console/v2.py +++ /dev/null @@ -1,105 +0,0 @@ -from colorama import Fore -import os -import time - -DISC_SHAPE = "o" -DISC_A = Fore.RED + DISC_SHAPE + Fore.RESET -DISC_B = Fore.BLUE + DISC_SHAPE + Fore.RESET -EMPTY = Fore.LIGHTBLACK_EX + "_" + Fore.RESET - -class bord: - def __init__(self, w, h): - self.width = w - self.height = h - self.board = [EMPTY] * (w * h) - - def print(self): - for y in range(self.height -1, -1, -1): - for x in range(self.width): - print(self.board[x + y * self.width], end=" ") - print("\n", end="") - - def recursive_solve(self, pos, check_for, direction, current_length): - overflow = (pos % self.width) + direction - if overflow == self.width or overflow == -1: # horizontal overflow - return current_length - new_position = pos + direction - if new_position < 0 or new_position > self.width * self.height - 1: # vertical overflow - return current_length - if self.board[new_position] != check_for: - return current_length - return self.recursive_solve(new_position, check_for, direction, current_length + 1) - - def check_win(self, pos): - directions = [ - self.width, # north - self.width + 1, # northeast - 1, # east - -self.width + 1, # southeast - -self.width, # south - -self.width - 1, # southwest - -1, # west - self.width - 1, # northwest - ] - values = list() - for direction in directions: - values.append(self.recursive_solve(pos, self.board[pos], direction, 0)) - joined_directions = [ - values[0] + values[4], - values[1] + values[5], - values[2] + values[6], - values[3] + values[7] - ] - won = any(i >= 3 for i in joined_directions) - if won: - for i, value in enumerate(joined_directions): - if value >= 3: - start_pos = pos + directions[i] * values[i] - end_pos = pos + directions[i+4] * values[i+4] - print(start_pos, end_pos) - self.board[start_pos] = "x" - self.board[end_pos] = "x" - return won - - def debug(self, pos): - self.board[pos] = "x" - directions = [ - self.width, # 0: north - self.width + 1, # 1: northeast - 1, # 2: east - -self.width + 1, # 3: southeast - -self.width, # 4: south - -self.width - 1, # 5: southwest - -1, # 6: west - self.width - 1, # 7: northwest - ] - - for index, direction in enumerate(directions): - new_position = pos + direction - if new_position > len(self.board) - 1 or new_position < 0: continue - if index in range(1, 4) and pos % self.width == self.width -1: continue - if index in range(5, 8) and pos % self.width == 0: continue - self.board[new_position] = "o" - - def drop_fisje(self, column, disc): - for row in range(self.height): - pos = column + row * self.width - if self.board[pos] == EMPTY: - self.board[pos] = disc - won = self.check_win(pos) - print(won) - return - -def main(): - disc_a = True - gert = bord(7, 6) - for x in range(len(gert.board)): - gert = bord(7, 6) - gert.debug(x) - gert.print() - print("\n\n", end='') - time.sleep(0.1) - -if __name__ == "__main__": - main() - diff --git a/voerbak/input b/voerbak/input new file mode 100644 index 0000000..1243fe0 --- /dev/null +++ b/voerbak/input @@ -0,0 +1,14 @@ +7 6 +4 +3 +3 +2 +1 +2 +2 +7 +1 +7 +1 +7 +1 diff --git a/voerbak/main.py b/voerbak/main.py new file mode 100644 index 0000000..433f130 --- /dev/null +++ b/voerbak/main.py @@ -0,0 +1,90 @@ +from colorama import Fore +import os + +DISC_SHAPE = "o" +DISC_A = Fore.RED + DISC_SHAPE + Fore.RESET +DISC_B = Fore.BLUE + DISC_SHAPE + Fore.RESET +EMPTY = Fore.LIGHTBLACK_EX + "_" + Fore.RESET + +class bord: + def __init__(self, w, h): + self.width = w + self.height = h + self.board = [[EMPTY for x in range(self.width)] for u in range(self.height)] + + def print(self): + print("\n".join([" ".join(self.board[y]) for y in range(len(self.board) -1, -1, -1)])) + + def outside_board(self, coords): + return coords[0] < 0 or \ + coords[1] < 0 or \ + coords[0] > self.height - 1 or \ + coords[1] > self.width - 1 + + def recursive_solve(self, coords, check_for, direction, current_length): + new_position = ( + coords[0] + direction[0], + coords[1] + direction[1] + ) + if self.outside_board(new_position) or self.board[new_position[0]][new_position[1]] != check_for: + return current_length + else: + return self.recursive_solve(new_position, check_for, direction, current_length + 1) + + def check_win(self, coords): + directions = [ + ( 1, 0), + ( 1, 1), + ( 0, 1), + (-1, 1), + (-1, 0), + (-1, -1), + ( 0, -1), + ( 1, -1) + ] + values = list() + for direction in directions: + values.append(self.recursive_solve(coords, self.board[coords[0]][coords[1]], direction, 0)) + joined_directions = [ + values[0] + values[4], + values[1] + values[5], + values[2] + values[6], + values[3] + values[7] + ] + won = any(i >= 3 for i in joined_directions) + if won: + for i, value in enumerate(joined_directions): + if value >= 3: + start_pos = ( + coords[0] + directions[i][0] * values[i], + coords[1] + directions[i][1] * values[i], + ) + end_pos = ( + coords[0] + directions[i+4][0] * values[i+4], + coords[1] + directions[i+4][1] * values[i+4], + ) + print(start_pos, end_pos) + + return won + + def drop_fisje(self, column, disc): + for row, value in enumerate(self.board): + if self.board[row][column] == EMPTY: + self.board[row][column] = disc + won = self.check_win((row, column)) + print(won) + return + +def main(): + disc_a = True + gert = bord(11, 8) + while True: + gert.print() + column = int(input("column?: ")) - 1 + os.system("clear") + gert.drop_fisje(column, DISC_A if disc_a else DISC_B) + disc_a = not disc_a + +if __name__ == "__main__": + main() + diff --git a/voerbak/makefile b/voerbak/makefile new file mode 100644 index 0000000..a4102d4 --- /dev/null +++ b/voerbak/makefile @@ -0,0 +1,21 @@ +CC = gcc +LD = gcc +RM = rm -f +CFLAGS = + +SOURCES := $(wildcard *.c) +OBJECTS := $(patsubst %.c,%.o, $(SOURCES)) + +all: voerbak + +.cpp.o: + $(CC) -c $(CFLAGS) $< + +voerbak: $(OBJECTS) + $(CC) $(OBJECTS) -o voerbak + +clean: + $(RM) voerbak + +distclean: clean + $(RM) *.o diff --git a/voerbak/test.py b/voerbak/test.py new file mode 100644 index 0000000..8cc1db9 --- /dev/null +++ b/voerbak/test.py @@ -0,0 +1,24 @@ +import subprocess + +w = 7 +h = 6 +column = 3 + +process = subprocess.Popen(["./voerbak"], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=None) + +process.stdin.write(bytearray(f"{w} {h}\n", "utf-8")) +process.stdin.flush() + +process.stdin.write(bytearray(f"{column}\n", "utf-8")) +process.stdin.flush() + +# process.stdin.write(b"0\n") +# process.stdin.flush() +# for c in iter(lambda: process.stdout.read(1), b''): +# sys.stdout.write(c) + +print(process.stdout.readlines(5)) + diff --git a/voerbak/v2.py b/voerbak/v2.py new file mode 100644 index 0000000..69e61aa --- /dev/null +++ b/voerbak/v2.py @@ -0,0 +1,105 @@ +from colorama import Fore +import os +import time + +DISC_SHAPE = "o" +DISC_A = Fore.RED + DISC_SHAPE + Fore.RESET +DISC_B = Fore.BLUE + DISC_SHAPE + Fore.RESET +EMPTY = Fore.LIGHTBLACK_EX + "_" + Fore.RESET + +class bord: + def __init__(self, w, h): + self.width = w + self.height = h + self.board = [EMPTY] * (w * h) + + def print(self): + for y in range(self.height -1, -1, -1): + for x in range(self.width): + print(self.board[x + y * self.width], end=" ") + print("\n", end="") + + def recursive_solve(self, pos, check_for, direction, current_length): + overflow = (pos % self.width) + direction + if overflow == self.width or overflow == -1: # horizontal overflow + return current_length + new_position = pos + direction + if new_position < 0 or new_position > self.width * self.height - 1: # vertical overflow + return current_length + if self.board[new_position] != check_for: + return current_length + return self.recursive_solve(new_position, check_for, direction, current_length + 1) + + def check_win(self, pos): + directions = [ + self.width, # north + self.width + 1, # northeast + 1, # east + -self.width + 1, # southeast + -self.width, # south + -self.width - 1, # southwest + -1, # west + self.width - 1, # northwest + ] + values = list() + for direction in directions: + values.append(self.recursive_solve(pos, self.board[pos], direction, 0)) + joined_directions = [ + values[0] + values[4], + values[1] + values[5], + values[2] + values[6], + values[3] + values[7] + ] + won = any(i >= 3 for i in joined_directions) + if won: + for i, value in enumerate(joined_directions): + if value >= 3: + start_pos = pos + directions[i] * values[i] + end_pos = pos + directions[i+4] * values[i+4] + print(start_pos, end_pos) + self.board[start_pos] = "x" + self.board[end_pos] = "x" + return won + + def debug(self, pos): + self.board[pos] = "x" + directions = [ + self.width, # 0: north + self.width + 1, # 1: northeast + 1, # 2: east + -self.width + 1, # 3: southeast + -self.width, # 4: south + -self.width - 1, # 5: southwest + -1, # 6: west + self.width - 1, # 7: northwest + ] + + for index, direction in enumerate(directions): + new_position = pos + direction + if new_position > len(self.board) - 1 or new_position < 0: continue + if index in range(1, 4) and pos % self.width == self.width -1: continue + if index in range(5, 8) and pos % self.width == 0: continue + self.board[new_position] = "o" + + def drop_fisje(self, column, disc): + for row in range(self.height): + pos = column + row * self.width + if self.board[pos] == EMPTY: + self.board[pos] = disc + won = self.check_win(pos) + print(won) + return + +def main(): + disc_a = True + gert = bord(7, 6) + for x in range(len(gert.board)): + gert = bord(7, 6) + gert.debug(x) + gert.print() + print("\n\n", end='') + time.sleep(0.1) + +if __name__ == "__main__": + main() + diff --git a/voerbak/voerbak.c b/voerbak/voerbak.c new file mode 100644 index 0000000..e7c6c86 --- /dev/null +++ b/voerbak/voerbak.c @@ -0,0 +1,110 @@ +#include +#include +#include +#include + +void printBoard(int board[], int width, int height) { + for (int i = 0; i < width * height; i++) + printf("%d", board[i]); + printf("\n"); + fflush(stdout); +} + +int recursiveSolve(int board[], int width, int height, int pos, int checkFor, int direction, int d_index, int currentLength) { + int newPos = pos + direction; + if (newPos > width * height - 1 || newPos < 0) return currentLength; + int row = pos % width; + if (row == width && d_index >= 1 && d_index <= 3) return currentLength; + if (row == 0 && d_index >= 5 && d_index <= 7) return currentLength; + if (board[newPos] != checkFor) return currentLength; + return recursiveSolve(board, width, height, newPos, checkFor, direction, d_index, currentLength + 1); +} + +bool checkWin(int board[], int width, int height, int pos) { + int directions[8] = { + width, // north + width + 1, // northeast + 1, // east + -width + 1, // southeast + -width, // south + -width -1, // southwest + -1, // west + width -1 // northwest + }; + + int values[8]; + for (int i = 0; i < 8; i++) + values[i] = recursiveSolve(board, width, height, pos, board[pos], directions[i], i, 0); + + int joinedValues[4] = { + values[0] + values[4], + values[1] + values[5], + values[2] + values[6], + values[3] + values[7] + }; + + bool won = false; + for (int i = 0; i < 4; i++) { + if (joinedValues[i] >= 3) { + won = won || true; + + int start_pos = pos + directions[i+0] * values[i+0]; + int end_pos = pos + directions[i+4] * values[i+4]; + printf("w:%d-%d\n", start_pos, end_pos); + fflush(stdout); + } + } + + return won; +} + +bool boardFull(int board[], int width, int height) { + for (int i = 0; i < width * height; i++) + if (board[i] == 0) return false; + return true; +} + +bool dropFisje(int board[], int width, int height, int column, int disc) { + for (int row = 0; row < height; row++) { + int pos = column + row * width; + if (board[pos] == 0) { + board[pos] = disc; + bool won = checkWin(board, width, height, pos); + return true; // success + } + } + printf("e:full\n"); + fflush(stdout); + return false; // unsuccessful drop on board full +} + +int main() { + printf("voerbak v2\n"); + int width, height; + scanf("%d %d", &width, &height); + + int board[width * height]; + memset(board, 0, sizeof board); + + bool player_1 = true; + int move = 0; + while (scanf("%d", &move) == 1) { + if (move == 0) break; + if (move < 1 || move > width) continue; + + bool dropSuccess = dropFisje(board, width, height, move - 1, player_1 + 1); + + player_1 = player_1 ^ dropSuccess; // only flip turns on successful drop + printf("m:%s\n", player_1 ? "true" : "false"); + fflush(stdout); + + if (boardFull(board, width, height)) { + printf("d:full\n"); + fflush(stdout); + } + + printBoard(board, width, height); + } + + return 0; +} -- cgit v1.2.3