
#include<conioh>
#include<timeh>
#include<stdioh>
#include<stdlibh>
int jisuan(int);
int comptotal;
char s1[]="A234567890JQK";
//char s2[4][5]={"红桃","黑桃","草花","方块"};
char s2[4]={3,4,5,6};
int poke[52];
int ch;
int win=0;
int computer[5],user[5];
int usertotal;
int users;
int k;
int main()
{ void xipai(int poke[]);
void ai();
int i,j;
////////////////////////////////////////////////////////上面是变量和声明
printf("\n这是简单的廿一点游戏:\n");
for(i=0;i<52;i++)
{
if(i%13==0)putchar('\n');
poke[i]=i;
printf("%c%c%c ",s2[i/13],s1[i%13]=='0''1':' ',s1[i%13]);
}
putchar('\n');
/////////////////////////////////////////////////////////主代码
k=0;
xipai(poke);
while(ch!=27)
{ comptotal=0;
usertotal=0;
if(k>=42)
{
printf("\n剩余牌数不足十张,重新洗牌");
xipai(poke);
k=0;
}
printf("\n\n\n\n\n\n\n\n新局开始:\n");
printf("现在共有牌%2d张\n",52-k);
if(win==0)
{
computer[0]=k++;
user[0]=k++;
printf("\n电脑做庄,要牌:");
ai();
}
else
{
printf("\n玩家做庄,要牌:\n\t回车要牌\n\t空格过牌");
user[0]=k++;
computer[0]=k++;
}
printf("\n玩家开始要牌:\n");
usertotal=jisuan(poke[user[0]]);
printf("%c%c%c 共%2d点\t",s2[poke[user[0]]/13],s1[poke[user[0]]%13]=='0''1':' ',s1[poke[user[0]]%13],usertotal);
users=0;
ch=1;
while(ch!=32&&users<4)
{
ch=getch();
switch(ch)
{
case 27:
goto end;
break;
case 32:
break;
case 13:
user[++users]=k;
usertotal+=jisuan(poke[user[users]]);
printf("\b\b\b\b\b\b\b\b\b%c%c%c 共%2d点\t",s2[poke[k]/13],s1[poke[k]%13]=='0''1':' ',s1[poke[k]%13],usertotal);
k++;
if(usertotal>=21)ch=32;
break;
default:
break;
}
}
if(win==1)
{
printf("\n电脑开始要牌:\n");
ai();
}
printf("\n\n\n玩家的点数是%2d",usertotal);
printf("\n电脑的点数是%2d",comptotal);
printf("\n\n本局结算:");
if(comptotal>21&&usertotal<=21)
{
printf("\n\n电脑爆牌了");
win=1;
printf("\n恭喜,你赢了");
}
if(usertotal>21&&comptotal<=21)
{
printf("\n\n你爆牌了");
printf("\n下次小心点");
win=0;
}
if(usertotal>21&&comptotal>21)
{
printf("\n\n你们两个,怎么都这么不小心啊,都撑死了还要吗");
}
if(usertotal<=21&&comptotal<=21)
{
if(usertotal>comptotal)
{
win=1;
printf("\n\n不错,你赢了");
}
else if(usertotal<comptotal)
{
win=0;
printf("\n\n撑死胆大的,饿死胆小的,没胆子,输了吧");
}
else
printf("\n\n平了,算你走运");
}
getch();
}
end:
return 0;
}
void xipai(int poke[])
{
int y,tmp,i,j;
for(j=0;j<7;j++)
for(i=0;i<52;i++)
{
srand(time(0));
y=rand()%10;
tmp=poke[i];
poke[i]=poke[(yii)%52];
poke[(yii)%52]=tmp;
}
}
///////////////////////////////////////////////子函数
void ai()
{
int i;
comptotal=jisuan(poke[computer[0]]);
printf("\n%c%c%c 共%2d点\t",s2[poke[computer[0]]/13],s1[poke[computer[0]]%13]=='0''1':' ',s1[poke[computer[0]]%13],comptotal);
for(i=0;i<4;i++)
{
if(comptotal<17)
{
computer[i+1]=k++;
comptotal+=jisuan(poke[computer[i+1]]);
printf("\b\b\b\b\b\b\b\b\b%c%c%c 共%2d点\t",s2[poke[computer[i+1]]/13],s1[poke[computer[i+1]]%13]=='0''1':' ',s1[poke[computer[i+1]]%13],comptotal);
}
}
}
int jisuan(int i)
{int dian;
switch(i%13)
{
case 0:
case 10:
case 11:
case 12:
dian=1;
break;
default:
dian=i%13+1;
}
return dian;
}
#include <graphicsh>
#include <conioh>
#include <timeh>
/////////////////////////////////////////////
// 定义常量、枚举量、结构体、全局变量
/////////////////////////////////////////////
#define WIDTH 10 // 游戏区宽度
#define HEIGHT 22 // 游戏区高度
#define SIZE 20 // 每个游戏区单位的实际像素
// 定义 *** 作类型
enum CMD
{
CMD_ROTATE, // 方块旋转
CMD_LEFT, CMD_RIGHT, CMD_DOWN, // 方块左、右、下移动
CMD_SINK, // 方块沉底
CMD_QUIT // 退出游戏
};
// 定义绘制方块的方法
enum DRAW
{
SHOW, // 显示方块
HIDE, // 隐藏方块
FIX // 固定方块
};
// 定义七种俄罗斯方块
struct BLOCK
{
WORD dir[4]; // 方块的四个旋转状态
COLORREF color; // 方块的颜色
} g_Blocks[7] = { {0x0F00, 0x4444, 0x0F00, 0x4444, RED}, // I
{0x0660, 0x0660, 0x0660, 0x0660, BLUE}, // 口
{0x4460, 0x02E0, 0x0622, 0x0740, MAGENTA}, // L
{0x2260, 0x0E20, 0x0644, 0x0470, YELLOW}, // 反L
{0x0C60, 0x2640, 0x0C60, 0x2640, CYAN}, // Z
{0x0360, 0x4620, 0x0360, 0x4620, GREEN}, // 反Z
{0x4E00, 0x4C40, 0x0E40, 0x4640, BROWN}}; // T
// 定义当前方块、下一个方块的信息
struct BLOCKINFO
{
byte id; // 方块 ID
char x, y; // 方块在游戏区中的坐标
byte dir:2; // 方向
} g_CurBlock, g_NextBlock;
// 定义游戏区
BYTE g_World[WIDTH][HEIGHT] = {0};
/////////////////////////////////////////////
// 函数声明
/////////////////////////////////////////////
void Init(); // 初始化游戏
void Quit(); // 退出游戏
void NewGame(); // 开始新游戏
void GameOver(); // 结束游戏
CMD GetCmd(); // 获取控制命令
void DispatchCmd(CMD _cmd); // 分发控制命令
void NewBlock(); // 生成新的方块
bool CheckBlock(BLOCKINFO _block); // 检测指定方块是否可以放下
void DrawBlock(BLOCKINFO _block, DRAW _draw = SHOW); // 画方块
void OnRotate(); // 旋转方块
void OnLeft(); // 左移方块
void OnRight(); // 右移方块
void OnDown(); // 下移方块
void OnSink(); // 沉底方块
/////////////////////////////////////////////
// 函数定义
/////////////////////////////////////////////
// 主函数
void main()
{
Init();
CMD c;
while(true)
{
c = GetCmd();
DispatchCmd(c);
// 按退出时,显示对话框咨询用户是否退出
if (c == CMD_QUIT)
{
HWND wnd = GetHWnd();
if (MessageBox(wnd, _T("您要退出游戏吗?"), _T("提醒"), MB_OKCANCEL | MB_ICONQUESTION) == IDOK)
Quit();
}
}
}
// 初始化游戏
void Init()
{
initgraph(640, 480);
srand((unsigned)time(NULL));
// 显示 *** 作说明
setfont(14, 0, _T("宋体"));
outtextxy(20, 330, _T(" *** 作说明"));
outtextxy(20, 350, _T("上:旋转"));
outtextxy(20, 370, _T("左:左移"));
outtextxy(20, 390, _T("右:右移"));
outtextxy(20, 410, _T("下:下移"));
outtextxy(20, 430, _T("空格:沉底"));
outtextxy(20, 450, _T("ESC:退出"));
// 设置坐标原点
setorigin(220, 20);
// 绘制游戏区边界
rectangle(-1, -1, WIDTH SIZE, HEIGHT SIZE);
rectangle((WIDTH + 1) SIZE - 1, -1, (WIDTH + 5) SIZE, 4 SIZE);
// 开始新游戏
NewGame();
}
// 退出游戏
void Quit()
{
closegraph();
exit(0);
}
// 开始新游戏
void NewGame()
{
// 清空游戏区
setfillstyle(BLACK);
bar(0, 0, WIDTH SIZE - 1, HEIGHT SIZE - 1);
ZeroMemory(g_World, WIDTH HEIGHT);
// 生成下一个方块
g_NextBlockid = rand() % 7;
g_NextBlockdir = rand() % 4;
g_NextBlockx = WIDTH + 1;
g_NextBlocky = HEIGHT - 1;
// 获取新方块
NewBlock();
}
// 结束游戏
void GameOver()
{
HWND wnd = GetHWnd();
if (MessageBox(wnd, _T("游戏结束。\n您想重新来一局吗?"), _T("游戏结束"), MB_YESNO | MB_ICONQUESTION) == IDYES)
NewGame();
else
Quit();
}
// 获取控制命令
DWORD m_oldtime;
CMD GetCmd()
{
// 获取控制值
while(true)
{
// 如果超时,自动下落一格
DWORD newtime = GetTickCount();
if (newtime - m_oldtime >= 500)
{
m_oldtime = newtime;
return CMD_DOWN;
}
// 如果有按键,返回按键对应的功能
if (kbhit())
{
switch(getch())
{
case 'w':
case 'W': return CMD_ROTATE;
case 'a':
case 'A': return CMD_LEFT;
case 'd':
case 'D': return CMD_RIGHT;
case 's':
case 'S': return CMD_DOWN;
case 27: return CMD_QUIT;
case ' ': return CMD_SINK;
case 0:
case 0xE0:
switch(getch())
{
case 72: return CMD_ROTATE;
case 75: return CMD_LEFT;
case 77: return CMD_RIGHT;
case 80: return CMD_DOWN;
}
}
}
// 延时 (降低 CPU 占用率)
Sleep(20);
}
}
// 分发控制命令
void DispatchCmd(CMD _cmd)
{
switch(_cmd)
{
case CMD_ROTATE: OnRotate(); break;
case CMD_LEFT: OnLeft(); break;
case CMD_RIGHT: OnRight(); break;
case CMD_DOWN: OnDown(); break;
case CMD_SINK: OnSink(); break;
case CMD_QUIT: break;
}
}
// 生成新的方块
void NewBlock()
{
g_CurBlockid = g_NextBlockid, g_NextBlockid = rand() % 7;
g_CurBlockdir = g_NextBlockdir, g_NextBlockdir = rand() % 4;
g_CurBlockx = (WIDTH - 4) / 2;
g_CurBlocky = HEIGHT + 2;
// 下移新方块直到有局部显示
WORD c = g_Blocks[g_CurBlockid]dir[g_CurBlockdir];
while((c & 0xF) == 0)
{
g_CurBlocky--;
c >>= 4;
}
// 绘制新方块
DrawBlock(g_CurBlock);
// 绘制下一个方块
setfillstyle(BLACK);
bar((WIDTH + 1) SIZE, 0, (WIDTH + 5) SIZE - 1, 4 SIZE - 1);
DrawBlock(g_NextBlock);
// 设置计时器,用于判断自动下落
m_oldtime = GetTickCount();
}
// 画方块
void DrawBlock(BLOCKINFO _block, DRAW _draw)
{
WORD b = g_Blocks[_blockid]dir[_blockdir];
int x, y;
int color = BLACK;
switch(_draw)
{
case SHOW: color = g_Blocks[_blockid]color; break;
case HIDE: color = BLACK; break;
case FIX: color = g_Blocks[_blockid]color / 3; break;
}
setfillstyle(color);
for(int i=0; i<16; i++)
{
if (b & 0x8000)
{
x = _blockx + i % 4;
y = _blocky - i / 4;
if (y < HEIGHT)
{
if (_draw != HIDE)
bar3d(x SIZE + 2, (HEIGHT - y - 1) SIZE + 2, (x + 1) SIZE - 4, (HEIGHT - y) SIZE - 4, 3, true);
else
bar(x SIZE, (HEIGHT - y - 1) SIZE, (x + 1) SIZE - 1, (HEIGHT - y) SIZE - 1);
}
}
b <<= 1;
}
}
// 检测指定方块是否可以放下
bool CheckBlock(BLOCKINFO _block)
{
WORD b = g_Blocks[_blockid]dir[_blockdir];
int x, y;
for(int i=0; i<16; i++)
{
if (b & 0x8000)
{
x = _blockx + i % 4;
y = _blocky - i / 4;
if ((x < 0) || (x >= WIDTH) || (y < 0))
return false;
if ((y < HEIGHT) && (g_World[x][y]))
return false;
}
b <<= 1;
}
return true;
}
// 旋转方块
void OnRotate()
{
// 获取可以旋转的 x 偏移量
int dx;
BLOCKINFO tmp = g_CurBlock;
tmpdir++; if (CheckBlock(tmp)) { dx = 0; goto rotate; }
tmpx = g_CurBlockx - 1; if (CheckBlock(tmp)) { dx = -1; goto rotate; }
tmpx = g_CurBlockx + 1; if (CheckBlock(tmp)) { dx = 1; goto rotate; }
tmpx = g_CurBlockx - 2; if (CheckBlock(tmp)) { dx = -2; goto rotate; }
tmpx = g_CurBlockx + 2; if (CheckBlock(tmp)) { dx = 2; goto rotate; }
return;
rotate:
// 旋转
DrawBlock(g_CurBlock, HIDE);
g_CurBlockdir++;
g_CurBlockx += dx;
DrawBlock(g_CurBlock);
}
// 左移方块
void OnLeft()
{
BLOCKINFO tmp = g_CurBlock;
tmpx--;
if (CheckBlock(tmp))
{
DrawBlock(g_CurBlock, HIDE);
g_CurBlockx--;
DrawBlock(g_CurBlock);
}
}
// 右移方块
void OnRight()
{
BLOCKINFO tmp = g_CurBlock;
tmpx++;
if (CheckBlock(tmp))
{
DrawBlock(g_CurBlock, HIDE);
g_CurBlockx++;
DrawBlock(g_CurBlock);
}
}
// 下移方块
void OnDown()
{
BLOCKINFO tmp = g_CurBlock;
tmpy--;
if (CheckBlock(tmp))
{
DrawBlock(g_CurBlock, HIDE);
g_CurBlocky--;
DrawBlock(g_CurBlock);
}
else
OnSink(); // 不可下移时,执行“沉底方块” *** 作
}
// 沉底方块
void OnSink()
{
int i, x, y;
// 连续下移方块
DrawBlock(g_CurBlock, HIDE);
BLOCKINFO tmp = g_CurBlock;
tmpy--;
while (CheckBlock(tmp))
{
g_CurBlocky--;
tmpy--;
}
DrawBlock(g_CurBlock, FIX);
// 固定方块在游戏区
WORD b = g_Blocks[g_CurBlockid]dir[g_CurBlockdir];
for(i = 0; i < 16; i++)
{
if (b & 0x8000)
{
if (g_CurBlocky - i / 4 >= HEIGHT)
{ // 如果方块的固定位置超出高度,结束游戏
GameOver();
return;
}
else
g_World[g_CurBlockx + i % 4][g_CurBlocky - i / 4] = 1;
}
b <<= 1;
}
// 检查是否需要消掉行,并标记
int row[4] = {0};
bool bRow = false;
for(y = g_CurBlocky; y >= max(g_CurBlocky - 3, 0); y--)
{
i = 0;
for(x = 0; x < WIDTH; x++)
if (g_World[x][y] == 1)
i++;
if (i == WIDTH)
{
bRow = true;
row[g_CurBlocky - y] = 1;
setfillstyle(WHITE, DIAGCROSS2_FILL);
bar(0, (HEIGHT - y - 1) SIZE + SIZE / 2 - 2, WIDTH SIZE - 1, (HEIGHT - y - 1) SIZE + SIZE / 2 + 2);
}
}
if (bRow)
{
// 延时 200 毫秒
Sleep(200);
// 擦掉刚才标记的行
IMAGE img;
for(i = 0; i < 4; i++)
{
if (row[i])
{
for(y = g_CurBlocky - i + 1; y < HEIGHT; y++)
for(x = 0; x < WIDTH; x++)
{
g_World[x][y - 1] = g_World[x][y];
g_World[x][y] = 0;
}
getimage(&img, 0, 0, WIDTH SIZE, (HEIGHT - (g_CurBlocky - i + 1)) SIZE);
putimage(0, SIZE, &img);
}
}
}
// 产生新方块
NewBlock();
}
#include
#include
//用到了rand函数,所以要有这个头文件
#include
//用到了time函数,所以要有这个头文件
int
main()
{
int
number;
//number用于存储随机数
int
guess=0;
//guess用于存储玩家猜的数
srand((unsigned)
time(null));//用系统时间作为rand函数使用的种子
number=rand()%100;
//随机除以100,取余数
number++;
//余数加1
printf("猜数字游戏\n");
printf("该数字在1到100之间\n");
while(guess!=number)
{
printf("请输入您所猜的数:");
scanf("%d",&guess);
//如果玩家猜的数较小,给予提示
if
(guess
number)
{
printf("大了\n");
}
}
//猜中则循环结束,输出猜中的数字
printf("猜对了,这个数字就是:%d\n",number);
return
0;
}
以上就是关于C语言程序设计课程设计扑克牌游戏,怎么做全部的内容,包括:C语言程序设计课程设计扑克牌游戏,怎么做、求一个用C语言编写的小游戏代码、C语言编程 猜数游戏等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)