Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C language to realize card games

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use C language to achieve card games, many people may not know much about it. In order to make you understand better, the editor summed up the following content for you. I hope you can get something according to this article.

1. Basic requirements

A deck of playing cards without flower cards (J, Q, K, A, big and small king). Two people play a card game, one of which is the user and the other is the computer.

Each person deals five cards in each round, and each person uses these five cards to build a binary sorting tree.

Users play first and take turns to play cards, only one card at a time and bigger than others, for example, the user plays 3, the computer plays a card larger than 3, and if not, you can't choose.

The first person to finish wins.

two。 Running interface

1. First page

two。 Game description

3. Start the game.

4. Start playing cards.

5. Game over

3. Code interpretation # include#include#include#includeint card [5] [2]; / / Mark the cards already played by the player and the user int playercard [5]; / / player's hand int computercard [5]; / / computer's hand char bhuase [5]; char chuase [5]; typedef struct node {int data; struct node * LChild; struct node * RChild;} node;typedef struct tree {int data Struct node * LChild; struct node * RChild;} tree;// game description void PlayingCardsyxsm () {printf ("each person deals five cards per round, each with five cards to build a binary tree, and the user plays first and takes turns to play,\ n"); printf ("can only play one card at a time and be bigger than others,\ n") Printf (for example, the user gives 3, the computer has to calculate the cards larger than 3, and if there is no card, it cannot be selected. \ n "); printf (" the first person to finish wins. \ n ");} / / license void PlayingCardsfp () {int player; int computer; / / players and computers each deal five cards for (int I = 0; I)

< 5; i ++ ){ player = rand() % 9 + 2; printf("玩家发到的牌:%d \n", player); playercard[i] = player; computer = rand() % 9 + 2; printf("电脑发到的牌:%d \n", computer); computercard[i] = computer; }}// 出牌 void PlayingCardscp(){ int player = 0; // 玩家当前回合出的牌 int computer = 0; // 电脑当前回合出的牌 int playercount = 0; // 玩家的出牌数 int computercount = 0; // 电脑的出牌数 bool flag = false; // 当每次都出现最大值时,游戏同样最多进行5回合 for(int m = 0; m < 5; m ++ ) { // 在双方都有牌出的时候,一共最多进行5回合出牌 for(int k = 0; k < 5; k ++ ) { for(int j = 0; j < 5; j ++ ) { // 确定当前牌可以出,大于对方的出牌,且自己没有出过这张牌 if(playercard[j] >

Computer & & card [j] [0] = 0) {printf ("player play% c% d\ n", bhuase [j], playercard [j]); playercount + + / / the number of cards played by the player + 1 / / indicates that the player has already played this card. Card [j] [0] = 1; player = playercard [j]; Sleep / / in the header function # include, it acts as a hibernation program, break }} / / if the player has played five cards, the player wins Exit loop if (playercount = = 5) {printf ("player wins!") ; flag = true; break;} for (int j = 0; j

< 5; j ++ ) { // 确定当前牌可以出,大于对方的出牌,且自己没有出过这张牌 if(computercard[j] >

Player & & card [j] [1] = 0) {printf ("computer license% c% d\ n", chuase [j], computercard [j]); computercount + + / / the number of cards sold by the computer + 1 / / Mark that the computer has already played this card card [j] [1] = 1; computer = computercard [j]; Sleep Break } / / if the computer has played five cards, the computer wins Exit loop if (computercount = = 5) {printf ("computer wins!") ; flag = true; break }} / / if the player's card is larger than the computer's largest card, reset the computer's current round card value to 0 and proceed to the next round of if (player > computer) computer = 0 / / if the computer card is larger than the player's largest card, reset the player's current round card value to 0 and proceed to the next round if (computer > player) player = 0; if (flag) break }} / / the player's hand void PlayingCardsxswj () {printf ("player\ n") after dealing; printf ("% d% d\ n", playercard [0], playercard [1], playercard [2], playercard [3], playercard [4], playercard [5]) } / / computer hand void PlayingCardsxsdn () {printf ("computer\ n"); printf ("% d% d\ n", computercard [0], computercard [1], computercard [2], computercard [3], computercard [4], computercard [5]);} / / initialization tree with empty void treechushihua (node * t) {t = NULL } / / establish a balanced binary tree node* treecharu (node* t, int key) {/ / if the header node is empty, set the current node to the root node if (t = = NULL) {node* p; p = (node*) malloc (sizeof (node)); p-> data = key; p-> LChild = NULL P-> RChild = NULL; t = p;} / / if the header node is not empty, then insert the balanced binary tree else {/ / the value of the insertion node is less than the root node, then insert the left subtree if (key

< t->

Data) t-> LChild = treecharu (t-> LChild, key); / / if the value of the insertion node is greater than or equal to the root node, insert the right subtree else t-> RChild = treecharu (t-> RChild, key);} return t } / / store the player's hand in the balanced binary tree node * jianlib (node * t) {int I, key; for (I = 0; I)

