"扫雷"小游戏C代码
#includestdio.h
#includemath.h
#includetime.h
#includestdlib.h
main( )
{char a[102][102],b[102][102],c[102][102],w;
int i,j; /*循环变量*/
int x,y,z[999]; /*雷的位置*/
int t,s; /*标记*/
int m,n,lei; /*计数*/
int u,v; /*输入*/
int hang,lie,ge,mo; /*自定义变量*/
srand((int)time(NULL)); /*启动随机数发生器*/
leb1: /*选择模式*/
printf("\n 请选择模式:\n 1.标准 2.自定义\n");
scanf("%d",mo);
if(mo==2) /*若选择自定义模式,要输入三个参数*/
{do
{t=0; printf("请输入\n行数 列数 雷的个数\n");
scanf("%d%d%d",hang,lie,ge);
if(hang2){printf("行数太少\n"); t=1;}
if(hang100){printf("行数太多\n");t=1;}
if(lie2){printf("列数太少\n");t=1;}
if(lie100){printf("列数太多\n");t=1;}
if(ge1){printf("至少要有一个雷\n");t=1;}
if(ge=(hang*lie)){printf("雷太多了\n");t=1;}
}while(t==1);
}
else{hang=10,lie=10,ge=10;} /*否则就是选择了标准模式(默认参数)*/
for(i=1;i=ge;i=i+1) /*确定雷的位置*/
{do
{t=0; z[i]=rand( )%(hang*lie);
for(j=1;ji;j=j+1){if(z[i]==z[j]) t=1;}
}while(t==1);
}
for(i=0;i=hang+1;i=i+1) /*初始化a,b,c*/
{for(j=0;j=lie+1;j=j+1) {a[i][j]='1'; b[i][j]='1'; c[i][j]='0';} }
for(i=1;i=hang;i=i+1)
{for(j=1;j=lie;j=j+1) {a[i][j]='+';} }
for(i=1;i=ge;i=i+1) /*把雷放入c*/
{x=z[i]/lie+1; y=z[i]%lie+1; c[x][y]='#';}
for(i=1;i=hang;i=i+1) /*计算b中数字*/
{for(j=1;j=lie;j=j+1)
{m=48;
if(c[i-1][j-1]=='#')m=m+1; if(c[i][j-1]=='#')m=m+1;
if(c[i-1][j]=='#')m=m+1; if(c[i+1][j+1]=='#')m=m+1;
if(c[i][j+1]=='#')m=m+1; if(c[i+1][j]=='#')m=m+1;
if(c[i+1][j-1]=='#')m=m+1; if(c[i-1][j+1]=='#')m=m+1;
b[i][j]=m;
}
}
for(i=1;i=ge;i=i+1) /*把雷放入b中*/
{x=z[i]/lie+1; y=z[i]%lie+1; b[x][y]='#';}
lei=ge; /*以下是游戏设计*/
do
{leb2: /*输出*/
system("cls");printf("\n\n\n\n");
printf(" ");
for(i=1;i=lie;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c ",w);
}
printf("\n |");
for(i=1;i=lie;i=i+1){printf("---|");}
printf("\n");
for(i=1;i=hang;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c |",w);
for(j=1;j=lie;j=j+1)
{if(a[i][j]=='0')printf(" |");
else printf(" %c |",a[i][j]);
}
if(i==2)printf(" 剩余雷个数");
if(i==3)printf(" %d",lei);
printf("\n |");
for(j=1;j=lie;j=j+1){printf("---|");}
printf("\n");
}
scanf("%d%c%d",u,w,v); /*输入*/
u=u+1,v=v+1;
if(w!='#'a[u][v]=='@')
goto leb2;
if(w=='#')
{if(a[u][v]=='+'){a[u][v]='@'; lei=lei-1;}
else if(a[u][v]=='@'){a[u][v]='?'; lei=lei+1;}
else if(a[u][v]=='?'){a[u][v]='+';}
goto leb2;
}
a[u][v]=b[u][v];
leb3: /*打开0区*/
t=0;
if(a[u][v]=='0')
{for(i=1;i=hang;i=i+1)
{for(j=1;j=lie;j=j+1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1; if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=1;i=hang;i=i+1)
{for(j=lie;j=1;j=j-1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1; if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=hang;i=1;i=i-1)
{for(j=1;j=lie;j=j+1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1; if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=hang;i=1;i=i-1)
{for(j=lie;j=1;j=j-1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1;if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=1;i=hang;i=i+1) /*检测0区*/
{for(j=1;j=lie;j=j+1)
{if(a[i][j]=='0')
{if(a[i-1][j-1]=='+'||a[i-1][j-1]=='@'||a[i-1][j-1]=='?')t=1;
if(a[i-1][j+1]=='+'||a[i-1][j+1]=='@'||a[i-1][j+1]=='?')t=1;
if(a[i+1][j-1]=='+'||a[i+1][j-1]=='@'||a[i+1][j-1]=='?')t=1;
if(a[i+1][j+1]=='+'||a[i+1][j+1]=='@'||a[i+1][j+1]=='?')t=1;
if(a[i+1][j]=='+'||a[i+1][j]=='@'||a[i+1][j]=='?')t=1;
if(a[i][j+1]=='+'||a[i][j+1]=='@'||a[i][j+1]=='?')t=1;
if(a[i][j-1]=='+'||a[i][j-1]=='@'||a[i][j-1]=='?')t=1;
if(a[i-1][j]=='+'||a[i-1][j]=='@'||a[i-1][j]=='?')t=1;
}
}
}
if(t==1)goto leb3;
}
n=0; /*检查结束*/
for(i=1;i=hang;i=i+1)
{for(j=1;j=lie;j=j+1)
{if(a[i][j]!='+'a[i][j]!='@'a[i][j]!='?')n=n+1;}
}
}
while(a[u][v]!='#'n!=(hang*lie-ge));
for(i=1;i=ge;i=i+1) /*游戏结束*/
{x=z[i]/lie+1; y=z[i]%lie+1; a[x][y]='#'; }
printf(" ");
for(i=1;i=lie;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c ",w);
}
printf("\n |");
for(i=1;i=lie;i=i+1){printf("---|");}
printf("\n");
for(i=1;i=hang;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c |",w);
for(j=1;j=lie;j=j+1)
{if(a[i][j]=='0')printf(" |");
else printf(" %c |",a[i][j]);
}
if(i==2)printf(" 剩余雷个数");
if(i==3)printf(" %d",lei); printf("\n |");
for(j=1;j=lie;j=j+1) {printf("---|");}
printf("\n");
}
if(n==(hang*lie-ge)) printf("你成功了!\n");
else printf(" 游戏结束!\n");
printf(" 重玩请输入1\n");
t=0;
scanf("%d",t);
if(t==1)goto leb1;
}
/*注:在DEV c++上运行通过。行号和列号都从0开始,比如要确定第0行第9列不是“雷”,就在0和9中间加入一个字母,可以输入【0a9】三个字符再按回车键。3行7列不是雷,则输入【3a7】回车;第8行第5列是雷,就输入【8#5】回车,9行0列是雷则输入【9#0】并回车*/
“贪吃蛇”C代码,在dev C++试验通过(用4个方向键控制)
#include stdio.h
#include stdlib.h
#include conio.h
#include time.h
#include Windows.h
#define W 78 //游戏框的宽,x轴
#define H 26 //游戏框的高,y轴
int dir=3; //方向变量,初值3表示向“左”
int Flag=0; //吃了食物的标志(1是0否)
int score=0; //玩家得分
struct food{ int x; //食物的x坐标
int y; //食物的y坐标
}fod; //结构体fod有2个成员
struct snake{ int len; //蛇身长
int speed; //移动速度
int x[100]; //蛇身某节x坐标
int y[100]; //蛇身某节y坐标
}snk; //结构体snk有4个成员
void gtxy( int x,int y) //控制光标移动的函数
{ COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void gtxy( int x,int y); //以下声明要用到的几个自编函数
void csh( ); //初始化界面
void keymove( ); //按键操作移动蛇
void putFod( ); //投放食物
int Over( ); //游戏结束(1是0否)
void Color(int a); //设定显示颜色的函数
int main( ) //主函数
{ csh( );
while(1)
{ Sleep(snk.speed);
keymove( );
putFod( );
if(Over( ))
{ system(“cls”);
gtxy(W/2+1,H/2); printf(“游戏结束!T__T”);
gtxy(W/2+1,H/2+2); printf(“玩家总分:%d分”,score);
getch( );
break;
}
}
return 0;
}
void csh( ) //初始化界面
{ int i;
gtxy(0,0);
CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下两行是隐藏光标的设置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),cursor_info);
for(i=0;i=W;i=i+2) //横坐标要为偶数,因为这个要打印的字符占2个位置
{Color(2); //设定打印颜色为绿色
gtxy(i,0); printf("■"); //打印上边框
gtxy(i,H); printf("■"); //打印下边框
}
for(i=1;iH;i++)
{ gtxy(0,i); printf("■"); //打印左边框
gtxy(W,i); printf("■"); //打印右边框
}
while(1)
{ srand((unsigned)time(NULL)); //初始化随机数发生器srand( )
fod.x=rand()%(W-4)+2; //随机函数rand( )产生一个从0到比”(W-4)”小1的数再加2
fod.y=rand()%(H-2)+1; //随机函数rand( )产生一个从0到比”(H-2)”小1的数再加1
if (fod.x%2==0) break; //fod.x是食物的横坐标,要是2的倍数(为偶数)
}
Color(12); //设定打印颜色为淡红
gtxy(fod.x,fod.y); printf("●"); //到食物坐标处打印初试食物
snk.len=3; //蛇身长初值为3节
snk.speed=350; //刷新蛇的时间,即移动速度初值为350毫秒
snk.x[0]=W/2+1; //蛇头横坐标要为偶数(因为W/2=39)
snk.y[0]=H/2; //蛇头纵坐标
Color(9); //设定打印颜色为淡蓝
gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头
for(i=1;isnk.len;i++)
{ snk.x[i]=snk.x[i-1]+2; snk.y[i]=snk.y[i-1];
gtxy(snk.x[i],snk.y[i]); printf("■"); //打印蛇身
}
Color(7, 0); //恢复默认的白字黑底
return;
}
void keymove( ) //按键操作移动蛇
{ int key;
if( kbhit( ) ) //如有按键输入才执行下面操作
{ key=getch( );
if (key==224) //值为224表示按下了方向键,下面要再次获取键值
{ key=getch( );
if(key==72dir!=2)dir=1; //72表示按下了向上方向键
if(key==80dir!=1)dir=2; //80为向下
if(key==75dir!=4)dir=3; //75为向左
if(key==77dir!=3)dir=4; //77为向右
}
if (key==32)
{ while(1) if((key=getch( ))==32) break; } //32为空格键,这儿用来暂停
}
if (Flag==0) //如没吃食物,才执行下面操作擦掉蛇尾
{ gtxy(snk.x[snk.len-1],snk.y[snk.len-1]); printf(" "); }
int i;
for (i = snk.len - 1; i 0; i--) //从蛇尾起每节存储前一节坐标值(蛇头除外)
{ snk.x[i]=snk.x[i-1]; snk.y[i]=snk.y[i-1]; }
switch (dir) //判断蛇头该往哪个方向移动,并获取最新坐标值
{ case 1: snk.y[0]--; break; //dir=1要向上移动
case 2: snk.y[0]++; break; //dir=2要向下移动
case 3: snk.x[0]-=2; break; //dir=3要向左移动
case 4: snk.x[0]+=2; break; //dir=4要向右移动
}
Color(9);
gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头
if (snk.x[0] == fod.x snk.y[0] == fod.y) //如吃到食物则执行以下操作
{ printf("\7"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //7是响铃
else Flag = 0; //没吃到食物Flag的值为0
if(snk.speed150) snk.speed= snk.speed+5; //作弊码,不让速度无限加快
}
void putFod( ) //投放食物
{ if (Flag == 1) //如吃到食物才执行以下操作,生成另一个食物
{ while (1)
{ int i,n= 1;
srand((unsigned)time(NULL)); //初始化随机数发生器srand( )
fod.x = rand( ) % (W - 4) + 2; //产生在游戏框范围内的一个x坐标值
fod.y = rand( ) % (H - 2) + 1; //产生在游戏框范围内的一个y坐标值
for (i = 0; i snk.len; i++) //随机生成的食物不能在蛇的身体上
{ if (fod.x == snk.x[i] fod.y == snk.y[i]) { n= 0; break;} }
if (n fod.x % 2 == 0) break; //n不为0且横坐标为偶数,则食物坐标取值成功
}
Color(12); //设定字符为红色
gtxy(fod.x, fod.y); printf("●"); //光标到取得的坐标处打印食物
}
return;
}
int Over( ) //判断游戏是否结束的函数
{ int i;
Color(7);
gtxy(2,H+1); printf(“暂停键:space.”); //以下打印一些其它信息
gtxy(2,H+2); printf(“游戏得分:%d”,score);
if (snk.x[0] == 0 || snk.x[0] == W) return 1; //蛇头触碰左右边界
if (snk.y[0] == 0 || snk.y[0] == H) return 1; //蛇头触碰上下边界
for (i = 1; i snk.len; i++)
{ if (snk.x[0] == snk.x[i] snk.y[0] == snk.y[i]) return 1; } //蛇头触碰自身
return 0; //没碰到边界及自身时就返回0
}
void Color(int a) //设定颜色的函数
{ SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ),a ); }
Rem 窗体创建三个单选框按钮,Option1、Option2、Option3
Private Sub Form_Activate()
Option1.Caption = "石头"
Option2.Caption = "剪刀"
Option3.Caption = "布"
Option1.Value = False
Option2.Value = False
Option3.Value = False
End Sub
Private Sub Option1_Click()
Randomize
Select Case Int(3 * Rnd)
Case 0: MsgBox "对方也出石头!继续!"
Case 1: MsgBox "哈哈!你赢了!对方出的是剪刀!奖励你一个苹果!"
Case 2: MsgBox "你输了!对方出的是布哦!不好意思,苹果给对方了哈!"
End Select
Option1.Value = False
End Sub
Private Sub Option2_Click()
Randomize
Select Case Int(3 * Rnd)
Case 0: MsgBox "你输了!对方出的是石头哦!不好意思,苹果给对方了哈!"
Case 1: MsgBox "对方也出剪刀!继续!"
Case 2: MsgBox "哈哈!你赢了!对方出的是布!奖励你一个苹果!"
End Select
Option2.Value = False
End Sub
Private Sub Option3_Click()
Randomize
Select Case Int(3 * Rnd)
Case 0: MsgBox "哈哈!你赢了!对方出的是石头!奖励你一个苹果!"
Case 1: MsgBox "你输了!对方出的是剪刀哦!不好意思,苹果给对方了哈!"
Case 2: MsgBox "对方也出布!继续!"
End Select
Option3.Value = False
End Sub
/*一个火柴人游戏,亲自验证,可运行*/
/*在编译时添加如下命令:-std=c++11,否则会编译错误*/
#include cstdio
#include cstdlib
#include Windows.h
#include thread
#include conio.h
using namespace std;
const unsigned char CTRL_KEY = 0XE0;
const unsigned char LEFT = 0X4B;
const unsigned char RIGHT = 0X4D;
const unsigned char DOWN = 0X50;
const unsigned char UP = 0X48;
int men2[2] = {0,0};
int women2[2]={10,10};
int Game();
void gotoxy( int x, int y ) //光标移动到(x,y)位置
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
int clean( int mm, int nn )
{
gotoxy ( mm, nn );
printf ( " " );
gotoxy ( mm,nn+1);
printf ( " " );
gotoxy ( mm,nn+2);
printf (" ");
}
int men( int x, int y )
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN);
gotoxy( x, y );
printf(" O");
gotoxy( x, y+1 );
printf("H");
gotoxy( x, y+2 );
printf("I I");
}
int women( int i, int j )
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);
gotoxy( i+1,j );
printf(" O");
gotoxy( i+1,j+1 );
printf("H");
gotoxy( i,j+2 );
printf("/I I\\");
}
int m=10, n=10;
int x=0;int y=0;
int TorF()
{
if ( x == m y == n ) return 1;
else return 0;
}
int womenmove()
{
int turn;
int YNbreak=0;
while( YNbreak == 0 )
{
YNbreaak = TorF();
turn=rand()%3;
clean( m, n );
if( m x ) m++;
else m--;
if( m == x )
{
if( n y ) n++;
else n--;
}
if ( m 0 ) m = 0;
if ( m = 75 ) m = 75;
if ( n 0 ) n = 0;
if ( n = 22 ) n = 22;
women( m,n );
women2[0]=m;
women2[1]=n;
Sleep(100);
}
system ( "cls" );
gotoxy ( 28, 10 );
printf ( "You died!!!\n" );
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE);
system ( "pause" );
exit(0);
return 0;
}
int menmove()
{
system( "cls" );
while (1)
{
switch( getch())
{
case UP:y--;break;
case DOWN:y++;break;
case LEFT:x--;break;
case RIGHT:x++;break;
}
system( "cls" );
if ( x 0 ) x = 0;
if ( x 77 ) x = 77;
if ( y 0 ) y = 0;
if ( y 22 ) y = 22;
men( x, y );
men2[0] = x;
men2[1] = y;
}
}
int Game()
{
women( 10, 10 );
men( 0, 0 );
int t = 0;
thread qq( womenmove );
menmove();
qq.join();
return 0;
}
int main()
{
system( "mode con cols=80 lines=25" );
printf ( "游戏开始后,随机按下一个键,唤醒你的蓝色小人.如果你被红色的老女人碰到了,那么你就死了\n" );
printf ( "方向键操控小人\n" );
system ( "pause" );
system ( "cls" );
Game();
return 0;
}
/*留下您的赞再拿走,谢谢!*/
那你就自己做个猜数字好了
import java.util.*;
import java.io.*;
public class CaiShu{
public static void main(String[] args) throws IOException{
Random a=new Random();
int num=a.nextInt(100);
System.out.println("请输入一个100以内的整数:");
for (int i=0;i=9;i++){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str=bf.readLine();
int shu=Integer.parseInt(str);
if (shunum)
System.out.println("输入的数大了,输小点的!");
else if (shunum)
System.out.println("输入的数小了,输大点的!");
else {
System.out.println("恭喜你,猜对了!");
if (i=2)
System.out.println("你真是个天才!");
else if (i=6)
System.out.println("还将就,你过关了!");
else if (i=8)
System.out.println("但是你还……真笨!");
else
System.out.println("你和猪没有两样了!");
break;}
}
}
}