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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
#include <fstream>
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
const short DIMENSION = 4;
const short SPACES = DIMENSION*DIMENSION;
const short MAX_NUMBER = 8;
//initializes vacant array so that all elements are true
void initialize(bool vacant[][DIMENSION]);
//initializes cards array with random values
void initialize(char cards[][DIMENSION], bool vacant[][DIMENSION]);
//inputs char (1-8) into 2 random places in cards array
void insert(char cards[][DIMENSION], bool vacant[][DIMENSION], short number);
//converts short digit (1-8) to equivalent char type
char convert(short digit);
//displays cards array
void display(char cards[][DIMENSION]);
//initializes revealed cards
void initialize_revealed(char revealed_cards[][DIMENSION]);
//returns false if there are still some cards that are not revealed (denoted by 'x')
//returns true otherwise
bool finished(char revealed_cards[][DIMENSION]);
//clears screen
void clr();
int main()
{
using namespace std;
char choice = 'n';
char cards[DIMENSION][DIMENSION];
char revealed_cards[DIMENSION][DIMENSION];
bool vacant[DIMENSION][DIMENSION];
do
{
initialize(cards, vacant);
initialize_revealed(revealed_cards);
display(revealed_cards);
bool end = false;
short count(0);
while(!end)
{
cout << "Enter coordinates of first desired location in x,y format: ";
char comma;
short x1, y1, x2, y2;
cin >> x1 >> comma >> y1;
revealed_cards[y1-1][x1-1] = cards[y1-1][x1-1];
clr();
display(revealed_cards);
cout << "Enter coordinates of second desired location in x,y format: ";
cin >> x2 >> comma >> y2;
revealed_cards[y2-1][x2-1] = cards[y2-1][x2-1];
clr();
display(revealed_cards);
if(cards[y2-1][x2-1] != cards[y1-1][x1-1])
{
revealed_cards[y2-1][x2-1] = 'x';
revealed_cards[y1-1][x1-1] = 'x';
}
end = finished(revealed_cards);
count++;
}
cout << "Finished in " << count << " tries.\n";
cout << "Do you wish to run this program again? (y/n): ";
cin >> choice;
choice = tolower(choice);
while((choice != 'y') && (choice != 'n'))
{
cout << "Incorrect input. Do you wish to run this program again? (y/n): ";
cin >> choice;
}
}while(choice == 'y');
cout << "End of the program.\nEnter key to continue\n\n";
system("pause");
return 0;
}
void initialize(bool vacant[][DIMENSION])
{
using namespace std;
for(short i = 0; i < DIMENSION; i++)
{
for(short j = 0; j < DIMENSION; j++)
{
vacant[i][j] = true;
}
}
}
void initialize(char cards[][DIMENSION], bool vacant[][DIMENSION])
{
using namespace std;
srand(time(0));
for(short i = 0; i < MAX_NUMBER; i++)
{
insert(cards, vacant, i);
insert(cards, vacant, i);
}
return;
}
void insert(char cards[][DIMENSION], bool vacant[][DIMENSION], short number)
{
using namespace std;
short random = (rand() % SPACES);
short row, column;
row = random / DIMENSION;
column = random % DIMENSION;
char char_number = convert(number + 1);
while(!vacant[row][column])
{
column++;
if(column >= DIMENSION)
{
column = 0;
row++;
if(row >= DIMENSION)
{
row = 0;
}
}
}
cards[row][column] = char_number;
vacant[row][column] = false;
}
char convert(short digit)
{
using namespace std;
if(digit == 1)
{
return '1';
}
else if(digit == 2)
{
return '2';
}
else if(digit == 3)
{
return '3';
}
else if(digit == 4)
{
return '4';
}
else if(digit == 5)
{
return '5';
}
else if(digit == 6)
{
return '6';
}
else if(digit == 7)
{
return '7';
}
else//digit == 8
{
return '8';
}
}
void display(char cards[][DIMENSION])
{
using namespace std;
cout << " 1 2 3 4\n";
cout << " --------\n";
for(short i = 0; i < DIMENSION; i++)
{
cout << i+1 << " | ";
for(short j = 0; j < DIMENSION; j++)
{
cout << cards[i][j] << " ";
}
cout << endl;
}
return;
}
void initialize_revealed(char revealed_cards[][DIMENSION])
{
using namespace std;
for(short i = 0; i < DIMENSION; i++)
{
for(short j = 0; j < DIMENSION; j++)
{
revealed_cards[i][j] = 'x';
}
}
return;
}
bool finished(char revealed_cards[][DIMENSION])
{
using namespace std;
for(short i = 0; i < DIMENSION; i++)
{
for(short j = 0; j < DIMENSION; j++)
{
if(revealed_cards[i][j] == 'x')
{
return false;
}
}
}
return true;
}
void clr()
{
using namespace std;
cout << string(50, '\n');
}
|