< 5; i ++ ) { key = playercard[i]; t = treecharu(t, key); } return t;} // 将电脑手牌存储到平衡二叉树中 node *jianlic(node *t) { int i, key; for(i = 0; i < 5; i ++ ) { key = computercard[i]; t = treecharu(t, key); } return t;} // 顺序输出玩家或电脑手牌 void treepaixu1(node *t) { if(t != NULL) { treepaixu1(t->

LChild); printf ("% d", t-> data); treepaixu1 (t-> RChild);}} / / preorder traversal of the player's balanced binary tree void treepaixu2 (node * t, int * p) {if (t = = NULL) return Else {/ / preorder traversal to order the player's hand treepaixu2 (t-> LChild, p); playercard [(* p) + +] = t-> data; treepaixu2 (t-> RChild, p) }} / / preorder traversal computer balanced binary tree void treepaixu3 (node * t, int * p) {if (t = = NULL) return; else {/ / preorder traversal, treepaixu3 (t-> LChild, p); computercard [(* p) + +] = t-> data Treepaixu3 (t-> RChild, p);}} / / main function int main () {int k = 0; / / Random function, random number seed is obtained by time seed to obtain random number srand ((unsigned) time (NULL)); int n = 0 / / Select menu while (k! =-1) {puts ("); puts ("); puts (""); printf ("\ t\ t\ tcards games * card games *\ n"); printf ("\ t\ t\ tcards games 1. Game description: *\ n "); printf ("\ t\ t\ tgame explanation 2. " Start the game *\ n "); printf ("\ t\ t\ tstarting games 3. " Start playing cards *\ n "); printf ("\ t\ t\ tplaying cards 4. Game ends *\ n "); printf ("\ t\ t\ t*\ n "); puts ("); printf ("\ t\ t *\ n ") Printf ("\ t\ t *\ n"); puts (""); printf ("\ t\ t Please enter (1, 2, 3, 4):\ n"); scanf ("% d", & k) Switch (k) {/ / Game description case 1: PlayingCardsyxsm (); break; / / licensing stage case 2: {/ / licensing PlayingCardsfp () / / create the player's binary tree node * T1 = NULL; T1 = jianlib (T1); printf ("player's hand is:"); treepaixu1 (T1) / / create a computer binary tree node * T2 = NULL; T2 = jianlic (T2); puts ("") Printf ("computer hand:"); treepaixu1 (T2); / / player hand ordering n = 0 Treepaixu2 (T1, & n); / / computer hand ordering n = 0; treepaixu3 (T2, & n) Puts ("); / / output player's and computer's hand PlayingCardsxswj (); PlayingCardsxsdn (); break } / / case 3: {PlayingCardscp (); break;} / / quit the game case 4:k=-1 Break;}} return 0;} after reading the above, do you have any further understanding of how to implement card games in C language? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report