diff options
Diffstat (limited to 'api/game')
-rw-r--r-- | api/game/socket.py | 39 | ||||
-rwxr-xr-x | api/game/voerbak | bin | 0 -> 16592 bytes | |||
-rw-r--r-- | api/game/voerbak.c | 100 | ||||
-rw-r--r-- | api/game/voerbak_connector.py | 79 |
4 files changed, 200 insertions, 18 deletions
diff --git a/api/game/socket.py b/api/game/socket.py index c4e5ac7..d6dff1b 100644 --- a/api/game/socket.py +++ b/api/game/socket.py @@ -1,30 +1,33 @@ from flask import Blueprint, request, make_response -from flask_socketio import SocketIO, emit, disconnect, Namespace, emit +from flask_socketio import SocketIO, emit, disconnect, emit +from game.voerbak_connector import bord +from db import cursor import time import json -class GameSocketNamespace(Namespace): - def connect(self): - print("new connection") - emit("gert", {"gert": "banaan"}) +namespace = "/game/socket" - def on_connect(self): - print("new connection") - emit("gert", {"gert": "banaan"}) +class game: + def __init__(self, game_id): + self.game_id = game_id + self.board = bord(7, 6) - def on_disconnect(self): - print("disconnect") + def move(self, user_id, column): + # player_1 = cursor.execute("select player_1_id from games where game_id = ?", [self.game_id]).fetchone()[0] + # player_1_move = player_1 == user_id + # if not self.board.player_1 == player_1_move: return + self.board.drop_fisje(column) - def new_move(self, data): - print("new_move") +def run(app): + io = SocketIO(app) + + @io.on("new_move", namespace) + def new_move(data): print(data) - def resign(self, data): - print("resign") + @io.on("resign", namespace) + def resign(data): print(data) -def run(app): - socketio = SocketIO(app) - socketio.on_namespace(GameSocketNamespace("/game/socket")) - socketio.run(app, host="127.0.0.1", port=5000, debug=True) + io.run(app, host="127.0.0.1", port=5000, debug=True) diff --git a/api/game/voerbak b/api/game/voerbak Binary files differnew file mode 100755 index 0000000..e7b7a14 --- /dev/null +++ b/api/game/voerbak diff --git a/api/game/voerbak.c b/api/game/voerbak.c new file mode 100644 index 0000000..f0ecb4a --- /dev/null +++ b/api/game/voerbak.c @@ -0,0 +1,100 @@ +#include <stdio.h> +#include <stdlib.h> +#include <memory.h> +#include <stdbool.h> + +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 currentLength) { + int overflow = (pos % width) + direction; + if (overflow == width || overflow == -1) + return currentLength; + int newPos = pos + direction; + if (newPos < 0 || newPos > width * height - 1) + return currentLength; + if (board[newPos] != checkFor) + return currentLength; + return recursiveSolve(board, width, height, newPos, checkFor, direction, 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], 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 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); + + printBoard(board, width, height); + } + + return 0; +} diff --git a/api/game/voerbak_connector.py b/api/game/voerbak_connector.py new file mode 100644 index 0000000..6274ada --- /dev/null +++ b/api/game/voerbak_connector.py @@ -0,0 +1,79 @@ +from colorama import Fore +import logging as log +import subprocess +import os + +VERBOSE = log.ERROR +log.basicConfig(format="[ %(levelname)s ]: %(message)s", level=VERBOSE) + +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 + +VOERBAK_LOCATION = "./voerbak" +if os.name == "nt": VOERBAK_LOCATION += ".exe" + +class bord: + def __init__(self, w, h): + self.width = w + self.height = h + self.player_1 = True + self.board = "0" * (w * h) + self.win_positions = [] + self.process = subprocess.Popen(["./voerbak"], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=None) + self.process.stdin.write(bytearray(f"{w} {h}\n", "utf-8")) + self.process.stdin.flush() + + def get_output(self): + return self.process.stdout.readline().decode()[:-1] + + def update_board(self): + buffer = self.get_output() + while not buffer.isdigit(): + if buffer.startswith("w:"): + self.win_positions.append(buffer[2:].split("-")) + log.info(f"won: {buffer[2:].split('-')}") + elif buffer.startswith("e:"): + log.warning(buffer[2:]) + elif buffer.startswith("m:"): + substr = buffer[2:] + self.player_1 = True if substr == "true" else False + buffer = self.get_output() + self.board = buffer + + def print(self): + for y in range(self.height -1, -1, -1): + for x in range(self.width): + state = self.board[x + y * self.width] + char = [EMPTY, + DISC_A, + DISC_B + ] + print(char[int(state)], end=" ") + print("\n", end="") + + def drop_fisje(self, column): + self.process.stdin.write(bytearray(f"{column}\n", "utf-8")) + self.process.stdin.flush() + self.update_board() + +def main(): + gert = bord(7, 6) + while True: + print(gert.player_1) + if len(gert.win_positions) > 0: + print(f"won: {gert.win_positions}") + exit(0) + gert.print() + column = int(input("column?: ")) - 1 + if column not in range(gert.width): + continue + gert.drop_fisje(column + 1) + +if __name__ == "__main__": + main() + |