作业介绍
第19课-记忆卡牌
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
using namespace std;
int main(){
int num[5][5]={0};//16张卡牌背面的数字,由1~8组成,初始化为0
int arr[5][5]={0};//标记卡牌是否被翻出,默认0未被翻出
int flag[9]={0};//标记数字1~8在num数组中出现的次数,默认为0
int temp;//随机生成的数字,由1-8组成
int ix,iy,ia,ib;
int count = 0;
cout << "记忆卡牌游戏规则" << endl;
cout << "现有16张数字卡牌,由数字1~8组成,每个数字只出现2次" << endl;
cout << "游戏者需要将卡牌翻至背面,找出所有相同的数字" << endl;
cout << "每一轮只允许翻两张卡牌" << endl;
cout << "如果卡牌数字相同,则将卡牌上有数字的一面显示出来" << endl;
cout << "否则将卡牌翻回正面,进行下一轮" << endl;
cout << "系统测量游戏者找出所有相同数字的卡牌所花费的步数(即进行多少轮)" << endl;
system("pause");
cout << endl;
srand(time(0));
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
while(true){
temp = rand()%8+1;
if(flag[temp]<2){
num[i][j]=temp;
flag[temp]++;
break;
}else{
continue;
}
}
}
}
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
cout << "* ";
}
cout << endl;
}
while(true){
count++;
int sum=0;
cout << "请输入第一次翻开卡片的行数和列数(使用空格隔开):" << endl;
cin >> ix >> iy;
arr[ix][iy]++;
system("cls");
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
if(arr[i][j] > 0) cout << num[i][j] << " ";
else cout << "* ";
}
cout << endl;
}
cout << "请输入第二次翻开卡片的行数和列数(使用空格隔开):" << endl;
cin >> ia >> ib;
arr[ia][ib]++;
system("cls");
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
if(arr[i][j] > 0) cout << num[i][j] << " ";
else cout << "* ";
}
cout << endl;
}
if(num[ix][iy] == num[ia][ib]){
cout << "恭喜您,此轮翻牌成功" << endl;
}
else{
cout << "很遗憾,此轮翻牌失败" << endl;
arr[ix][iy]--;
arr[ia][ib]--;
}
cout << "2秒后进入下一轮"<<endl;
Sleep(2000);
system("cls");
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
if(arr[i][j] > 0){
sum += 1;
cout << num[i][j] << " ";
}
else cout << "* ";
}
cout << endl;
}
if(sum == 16){
/*
string steps = "恭喜完成翻翻乐,使用步数为:"; //messagebox显示使用步数
char num1[100];
itoa(count,num1,10); //将记录玩家步数的变量sum,转成字符串,赋值给iToA数组
steps += num1;
MessageBox(0,steps.c_str(),"提示",MB_OK);//将获得的字符串显示在messagebox上
*/
cout << endl;
cout << "一共使用" << count << "步";
break;
}
}
return 0;
}
`
- 状态
- 已结束
- 题目
- 12
- 开始时间
- 2023-11-1 0:00
- 截止时间
- 2023-12-31 23:59
- 可延期
- 24 小时