c++ memory matching game 記憶配對數字遊戲
(No: 0, 1, 2, 3, 4) (26 points) Write a program of the memory matching game.
(8 points) Read n and generate an n by n board randomly filled with the pairs of numbers from 1 to the n2/2. If n is odd, 0 is filled.
(6 points) The player can select two numbers each time.
(8 points) If two numbers match, they are left exposed. Otherwise, they return hiding.
(4 points) The game continues until all numbers are exposed and shows the times of wrong guesses.
A possible run may look like:
Enter the number: 4
* * * *
* * * *
* * * *
* * * *
Select numbers: 2 2 3 3
* * * *
* 6 * *
* * 1 *
* * * *
-------------------------
* * * *
* * * *
* * * *
* * * *
Select numbers: 2 2 4 3
* * * *
* 6 * *
* * * *
* * 6 *
Select numbers: 3 3 2 1
* * * *
2 6 * *
* * 1 *
* * 6 *
-------------------------
* * * *
* 6 * *
* * * *
* * 6 *
...
...
5 4 7 1
2 6 5 3
8 3 1 8
4 7 6 2
Times of wrong guesses: 5
//資工三丙 許巧慧 修改後的可自己設定n階,
#include
#include
#include
#include
using namespace std;
int main(void)
{
srand(time(0)); //取亂數的前面有寫上即可 不要放在迴圈內 不然亂數不完全都是一樣的
char comma, ans = 'y';
int n,m;
int member,temp=0;
int r1, c1, r2, c2, i(0),z;
bool gameover = false;
int moves;//開牌的次數
do{
cout << "輸入n階矩陣 元素是偶數唷"< cin >> n;
m=n*n;
temp = m%2;
}
while(temp!=0);
int start[m],cards[n][n];
bool cardstatus[n][n];
//初始化
cout<
member=0;
for(int s=0; s {
member = member+1;
start[s] = member;
s=s+1;
start[s] = member;
}
cout<
int temp2;
for (int x=0; x {
int i=rand()%(m-1)+1;
temp2=start[x];
start[x]=start[i];
start[i]=temp2;
}
cout<
//把亂數放進卡片裡,測試用的,請自行刪掉
z=0;
for (int r=0; r {
for (int c=0; c {
cards[r][c]=start[z];
// cout<<<" ";="" 測試用,自行刪掉 z++;
}
cout<
}
do
{
moves = 0;
/*******************************/
//初始主畫面排版面
cout<<" ";
for (int i=1; i<=n; i++)
{
cout<<<" ";="" ="" }
cout<<" "<<"\n";
cout<<" ";
for (int i=0; i<=n; i++)
{
cout<<" -";
}
cout<
for (int r=0; r {
cout<<<" |="" "; for (int c=0; c {
cout<<"* ";
// cout<<<" ";="" ="" 測試用的,自行刪掉="" cardstatus[r][c] = false; //牌都沒有被翻過
}
cout<
}
cout<
/*******************************/
//遊戲開始前 選擇牌
do
{
cout<<"請選擇第一張牌:(行,列)";
cin>>r1>>comma>>c1;
while((1>r1||r1>n)||(1>c1||c1>n)){
cout<<"r1重新輸入";
cout<<"請選擇第一張牌:(行,列)";
cin>>r1>>comma>>c1;
};
while(cardstatus[r1-1][c1-1]!= false){
cout<<"請選擇第一張牌:(行,列)";
cin>>r1>>comma>>c1;
while((1>r1||r1>n)||(1>c1||c1>n)){
cout<<"重新輸入";
cout<<"請選擇第一張牌:(行,列)";
cin>>r1>>comma>>c1;
};
};
//*****//
do
{
if(r1==r2&&c1==c2)cout<<"和第一張牌重複";
cout<<"請選擇第二張牌:(行,列)";
cin>>r2>>comma>>c2;
while((1>r2||r2>n)||(1>c2||c2>n)){
cout<<"重新輸入";
cout<<"請選擇第二張牌:(行,列)";
cin>>r2>>comma>>c2;
};
while(cardstatus[r2-1][c2-1]!= false){
cout<<"請選擇第二張牌:(行,列)";
cin>>r2>>comma>>c2;
while((1>r2||r2>n)||(1>c2||c2>n)){
cout<<"重新輸入";
cout<<"請選擇第二張牌:(行,列)";
cin>>r2>>comma>>c2;
};
};
}while(r1==r2&&c1==c2);
//fix
r1--;
c1--;
r2--;
c2--;
/*******************************/
//選擇牌後的排版
cout<<" ";
for (int i=1; i<=n; i++)
{
cout<<<" ";="" ="" }
cout<<" "<<"\n";
cout<<" ";
for (int i=0; i<=n; i++)
{
cout<<" -";
}
cout< for (int r=0; r {
cout<<<" |="" "; for (int c=0; c {
if((r==r1)&&(c==c1))
{
cout<<<" "; }
else if((r==r2)&&(c==c2))
{
cout<<<" "; }
else if(cardstatus[r][c] == true) //如果狀態牌是開,表示配對成功的牌
{
cout<<<" ";="" 持續開著 }
else //沒有被選的
{
// cout<<<" ";="" ="" 測試用的,自行刪掉 cout<<"* ";
}
}
cout<
}
cout<<"請案enter"< cin.get(); //按enter
cin.get();
/*******************************/
//遊戲判斷
//兩張牌相同,表示這兩張牌狀態是開的 true
if (cards[r1][c1]==cards[r2][c2])
{
cout << "對到了,你好厲害喔!"<
cardstatus[r1][c1] = true; //牌保持開的
cardstatus[r2][c2] = true;
}
//跳畫面用
for (int b=0; b<=20; b++)
cout< //SYSTEM("CLS");
/****************/
//要進行下次選擇的排版
cout<<" ";
for (int i=1; i<=n; i++)
{
cout<<<" ";="" ="" }
cout<<" "<
cout<<" ";
for(int i=0; i<=n; i++)
{
cout<<" -";
}
cout<
for (int r=0; r {
cout<<<" |="" "; for (int c=0; c {
if (cardstatus[r][c] == true)//如果某些牌是以前配對成功的
{
cout<<<" ";="" 顯示他的號碼 }
else
{
cout<<"* ";
}
}
cout<
}
/****************/
//判斷遊戲是否結束了?
for (int r=0; r {
for (int c=0; c {
//如果配對未完成的
if(cardstatus[r][c]==false)
{
gameover = false;//遊戲還沒結束
break;
}
//如果配對都完成的
if(cardstatus[r][c]==true)
{
gameover = true;//遊戲結束
}
}
}
moves++;
cout << "你猜了"<<<"次"< }while(gameover != true); //如果遊戲還沒結束,持續迴圈
cout << "你贏了唷!!"<
cout << "還要繼續按y";
ans = cin.get();
}while(ans == 'y' || ans == 'Y'); //還要繼續按y
cin.get();
system("pause");
return 0;
}
參考:http://www.dreamincode.net/forums/topic/70680-memory-matching-game/
Previous in This Category: 一般時間運作-先輸入(時 分 秒)









