Immediate and invisible input
parent
4c8ab9a09e
commit
4c3ff7608d
63
main.c
63
main.c
|
@ -40,50 +40,51 @@ void init_board(board *b) {
|
||||||
|
|
||||||
void game_start() {
|
void game_start() {
|
||||||
printf("\e[?1049h");
|
printf("\e[?1049h");
|
||||||
|
struct termios oldtio, newtio;
|
||||||
|
tcgetattr(STDIN_FILENO, &oldtio);
|
||||||
|
newtio = oldtio;
|
||||||
|
newtio.c_lflag &= ~(ICANON | ECHO);
|
||||||
|
tcsetattr(STDIN_FILENO, TCSANOW, &newtio);
|
||||||
board *b = new_board();
|
board *b = new_board();
|
||||||
game_loop(b);
|
game_loop(b);
|
||||||
print_score(b);
|
print_score(b);
|
||||||
free_board(b);
|
free_board(b);
|
||||||
printf("\e[?1049l");
|
printf("\e[?1049l");
|
||||||
|
tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
|
||||||
|
}
|
||||||
|
|
||||||
|
enum direction get_input()
|
||||||
|
{
|
||||||
|
switch(fgetc(stdin)) {
|
||||||
|
case 'a':
|
||||||
|
return west;
|
||||||
|
case 's':
|
||||||
|
return south;
|
||||||
|
case 'w':
|
||||||
|
return north;
|
||||||
|
case 'd':
|
||||||
|
return east;
|
||||||
|
case 'q':
|
||||||
|
return quit;
|
||||||
|
default:
|
||||||
|
return get_input();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void game_loop(board *b) {
|
void game_loop(board *b) {
|
||||||
char c[16];
|
|
||||||
int d;
|
|
||||||
bool r = true;
|
|
||||||
while(move_possible_any(b)) {
|
while(move_possible_any(b)) {
|
||||||
place_new_piece(b);
|
place_new_piece(b);
|
||||||
print_board(b);
|
print_board(b);
|
||||||
INPUT:
|
while(true) {
|
||||||
printf("Make a move:\n");
|
enum direction d = get_input();
|
||||||
fgets(c, sizeof(c), stdin);
|
|
||||||
switch(c[0]) {
|
if (d == quit) {
|
||||||
case 'a':
|
return;
|
||||||
d = west;
|
} else if(move_possible(b, d)) {
|
||||||
|
make_move(b, d);
|
||||||
break;
|
break;
|
||||||
case 's':
|
}
|
||||||
d = south;
|
|
||||||
break;
|
|
||||||
case 'w':
|
|
||||||
d = north;
|
|
||||||
break;
|
|
||||||
case 'd':
|
|
||||||
d = east;
|
|
||||||
break;
|
|
||||||
case 'q':
|
|
||||||
r = false;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
printf("Invalid move: %c\n", c[0]);
|
|
||||||
goto INPUT;
|
|
||||||
}
|
}
|
||||||
if(!r)
|
|
||||||
return;
|
|
||||||
if(!move_possible(b, d)) {
|
|
||||||
printf("Move not possible: %c\n", c[0]);
|
|
||||||
goto INPUT;
|
|
||||||
}
|
|
||||||
make_move(b, d);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
3
main.h
3
main.h
|
@ -6,6 +6,8 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <termios.h>
|
||||||
|
|
||||||
typedef struct board board;
|
typedef struct board board;
|
||||||
typedef unsigned int uint;
|
typedef unsigned int uint;
|
||||||
|
@ -16,6 +18,7 @@ enum direction {
|
||||||
south,
|
south,
|
||||||
west,
|
west,
|
||||||
east,
|
east,
|
||||||
|
quit,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct board {
|
struct board {
|
||||||
|
|
Loading…
Reference in New Issue