当前位置:首页 » 编程语言 » c语言小程序游戏

c语言小程序游戏

发布时间: 2025-08-17 18:23:09

1. 扑克牌问题(c语言

其实这是典型的利用链表求解的问题,但是此题用链表的话还需要应用一个长度为13的数组做辅助,所以我直接用数组来进行演示,将在手中的牌进行标记,将放到桌子上的牌进行赋值。
我在很小的时候我的姥爷就曾给我表演过这个魔术,当时我自己用扑克牌弄了将近两个小时才终于知道了这13张牌的顺序是什么,有纤姿兴趣的话你可以自己试一试,很有意思。
下面的代码你可以进行调试来帮助理解整个过程
#include <stdio.h>
void main()
{
int poker[13];//十三张扑克牌
for(int i = 0;i<13;i++)//初始化
poker[i] = 0;//0代表牌还在手中,不为0代表放到了桌子上
int remain = 13;//手中剩余的扑克数
int j = 12;//从最下边开始抽牌
int order = 1;//当order为2时,将此时的牌放到桌子上
int number = 1;//从A开始
while(remain != 0)
{
if(j == -1)
{
j = 12;//返回最后一张牌
continue;
}
if(poker[j] != 0)//如果牌已不在手中,则到下一张牌
{
j--;
continue;
}
if(order == 2)
{
poker[j]= number++;//将扑克赋值
remain--;//手中剩余的牌数减1
order = 1;
continue;
}
order++;
j--;
}
//输出一开始小明手中扑克牌的顺序
for(i = 0; i < 13;i++)
printf("%d ",poker[i]);
}
想要写出一个程序重点在于理解这个过程,只有对这个过程熟悉了旁竖庆才有可能把运握它用代码来演示出来。

2. 会C语言的朋友帮个忙啊 帮我编个小程序

这些源码我都有啊,差不多吧。先贴2个吧,我是在网上下的,
贪食蛇
#define N 200
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;/*得分*/
int gamespeed=50000;/*游戏速度自己调整*/
struct Food
{
int x;/*食物的横坐标*/
int y;/*食物的纵坐标*/
int yes;/*判断是否要出现食物的变量*/
}food;/*食物的结构体*/
struct Snake
{
int x[N];
int y[N];
int node;/*蛇的节数*/
int direction;/*蛇移动方向*/
int life;/* 蛇的生命,0活着,1死亡*/
}snake;
void Init(void);/*图形驱动*/
void Close(void);/*图形结束*/
void DrawK(void);/*开始画面*/
void GameOver(void);/*结束游戏*/
void GamePlay(void);/*玩游戏具体过程*/
void PrScore(void);/*输出成绩*/
/*主函数*/
void main(void)
{
Init();/*图形驱动*/
DrawK();/*开始画面*/
GamePlay();/*玩游戏具体过程*/
Close();/*图形结束*/
}
/*图形驱动*/
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/
void DrawK(void)
{
/*setbkcolor(LIGHTGREEN);*/
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/
for(i=50;i<=600;i+=10)/*画围墙*/
{
rectangle(i,40,i+10,49); /*上边*/
rectangle(i,451,i+10,460);/*下边*/
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10); /*左边*/
rectangle(601,i,610,i+10);/*右边*/
}
}
/*玩游戏具体过程*/
void GamePlay(void)
{
randomize();/*随机数发生器*/
food.yes=1;/*1表示需要出现新食物,0表示已经存在食物*/
snake.life=0;/*活着*/
snake.direction=1;/*方向往右*/
snake.x[0]=100;snake.y[0]=100;/*蛇头*/
snake.x[1]=110;snake.y[1]=100;
snake.node=2;/*节数*/
PrScore();/*输出得分*/
while(1)/*可以重复玩游戏,压ESC键结束*/
{
while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/
{
if(food.yes==1)/*需要出现新食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60;
while(food.x%10!=0)/*食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到*/
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;/*画面上有食物了*/
}
if(food.yes==0)/*画面上有食物了就要显示*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/
switch(snake.direction)
{
case 1:snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
for(i=3;i<snake.node;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();/*显示失败*/
snake.life=1;
break;
}
}
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||
snake.y[0]>455)/*蛇是否撞到墙壁*/
{
GameOver();/*本次游戏结束*/
snake.life=1; /*蛇死*/
}
if(snake.life==1)/*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y)/*吃到食物以后*/
{
setcolor(0);/*把画面上的食物东西去掉*/
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/
snake.node++;/*蛇的身体长一节*/
food.yes=1;/*画面上需要出现新的食物*/
score+=10;
PrScore();/*输出新得分*/
}
setcolor(4);/*画出蛇*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,
snake.y[i]-10);
delay(gamespeed);
setcolor(0);/*用黑色去除蛇的的最后一节*/
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
} /*endwhile(!kbhit)*/
if(snake.life==1)/*如果蛇死就跳出循环*/
break;
key=bioskey(0);/*接收按键*/
if(key==ESC)/*按ESC键退出*/
break;
else
if(key==UP&&snake.direction!=4)
/*判断是否往相反的方向移动*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
/*游戏结束*/
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
/*输出成绩*/
void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,20,str);
}
/*图形结束*/
void Close(void)
{
getch();
closegraph();
}

计算器
#include <dos.h> /*DOS接口函数*/
#include <math.h> /*数学函数的定义*/
#include <conio.h> /*屏幕操作函数*/
#include <stdio.h> /*I/O函数*/
#include <stdlib.h> /*库函数*/
#include <stdarg.h> /*变量长度参数表*/
#include <graphics.h> /*图形函数*/
#include <string.h> /*字符串函数*/
#include <ctype.h> /*字符操作函数*/
#define UP 0x48 /*光标上移键*/
#define DOWN 0x50 /*光标下移键*/
#define LEFT 0x4b /*光标左移键*/
#define RIGHT 0x4d /*光标右移键*/
#define ENTER 0x0d /*回车键*/
void *rar; /*全局变量,保存光标图象*/
struct palettetype palette; /*使用调色板信息*/
int GraphDriver; /* 图形设备驱动*/
int GraphMode; /* 图形模式值*/
int ErrorCode; /* 错误代码*/
int MaxColors; /* 可用颜色的最大数值*/
int MaxX, MaxY; /* 屏幕的最大分辨率*/
double AspectRatio; /* 屏幕的像素比*/
void drawboder(void); /*画边框函数*/
void initialize(void); /*初始化函数*/
void computer(void); /*计算器计算函数*/
void changetextstyle(int font, int direction, int charsize); /*改变文本样式函数*/
void mwindow(char *header); /*窗口函数*/
int specialkey(void) ; /*获取特殊键函数*/
int arrow(); /*设置箭头光标函数*/
/*主函数*/
int main()
{
initialize();/* 设置系统进入图形模式 */
computer(); /*运行计算器 */
closegraph();/*系统关闭图形模式返回文本模式*/
return(0); /*结束程序*/
}
/* 设置系统进入图形模式 */
void initialize(void)
{
int xasp, yasp; /* 用于读x和y方向纵横比*/
GraphDriver = DETECT; /* 自动检测显示器*/
initgraph( &GraphDriver, &GraphMode, "" );
/*初始化图形系统*/
ErrorCode = graphresult(); /*读初始化结果*/
if( ErrorCode != grOk ) /*如果初始化时出现错误*/
{
printf("Graphics System Error: %s\n",
grapherrormsg( ErrorCode ) ); /*显示错误代码*/
exit( 1 ); /*退出*/
}
getpalette( &palette ); /* 读面板信息*/
MaxColors = getmaxcolor() + 1; /* 读取颜色的最大值*/
MaxX = getmaxx(); /* 读屏幕尺寸 */
MaxY = getmaxy(); /* 读屏幕尺寸 */
getaspectratio( &xasp, &yasp ); /* 拷贝纵横比到变量中*/
AspectRatio = (double)xasp/(double)yasp;/* 计算纵横比值*/
}
/*计算器函数*/
void computer(void)
{
struct viewporttype vp; /*定义视口类型变量*/
int color, height, width;
int x, y,x0,y0, i, j,v,m,n,act,flag=1;
float num1=0,num2=0,result; /*操作数和计算结果变量*/
char cnum[5],str2[20]={""},c,temp[20]={""};
char str1[]="1230.456+-789*/Qc=^%";/* 定义字符串在按钮图形上显示的符号 */
mwindow( "Calculator" ); /* 显示主窗口 */
color = 7; /*设置灰颜色值*/
getviewsettings( &vp ); /* 读取当前窗口的大小*/
width=(vp.right+1)/10; /* 设置按钮宽度 */
height=(vp.bottom-10)/10 ; /*设置按钮高度 */
x = width /2; /*设置x的坐标值*/
y = height/2; /*设置y的坐标值*/
setfillstyle(SOLID_FILL, color+3);
bar( x+width*2, y, x+7*width, y+height );
/*画一个二维矩形条显示运算数和结果*/
setcolor( color+3 ); /*设置淡绿颜色边框线*/
rectangle( x+width*2, y, x+7*width, y+height );
/*画一个矩形边框线*/
setcolor(RED); /*设置颜色为红色*/
outtextxy(x+3*width,y+height/2,"0."); /*输出字符串"0."*/
x =2*width-width/2; /*设置x的坐标值*/
y =2*height+height/2; /*设置y的坐标值*/
for( j=0 ; j<4 ; ++j ) /*画按钮*/
{
for( i=0 ; i<5 ; ++i )
{
setfillstyle(SOLID_FILL, color);
setcolor(RED);
bar( x, y, x+width, y+height ); /*画一个矩形条*/
rectangle( x, y, x+width, y+height );
sprintf(str2,"%c",str1[j*5+i]);
/*将字符保存到str2中*/
outtextxy( x+(width/2), y+height/2, str2);
x =x+width+ (width / 2) ; /*移动列坐标*/
}
y +=(height/2)*3; /* 移动行坐标*/
x =2*width-width/2; /*复位列坐标*/
}
x0=2*width;
y0=3*height;
x=x0;
y=y0;
gotoxy(x,y); /*移动光标到x,y位置*/
arrow(); /*显示光标*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,""); /*设置str2为空串*/
while((v=specialkey())!=45) /*当压下Alt+x键结束程序,否则执行下面的循环*/
{
while((v=specialkey())!=ENTER) /*当压下键不是回车时*/
{
putimage(x,y,rar,XOR_PUT); /*显示光标图象*/
if(v==RIGHT) /*右移箭头时新位置计算*/
if(x>=x0+6*width)
/*如果右移,移到尾,则移动到最左边字符位置*/
{
x=x0;
m=0;
}
else
{
x=x+width+width/2;
m++;
} /*否则,右移到下一个字符位置*/
if(v==LEFT) /*左移箭头时新位置计算*/
if(x<=x0)
{
x=x0+6*width;
m=4;
} /*如果移到头,再左移,则移动到最右边字符位置*/
else
{
x=x-width-width/2;
m--;
} /*否则,左移到前一个字符位置*/
if(v==UP) /*上移箭头时新位置计算*/
if(y<=y0)
{
y=y0+4*height+height/2;
n=3;
} /*如果移到头,再上移,则移动到最下边字符位置*/
else
{
y=y-height-height/2;
n--;
} /*否则,移到上边一个字符位置*/
if(v==DOWN) /*下移箭头时新位置计算*/
if(y>=7*height)
{
y=y0;
n=0;
} /*如果移到尾,再下移,则移动到最上边字符位置*/
else
{
y=y+height+height/2;
n++;
} /*否则,移到下边一个字符位置*/
putimage(x,y,rar,XOR_PUT); /*在新的位置显示光标箭头*/
}
c=str1[n*5+m]; /*将字符保存到变量c中*/
if(isdigit(c)||c=='.') /*判断是否是数字或小数点*/
{
if(flag==-1) /*如果标志为-1,表明为负数*/
{
strcpy(str2,"-"); /*将负号连接到字符串中*/
flag=1;
} /*将标志值恢复为1*/
sprintf(temp,"%c",c); /*将字符保存到字符串变量temp中*/
strcat(str2,temp); /*将temp中的字符串连接到str2中*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,str2); /*显示字符串*/
}
if(c=='+')
{
num1=atof(str2); /*将第一个操作数转换为浮点数*/
strcpy(str2,""); /*将str2清空*/
act=1; /*做计算加法标志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0."); /*显示字符串*/
}
if(c=='-')
{
if(strcmp(str2,"")==0) /*如果str2为空,说明是负号,而不是减号*/
flag=-1; /*设置负数标志*/
else
{
num1=atof(str2); /*将第二个操作数转换为浮点数*/
strcpy(str2,""); /*将str2清空*/
act=2; /*做计算减法标志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*画矩形*/
outtextxy(5*width,height,"0."); /*显示字符串*/
}
}
if(c=='*')
{
num1=atof(str2); /*将第二个操作数转换为浮点数*/
strcpy(str2,""); /*将str2清空*/
act=3; /*做计算乘法标志值*/
setfillstyle(SOLID_FILL,color+3); bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0."); /*显示字符串*/
}
if(c=='/')
{
num1=atof(str2); /*将第二个操作数转换为浮点数*/
strcpy(str2,""); /*将str2清空*/
act=4; /*做计算除法标志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0."); /*显示字符串*/
}
if(c=='^')
{
num1=atof(str2); /*将第二个操作数转换为浮点数*/
strcpy(str2,""); /*将str2清空*/
act=5; /*做计算乘方标志值*/
setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*画矩形*/
outtextxy(5*width,height,"0."); /*显示字符串*/
}
if(c=='%')
{
num1=atof(str2); /*将第二个操作数转换为浮点数*/
strcpy(str2,""); /*将str2清空*/
act=6; /*做计算模运算乘方标志值*/
setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*画矩形*/
outtextxy(5*width,height,"0."); /*显示字符串*/
}
if(c=='=')
{
num2=atof(str2); /*将第二个操作数转换为浮点数*/
switch(act) /*根据运算符号计算*/
{
case 1:result=num1+num2;break; /*做加法*/
case 2:result=num1-num2;break; /*做减法*/
case 3:result=num1*num2;break; /*做乘法*/
case 4:result=num1/num2;break; /*做除法*/
case 5:result=pow(num1,num2);break; /*做x的y次方*/
case 6:result=fmod(num1,num2);break; /*做模运算*/
}
setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*覆盖结果区*/
sprintf(temp,"%f",result); /*将结果保存到temp中*/
outtextxy(5*width,height,temp); /*显示结果*/
}
if(c=='c')
{
num1=0; /*将两个操作数复位0,符号标志为1*/
num2=0;
flag=1;
strcpy(str2,""); /*将str2清空*/
setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*覆盖结果区*/
outtextxy(5*width,height,"0."); /*显示字符串*/
}
if(c=='Q')exit(0); /*如果选择了q回车,结束计算程序*/
}
putimage(x,y,rar,XOR_PUT); /*在退出之前消去光标箭头*/
return; /*返回*/
}
/*窗口函数*/
void mwindow( char *header )
{
int height;
cleardevice(); /* 清除图形屏幕 */
setcolor( MaxColors - 1 ); /* 设置当前颜色为白色*/
setviewport( 20, 20, MaxX/2, MaxY/2, 1 ); /* 设置视口大小 */
height = textheight( "H" ); /* 读取基本文本大小 */
settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );/*设置文本样式*/
settextjustify( CENTER_TEXT, TOP_TEXT );/*设置字符排列方式*/
outtextxy( MaxX/4, 2, header ); /*输出标题*/
setviewport( 20,20+height+4, MaxX/2+4, MaxY/2+20, 1 ); /*设置视口大小*/
drawboder(); /*画边框*/
}
void drawboder(void) /*画边框*/
{
struct viewporttype vp; /*定义视口类型变量*/
setcolor( MaxColors - 1 ); /*设置当前颜色为白色 */
setlinestyle( SOLID_LINE, 0, NORM_WIDTH );/*设置画线方式*/
getviewsettings( &vp );/*将当前视口信息装入vp所指的结构中*/
rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top ); /*画矩形边框*/
}
/*设计鼠标图形函数*/
int arrow()
{
int size;
int raw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4}; /*定义多边形坐标*/
setfillstyle(SOLID_FILL,2); /*设置填充模式*/
fillpoly(8,raw); /*画出一光标箭头*/
size=imagesize(4,4,16,16); /*测试图象大小*/
rar=malloc(size); /*分配内存区域*/
getimage(4,4,16,16,rar); /*存放光标箭头图象*/
putimage(4,4,rar,XOR_PUT); /*消去光标箭头图象*/
return 0;
}
/*按键函数*/
int specialkey(void)
{
int key;
while(bioskey(1)==0); /*等待键盘输入*/
key=bioskey(0); /*键盘输入*/
key=key&0xff? key&0xff:key>>8; /*只取特殊键的扫描值,其余为0*/
return(key); /*返回键值*/
}


