Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Exercise 12.16
- //
- // Created by Gustavo Silveira on 11/21/13.
- // Copyright (c) 2013 Gustavo Silveira. All rights reserved.
- //
- // !! The "system("clear")" doesn't work on my compiler, but maybe it will work on yours.
- #include <iostream>
- #include <time.h>
- #include <stdlib.h>
- #include <cstdlib>
- #include <unistd.h>
- #include <term.h>
- using namespace std;
- int C(4);
- int r1, c1, r2, c2; //row one and two, collum one and two
- int a,b,c,d;
- int tries = 0; //accumulator for how many tries untill the end
- char front[4][4] = {
- {'1','2','3','4'},
- {'5','6','7','8'},
- {'1','2','3','4'},
- {'5','6','7','8'},
- };
- char back [4][4] = {
- {'*','*','*','*'},
- {'*','*','*','*'},
- {'*','*','*','*'},
- {'*','*','*','*'},
- };
- void swap(char&a, char&b);
- void sort(char Front[][4]);
- void print(char ar[][4]);
- bool check(char Back[][4]);
- void input();
- int main ()
- {
- srand(time(0));
- system("clear");
- sort(front); //Shuffle numbers in the chosen array
- print(back); //Print the game
- while(true) {
- input(); //Inputs and checks if it is a valid number
- if (front[r1][c1] == front[r2][c2]) { //If the two cards match it will print those cards
- back[r1][c1] = front[r1][c1];
- back[r2][c2] = front[r2][c2];
- tries++;
- system("clear"); //Clear screen (doesn't work on XCode)
- check(back);
- print(back);
- }
- if (front[r1][c1] != front[r2][c2]) { //If cards don't match it will print and after some seconds it be back "*"
- back[r1][c1] = front[r1][c1];
- back[r2][c2] = front[r2][c2];
- system("clear");
- print(back);
- cout << endl;
- sleep(3); //Wait to come to the previous screen
- system("clear");
- back[r1][c1] = '*';
- back[r2][c2] = '*';
- print(back);
- }
- tries++; //count how many tries untill find all the matches
- if (check(back) == true) { //Check if the game is over and tells how many tries were needed
- cout << "You finished in " << tries << " tries.";
- cout << endl;
- break;
- }
- }
- return 0;
- }
- void input() {
- cout << endl << "Type 4 ints separated by a space, row 1, column 1, row 2, column 2: ";
- cin >> a >> b >> c >> d;
- cout << endl;
- r1=a-1;
- c1=b-1;
- r2=c-1;
- c2=d-1;
- while (((r1==r2) && (c1==c2)) || ((r1>=C)||(c1>=C)||(r2>=C)||(c2>=C))) {
- cout << "Wrong value, try different cards: ";
- cin >> a >> b >> c >> d;
- cout << endl;
- r1=a-1;
- c1=b-1;
- r2=c-1;
- c2=d-1;
- }
- }
- bool check(char Back[][4]) {
- for (int r=0; r<C; r++) {
- for (int c=0; c<C; c++) {
- if (back[r][c] == '*') {
- return false;
- }
- }
- }
- return true;
- }
- void print(char ar[][4]) {
- cout << "############## Memory Game ##############\n";
- cout << " 1 2 3 4\n";
- cout << " ----------\n";
- for (int r=0; r<C; r++) {
- cout << r+1 << "| ";
- for (int c=0; c<C; c++) {
- cout << ar[r][c] << " ";
- }
- cout << endl;
- }
- }
- void swap(char&a, char&b) {
- char tmp = a;
- a = b;
- b = tmp;
- }
- void sort(char Front[][4]) {
- for (int i=0; i<500; i++) {
- swap(front[rand()%4][rand()%4], front[rand()%4][rand()%4]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement