diff options
Diffstat (limited to 'input.c')
| -rw-r--r-- | input.c | 74 |
1 files changed, 54 insertions, 20 deletions
@@ -2,11 +2,57 @@ #include <stdlib.h> #include "input.h" #include "board.h" + +#define POSITION board[cursorPosX][cursorPosY] char hand = ' '; int turnPart = 1; int turns = 0; -int InputBoard(char board[8][8], int keypress) { +enum piece { + WHITE, + BLACK, + BOARD, +}; + +enum piece CheckPiece(int hand) { + if (hand >= 'A' && hand <= 'Z' ) { + return WHITE; + } + else if (hand >= 'a' && hand <= 'z' ) { + return BLACK; + } + else { + return BOARD; + } +} + +void HandleInput(char board[8][8]) { + if ( hand == ' ' && POSITION != ' '){ + hand = POSITION; + POSITION = ' '; + turnPart++; + } + + /* Simply doing nothing with these felonius inputs */ + else if ( CheckPiece(hand) == CheckPiece(POSITION) ) {} + else if ( hand == ' ' && POSITION == ' ' ) {} + + + else if ( hand != POSITION) { + board[cursorPosX][cursorPosY] = hand; + hand = ' '; + turnPart++; + } + else if ( hand != ' ' && POSITION == ' ') { + board[cursorPosX][cursorPosY] = hand; + hand = ' '; + turnPart++; + } + if (turnPart > 1 && turnPart & 1) turns++; +} + + +int InputBoard(char board[8][8], int keypress) { /* Handle key input */ switch (keypress) { @@ -14,6 +60,12 @@ int InputBoard(char board[8][8], int keypress) { endwin(); exit(0); break; + case 'h': + hand = CheckPiece(hand); + break; + case 'p': + POSITION = CheckPiece(POSITION); + break; case KEY_UP: if (cursorPosX > 0) cursorPosX--; break; @@ -27,25 +79,7 @@ int InputBoard(char board[8][8], int keypress) { if (cursorPosY < 7) cursorPosY++; break; case '\n': /* This is the ENTER key. KEY_ENTER does not work */ - if ( hand == ' ' && board[cursorPosX][cursorPosY] != ' '){ - hand = board[cursorPosX][cursorPosY]; - board[cursorPosX][cursorPosY] = ' '; - turnPart++; - } - - else if ( hand != ' ' && board[cursorPosX][cursorPosY] != ' ') { - board[cursorPosX][cursorPosY] = hand; - hand = ' '; - turnPart++; - } - else if ( hand != ' ' && board[cursorPosX][cursorPosY] == ' ') { - board[cursorPosX][cursorPosY] = hand; - hand = ' '; - turnPart++; - } - if (turnPart & 1) turns++; - - + HandleInput(board); break; } |