3. 用C语言编写的小游戏代码是什么

“猜数字小游戏”,每个数字后按空格,最后按回车确认

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

int a[4],b[4];

int count=0; //计算猜测次数

void csh( ); //初始化

void start( ); //开始游戏

int main( )

{ csh( );

start( );

}

void csh( ) //初始化

{ printf(" 猜 数 字 小 游 戏 ");

printf(“ 猜四个数字,如数字与顺序都正确记为A,数字正确位置不对记为B. ”);

}

void start( ) //开始游戏

{int m,n; //m是完全猜对的个数,n是顺序不对的个数

while(1)

{srand((unsigned)time(NULL)); //初始化随机数发生器srand( )

while(1) { for(int i=0;i<4;i++) a[i]=rand( )%10; //rand( )函数每次随机产生一个0-9的数

if( (a[3]!=a[2]&&a[3]!=a[1]&&a[3]!=a[0])&&

(a[2]!=a[1]&&a[2]!=a[0])&&a[1]!=a[0] ) break; } //4个随机数各自不相等

printf(" 请依次输入4个一位整数: ");

while(1)

{for(int i=0;i<4;i++) scanf(“%d”,&b[i]);

printf(" 你输入的是:%d %d %d %d ",b[0],b[1],b[2],b[3]);

m=0;n=0;

for(int i=0;i<4;i++)

{for(int j=0;j<4;j++)

{ if(b[i]==a[j]&&i==j)m=m+1; if(b[i]==a[j]&&i!=j)n=n+1; }

}

count=count+1;

printf(" %dA %dB 你试了%d次 ",m,n,count);

if(m==4)break;

if(count==8){ count=0; break; }

}

printf(" ");

if(m==4)printf(" 你猜对了(^-^)! 就是:%d %d %d %d ",a[0],a[1],a[2],a[3]);

else printf(" 你输了(T-T)!哈哈!应该是:%d %d %d %d ",a[0],a[1],a[2],a[3]);

int z;

printf(" (要继续吗?1或0) ");

scanf(“%d”,&z);

if(z==0) break;

}

}

