Created
November 21, 2018 18:21
-
-
Save Ademking/5b0285d65046cd4c25e8734efd4a15ca to your computer and use it in GitHub Desktop.
Memory Game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <cstdlib> | |
| #include <time.h> | |
| #include <ctime> | |
| using namespace std; | |
| void shuffle(char [][4]); | |
| void cls(); | |
| void sleepcp(int milliseconds); | |
| int main() | |
| { | |
| char comma, ans = 'y'; | |
| int r1, c1, r2, c2, i(0); | |
| char cards[4][4]; | |
| char cardstatus[4][4]; // array to store whether card is flipped or not, you can use char if bool doesn't work for you. | |
| bool gameover = false; | |
| int moves; | |
| do | |
| { | |
| moves = 0; | |
| cls(); // make sure the screen is empty | |
| cout << "============ Jeux de Memoire ============" << endl; | |
| cout << "\n" << endl; | |
| // shuffle | |
| shuffle(cards); | |
| // display board | |
| cout<<" 1 2 3 4\n"; | |
| cout<<" "; | |
| for (int i=0; i<=8; i++) | |
| { | |
| cout<<"-"; | |
| } | |
| cout<<endl; | |
| for (int r=0; r<4; r++) | |
| { | |
| cout<<r+1<<" | "; | |
| for (int c=0; c<4; c++) | |
| { | |
| cout<<"* "; | |
| cardstatus[r][c] = false; // use 0 for false and 1 for true if true/false is not supported. | |
| } | |
| cout<<endl; | |
| } | |
| cout<<endl; | |
| do // game starts from here as user will do moves here onwards. | |
| { | |
| do | |
| { | |
| //selection | |
| cout<<"\n\n[Case 1]:\n"; | |
| cout<<"Enter numero la ligne :\n"; | |
| cin>>r1; | |
| cout<<"Enter la colonne :\n"; | |
| cin>>c1; | |
| if(cardstatus[r1-1][c1-1] == true) // check if the card is flipped. | |
| { | |
| cout << "Cette case est déja affichée ! Selectionner une autre case."; | |
| } | |
| }while(cardstatus[r1-1][c1-1]!= false); | |
| do | |
| { | |
| cout<<"\n\n[Case 2]:\n"; | |
| cout<<"Enter la ligne :\n"; | |
| cin>>r2; | |
| cout<<"Enter la colonne :\n"; | |
| cin>>c2; | |
| if(cardstatus[r2-1][c2-1] == true) // check if the card is flipped. | |
| { | |
| cout << "Cette case est déja affichée ! Selectionner une autre case."; | |
| } | |
| if((r1==r2)&&(c1==c2)) | |
| { | |
| cout << "Vous ne pouvez pas selectionner la meme case 2 fois! \n"; | |
| continue; | |
| } | |
| }while(cardstatus[r2-1][c2-1]!= false); | |
| //fix | |
| r1--; | |
| c1--; | |
| r2--; | |
| c2--; | |
| //reveal | |
| cout<<" 1 2 3 4\n"; | |
| cout<<" "; | |
| for (int i=0; i<=8; i++) | |
| { | |
| cout<<"-"; | |
| } | |
| cout<<endl; | |
| for (int r=0; r<4; r++) | |
| { | |
| cout<<r+1<<" | "; | |
| for (int c=0; c<4; c++) | |
| { | |
| if ((r==r1)&&(c==c1)) | |
| { | |
| cout<<cards[r][c]<<" "; | |
| } | |
| else if((r==r2)&&(c==c2)) | |
| { | |
| cout<<cards[r][c]<<" "; | |
| } | |
| else if (cardstatus[r][c] == true) | |
| { | |
| cout<<cards[r][c]<<" "; | |
| } | |
| else | |
| { | |
| cout<<"* "; | |
| } | |
| } | |
| cout<<endl; | |
| } | |
| //match? | |
| if (cards[r1][c1]==cards[r2][c2]) // if cards match, keep them flipped. | |
| { | |
| cout << "Bravo!"<<endl; | |
| cardstatus[r1][c1] = true; | |
| cardstatus[r2][c2] = true; | |
| } | |
| cin.get(); | |
| cin.get(); | |
| //this pushes the next board onto a blank screen | |
| cls(); | |
| cout<<" 1 2 3 4\n"; | |
| for (int r=0; r<4; r++) // reprint the board, makes it easy for user to put a new guess. | |
| { | |
| cout<<r+1<<" | "; | |
| for (int c=0; c<4; c++) | |
| { | |
| if (cardstatus[r][c] == true) | |
| { | |
| cout<<cards[r][c]<<" "; | |
| } | |
| else | |
| { | |
| cout<<"* "; | |
| } | |
| } | |
| cout<<endl; | |
| } | |
| gameover = true; | |
| for (int r=0; r<4; r++) // check all card status, they all should be true/flipped to end the game. | |
| { | |
| for (int c=0; c<4; c++) | |
| { | |
| if(cardstatus[r][c]==false) | |
| { | |
| gameover = false; | |
| break; | |
| } | |
| } | |
| if(gameover == false) | |
| { | |
| break; | |
| } | |
| } | |
| moves++; // counting moves here. | |
| //repeat | |
| }while(gameover != true); // loop, the only way to get out is to finish the game! | |
| cout << "Felicitations, vous avez gagne!!!"<<endl; | |
| cout << moves << " coups pour finir."<<endl<<endl; | |
| cout << "Voulez-vous rejouer? (o=Oui/n=Non) : "; | |
| ans = cin.get(); | |
| }while(ans == 'o' || ans == 'O'); // If user wants to play game again. | |
| cin.get(); | |
| return 0; | |
| } | |
| void shuffle(char cards[][4]) | |
| { | |
| char start[16]={'A', 'B' , 'C', 'D' , 'E' , 'F' , 'G' , 'H', 'A', 'B' , 'C', 'D' , 'E' , 'F' , 'G' , 'H'} ; | |
| for (int s=0; s<=20; s++) | |
| { | |
| for (int x=0; x<16; x++) | |
| { | |
| srand((unsigned)time(NULL)); | |
| int i=rand()%15+1; | |
| int temp=start[x]; | |
| start[x]=start[i]; | |
| start[i]=temp; | |
| } | |
| } | |
| int i=0; | |
| for (int r=0; r<4; r++) // put values in cards here | |
| { | |
| cout << " "; | |
| for (int c=0; c<4; c++) | |
| { | |
| cards[r][c]=start[i]; | |
| cout<< cards[r][c] << " "; | |
| i=i+1; | |
| } | |
| cout<<endl; | |
| } | |
| sleepcp(1000); | |
| cls(); | |
| } | |
| // Sleep for n milliseconds | |
| void sleepcp(int milliseconds) | |
| { | |
| clock_t time_end; | |
| time_end = clock() + milliseconds * CLOCKS_PER_SEC/1000; | |
| while (clock() < time_end) | |
| { | |
| } | |
| } | |
| // clear the screen ( only windows users ) | |
| void cls() { | |
| system("cls"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment