From 7cce4cff421005566ec506a2627b6c188fc05d9c Mon Sep 17 00:00:00 2001 From: Joursoir Date: Thu, 18 Feb 2021 12:58:15 +0000 Subject: draw field for custom cols and rows, make field is more beautiful --- clui.cpp | 55 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/clui.cpp b/clui.cpp index 9dc3140..a157ae3 100644 --- a/clui.cpp +++ b/clui.cpp @@ -2,6 +2,8 @@ #include "GameField.hpp" +#define NC_MOVE_X 2 +#define NC_MOVE_Y 2 #define SYMBOL_PLAYERONE 'x' #define SYMBOL_PLAYERTWO 'o' @@ -43,15 +45,29 @@ void dbgprint(const char *msg) void drawGame(int cols, int rows, int symbol) { - /* MAKE: draw for custom cols and rows */ - mvprintw(0, 0, "012"); - mvprintw(1, 0, "345"); - mvprintw(2, 0, "678"); + int i, j; + for(j = 0; j < cols * 2 - 1; j++) { + move(j, 0); + if(j % 2 == 0) { + addch('~'); + for(i = 1; i < rows; i++) { + addch('|'); + addch('~'); + } + } + else { + addch('-'); + for(i = 1; i < rows; i++) { + addch('|'); + addch('-'); + } + } + } min_y = 0; - max_y = cols-1; + max_y = cols * 2 - 2; min_x = 0; - max_x = rows-1; + max_x = rows * 2 - 2; cursor_y = max_y / 2; cursor_x = max_x / 2; @@ -74,13 +90,16 @@ void printMove(int y, int x, int symbol) updateCursor(); } -void handleMove() +void gameMove() { - if(!game_field->CanMove(cursor_y, cursor_x)) { + int game_y = cursor_y > 0 ? cursor_y - cursor_y / 2 : 0; + int game_x = cursor_x > 0 ? cursor_x - cursor_x / 2 : 0; + + if(!game_field->CanMove(game_y, game_x)) { dbgprint("yet 'x' or 'o'"); return; } - game_field->Move(cursor_y, cursor_x); + game_field->Move(game_y, game_x); printMove(cursor_y, cursor_x, player_symbol); changePlayer(); @@ -105,31 +124,23 @@ void handleButtons() switch(ch) { case KEY_LEFT: - if(cursor_x != min_x) { - cursor_x -= 1; - } + if(cursor_x > min_x) cursor_x -= NC_MOVE_X; else dbgprint("left border"); break; case KEY_DOWN: - if(cursor_y != max_y) { - cursor_y += 1; - } + if(cursor_y < max_y) cursor_y += NC_MOVE_Y; else dbgprint("down border"); break; case KEY_UP: - if(cursor_y != min_y) { - cursor_y -= 1; - } + if(cursor_y > min_y) cursor_y -= NC_MOVE_Y; else dbgprint("up border"); break; case KEY_RIGHT: - if(cursor_x != max_x) { - cursor_x += 1; - } + if(cursor_x < max_x) cursor_x += NC_MOVE_X; else dbgprint("right border"); break; case key_enter: { - handleMove(); + gameMove(); break; } case key_restart: { -- cgit v1.2.3-18-g5258