4. 用C++编写的小游戏源代码

五子棋的代码:

#include<iostream>

#include<stdio.h>

#include<stdlib.h>

#include <time.h>

using namespace std;

const int N=15; //15*15的棋盘

const char ChessBoardflag = ' '; //棋盘标志

const char flag1='o'; //玩家1或电脑的棋子标志

const char flag2='X'; //玩家2的棋子标志

typedef struct Coordinate //坐标类

{

int x; //代表行

int y; //代表列

}Coordinate;

class GoBang //五子棋类

{

public:

GoBang() //初始化

{

InitChessBoard();

}

void Play() //下棋

{

Coordinate Pos1; // 玩家1或电脑

Coordinate Pos2; //玩家2

int n = 0;

while (1)

{

int mode = ChoiceMode();

while (1)

{

if (mode == 1) //电脑vs玩家

{

ComputerChess(Pos1,flag1); // 电脑下棋

if (GetVictory(Pos1, 0, flag1) == 1) //0表示电脑,真表示获胜

break;

PlayChess(Pos2, 2, flag2); //玩家2下棋

if (GetVictory(Pos2, 2, flag2)) //2表示玩家2

break;

}

else //玩家1vs玩家2

{

PlayChess(Pos1, 1, flag1); // 玩家1下棋

if (GetVictory(Pos1, 1, flag1)) //1表示玩家1

break;

PlayChess(Pos2, 2, flag2); //玩家2下棋

if (GetVictory(Pos2, 2, flag2)) //2表示玩家2

break;

}

}

cout << "***再来一局***" << endl;

cout << "y or n :";

char c = 'y';

cin >> c;

if (c == 'n')

break;

}

}

protected:

int ChoiceMode() //选择模式

{

int i = 0;

system("cls"); //系统调用,清屏

InitChessBoard(); //重新初始化棋盘

cout << "***0、退出 1、电脑vs玩家 2、玩家vs玩家***" << endl;

while (1)

{

cout << "请选择:";

cin >> i;

if (i == 0) //选择0退出

exit(1);

if (i == 1 || i == 2)

return i;

cout << "输入不合法" << endl;

}

}

void InitChessBoard() //初始化棋盘

{

for (int i = 0; i < N + 1; ++i)

{

for (int j = 0; j < N + 1; ++j)

{

_ChessBoard[i][j] = ChessBoardflag;

}

}

}

void PrintChessBoard() //打印棋盘,这个函数可以自己调整

{

system("cls"); //系统调用,清空屏幕

for (int i = 0; i < N+1; ++i)

{

for (int j = 0; j < N+1; ++j)

{

if (i == 0) //打印列数字

{

if (j!=0)

printf("%d ", j);

else

printf(" ");

}

else if (j == 0) //打印行数字

printf("%2d ", i);

else

{

if (i < N+1)

{

printf("%c |",_ChessBoard[i][j]);

}

}

}

cout << endl;

cout << " ";

for (int m = 0; m < N; m++)

{

printf("--|");

}

cout << endl;

}

}

void PlayChess(Coordinate& pos, int player, int flag) //玩家下棋

{

PrintChessBoard(); //打印棋盘

while (1)

{

printf("玩家%d输入坐标:", player);

cin >> pos.x >> pos.y;

if (JudgeValue(pos) == 1) //坐标合法

break;

cout << "坐标不合法,重新输入" << endl;

}

_ChessBoard[pos.x][pos.y] = flag;

}

void ComputerChess(Coordinate& pos, char flag) //电脑下棋

{

PrintChessBoard(); //打印棋盘

int x = 0;

int y = 0;

while (1)

{

x = (rand() % N) + 1; //产生1~N的随机数

srand((unsigned int) time(NULL));

y = (rand() % N) + 1; //产生1~N的随机数

srand((unsigned int) time(NULL));

if (_ChessBoard[x][y] == ChessBoardflag) //如果这个位置是空的,也就是没有棋子

break;

}

pos.x = x;

pos.y = y;

_ChessBoard[pos.x][pos.y] = flag;

}

int JudgeValue(const Coordinate& pos) //判断输入坐标是不是合法

{

if (pos.x > 0 && pos.x <= N&&pos.y > 0 && pos.y <= N)

{

if (_ChessBoard[pos.x][pos.y] == ChessBoardflag)

{

return 1; //合法

}

}

return 0; //非法

}

int JudgeVictory(Coordinate pos, char flag) //判断有没有人胜负(底层判断)

{

int begin = 0;

int end = 0;

int begin1 = 0;

int end1 = 0;

//判断行是否满足条件

(pos.y - 4) > 0 ? begin = (pos.y - 4) : begin = 1;

(pos.y + 4) >N ? end = N : end = (pos.y + 4);

for (int i = pos.x, j = begin; j + 4 <= end; j++)

{

if (_ChessBoard[i][j] == flag&&_ChessBoard[i][j + 1] == flag&&

_ChessBoard[i][j + 2] == flag&&_ChessBoard[i][j + 3] == flag&&

_ChessBoard[i][j + 4] == flag)

return 1;

}

//判断列是否满足条件

(pos.x - 4) > 0 ? begin = (pos.x - 4) : begin = 1;

(pos.x + 4) > N ? end = N : end = (pos.x + 4);

for (int j = pos.y, i = begin; i + 4 <= end; i++)

{

if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j] == flag&&

_ChessBoard[i + 2][j] == flag&&_ChessBoard[i + 3][j] == flag&&

_ChessBoard[i + 4][j] == flag)

return 1;

}

