Programming exercises, self-made 21-point game

Source: Internet
Author: User

#include <stdio.h> #include <time.h> #include <stdlib.h>int N; Card sequence int integral_player; Player Integral int integral_computer; Computer Integral int card[52]= { 102,103,104,105,106,107,108,109,110,111,112,113,114,202,203,204,205,206,207,208,209,210,211,212,213,214, 302 , 303,304,305,306,307,308,309,310,311,312,313,314,402,403,404,405,406,407,408,409,410,411,412,413,414,};int p layer1[10]= {0,};int player2[10]= {0,};int player1_card=0;int player2_card=0;void show_cards (int i); token void riffle (); Shuffle void Send_cards (); licensing void Add_player (); Player to sign void Add_computer (); Computer to card void Cal_point (); Number of calculated points void integral_read (); Integral read, showing void Integral_write ();    The integral is written to int main () {char choose; Integral_read ();        Integral read do {//printf ("Shuffle Test \ n"); Riffle (); Shuffle Send_cards (); Licensing Add_player (); Players want card add_computer (); Computer to card cal_point (); Compute points integral_write (); The integral is written to printf ("******************* SUCCESS *****************\ n ");        printf ("Press N to exit, other keys continue ... \ n");        Fflush (stdin);        Choose=getchar ();    printf ("\ n");    } while (Choose! = ' n ' &&choose!= ' n '); return 0;}    void Riffle () {int a,t,i;    Srand (Time (0));        /*for (i=0; i<52; i++) {if (i%13==0) {printf ("\ n");    } show_cards (i);    } printf ("\ n"); printf ("The above is the normal card sequence, followed by the shuffle after the card sequence \ n");        Shuffle Test */for (i=0; i<52; i++) {A=rand ()%52;        T=card[i];        Card[i]=card[a];    card[a]=t; } n=0; Card sequence reset; player1_card=0; Player card sequence reset; player2_card=0;        Computer card sequence reset;/*for (i=0; i<52; i++)//Shuffle Test {if (i%13==0) {printf ("\ n");    } show_cards (i);    } printf ("\ n"); */}void send_cards () {int J;    printf ("Player takes the Card:");        for (j=0; j<2; J + +) {show_cards (N);        Player1[player1_card]=card[n];        player1_card++;    n++;    } printf ("\ n");    printf ("Computer Take Card:"); for (j=0; j< 2;        J + +) {show_cards (N);        Player2[player2_card]=card[n];        player2_card++;    n++; } printf ("\ n");}    void show_cards (int i) {int choose;    choose=card[i]/100;            Switch (choose) {case 1:if (card[i]%100==14) {printf ("sa\t");        Break            } else if (card[i]%100==13) {printf ("sk\t");        Break            } else if (card[i]%100==12) {printf ("sq\t");        Break            } else if (card[i]%100==11) {printf ("sj\t");        Break            } else {printf ("s%d\t", card[i]%100);        Break            } Case 2:if (card[i]%100==14) {printf ("da\t");        Break            } else if (card[i]%100==13) {printf ("dk\t");        Break            } else if (card[n]%100==12) {printf ("dq\t");        Break } ElSe if (card[i]%100==11) {printf ("dj\t");        Break            } else {printf ("d%d\t", card[i]%100);        Break            } Case 3:if (card[i]%100==14) {printf ("ca\t");        Break            } else if (card[i]%100==13) {printf ("ck\t");        Break            } else if (card[i]%100==12) {printf ("cq\t");        Break            } else if (card[i]%100==11) {printf ("cj\t");        Break            } else {printf ("c%d\t", card[i]%100);        Break            } Case 4:if (card[n]%100==14) {printf ("ha\t");        Break            } else if (card[n]%100==13) {printf ("hk\t");        Break            } else if (card[n]%100==12) {printf ("hq\t");        Break            } else if (card[i]%100==11) {printf ("hj\t");        Break            } else {printf ("h%d\t", card[i]%100);        Break    }}}void Add_player () {int choose;        Do {printf ("Players: 1. Card 2. Do not \040");        scanf ("%d", &choose);            Switch (choose) {case 1:show_cards (N);            Player1[player1_card]=card[n];            player1_card++;            n++;            printf ("\ n");        Continue            Case 2:printf ("no \ n");        Return }} while (1);}    void Add_computer () {int i;        while (1) {int choose=2;        int point=0;        for (i=0; i<player2_card; i++) {point+=player2[i]%100;        } if (point<=18) {choose=1;        } printf ("Computer: 1. Card 2. Do not \ \");            Switch (choose) {case 1:printf ("to a single \ n");            Show_cards (N);            Player2[player2_card]=card[n];         player2_card++;   n++;            printf ("\ n");        Continue            Case 2:printf ("no \ n");        Return    }}}void Cal_point () {int winner1;    int winner2;    int i;    int point1;    int Point2;    int point;    point1=0;    point2=0;        for (i=0; i<player1_card; i++) {point=player1[i]%100;            Switch (point) {case 14:point=11;        Break            Case 13:point=10;        Break            Case 12:point=10;        Break            Case 11:point=10;        Break            Case 10:point=10;;        Break            default:point=player1[i]%100;        Break    } Point1+=point;        } for (i=0; i<player2_card; i++) {point=player2[i]%100;            Switch (point) {case 14:point=11;        Break            Case 13:point=10;        Break            Case 12:point=10; BReak;            Case 11:point=10;        Break            Case 10:point=10;;        Break            default:point=player2[i]%100;        Break    } Point2+=point;    } printf ("Player's points:%d\t", point1);    printf ("Computer points:%d\n\n", Point2);        if (point1>21) {printf ("The player explodes \ n \ nthe");    winner1=0;    } else winner1=1;        if (point2>21) {printf ("computer exploded \ n \ nthe");    winner2=0;    } else winner2=1;        if (winner1>winner2) {printf ("player wins, get 1 points \ n");        integral_player++;    Return        } else if ((winner1=winner2==1)) {if (Point1>point2) {printf ("player wins, get 1 points \ n");        integral_player++;        Return        } else{printf ("Computer wins, get 1 points \ n");        integral_computer++;        Return        }} else if ((winner1=winner2==0)) {printf ("Computer wins, get 1 points \ n");        integral_computer++;    Return     } else if (Winner1<winner2) {   printf ("Computer wins, get 1 points \ n");        integral_computer++;        Return } else return;}    void Integral_read () {FILE *fp;        if ((Fp=fopen ("Integral.txt", "R")) ==null) {printf ("Integral file cannot open!");    Exit (0); } fscanf (FP, "%d%d", &integral_player,&integral_computer);    Read-in Integration fclose (FP); printf ("The player%d wins%d defeat \ n", Integral_player,integral_computer);}    void Integral_write () {FILE *p;        if ((P=fopen ("Integral.txt", "W")) ==null) {printf ("Integral.txt file cannot open!");    Exit (0);    } fprintf (P, "%d%d", integral_player,integral_computer); Fclose (P);}







There's a place that's not perfect.

such as a can only as 11 use can not see the situation do 1 calculation

If 2 cards are the same, not a leopard.


Continue to improve next time



Programming exercises, self-made 21-point game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.