游戏规则:玩家电脑各发2张牌,玩家两张牌均为明牌,电脑一明一暗;玩家根据点数大小选择是否继续要牌,当玩家点数为21点时玩家直接获胜,超过21点电脑获胜;当玩家选择不要牌,开始电脑回合,电脑点数大于21点玩家获胜,电脑点数大于等于玩家,电脑获胜。
目录
1、打印欢迎界面
int main() { playgame(); return 0; }
void playgame() { int input = 0; do { welcome(); printf("请选择:>"); scanf("%d", &input); switch (input) { case 1: printf("开始游戏\n"); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,请重新选择\n"); break; } } while (input); }
void welcome() { printf("******************************\n"); printf("********** 21点 *************\n"); printf("*** 1.play 0.exit ***\n"); printf("******************************\n"); }
2、打印牌池和初始手牌
(1)添加game()函数
void playgame() { int input = 0; do { ... ... { case 1: printf("开始游戏\n"); game(); break; ... ... } } while (input); }
(2)创建game.h 和 game.c 用于游戏主程序
//text.c
#include "game.h" void game() { int board[ROW][COL] = { 0 }; int PlayerCard[13] = { 0 }; int ComputerCard[13] = { 0 }; initial(board, ROW, COL, PlayerCard, ComputerCard); show(board, ROW, COL); Computer(board, ROW, COL, ComputerCard); Player(board, ROW, COL, PlayerCard); }
//game.h
#define ROW 4 #define COL 13 #include<stdio.h> #include<time.h> #include<Windows.h> void initial(int board[ROW][COL], int row, int col, int PlayerCard[],int ComputerCard[]); void show(int board[ROW][COL], int row, int col); void Computer(int board[ROW][COL], int row, int col,int ComputerCard[]); void Player(int board[ROW][COL], int row, int col, int PlayerCard[]);
//game.c
#include"game.h" void initial(int board[ROW][COL], int row, int col, int PlayerCard[], int ComputerCard[]) { int i = 0; int j = 0; for (i = 0;i < row;i++) { for (j = 0;j < col;j++) { if (j < 9) board[i][j] = j + 1; else board[i][j] = 10; } } for (i = 0;i < 13;i++) { PlayerCard[i] = 0; ComputerCard[i] = 0; } } void show(int board[ROW][COL], int row, int col) { int i = 0; int j = 0; printf("牌池:\n"); for (i = 0;i < row;i++) { for (j = 0;j < col;j++) { printf("%d ", board[i][j]); } printf("\n"); } } int give(int board[ROW][COL], int row, int col) { int card = 0; while (1) { int i =