int len = 0;

//判断主对角线是否满足条件

pos.x > pos.y ? len = pos.y - 1 : len = pos.x - 1;

if (len > 4)

len = 4;

begin = pos.x - len; //横坐标的起始位置

begin1 = pos.y - len; //纵坐标的起始位置

pos.x > pos.y ? len = (N - pos.x) : len = (N - pos.y);

if (len>4)

len = 4;

end = pos.x + len; //横坐标的结束位置

end1 = pos.y + len; //纵坐标的结束位置

for (int i = begin, j = begin1; (i + 4 <= end) && (j + 4 <= end1); ++i, ++j)

{

if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j + 1] == flag&&

_ChessBoard[i + 2][j + 2] == flag&&_ChessBoard[i + 3][j + 3] == flag&&

_ChessBoard[i + 4][j + 4] == flag)

return 1;

}

//判断副对角线是否满足条件

(pos.x - 1) >(N - pos.y) ? len = (N - pos.y) : len = pos.x - 1;

if (len > 4)

len = 4;

begin = pos.x - len; //横坐标的起始位置

begin1 = pos.y + len; //纵坐标的起始位置

(N - pos.x) > (pos.y - 1) ? len = (pos.y - 1) : len = (N - pos.x);

if (len>4)

len = 4;

end = pos.x + len; //横坐标的结束位置

