>>812284
you forgot to set_mode(0) on exit. Here, I made it atexit() for you:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
char maze[6][8] = {
"#######",
"# #",
"# $W #",
"# ~ #",
"# #",
"#######",
};
int player_x = 1;
int player_y = 1;
void print_maze (void) {
printf("\033[2J\033[1;1H");
char copy[sizeof(maze[0])];
for (int i = 0; i < 6; i++) {
if (player_y == i) {
strcpy(copy, maze[i]);
copy[player_x] = '@';
printf("%s\n", copy);
}
else {
printf("%s\n", maze[i]);
}
}
}
/* https://rosettacode.org/wiki/Keyboard_input/Keypress_check#C */
void set_mode(int want_key) {
static struct termios old, new;
if (!want_key) {
tcsetattr(STDIN_FILENO, TCSANOW, &old);
return;
}
tcgetattr(STDIN_FILENO, &old);
new = old;
new.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &new);
}
void reset_mode (void) { set_mode(0); }
char look (int x, int y) {
if (y < 0 || y > sizeof(*maze)) return '\0';
if (x < 0 || x > sizeof(maze[0])) return '\0';
return maze[y][x];
}
int sees (int d_x, int d_y, char *s) {
int at_x = player_x + d_x;
int at_y = player_y + d_y;
while (*s) {
if (*s != look(at_x, at_y)) return 0;
at_x += d_x;
at_y += d_y;
s++;
}
return 1;
}
int getChoice(char *question) {
char choice;
printf("%s\n",question);
printf("Choice:");
choice=getchar();
printf("\n");
return choice;
}
void step (int x, int y) {
if (sees(x, y, " ")) { player_y += y; player_x += x; }
else if (sees(x, y, "~")) { player_y += y; player_x += x;
printf("Ooh! A dollar! You pick it up nonchalantly.\n");
getchar();
maze[player_y][player_x] = ' ';
}
else if (sees(x, y, "$ ")) { player_y += y; player_x += x;
maze[player_y][player_x] = ' ';
maze[player_y+y][player_x+x] = '$';
}
else if (sees(x, y, "$W")) { player_y += y; player_x += x;
printf("You hear a strangled scream about 'rock privilege'... weird.\n");
getchar();
maze[player_y][player_x] = ' ';
maze[player_y+y][player_x+x] = '$';
}
else if (sees(x, y, "W")) { player_y += y; player_x += x;
char *question="A bluehaired non-binary appears, what would you like to do?\n \
1: Use your priviledge to kill the nonbinary?\n \
2: Apologize for being a cis white male";
switch (getChoice(question)) {
case '1':
printf("You use (((priviledge))) to to kill the nonbinary\n");
getchar();
break;
case '2':
printf("You apologize for being a cis white male. You are sent to prison.\n");
printf("YOU LOOSE, GAME OVER\n");
exit(0);
}
maze[player_y][player_x] = ' ';
}
}
int main () {
atexit(reset_mode);
set_mode(1);
while (1) {
print_maze();
switch(getchar()) {
case 'h': step(-1, 0); break;
case 'j': step(0, 1); break;
case 'k': step(0, -1); break;
case 'l': step(1, 0); break;
case 27: exit(0);
}
}
}