标签:lse 结构 math attr get 检查 down 清除 时间
#include <stdio.h> #include <stdlib.h> #include<windows.h> #include <windows.h> #include<conio.h> #include<time.h> #include <math.h> //**************************************// #define X 23//地图的x轴 #define Y 75//地图的y轴 #define UP 0 #define DOWN 1 #define LEFT 2 #define RIGHT 3 #define WAIT_TIME 200//等待蛇刷新的时间,可以说是速度 修改可变速 int map_0[X][Y];//地图 int Snake[X*Y][2]; // 蛇 int Slength; //蛇的长度 int direction; int score=0; int pdEatFood=0; void start() { int select = 0; A: csh(); huaMap(); putfood(); while(1) { huaSnake(); Sleep(WAIT_TIME); intokey(); move(); dy_fs(); if(gameover()) { system("cls"); //清除屏幕内容 break; } if(map_0[Snake[0][0]][Snake[0][1]]==-1) { map_0[Snake[0][0]][Snake[0][1]]=0; pdEatFood=1; putfood(); score+=10; } } printf("Game Over\n"); printf("最终得分: %d\n",score); printf("按1重玩,按其他键退出\n"); select = getch(); if(select == ‘1‘) { goto A; }else { printf("Bye Bye!!!\n"); return ; } } void csh()//初始化 { srand((unsigned)time(NULL)); //设置种子为现在的时间 Slength=4; gotoxy(0,0); CONSOLE_CURSOR_INFO cursor_info = {1, 0}; //清除光标 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); int x,y; Snake[0][0]=X/2; Snake[0][1]=Y/2; for(x=0;x<X;x++){ map_0[x][0]=1; map_0[x][Y-1]=1; } for(y=1;y<Y-1;y++){ map_0[0][y]=1; map_0[X-1][y]=1; } for(x=1;x<4;x++){ //初始化蛇的坐标 Snake[x][0]=Snake[0][0]+x; Snake[x][1]=Snake[0][1]; } direction=UP; } void huaMap()//画地图 { int x,y; for(x=0;x<X;x++){ for(y=0;y<Y;y++){ if(map_0[x][y]==1){ printf("#"); } if(map_0[x][y]==0){ printf(" "); } } printf("\n"); } } void huaSnake()//画蛇 { int x; for(x=0;x<Slength;x++) { gotoxy(Snake[x][0],Snake[x][1]); printf("@"); } } void gotoxy(int i,int j)//移动光标 { COORD position={j,i}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position); } void move() { int i; gotoxy(Snake[Slength-1][0],Snake[Slength-1][1]);//擦除尾巴 printf(" "); for(i=Slength-1;i>0;i--) //从尾巴开始,每一个点的位置等于它前面一个点的位置 { Snake[i][0]=Snake[i-1][0]; Snake[i][1]=Snake[i-1][1]; } switch(direction) { case UP: Snake[0][0]--; break; case DOWN: Snake[0][0]++; break; case LEFT: Snake[0][1]--; break; case RIGHT: Snake[0][1]++; break; } if(pdEatFood){ Slength++; pdEatFood=0; } } void intokey() { if(kbhit()!=0) //hit()函数 检查当前是否有键盘输入,若有则返回一个非0值,否则返回0 { char in; while(!kbhit()==0) //如果玩家输入了多个按键,以最后一个按键为准 in=getch(); switch(in) { case ‘w‘: case ‘W‘: if(direction!=DOWN) //防止缩头 direction=UP; break; case ‘s‘: case ‘S‘: if(direction!=UP) direction=DOWN; break; case ‘a‘: case ‘A‘: if(direction!=RIGHT) direction=LEFT; break; case ‘d‘: case ‘D‘: if(direction!=LEFT) direction=RIGHT; break; case ‘p‘: case ‘P‘: gotoxy(X,0); system("pause"); gotoxy(X,0); printf(" "); // 消去下面的按任意键继续 break; } } } int check(int ii,int jj){// 检查是否能投放食物 if(map_0[ii][jj]==1) return 0; if(ii==0 || jj==0 || ii==X-1 || jj==Y-1) return 0; int i; for(i=0;i<Slength;i++){ if(ii==Snake[i][0] && jj==Snake[i][1]) return 0; } return 1; } void putfood() { int i,j; do{ i=rand()%X; j=rand()%Y; }while(check(i,j)==0); map_0[i][j]=-1; gotoxy(i,j); printf("$"); } int gameover() { int isgameover=0; int sX,sY; sX=Snake[0][0],sY=Snake[0][1]; if(sX==0 || sX==X-1 || sY==0 || sY==Y-1) isgameover=1; int i; for(i=1;i<Slength;i++){ if(sX==Snake[i][0] && sY==Snake[i][1]) isgameover=1; } return isgameover; } void dy_fs() { gotoxy(X,0); gotoxy(X+1,0); printf("得分: %d\n",score); printf("控制: A W S D 键",score); } //*************************// int pop,sum[101],socore[101],nn; int vis[52]; struct node { int x; char y; } q[52]; struct node2 { int x[101]; char y[101]; }; struct typp { int x; int y } typ[101]; struct node2 people[101]; void ui() { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); printf("\n\n"); SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED); printf(" *******欢迎来到游戏世界****\n\n\n"); SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); printf(" ********1.比较大小********\n\n\n"); printf(" ********2.扎金花*********\n\n\n"); printf(" ********3.贪吃蛇*******\n\n\n"); printf(" ********4.规则查询*******\n\n\n"); printf(" ********5.退出游戏********\n\n\n"); printf(" 请输入序号继续:"); } void ui2() { printf("***********当前比分: "); for(int i=1; i<=pop; i++) { printf("%d号选手: %d ",i,socore[i]); } printf("\n\n\n"); printf(" ***********1.继续游戏*********\n\n"); printf(" ***********2.结束游戏*********\n\n"); printf(" 请输入序号继续:"); } void store_cards()//存储牌 { int r=1; for(int i=1; i<=13; i++) { for(int j=0; j<4; j++) { int a=i; q[r].x=i; q[r++].y=65+j; vis[i]=0; } } renovate(); } void renovate()//刷新 { srand((unsigned)time(0)); for(int i=1; i<=52; i++) { int a; a=rand()%52; if(a==0) { i--; continue; } struct node swap; swap=q[i]; q[i]=q[a]; q[a]=swap; } } void deal_cards(int number)//发牌记录总和 { int a,b; srand((unsigned)time(0)); for(int i=1; i<=nn; i++) { a=rand()%52; if(a==0) { i--; continue; } if(vis[a]==0) { people[number].x[i]=q[a].x; people[number].y[i]=q[a].y; sum[number]+=people[number].x[i]; vis[a]=1; } else { i--; } } printf("\n"); } void Show_cards(int number)//牌面展示 { printf("%d号选手:\n",number);//选手展示 for(int i=1; i<=nn; i++) { Sleep(900); printf("%d %c ",people[number].x[i],people[number].y[i]);//牌面 } printf("\n"); } /*******************比较大小**************/ int compare_judge(int pop,int nn) { if(pop*nn>52||pop*nn<=0) return 1; else return 0; } void compare(int sum[])//比较大小 { int n=0,b,a[110]={0},k=1,c=0,d=0,pp=0; for(int i=1; i<=pop; i++) { if(sum[i]>n) { n=sum[i]; b=i; c=b;//假如相同,记录 } else if(sum[i]==n) { d=b; if(c!=d){k=1;pp=0;} a[k++]=i; pp++; } } if(pp==0){ printf("\n\n%d号赢\n\n",b); socore[b]++;} else { printf("\n\n"); for(int i=1;i<=pp;i++) { printf("%d ",a[i]); socore[a[i]]++; } printf("%d号赢\n\n",b); } } void swap(int n,int a,int b) { int temp; temp=people[n].x[a]; people[n].x[a]=people[n].x[b]; people[n].x[b]=temp; temp=people[n].y[a]; people[n].y[a]=people[n].y[b]; people[n].y[b]=temp; } void typ_store(int n)//扎金花牌面分类 { if(people[n].x[1]>people[n].x[2]) swap(n,1,2); if(people[n].x[2]>people[n].x[3]) swap(n,2,3); if(people[n].x[1]>people[n].x[2]) swap(n,1,2); if(people[n].x[1]==people[n].x[2]&&people[n].x[2]==people[n].x[3])//炸弹 { typ[n].x=1; typ[n].y=people[n].x[1]+people[n].x[2]+people[n].x[3];// } else if(people[n].x[2]-people[n].x[1]==1&&people[n].x[3]-people[n].x[2]==1)//顺子 { if(people[n].y[1]==people[n].y[2]&&people[n].y[2]==people[n].y[3])//同花顺 { typ[n].x=2; typ[n].y=people[n].x[1]+people[n].x[2]+people[n].x[3]; } else { typ[n].x=4; typ[n].y=people[n].x[1]+people[n].x[2]+people[n].x[3]; } } else if(people[n].y[1]==people[n].y[2]&&people[n].y[2]==people[n].y[3])//同花 { typ[n].x=3; typ[n].y=people[n].x[1]+people[n].x[2]+people[n].x[3]; } else if(people[n].x[1]==people[n].x[2]||people[n].x[1]==people[n].x[3]||people[n].x[2]==people[n].x[3])//对子 { typ[n].x=5; if(people[n].x[1]==people[n].x[2]) { typ[n].y=people[n].x[1]+people[n].x[2]; } else if(people[n].x[1]==people[n].x[2]) { typ[n].y=people[n].x[1]+people[n].x[3]; } else if(people[n].x[2]==people[n].x[3]) { typ[n].y=people[n].x[2]+people[n].x[3]; } } else { typ[n].x=6; typ[n].y=people[n].x[1]+people[n].x[2]+people[n].x[3]; } } int cmp(const void *a,const void *b) //结构体排序 { return (*(struct typp*)a).x<(*(struct typp*)b).x; } void compare2() { int min=15; int c; for(int i=1; i<=pop; i++) { if(typ[i].x<min) { min=typ[i].x; c=i; } } int b=min; for(int i=2; i<=pop; i++) { if(typ[i].x!=b) continue; else { if(typ[i].y>typ[c].y) c=i; else if(typ[i].y==typ[c].y)//总和相同 { if(people[i].y[1]>people[c].y[1]) { c=i; } else if(people[i].y[1]==people[c].y[1])//第一张牌相同 { if(people[i].y[2]>people[c].y[2]) { c=i; } else if(people[i].y[2]==people[c].y[2])//第二张牌相同 { if(people[i].y[3]>people[c].y[3]) { c=i; } } } } } } printf("\n\n%d号赢\n\n",c); socore[c]++; } void ccccccccccc() { } ////////////////////////////////////// int main() { for(;;)//游戏无限循环 { ui(); int a; scanf("%d",&a); memset( socore, 0, sizeof(socore) ); /**************比大小****************/ if(a==1) { store_cards(); for(;;) { printf(" 请输入游戏人数:"); scanf("%d",&pop); printf(" 请输入每个人可获得的手牌数:"); scanf("%d",&nn); if(compare_judge(pop,nn)) { system("cls"); printf(" 超出牌数范围,请重新输入:\n"); continue; } else break; } system("cls"); for(;;)//比较无限循环 { memset( vis, 0, sizeof(vis) ); memset( sum, 0, sizeof(sum) ); int b; printf("\n\n\n"); renovate(); for(int i=1; i<=pop; i++) { deal_cards(i); Show_cards(i); } compare(sum); for(;;)//判断是否继续 { ui2(); scanf("%d",&b); if(b==1||b==2) { system("cls"); break; } else { system("cls"); printf(" 错误指令\n"); continue; } break; } if(b==2) break; } } /*******************扎金花****************************************/ else if(a==2) { nn=3; int b; store_cards(); printf(" 请输入游戏人数:"); scanf("%d",&pop); system("cls"); for(;;) { memset( vis, 0, sizeof(vis) ); memset( sum, 0, sizeof(sum) ); for(int i=1; i<=pop; i++) { deal_cards(i); typ_store(i); Show_cards(i); if(typ[i].x==6) printf("类型:普通\n"); else if(typ[i].x==1) printf("类型:炸弹\n"); else if(typ[i].x==2) printf("类型:同花顺\n"); else if(typ[i].x==3) printf("类型:同花\n"); else if(typ[i].x==4) printf("类型:顺子\n"); else if(typ[i].x==5) printf("类型:对子\n"); } compare2(); for(;;)//判断是否继续 { ui2(); scanf("%d",&b); if(b==1||b==2) { system("cls"); break; } else { system("cls"); printf(" 错误指令\n"); continue; } break; } if(b==2) break; } } /***************规则查询*********************************/ else if(a==4) { system("cls"); FILE*p; p=fopen("规则.txt","r"); char ch=‘c‘; for(; ch!=‘a‘;) { ch=fgetc(p); putchar(ch); } fclose(p); for(;;) { int b; printf(" 按0继续:"); scanf("%d",&b); if(b==0) { system("cls"); break; } else { system("cls"); printf(" 错误指令\n"); continue; } break; } } /***************退出游戏*********************************/ else if(a==5) { printf(" 按任意键退出"); return 0; } /*********************贪吃蛇****************************/ else if(a==3) { start(); } /***************错误指令********************************/ else { system("cls"); printf(" 错误指令\n"); } } }
标签:lse 结构 math attr get 检查 down 清除 时间
原文地址:https://www.cnblogs.com/hahaee/p/12196038.html