输入搜索关键词并按下Enter键开始搜索
历时4天,这是在各种课上摸鱼划水写出来的21点程序,可能还不够完善,但已经可以稳定运行。
// =============================================================// // // // Copyright (C) 2022 Bec Yan // // All rights reserved // // // // filename : 21points.c // // description : A simple application to play // // 21 points games with AI. // // // // created by Bec Yan at 22/03/2022 09:52:50 // // mail : becyan@zju.edu.cn // // // // =============================================================// #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include <conio.h> int pla[53], ser[53], number[53], color[53], dollar = 0, cent = 0, now = 0, value = 0, visitor_card = 0, cro_value = 0, visitor_A = 0, cro_A = 0, preplay_money = 0; char name[20], char_set[5][30] = {""}, ch; void stop() { ch = getch(); } // to make it easier to read ONLY. void init_casino() // print "hello" to visitors ONLY. { printf(" ____ _ _ _____ _ \n | _ \\ ( ) | | / ____| (_) \n | |_) | ___ ___|/ ___ | | _ ___ ___ _ _ __ _ _ | | __ _ ___ _ _ __ ___ \n | _ < / _ \\/ __| / __| | | | | | \\ \\/ / | | | '__| | | | | | / _` / __| | '_ \\ / _ \\ \n | |_) | __/ (__ \\__ \\ | |___| |_| |> <| |_| | | | |_| | | |___| (_| \\__ \\ | | | | (_) |\n |____/ \\___|\\___| |___/ |______\\__,_/_/\\_ \\__,_|_| \\__, | \\_____\\__,_|___/_|_| |_|\\___/ \n __/ | \n |___/ \n"); printf("\n\n Bonjour, welcome to Bec's Luxury Casino in Saint Danis!\n"); } void init_poker() // cards from 1 to 13 in 4 colors. { int i, j, k = 0; for (i = 1; i <= 4; i++) for (j = 1; j <= 13; j++) { k++; number[k] = j; color[k] = i; // printf("CARD #%d: %d %d\n",k,color[k],number[k]); } strcpy(char_set[1], "\033[40;31mHeart\033[0m"); strcpy(char_set[2], "\033[40;31mDiamond\033[0m"); strcpy(char_set[3], "Spade"); strcpy(char_set[4], "Club"); } void say_cro(char s[255]) // cro says to visitors, with NO ENTER at last. { printf("\033[47;31mJohn\033[0m:\n %s", s); } void say_me(char s[255]) // visitor(me) says to cro, with NO ENTER at last. { printf("\033[40;33m%s\033[0m:\n %s", name, s); } void get_card(int num) // get a card with the number on the table, and PRINT it. { char card_num[3]; int card_value = 0; if ((number[ser[num]] < 11) && (number[ser[num]] > 1)) { itoa(number[ser[num]], card_num, 10); card_value = number[ser[num]]; } else { switch (number[ser[num]]) { case 11: strcpy(card_num, "J"); card_value = 10; break; case 12: strcpy(card_num, "Q"); card_value = 10; break; case 13: strcpy(card_num, "K"); card_value = 10; break; case 1: strcpy(card_num, "A"); card_value = 11; if (value + card_value > 21) { card_value = 1; } else { visitor_A++; } break; default: break; } } say_cro((char *)"The #"); printf("%d card you got is :", num); printf("%s %s\n", char_set[color[ser[num]]], card_num); value = value + card_value; if ((value > 21) && (visitor_A > 0)) { visitor_A--; value = value - 10; } printf(" Now value is: %d\n", value); visitor_card++; } void show_chips() // a frame to show your balance. { printf(" ╔══════════════════════╗\n"); printf(" ║ CHIPS: $%-4d¢%-4d║\n", dollar, cent); printf(" ╚══════════════════════╝\n"); } void player_set() // ask for information and introduce. { int i; printf(" Please insert your name: "); scanf("%s", &name); printf("\n How much dollars(US$) do you want to charge?\n $"); scanf("%d", &dollar); preplay_money = dollar; system("cls"); say_cro((char *)""); printf("Hello, Sir \033[40;33m%s\033[0m.\n I'm Croupier \033[40;31mJohn\033[0m. Here is your chips of $%d.\n", name, dollar); show_chips(); } void shuffle() // refresh the cards in hands. { int i, t; srand((unsigned)time(NULL)); for (i = 1; i <= 52; i++) pla[i] = 0; for (i = 1; i <= 52; i++) { // ti++; t = rand() % 52 + 1; // printf("checking: %d, pla[%d]=%d\n",t,t,pla[t]); if (pla[t] == 1) { i--; continue; } pla[t] = 1; ser[i] = t; // printf(" PUT IN #%d %d, %s%d\n",i,t,char_set[color[i]],number[i]); } // printf("check times: %d",ti); } void cro_card(int num) { char card_num[3]; int card_value = 0; if ((number[ser[num]] < 11) && (number[ser[num]] > 1)) { itoa(number[ser[num]], card_num, 10); card_value = number[ser[num]]; } else { switch (number[ser[num]]) { case 11: strcpy(card_num, "J"); card_value = 10; break; case 12: strcpy(card_num, "Q"); card_value = 10; break; case 13: strcpy(card_num, "K"); card_value = 10; break; case 1: strcpy(card_num, "A"); card_value = 11; if (cro_value + card_value > 21) { card_value = 1; } else { cro_A++; } break; default: break; } } say_cro((char *)"The #"); printf("%d card I got is :", num - visitor_card); printf("%s %s\n", char_set[color[ser[num]]], card_num); cro_value = cro_value + card_value; if ((cro_value > 21) && (cro_A > 0)) { cro_A--; cro_value = cro_value - 10; } printf(" Now I have %d points.\n", cro_value); } void cro_turn() { int cro_c = 1, i, left, ran; say_cro((char *)"Now it's my turn.\n"); // stop(); while (cro_value < 21) { // printf("%d\n",now+1); // stop(); // check whether he'll get a card. left = 21 - cro_value; if (left > 10) { cro_card(++now); stop(); } else { ran = rand() % 100 + 1; if ((ran <= left * 10) && (cro_value <= value)) { cro_card(++now); stop(); } else { break; } } } say_cro((char *)"Finally I have"); printf(" %d points.\n", cro_value); if (cro_value > 21) printf(" Well, I burst.\n"); } void check_replay(); void replay(); void play() { int i, play_money, first_time = 0, charge_money = 0; if ((dollar != 0) || (cent != 0)) { say_cro((char *)"How much money do you bet in, which should be multiples of 2?\n ¢"); scanf("%d", &play_money); } while ((play_money % 2 != 0) || (play_money > dollar * 100 + cent) || ((dollar == 0) && (cent == 0))) { if ((play_money > dollar * 100 + cent)||((dollar == 0) && (cent == 0))) { say_cro((char *)"You don't have that much, Sir.\n Would you like to charge?\n"); say_cro((char *)"To charge, Press Y. If not, Press N.\n"); //PRESS N NOT EXIT!!! stop(); while ((ch != 'Y') && (ch != 'N') && (ch != 'y') && (ch != 'n')) { stop(); } if ((ch == 'Y') || (ch == 'y')) { say_me((char *)"Charge for me, please.\n"); stop(); say_cro((char *)"How much dollars you like to charge, Sir?\n $"); scanf("%d", &charge_money); dollar = dollar + charge_money; show_chips(); replay(); ch = ' '; } else { say_me((char *)"No, thanks. I think that's enough.\n"); } } else { say_cro((char *)"Bets should be multiples of 2, retry it please.\n ¢"); scanf("%d", &play_money); } } value = 0; for (i = 1; i <= 2; i++) { get_card(i); now = i; } if (value == 21) { say_cro((char *)"Congratulations! You got 21 points at the very beginning!\n"); printf(" You can get bets at 2:3!\n"); play_money = play_money / 2 * 3; first_time = 1; } while (value < 21) { say_cro((char *)"Another, Press A. If not, Press S.\n"); ch = ' '; stop(); while ((ch != 'A') && (ch != 'S') && (ch != 'a') && (ch != 's')) { stop(); } if ((ch == 'A') || (ch == 'a')) { say_me((char *)"Another for me, please.\n"); get_card(++now); ch = ' '; } else { say_me((char *)"No, thanks. I think that's enough.\n"); break; } } if ((value <= 21) && (first_time == 0)) { say_cro((char *)"That's fine. "); printf("You got %d points at last.\n", value); cro_turn(); if ((cro_value > value) && (cro_value <= 21)) { say_cro((char *)"Sorry, Sir. I have bigger points than you.\n"); play_money = -play_money; } else if (cro_value == value) { say_cro((char *)"Well, your bets will be returned.\n"); play_money = 0; } else { say_cro((char *)"You win, Sir!\n"); } } else if (value > 21) { say_cro((char *)"You burst.\n"); play_money = -play_money; } cent = cent + play_money; if (cent < 0) { dollar = dollar - (-cent) / 100; if (cent % 100 != 0) dollar--; cent = (100 - (-cent) % 100) % 100; //-260 } else if (cent > 100) { dollar = dollar + cent / 100; cent = cent % 100; } show_chips(); check_replay(); } void replay() { now = 0; visitor_card = 0; value = 0; cro_value = 0; visitor_A = 0; cro_A = 0; shuffle(); play(); } void check_replay() { say_cro((char *)"Play again, Press Y. If not, Press N.\n"); stop(); while ((ch != 'Y') && (ch != 'N') && (ch != 'y') && (ch != 'n')) { stop(); } if ((ch == 'Y') || (ch == 'y')) { say_me((char *)"Another game, please.\n"); stop(); system("cls"); show_chips(); replay(); ch = ' '; } else { say_me((char *)"No, thanks. I think that's enough.\n"); return; } } int main() { int i, t; init_casino(); player_set(); init_poker(); say_cro((char *)"Shall we start now? If so, press any key.\n"); stop(); say_me((char *)"OK, let's start.\n"); shuffle(); play(); stop(); return 0; }
0 条评论