summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2022-11-22 12:54:50 -0800
committerKai Stevenson <kai@kaistevenson.com>2022-11-22 12:54:50 -0800
commit27ba66b8b9610b3c9a962f1509fa90c3540f6a52 (patch)
treec70cbb210550274f96d2cc21a5dcc814d5aca395
parent377cb89311fb4ddbb58840e19d02658db41f1ee8 (diff)
improved input handling
-rw-r--r--ttt.c32
1 files changed, 23 insertions, 9 deletions
diff --git a/ttt.c b/ttt.c
index 308bf29..fcb3724 100644
--- a/ttt.c
+++ b/ttt.c
@@ -2,20 +2,23 @@
#include <stdlib.h>
#include <string.h>
void draw_board(int boardState[3][3]) {
+ printf(" ABC\n");
for(int r = 0; r < 3; r++) {
- char* line = calloc(3, 1);
+ char* line = calloc(5, 1);
+ line[0] = '1' + r;
+ line[1] = ' ';
for(int c = 0; c < 3; c++) {
if(boardState[c][r] == 0) {
- line[c] = '-';
+ line[c + 2] = '-';
}
else if(boardState[c][r] == 1) {
- line[c] = 'x';
+ line[c + 2] = 'x';
}
else if(boardState[c][r] == 2) {
- line[c] = 'o';
+ line[c + 2] = 'o';
}
else {
- line[c] = 'E';
+ line[c + 2] = 'E';
}
}
printf(line);
@@ -105,11 +108,22 @@ int get_game_state(int boardState[3][3]) {
}
}
int* user_input_pos(int boardState[3][3], int turn) {
+ char* tip = malloc(3 * sizeof(char));
int* ip = malloc(2 * sizeof(int));
- printf("Enter X coordinate: ");
- scanf("%d", &ip[0]);
- printf("Enter Y coordinate: ");
- scanf("%d", &ip[1]);
+ printf("Enter coordinates: ");
+ scanf("%2s", tip);
+ for(int i = 0; i < 2; i++) {
+ if(tip[i] <= '9' && tip[i] >= '0') {
+ ip[1] = tip[i] - '0' - 1;
+ }
+ else if(tip[i] <= 'Z' && tip[i] >= 'A') {
+ ip[0] = tip[i] - 'A';
+ }
+ else {
+ ip[0] = tip[i] - 'a';
+ }
+ }
+ free(tip);
return ip;
}
struct PosInfo {