find the pairs
2015-11-20 10:55
357 查看
简版c语言“找你妹”(find the pairs)游戏
功能:
1.使用8个随机对(pairs)填充4x4矩阵(元素为ASCII字符);
2.短暂的显示随机矩阵;
3.允许玩家随机选择两个元素,并显示内容;
4.若玩家选择正确,则保持显示,否则隐藏;
5.玩家可以在任何时候按q退出游戏;
6.一旦8对find成功,结束游戏;
demo1:
demo2:
感谢bin童鞋和yu童鞋。
如有不对,欢迎指正!
功能:
1.使用8个随机对(pairs)填充4x4矩阵(元素为ASCII字符);
2.短暂的显示随机矩阵;
3.允许玩家随机选择两个元素,并显示内容;
4.若玩家选择正确,则保持显示,否则隐藏;
5.玩家可以在任何时候按q退出游戏;
6.一旦8对find成功,结束游戏;
demo1:
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<windows.h> void printMatrix(int choose[],char c[]) { int i,j; char question = 63; for(i = 0;i<4;i++) { for(j = 0;j<4;j++) { if(choose[i*4+j] == 1) printf("%c ",c[i*4+j]); else printf("%c ",question); } printf("\n"); } } int main() { int i,j; srand(time(0)); char c[16] = {0}; for(i = 0;i<8;i++) { c[i] = rand()%128; c[i+8] = c[i]; } for(i = 0;i<100;i++) { int j1 = rand()%16; int j2 = rand()%16; char tmp = c[j1]; c[j1] = c[j2]; c[j2] = tmp; } for(i = 0;i<4;i++) { for(j = 0;j<4;j++) { printf("%c ",c[i*4+j]); } printf("\n"); } Sleep(2000); int choose[16] = {0}; int k = 0; while(1) { system("cls"); printMatrix(choose,c); printf("please input your choose ? Row Column\n"); int row1,column1; scanf("%d %d",&row1,&column1); int num1 = row1*4+column1-5; choose[num1] = 1; system("cls"); printMatrix(choose,c); printf("please input your choose ? Row Column\n"); int row2,column2; scanf("%d %d",&row2,&column2); int num2 = row2*4+column2-5; choose[num2] = 1; system("cls"); printMatrix(choose,c); Sleep(2000); if(c[num1] != c[num2]) { choose[num1] = 0; choose[num2] = 0; } k++; int mark = 1; int q; for(q = 0;q<16;q++) { if(choose[q]==0) { mark = 0; break; } } if(mark == 1) break; } system("cls"); printMatrix(choose,c); printf("total steps:%d",k); return 0; }
demo2:
#include <iostream> #include <vector> #include <windows.h> #include <string> #include <conio.h> using namespace std; void main() { char * a1 = new char[16]; bool * a2 = new bool[16]; memset(a2, false, 16); for(int i = 0; i < 16; i += 2){ char temp = rand() % 256; do{ temp = rand() % 256; }while(temp == '\r' || temp == ' ' || temp < 0); a1[i] = temp; a1[i + 1] = temp; } for(int i = 0; i < 16; i++){ int buffer = rand() % 16; swap(a1[i], a1[buffer]); } for(int i = 0; i < 16; i++){ cout<<a1[i]; if((i + 1) % 4 == 0 && i > 0){ cout<<endl; } else { cout<<" "; } } //Sleep(3000); //system("cls"); //产生随机数组完毕 string str1 = "", str2 = ""; int select1, select2; char isGoingOn ; while(true){ cout<<"输入这次判断的结果:"<<endl; char ch = _getch(); cout<<ch; while(isdigit(ch)){ str1 += ch; ch = _getch(); cout<<ch; } if(ch == 'q') exit(0); ch = _getch(); cout<<ch; while(isdigit(ch)){ str2 += ch; ch = _getch(); cout<<ch; } if(ch == 'q') exit(0); int index1 = stoi(str1) - 1; int index2 = stoi(str2) - 1; if(a1[index1] == a1[index2] && index1 != index2){ a2[index1] = a2[index2] = true; } str1 = ""; str2 = ""; cout<<"输出如下:"<<endl; for(int i = 0; i < 16; i++){ if(a2[i]) cout<<a1[i]; else cout<<" "; if((i + 1) % 4 == 0 && i > 0){ cout<<endl; } else { cout<<" "; } } } delete a1; delete a2; return 0; }
感谢bin童鞋和yu童鞋。
如有不对,欢迎指正!
相关文章推荐
- 如何组织构建多文件 C 语言程序(二)
- 如何写好 C main 函数
- Lua和C语言的交互详解
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- 使用C语言判断英文字符大小写的方法
- c语言实现的带通配符匹配算法
- C语言实现顺序表基本操作汇总
- C语言中计算正弦的相关函数总结
- 使用C语言详解霍夫曼树数据结构
- 探讨C语言的那些小秘密之断言
- C语言实现BMP转换JPG的方法
- 深入探讨C语言中局部变量与全局变量在内存中的存放位置
- C语言查找数组里数字重复次数的方法
- C语言泛型编程实例教程
- C语言中使用lex统计文本文件字符数