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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int row = 4;
const int col = 4;
void checkCards(int myArray[][col], bool cardBoard[][col], int x1, int x2, int y1, int y2);
void showArray (int array [][col]);
void shuffleCards (int myArray [][col]);
void displayBoard(int myArray[][col], char cardBoard[][col]);
void initializeBoard(bool cardBoard[][col], int myArray[][col]);
bool gameOver (bool cardBoard[][col]);
int main () {
srand (time(0));
int myArray [row][col] = { {1,1,2,2},{3,3,4,4},{5,5,6,6},{7,7,8,8} };
bool cardBoard[row][col];
int x1, x2, y1, y2 = 0;
shuffleCards(myArray);
initializeBoard(cardBoard, myArray);
showArray(myArray);
do {
cout << "Please enter the x position followed by the y position of the card you would like to flip." << endl;
cin >> x1 >> y1;
cout << "Enter the x and y position of the second card." << endl;
cin >> x2 >> y2;
while ((x1 == x2) && (y1 == y2)) {
cout << "Those coordinates were duplicates!" << endl;
cout << "Please enter the x position followed by the y position of the card you would like to flip." << endl;
cin >> x1 >> y1;
cout << "Enter the x and y position of the second card." << endl;
cin >> x2 >> y2;
}
checkCards (myArray, cardBoard, x1, x2, y1, y2);
}
while ((gameOver(cardBoard)) != (false));
return 0;
}
//------------------------------------------------------------------------------------------------
void showArray (int array [][col]) {
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
cout << array[i][j];
}
cout << endl;
}
}
//------------------------------------------------------------------------------------------------
void shuffleCards(int myArray[][col]) {
for (int s=0; s <= 20; s++){
int x = rand() % 4;
int y = rand() % 4;
for (int i=0; i<row; i++) {
int temp=myArray[i][i];
myArray[i][i]=myArray[x][y];
myArray[x][y]=temp;
}
}
}
//------------------------------------------------------------------------------------------------
void displayBoard(int myArray[][col], bool cardBoard[][col]) {
cout << " 1 2 3 4 \n";
cout << " =========\n";
for(int x=0;x <row; x++)
{
cout << x+1 << " | ";
for(int y=0;y <col; y++)
{
if (cardBoard [x][y])
cout << myArray [x][y] << " ";
else
cout << "* ";
}
cout << "|\n";
}
cout << " =========\n";
}
//------------------------------------------------------------------------------------------------
void initializeBoard(bool cardBoard[][col], int myArray[][col]) {
cout << " 1 2 3 4 \n";
cout << " =========\n";
for(int x=0;x < row ; x++)
{
cout << x+1 << " | ";
for(int y=0;y < col; y++)
{
cardBoard [x][y] = false;
cout << "* ";
}
cout << "|\n";
}
cout << " =========\n";
}
//------------------------------------------------------------------------------------------------
void checkCards(int myArray[][col], bool cardBoard[][col], int x1, int x2, int y1, int y2){
x1--;
x2--;
y1--;
y2--;
cout << myArray[x1][y1] << " " << myArray [x2][y2];
if (myArray[x1][y1] == myArray[x2][y2])
{
cardBoard[x1][y1] = myArray[x1][y1];
cardBoard[x2][y2] = myArray[x2][y2];
cout << "Congratulations, you found a match!";
cout << endl;
displayBoard(myArray, cardBoard);
}
else
{
cout << "No match found.";
cout << endl;
displayBoard(myArray, cardBoard);
}
}
bool gameOver (bool cardBoard[][col]){
bool gameFinish = false;
for (int x=0; x < row; x++)
for (int y=0; y < col; y++)
if ((cardBoard[x][y]) == true){
return gameFinish = true;
}
else{
return gameFinish = false;
}
}
|