Replace spcae and tabs and remove macro

This commit is contained in:
Night Kaly 2024-11-07 00:32:16 +00:00
parent dddb133a62
commit 46d95ea572
Signed by: night0721
SSH key fingerprint: SHA256:B/hgVwUoBpx5vdNsXl9w8XwZljA9766uk6T4ubZp5HM

116
ace.c
View file

@ -6,7 +6,6 @@
#define CLEAR_SCREEN() printf("\033[H\033[J") #define CLEAR_SCREEN() printf("\033[H\033[J")
#define MOVE_CURSOR(x, y) printf("\033[%d;%dH", (x), (y)) #define MOVE_CURSOR(x, y) printf("\033[%d;%dH", (x), (y))
#define RESET_COLOR() printf("\033[0m")
typedef struct { typedef struct {
int value; /* 1 to 13 (Ace to King) */ int value; /* 1 to 13 (Ace to King) */
@ -48,7 +47,7 @@ int main(void)
CLEAR_SCREEN(); CLEAR_SCREEN();
MOVE_CURSOR(1, 1); MOVE_CURSOR(1, 1);
printf("You Computer\n"); printf("You Computer\n");
player_score = calculate_score(player_hand, player_num_cards); player_score = calculate_score(player_hand, player_num_cards);
cpu_score = calculate_score(cpu_hand, cpu_num_cards); cpu_score = calculate_score(cpu_hand, cpu_num_cards);
@ -56,30 +55,30 @@ int main(void)
display_hand(player_hand, player_num_cards, 1, player_score); display_hand(player_hand, player_num_cards, 1, player_score);
display_hand(cpu_hand, cpu_num_cards, 0, cpu_score); display_hand(cpu_hand, cpu_num_cards, 0, cpu_score);
/* Check if the player has busted */ /* Check if the player has busted */
if (player_score > 21) { if (player_score > 21) {
printf("\n\033[38;2;255;0;0mBust! You lose.\033[0m\n"); printf("\n\033[38;2;255;0;0mBust! You lose.\033[0m\n");
break; break;
} }
printf("\nHit (h) or stand (s)? "); printf("\nHit (h) or stand (s)? ");
fflush(stdout); fflush(stdout);
char action; char action;
read(STDIN_FILENO, &action, 1); read(STDIN_FILENO, &action, 1);
if (action == 'h') { if (action == 'h') {
player_hand[player_num_cards++] = deal_card(); player_hand[player_num_cards++] = deal_card();
} else if (action == 's') { } else if (action == 's') {
break; break;
} }
} }
/* Computer's turn to deal /* Computer's turn to deal
* Customise here if you want more wins * Customise here if you want more wins
*/ */
while (cpu_score < 17) { while (cpu_score < 17) {
cpu_hand[cpu_num_cards++] = deal_card(); cpu_hand[cpu_num_cards++] = deal_card();
cpu_score = calculate_score(cpu_hand, cpu_num_cards); cpu_score = calculate_score(cpu_hand, cpu_num_cards);
} }
@ -88,18 +87,18 @@ int main(void)
player_score = calculate_score(player_hand, player_num_cards); player_score = calculate_score(player_hand, player_num_cards);
cpu_score = calculate_score(cpu_hand, cpu_num_cards); cpu_score = calculate_score(cpu_hand, cpu_num_cards);
/* Determine win or lose */ /* Determine win or lose */
if (player_score > 21) { if (player_score > 21) {
printf("\033[38;2;255;0;0mComputer wins!\033[0m\n"); printf("\033[38;2;255;0;0mComputer wins!\033[0m\n");
} else if (cpu_score > 21) { } else if (cpu_score > 21) {
printf("\033[38;2;0;255;0mYou win!\033[0m\n"); printf("\033[38;2;0;255;0mYou win!\033[0m\n");
} else if (player_score > cpu_score) { } else if (player_score > cpu_score) {
printf("\033[38;2;0;255;0mYou win!\033[0m\n"); printf("\033[38;2;0;255;0mYou win!\033[0m\n");
} else if (player_score < cpu_score) { } else if (player_score < cpu_score) {
printf("\033[38;2;255;0;0mComputer wins!\033[0m\n"); printf("\033[38;2;255;0;0mComputer wins!\033[0m\n");
} else { } else {
printf("\033[38;2;255;255;0mDraw!\033[0m\n"); printf("\033[38;2;255;255;0mDraw!\033[0m\n");
} }
display_hand(player_hand, player_num_cards, 1, player_score); display_hand(player_hand, player_num_cards, 1, player_score);
@ -124,19 +123,19 @@ int calculate_score(card hand[], int num_cards)
int score = 0, num_aces = 0; int score = 0, num_aces = 0;
for (int i = 0; i < num_cards; i++) { for (int i = 0; i < num_cards; i++) {
if (hand[i].value == 1) { if (hand[i].value == 1) {
score += 11; score += 11;
num_aces++; num_aces++;
} else if (hand[i].value > 10) { } else if (hand[i].value > 10) {
score += 10; score += 10;
} else { } else {
score += hand[i].value; score += hand[i].value;
} }
} }
while (score > 21 && num_aces > 0) { while (score > 21 && num_aces > 0) {
score -= 10; score -= 10;
num_aces--; num_aces--;
} }
return score; return score;
@ -145,49 +144,46 @@ int calculate_score(card hand[], int num_cards)
void display_hand(card hand[], int num_cards, int is_player, int total) void display_hand(card hand[], int num_cards, int is_player, int total)
{ {
const char *suits[] = {"\033[38;2;255;0;0m♥\033[0m", "\033[38;2;0;0;255m♠\033[0m", "\033[38;2;255;0;255m♦\033[0m", "\033[38;2;0;255;0m♣\033[0m"}; const char *suits[] = {"\033[38;2;255;0;0m♥\033[0m", "\033[38;2;0;0;255m♠\033[0m", "\033[38;2;255;0;255m♦\033[0m", "\033[38;2;0;255;0m♣\033[0m"};
if (is_player) { if (is_player) {
printf("Cards - "); printf("Cards - ");
} else { } else {
/* Alignment */ /* Alignment */
MOVE_CURSOR(2, 28); MOVE_CURSOR(2, 28);
printf("Cards - "); printf("Cards - ");
} }
for (int i = 0; i < num_cards; i++) { for (int i = 0; i < num_cards; i++) {
/* Bold */ /* Bold */
printf("\033[1m"); printf("\033[1m");
switch (hand[i].value) { switch (hand[i].value) {
case 1: case 1:
printf("A%s ", suits[hand[i].suit]); printf("A%s ", suits[hand[i].suit]);
break; break;
case 11: case 11:
printf("J%s ", suits[hand[i].suit]); printf("J%s ", suits[hand[i].suit]);
break; break;
case 12: case 12:
printf("Q%s ", suits[hand[i].suit]); printf("Q%s ", suits[hand[i].suit]);
break; break;
case 13: case 13:
printf("K%s ", suits[hand[i].suit]); printf("K%s ", suits[hand[i].suit]);
break; break;
default: default:
printf("%d%s ", hand[i].value, suits[hand[i].suit]); printf("%d%s ", hand[i].value, suits[hand[i].suit]);
} }
RESET_COLOR(); /* Reset color */
printf("\033[0m");
} }
printf("\n"); printf("\n");
if (is_player) { if (is_player) {
printf("Total - "); printf("Total - ");
} else { } else {
MOVE_CURSOR(3, 28); MOVE_CURSOR(3, 28);
printf("Total - "); printf("Total - ");
} }
/* Bold and inverted colors */ /* Bold and inverted */
printf("\033[1m\033[47;30m"); printf("\033[1m\033[47;30m %d \033[0m\n", total);
printf(" %d ", total);
RESET_COLOR();
printf("\n");
} }