1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void random(int[][4]);
int main()
{
int choice, r1, c1, r2, c2, cards[4][4], i(0);
bool cardstatus[4][4];
bool gameover = false;
int moves;
do
{
system("cls");
moves = 0;
random(cards);
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;
}
cout << endl;
}
cout << endl;
do
{
cout << "Please insert the row\n";
cin >> r1;
cout << "Please insert the column\n";
cin >> c1;
} while (cardstatus[r1 - 1][c1 - 1] != false);
do
{
cout << "Please insert the row\n";
cin >> r2;
cout << "Please insert the column\n";
cin >> c2;
} while (cardstatus[r2 - 1][c2 - 1] != false);
r1--;
c1--;
r2--;
c2--;
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;
}
if (cards[r1][c1] == cards[r2][c2])
{
cout << "Cards Matched!" << endl;
cout << "Congratulations, You won!!!" << endl;
cardstatus[r1][c1] = true;
cardstatus[r2][c2] = true;
}
else
{
cout << "You Lose!" << endl;
}
cout << "Enter 1 to play again. Enter 0 to quit. ";
cin >> choice;
} while (choice == 1);
cin.get();
return 0;
}
void random(int cards[][4])
{
int start[16] = { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 };
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++)
{
for (int c = 0; c<4; c++)
{
cards[r][c] = start[i];
i = i + 1;
}
cout << endl;
}
}
|