end1 = pos.y - len; //纵坐标的结束位置

for (int i = begin, j = begin1; (i + 4 <= end) && (j - 4 >= end1); ++i, --j)

{

if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j - 1] == flag&&

_ChessBoard[i + 2][j - 2] == flag&&_ChessBoard[i + 3][j - 3] == flag&&

_ChessBoard[i + 4][j - 4] == flag)

return 1;

}

for (int i = 1; i < N + 1; ++i) //棋盘有没有下满

{

for (int j =1; j < N + 1; ++j)

{

if (_ChessBoard[i][j] == ChessBoardflag)

return 0; //0表示棋盘没满

}

}

return -1; //和棋

}

bool GetVictory(Coordinate& pos, int player, int flag) //对JudgeVictory的一层封装,得到具体那个玩家获胜

{

int n = JudgeVictory(pos, flag); //判断有没有人获胜

if (n != 0) //有人获胜,0表示没有人获胜

{

PrintChessBoard();

if (n == 1) //有玩家赢棋

{

if (player == 0) //0表示电脑获胜,1表示玩家1,2表示玩家2

printf("***电脑获胜*** ");

else

printf("***恭喜玩家%d获胜*** ", player);

}

else

printf("***双方和棋*** ");

return true; //已经有人获胜

}

return false; //没有人获胜

}

private:

char _ChessBoard[N+1][N+1];

};

(4)c语言小程序游戏扩展阅读:

设计思路

1、进行问题分析与设计,计划实现的功能为,开局选择人机或双人对战,确定之后比赛开始。

2、比赛结束后初始化棋盘,询问是否继续比赛或退出,后续可加入复盘、悔棋等功能。

3、整个过程中,涉及到了棋子和棋盘两种对象,同时要加上人机对弈时的AI对象,即涉及到三个对象。

5. 给我提供个小游戏的C 语言代码

