/*
定义一个字符型数组,里面包含 A 2 3 4 5 6 7 8 9 10 J Q K
每轮靠着随机数当下标来发牌
在定义一个数来当用户接收字符型数组里的东西,同样也接收电脑的
*/
#include <stdio.h>
#include <stdlib.h>
int dianShu (char ch );
void Dian_Yong(char Din[5],char Yon[5],int a ,int b);
int main(int argc, const char * argv[]) {
// 定义一个字符型数组,里面包含 A 2 3 4 5 6 7 8 9 10 J Q K
char erShiYi[]={'A','2','3','4','5','6','7','8','9','0','J','Q','K'};
//定义字符型用户,字符型电脑,
char yongHu[5]={} ,Computer[5]={};
/**实际值用户,实际值电脑.a,b
* ,c,d,分别存储着点数的总和
*/
int yongHu1,Computer1,c = 0,d = 0;
//打印游戏界面
printf("******** 是否开始21点游戏 *******\n");
printf("@****@每人手中最多5张牌,超出不退出不增加@****@\n");
printf("***$$$$ Yes or N o $$$$***\n");
char flag;
flag = getc(stdin);
if(flag == 'Y' || flag == 'y')
{
//进入游戏,用户和电脑开始接收牌面,一人两张
//开始计算牌面的点数
for(int i = 0; i < 2; i ++)
{
yongHu[i] = erShiYi[arc4random_uniform(13)];
Computer[i] = erShiYi[arc4random_uniform(13)];
yongHu1 = dianShu(yongHu[i]);
c += yongHu1;
Computer1 = dianShu(Computer[i]);
d += Computer1;
}
//对电脑的牌进行判断,如果大于18,则开始停止要牌
//如过小与18,则在要一张牌,直到牌面大于18或者等于18
for ( int s = 2 ; ; s ++) {
if (d >= 18) {
printf("电脑停止要牌,请用户快一点\n");
break;
}else{
Computer[s] =erShiYi[arc4random_uniform(13)];
Computer1 = dianShu(Computer[s]);
d = d+Computer1;
}
}
//打印用户的两张牌,开始判断是否要牌
//并且查找为'0'的,转换成10输出
printf("你此刻拥有的牌是:");
for(int m = 0 ;m<2 ;m++){
if (yongHu[m] == '0') {
printf("%d\t",10);
}else{
printf("%c\t",yongHu[m]);
}
}
for (int j = 0; j <10; j++) {
printf("是否继续要牌?1是继续,其余则是放弃\n");
int i =-1;
scanf("%d",&i);//输入1,开始继续发牌
if (i!=1) {
//不等于1,开始和电脑牌面进行比较
if ( c <= 21 && d <=21)
{
if((c==21 && d ==21)||(c==d) )
{
printf("好可惜,平局\n");
Dian_Yong(Computer, yongHu,c,d);
return 0;
}
else if ( (c==21)||(c > d) ) {
printf("恭喜玩家获得胜利\n");
Dian_Yong(Computer, yongHu,c,d);
return 0;
}else if((d==21)||(c < d))
{
printf("很抱歉,你输了\n");
Dian_Yong(Computer, yongHu,c,d);
return 0;
}
}
else if (c > 21 && d >21){
printf("双方同时战死,可悲可弃\n");
Dian_Yong(Computer, yongHu,c,d);
}else if(c > 21 && d<22){
printf("很抱歉,你爆牌了\n");
Dian_Yong(Computer, yongHu,c,d);
return 0;
}else if(c <= 21 && d>21){
printf("恭喜玩家获得胜利,因为电脑爆牌了\n");
Dian_Yong(Computer, yongHu,c,d);
return 0;
}
return 0;
}else{
//等于1,继续发牌,并且计算发的牌面,继续相加
yongHu[j+2] =erShiYi[arc4random_uniform(13)];
printf("你获得一张%c\t",yongHu[j+2]);
printf("你现在所拥有的牌分别是:\n");
for(int m = 0 ;m<5 ;m++){
printf("%c\t",yongHu[m]);
}
yongHu1 = dianShu(yongHu[j+2]);
c += yongHu1;
}
}
}else{//输入No,退出游戏
printf("恭喜你成功退出游戏\n");
return 0;
}
}
//用来计算点数,直接返回点数的值
int dianShu (char ch )
{
int a = 0 ;
switch (ch) {
case 'A':
a = 1;
break;
case '2':
a = 2;
break;
case '3':
a = 3;
break;
case '4':
a = 4;
break;
case '5':
a = 5;
break;
case '6':
a = 6;
break;
case '7':
a = 7;
break;
case '8':
a = 8;
break;
case '9':
a = 9;
break;
case '0':
a = 10;
break;
case 'J':
a = 10;
break;
case 'Q':
a = 10;
break;
case 'K':
a = 10;
break;
default:
break;
}
return a;
}
/**
* 打印牌面
*
* @param Din 电脑手中的牌
* @param Yon 用户手中的牌
*/
void Dian_Yong(char Din[5],char Yon[5],int a,int b){
printf("电脑的牌是:\n");
for(int m = 0 ;m<5 ;m++){
if (Din[m] == '0') {
printf("10\t");
}else{
printf("%c\t",Din[m]);
}
}
printf("电脑点数总和为:%d",b);
printf("\n玩家的牌是:\n");
for(int m = 0 ;m<5 ;m++){
if (Yon[m] == '0') {
printf("%d\t",10);
}else{
printf("%c\t",Yon[m]);
}
}
printf("玩家点数总和为:%d",a);
}
|
|