2024-10-01 14:40:14 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <termios.h>
|
|
|
|
|
|
|
|
#define CLEAR_SCREEN() printf("\033[H\033[J")
|
|
|
|
#define MOVE_CURSOR(x, y) printf("\033[%d;%dH", (x), (y))
|
|
|
|
|
|
|
|
typedef struct {
|
2024-10-01 14:47:20 +02:00
|
|
|
int value; /* 1 to 13 (Ace to King) */
|
|
|
|
int suit; /* 0 to 3 (Hearts, Spades, Diamonds, Clubs) */
|
2024-10-01 14:40:14 +02:00
|
|
|
} card;
|
|
|
|
|
2024-11-05 00:27:50 +01:00
|
|
|
card deal_card(void);
|
2024-10-01 14:40:14 +02:00
|
|
|
int calculate_score(card hand[], int num_cards);
|
|
|
|
void display_hand(card hand[], int num_cards, int is_player, int total);
|
|
|
|
|
|
|
|
struct termios orig_termios;
|
|
|
|
|
2024-11-05 00:27:50 +01:00
|
|
|
int main(void)
|
2024-10-01 14:40:14 +02:00
|
|
|
{
|
2024-10-01 14:47:20 +02:00
|
|
|
srand(time(NULL));
|
2024-10-01 14:40:14 +02:00
|
|
|
|
2024-10-01 14:47:20 +02:00
|
|
|
card player_hand[10], cpu_hand[10];
|
|
|
|
int player_num_cards = 0, cpu_num_cards = 0;
|
2024-10-01 14:40:14 +02:00
|
|
|
|
|
|
|
/* Set terminal to return read syscall without enter */
|
2024-10-01 14:47:20 +02:00
|
|
|
struct termios new_termios;
|
2024-10-01 14:40:14 +02:00
|
|
|
|
|
|
|
tcgetattr(STDIN_FILENO, &orig_termios);
|
|
|
|
new_termios = orig_termios;
|
|
|
|
|
|
|
|
new_termios.c_lflag &= ~(ICANON | ECHO);
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
player_hand[player_num_cards++] = deal_card();
|
|
|
|
cpu_hand[cpu_num_cards++] = deal_card();
|
|
|
|
}
|
|
|
|
|
|
|
|
int player_score;
|
|
|
|
int cpu_score;
|
|
|
|
|
2024-10-01 14:47:20 +02:00
|
|
|
while (1) {
|
2024-10-01 14:40:14 +02:00
|
|
|
CLEAR_SCREEN();
|
|
|
|
MOVE_CURSOR(1, 1);
|
|
|
|
printf("You Computer\n");
|
2024-11-07 01:32:16 +01:00
|
|
|
|
2024-10-01 14:40:14 +02:00
|
|
|
|
|
|
|
player_score = calculate_score(player_hand, player_num_cards);
|
|
|
|
cpu_score = calculate_score(cpu_hand, cpu_num_cards);
|
|
|
|
|
|
|
|
display_hand(player_hand, player_num_cards, 1, player_score);
|
|
|
|
display_hand(cpu_hand, cpu_num_cards, 0, cpu_score);
|
|
|
|
|
2024-11-07 01:32:16 +01:00
|
|
|
/* Check if the player has busted */
|
|
|
|
if (player_score > 21) {
|
|
|
|
printf("\n\033[38;2;255;0;0mBust! You lose.\033[0m\n");
|
|
|
|
break;
|
|
|
|
}
|
2024-10-01 14:40:14 +02:00
|
|
|
|
2024-11-07 01:32:16 +01:00
|
|
|
printf("\nHit (h) or stand (s)? ");
|
2024-10-19 13:59:48 +02:00
|
|
|
fflush(stdout);
|
2024-10-01 14:40:14 +02:00
|
|
|
|
|
|
|
char action;
|
|
|
|
read(STDIN_FILENO, &action, 1);
|
|
|
|
|
2024-11-07 01:32:16 +01:00
|
|
|
if (action == 'h') {
|
|
|
|
player_hand[player_num_cards++] = deal_card();
|
|
|
|
} else if (action == 's') {
|
|
|
|
break;
|
|
|
|
}
|
2024-10-01 14:47:20 +02:00
|
|
|
}
|
2024-10-01 14:40:14 +02:00
|
|
|
|
2024-10-01 14:47:20 +02:00
|
|
|
/* Computer's turn to deal
|
2024-10-01 14:40:14 +02:00
|
|
|
* Customise here if you want more wins
|
|
|
|
*/
|
2024-10-01 14:47:20 +02:00
|
|
|
while (cpu_score < 17) {
|
2024-11-07 01:32:16 +01:00
|
|
|
cpu_hand[cpu_num_cards++] = deal_card();
|
2024-10-01 14:40:14 +02:00
|
|
|
cpu_score = calculate_score(cpu_hand, cpu_num_cards);
|
2024-10-01 14:47:20 +02:00
|
|
|
}
|
2024-10-01 14:40:14 +02:00
|
|
|
|
|
|
|
CLEAR_SCREEN();
|
|
|
|
MOVE_CURSOR(1, 1);
|
|
|
|
|
|
|
|
player_score = calculate_score(player_hand, player_num_cards);
|
|
|
|
cpu_score = calculate_score(cpu_hand, cpu_num_cards);
|
2024-11-07 01:32:16 +01:00
|
|
|
|
2024-10-01 14:40:14 +02:00
|
|
|
/* Determine win or lose */
|
2024-10-01 14:47:20 +02:00
|
|
|
if (player_score > 21) {
|
2024-11-07 01:32:16 +01:00
|
|
|
printf("\033[38;2;255;0;0mComputer wins!\033[0m\n");
|
2024-10-01 14:47:20 +02:00
|
|
|
} else if (cpu_score > 21) {
|
2024-11-07 01:32:16 +01:00
|
|
|
printf("\033[38;2;0;255;0mYou win!\033[0m\n");
|
2024-10-01 14:47:20 +02:00
|
|
|
} else if (player_score > cpu_score) {
|
2024-11-07 01:32:16 +01:00
|
|
|
printf("\033[38;2;0;255;0mYou win!\033[0m\n");
|
2024-10-01 14:47:20 +02:00
|
|
|
} else if (player_score < cpu_score) {
|
2024-11-07 01:32:16 +01:00
|
|
|
printf("\033[38;2;255;0;0mComputer wins!\033[0m\n");
|
2024-10-01 14:47:20 +02:00
|
|
|
} else {
|
2024-11-07 01:32:16 +01:00
|
|
|
printf("\033[38;2;255;255;0mDraw!\033[0m\n");
|
2024-10-01 14:47:20 +02:00
|
|
|
}
|
2024-10-01 14:40:14 +02:00
|
|
|
|
|
|
|
display_hand(player_hand, player_num_cards, 1, player_score);
|
2024-10-01 14:47:20 +02:00
|
|
|
display_hand(cpu_hand, cpu_num_cards, 0, cpu_score);
|
2024-10-01 14:40:14 +02:00
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
/* Reset terminal */
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
|
2024-10-01 14:47:20 +02:00
|
|
|
return 0;
|
2024-10-01 14:40:14 +02:00
|
|
|
}
|
|
|
|
|
2024-11-05 00:27:50 +01:00
|
|
|
card deal_card(void)
|
2024-10-01 14:40:14 +02:00
|
|
|
{
|
2024-10-01 14:47:20 +02:00
|
|
|
card card;
|
|
|
|
card.value = rand() % 13 + 1;
|
|
|
|
card.suit = rand() % 4;
|
|
|
|
return card;
|
2024-10-01 14:40:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int calculate_score(card hand[], int num_cards)
|
|
|
|
{
|
2024-10-01 14:47:20 +02:00
|
|
|
int score = 0, num_aces = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < num_cards; i++) {
|
2024-11-07 01:32:16 +01:00
|
|
|
if (hand[i].value == 1) {
|
|
|
|
score += 11;
|
|
|
|
num_aces++;
|
|
|
|
} else if (hand[i].value > 10) {
|
|
|
|
score += 10;
|
|
|
|
} else {
|
|
|
|
score += hand[i].value;
|
|
|
|
}
|
2024-10-01 14:47:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (score > 21 && num_aces > 0) {
|
2024-11-07 01:32:16 +01:00
|
|
|
score -= 10;
|
|
|
|
num_aces--;
|
2024-10-01 14:47:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return score;
|
2024-10-01 14:40:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void display_hand(card hand[], int num_cards, int is_player, int total)
|
|
|
|
{
|
2024-10-01 14:47:20 +02:00
|
|
|
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"};
|
2024-11-07 01:32:16 +01:00
|
|
|
|
2024-10-01 14:47:20 +02:00
|
|
|
if (is_player) {
|
2024-11-07 01:32:16 +01:00
|
|
|
printf("Cards - ");
|
2024-10-01 14:47:20 +02:00
|
|
|
} else {
|
2024-10-01 14:40:14 +02:00
|
|
|
/* Alignment */
|
2024-11-07 01:32:16 +01:00
|
|
|
MOVE_CURSOR(2, 28);
|
|
|
|
printf("Cards - ");
|
2024-10-01 14:47:20 +02:00
|
|
|
}
|
2024-10-01 14:40:14 +02:00
|
|
|
|
2024-10-01 14:47:20 +02:00
|
|
|
for (int i = 0; i < num_cards; i++) {
|
2024-10-01 14:40:14 +02:00
|
|
|
/* Bold */
|
|
|
|
printf("\033[1m");
|
2024-11-07 01:32:16 +01:00
|
|
|
switch (hand[i].value) {
|
|
|
|
case 1:
|
|
|
|
printf("A%s ", suits[hand[i].suit]);
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
printf("J%s ", suits[hand[i].suit]);
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
printf("Q%s ", suits[hand[i].suit]);
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
printf("K%s ", suits[hand[i].suit]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("%d%s ", hand[i].value, suits[hand[i].suit]);
|
|
|
|
}
|
|
|
|
/* Reset color */
|
|
|
|
printf("\033[0m");
|
2024-10-01 14:47:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
2024-10-01 14:40:14 +02:00
|
|
|
if (is_player) {
|
2024-11-07 01:32:16 +01:00
|
|
|
printf("Total - ");
|
2024-10-01 14:47:20 +02:00
|
|
|
} else {
|
2024-11-07 01:32:16 +01:00
|
|
|
MOVE_CURSOR(3, 28);
|
|
|
|
printf("Total - ");
|
2024-10-01 14:47:20 +02:00
|
|
|
}
|
2024-10-01 14:40:14 +02:00
|
|
|
|
2024-11-07 01:32:16 +01:00
|
|
|
/* Bold and inverted */
|
|
|
|
printf("\033[1m\033[47;30m %d \033[0m\n", total);
|
2024-10-01 14:40:14 +02:00
|
|
|
}
|