本原代码是基于C语言的原程序。是经典中的小游戏。-primitive code is based on the C language of the original procere. Classic is a small game.
一个小游戏,用C语言编写的:俄罗斯方块.C原码及应用程序都在里面哦 -a small game using the C language : Russian cubes. The original C code and application proceres inside oh
十全十美游戏原程序,c语言-perfect game program, language c
上数据结构时,自己用C语言做的小游戏,做得不好,请大家原谅。-structure on the data they used C language to the small games, done well, please forgive me.
大家都耍过文曲星中的猜数字的游戏吧 !! 最近我在学习C语言。写了个菜鸟的C语言的猜数字的游戏程序的原代码-rings off the viewing of the game! ! I recently learning C language. Wrote a birdie C language viewing of the games original code proceres
这是我在大学二年级学习C语言课程时,作为“练笔”而编写的一个小程序(当时在我眼里可却是一个大程序!)其主要特点有:1、正真做到了全中文界面(不需要UCDOS操作系支持) 2、大量的图形特技(如图像的显隐技术、图像穿插技术、多任务仿真技术等) 3、纯C语言打造(不含任何C++知识) 4、实现了街机“俄罗斯方块”绝大部分功能(如动画、声音、速度变化) 5、用户可根据自据的习惯自由地调整“游戏操作键” 6、方法独特,全部语句和技术都是我本人原创,没有参考过任何相关代码 7、防“跟踪调试”技术,防“版权篡改”技术 8、……-
这个程序是模仿Windows中的扫雷小游戏制作的,该程序只是实现了扫雷游戏的主体部分,诸如计分、升级部分都没有做。这个程序可以作为初学者学习C语言绘图和游戏的实例。 该程序在Turbo C2.0 下编译通过 由于扫雷游戏是用鼠标操作的,而Turbo C中提供的鼠标驱动程序在Windows xp下不可用,所以我随源程序提供了一个鼠标驱动的头文件,须将将该头文件复制到Turbo C2.0 的安装目录下的“include”文件夹中方可编译或运行,也可自行修改原文件使之包含该投文件。 注:该鼠标驱动程序是我在网上找到的,其出处我已无法考证,如果侵犯了作者的权利还请作者与我联系。 由于在我的电脑上Turbo C图形环境下显示数字会有问题(估计是系统问题),所以程序中雷周围的数字1-8我用a-h代替,看不顺眼的可以自己修改原程序。-
c语言 猜拳游戏的原代码就是这个 已经测试成功了呀-language of the original game is the code has been tested successfully ah
俄罗斯方块对战版c语言原代码。希望大家能喜欢。是比较简单的一个代码,游戏开发高手请指教。-Tetris screen version of the original C language code. Hope you will like. It is a relatively simple code, game development experts please advise.
用linuX 下的C语言 运用CURSES编写的俄罗斯方块游戏,很好,这个是本人原创,值得参考-linuX use the C language CURSES prepared by the Russian box game, well, this is the original, worthy of reference

6. c语言小程序--黑白棋

你想做什么?
人人对战的代码我有,但是人机对战的AI写不好
是个国际难题
//=====othello=====
#include<iostream>
#include<cstdio>

using namespace std;

int board[8][8],saveboard[60][8][8];
int cx,cy,col,pass,empty,black,white;

void init(){ //initialization
memset(board,-1,sizeof(board));
board[3][3]=0;
board[3][4]=1;
board[4][4]=0;
board[4][3]=1;
col=0;
pass=0;
empty=60;
black=2;
white=2;
}

int input(){
char s[1000]="";
scanf("%s",&s);
if(s[0]>='a' && s[0]<='h')
cy=s[0]-'a';
else if(s[0]>='A' && s[0]<='H')
cy=s[0]-'A';
else return 0;
if(s[1]>='1' && s[1]<='8'){
cx=s[1]-'1';
return 1;
}
return 0;
}

int judge(int x,int y){
int i,j,temp;
temp=(col+1)%2;
//left && up
if(board[x-1][y-1]==temp){
for(i=x-1,j=y-1; i>=0 && j>=0; i--,j--){
if(board[i][j]<0) break;
if(col==board[i][j]) return 1;
}
}
//up
if(board[x-1][y]==temp){
for(i=x-1; i>=0; i--){
if(board[i][y]<0) break;
if(col==board[i][y]) return 1;
}
}
//right && up
if(board[x-1][y+1]==temp){
for(i=x-1,j=y+1; i>=0 && j<8; i--,j++){
if(board[i][j]<0) break;
if(col==board[i][j]) return 1;
}
}
//right
if(board[x][y+1]==temp){
for(j=y+1; j<8; j++){
if(board[x][j]<0) break;
if(col==board[x][j]) return 1;
}
}
//right && down
if(board[x+1][y+1]==temp){
for(i=x+1,j=y+1; i<8 && j<8; i++,j++){
if(board[i][j]<0) break;
if(col==board[i][j]) return 1;
}
}
//down
if(board[x+1][y]==temp){
for(i=x+1; i<8; i++){
if(board[i][y]<0) break;
if(col==board[i][y]) return 1;
}
}
//left && down
if(board[x+1][y-1]==temp){
for(i=x+1,j=y-1; i<8 && j>=0; i++,j--){
if(board[i][j]<0) break;
if(col==board[i][j]) return 1;
}
}
//left
if(board[x][y-1]==temp){
for(j=y-1; j>=0; j--){
if(board[x][j]<0) break;
if(col==board[x][j]) return 1;
}
}
return 0;
}

