find the pairs

原创 2015年11月20日 10:55:35

简版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童鞋。

如有不对,欢迎指正!

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[leetcode] 373. Find K Pairs with Smallest Sums

You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Defi...

LeetCode 373 Find K Pairs with Smallest Sums

You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define...

LeetCode 373. Find K Pairs with Smallest Sums

原题网址:https://leetcode.com/problems/find-k-pairs-with-smallest-sums/ You are given two integer array...

Leetcode 373. Find K Pairs with Smallest Sums(Python)

睡不着咋办!题目大意给定两个数组,定义pair是一个二元组,每个元素分别取自两个数组。求pair和前k小的pair思路用堆,我们维持一个k最大堆即可代码class mHeap(object): ...

Leetcode 373. Find K Pairs with Smallest Sums

Leetcode 373. Find K Pairs with Smallest Sums

373. Find K Pairs with Smallest Sums

You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.  Def...

leetcode_373 Find K Pairs with Smallest Sums

题目分析给定两个递增的数组,从每个数组中任意取出一个数来组成不同的数字对,求前k个最小的数字对。 解题思路:方法1 利用暴力求解法; 方法2 借助STL中的multimap实现; 方法3 借助S...

[leetcode] 373. Find K Pairs with Smallest Sums

You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.  Def...

leetcode Find K Pairs with Smallest Sums

leetcode 373 find K pairs with Smallest Sums
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)