关闭

find the pairs

标签: c语言找你妹
368人阅读 评论(0) 收藏 举报
分类:

简版c语言“找你妹”(find the pairs)游戏
功能:
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童鞋。

如有不对,欢迎指正!

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:357584次
    • 积分:4248
    • 等级:
    • 排名:第7167名
    • 原创:131篇
    • 转载:24篇
    • 译文:5篇
    • 评论:60条
    最新评论