void move(int x,int y){
int i,j,temp,count;
temp=(col+1)%2;
count=0;
//left && up
if(board[x-1][y-1]==temp){
for(i=x-1,j=y-1; i>=0 && j>=0; i--,j--){
if(board[i][j]<0) break;
if(col==board[i][j]){
while(i!=x){
board[++i][++j]=col;
count++;
}
count--;
break;
}
}
}
//up
if(board[x-1][y]==temp){
for(i=x-1; i>=0; i--){
if(board[i][y]<0) break;
if(col==board[i][y]){
while(i!=x){
board[++i][y]=col;
count++;
}
count--;
break;
}
}
}
//right && up
if(board[x-1][y+1]==temp){
for(i=x-1,j=y+1; i>=0 && j<8; i--,j++){
if(board[i][j]<0) break;
if(col==board[i][j]){
while(i!=x){
board[++i][--j]=col;
count++;
}
count--;
break;
}
}
}
//right
if(board[x][y+1]==temp){
for(j=y+1; j<8; j++){
if(board[x][j]<0) break;
if(col==board[x][j]){
while(j!=y){
board[x][--j]=col;
count++;
}
count--;
break;
}
}
}
//right && down
if(board[x+1][y+1]==temp){
for(i=x+1,j=y+1; i<8 && j<8; i++,j++){
if(board[i][j]<0) break;
if(col==board[i][j]){
while(i!=x){
board[--i][--j]=col;
count++;
}
count--;
break;
}
}
}
//down
if(board[x+1][y]==temp){
for(i=x+1; i<8; i++){
if(board[i][y]<0) break;
if(col==board[i][y]){
while(i!=x){
board[--i][y]=col;
count++;
}
count--;
break;
}
}
}
//left && down
if(board[x+1][y-1]==temp){
for(i=x+1,j=y-1; i<8 && j>=0; i++,j--){
if(board[i][j]<0) break;
if(col==board[i][j]){
while(i!=x){
board[--i][++j]=col;
count++;
}
count--;
break;
}
}
}
//left
if(board[x][y-1]==temp){
for(j=y-1; j>=0; j--){
if(board[x][j]<0) break;
if(col==board[x][j]){
while(j!=y){
board[x][++j]=col;
count++;
}
count--;
break;
}
}
}
board[x][y]=col;
if(col){
black+=count;
white-=count;
black++;
}
else{
black-=count;
white+=count;
white++;
}
empty--;
}

void output(){
char c;
printf(" ");
for(int i=0; i<8; i++){
c='A'+i;
printf("%2c",c);
}
printf("\n");

for(int i=0; i<8; i++){
printf("%d",i+1);
for(int j=0; j<8; j++){
if(board[i][j]==-1)
c=' ';
else if(board[i][j]==0)
c='O';
else
c='X';
printf("%2c",c);
}
printf("\n");
}
printf("Black:%3d White:%3d\n",black,white);
}

int passjudge(){
int f=0;
for(int i=0; i<8; i++)
for(int j=0; j<8; j++)
if(board[i][j]<0)
f+=judge(i,j);
return f;
}

void startprint(){
printf("1、New game\n2、setboard\n0、Exit\n");
}

void pvp(){
while(empty && pass<2){
//black or white
col++;
col%=2;
output();
//input
if(!input()){
if(!passjudge()){
printf("Pass!\n");
pass++;
}
else {
col++;
printf("No pass!\nPlease input right stone!\n");
}
continue;
}
if(judge(cx,cy)){
move(cx,cy);
pass=0;
}
else{
col++;
printf("Miss stone\n");
}
}
output();
if(black>white)
printf("Black Win!\n");
else if(black<white)
printf("White Win!\n");
else
printf("Draw Game!\n");
startprint();
}

void setboard(){
char c;
for(int i=0; i<8; i++)
for(int j=0; scanf("%c",&c) && c!='\n'; j++){
if(j>=8) continue;
if(c=='o' && c=='O')
board[i][j]=0;
else if(c=='x' && c=='X')
board[i][j]=1;
}
printf("White start or Black start?\n(W/B)");
scanf("%c",&c);
if(c=='w' || c=='W')
col=1;
if(c=='b' || c=='B')
col=0;
}

int main(int argc, char* argv[]){
int n;
startprint();
while(scanf("%d",&n) && n){
init();
if(n==1)
pvp();
if(n==2){
setboard();
pvp();
}
}
return 0;
}

热点内容
我的世界国服pvp服务器ip地址 发布:2025-08-17 19:45:35 浏览:497
聊城电脑服务器 发布:2025-08-17 19:34:59 浏览:407
互联网编程语言 发布:2025-08-17 19:18:40 浏览:851
python主流框架 发布:2025-08-17 19:11:51 浏览:176
开源海量文件存储 发布:2025-08-17 19:07:05 浏览:193
带密码的发票有什么用 发布:2025-08-17 18:53:18 浏览:689
免费php模板下载 发布:2025-08-17 18:47:31 浏览:240
ubuntuphp开发 发布:2025-08-17 18:34:44 浏览:499
c语言小程序游戏 发布:2025-08-17 18:23:09 浏览:802
ios今日头条源码 发布:2025-08-17 18:23:02 浏览:311