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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <windows.h>
using namespace std;
int locX, locY;
int select4[4][4] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int select6[6][6] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0 };
int select8[8][8] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
class Project{
public:
string val;
bool faceUp;
Project(){ // Constructor
faceUp = false;
}
void setVal(string v){
val = v;
}
void turnTile(){
faceUp = !faceUp;
}
string getVal(){
return val;
}
bool getFaceUp(){
return faceUp;
}
//end class-wide functions
//begin 6X6 grid
void setTiles6(){ // shuffle tiles
string pool6[36] = { "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME",
"MD", "MA", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME",
"MD", "MA" };
int a, b;
string temp;
for (int i = 0; i<30; i++){ // shuffle
a = rand() % 36;
b = rand() % 36;
temp = pool6[a];
pool6[a] = pool6[b];
pool6[b] = temp;
}
for (int y = 0; y<6; y++){
for (int x = 0; x<6; x++){
move6[x][y].setVal(pool6[x + (y * 6)]); // shuffled tiles now together
}
}
}
void showGrid6(){ // Output current grid
system("cls");
cout << " 1 2 3 4 5 6" << endl << endl;
for (int y = 0; y<6; y++){
cout << y + 1 << " ";
for (int x = 0; x<6; x++){
if (move6[x][y].getFaceUp() == true){ // show the state
cout << move6[x][y].getVal() << " ";
}
else{ // facing down, can't see
cout << (char) 177 << " ";
}
}
cout << endl;
cout << " ";
for (int x = 0; x<6; x++){
if (select6[x][y] == 1){ // selector arrow at coord
cout << (char) 94 << " ";
}
else{ //no arrow at coord
cout << " ";
}
}
cout << endl;
}
cout << endl;
}
void userMoves6(){ //move pointer
while (1){
while (1){
if (GetAsyncKeyState(VK_UP) & 1){ // UP
if ((locY - 1) < 0){ // If out of bounds
locY = 5;
}
else{
locY--;
}
break;
}
if (GetAsyncKeyState(VK_DOWN) & 1){ // DOWN
if ((locY + 1) > 5){ // If out of bounds
locY = 0;
}
else{
locY++;
}
break;
}
if (GetAsyncKeyState(VK_LEFT) & 1){ // LEFT
if ((locX - 1) < 0){ // If out of bounds
locX = 5;
}
else{
locX--;
}
break;
}
if (GetAsyncKeyState(VK_RIGHT) & 1){ // RIGHT
if ((locX + 1) > 5){ // If out of bounds
locX = 0;
}
else{
locX++;
}
break;
}
if ((GetAsyncKeyState(VK_SPACE) & 1)){ // SPACE = flips tile pointed at
if (move6[locX][locY].getFaceUp() == true){ // if pick already matched tile
cout << "You can't select this tile again!" << endl << endl;
continue;
}
else{
move6[locX][locY].turnTile();
return;
}
}
}
// following every key pressed
for (int y = 0; y<6; y++){
for (int x = 0; x<6; x++){
select6[x][y] = 0; // Clear array
}
}
select6[locX][locY] = 1; // Sets array at right position
showGrid6(); // Updates grid
}
}
int chose6(){
int holderX, holderY, remaining = 36, tries = 0;
char again;
bool secondTurn = false;
srand(time(NULL));
while (1){
setTiles6();
do{ // Loops till all matches made
showGrid6();
userMoves6();
if (secondTurn == true){ // If player turned the second card we have to look if they match
secondTurn = false;
tries++;
if ((locX == holderX) && (locY == holderY)){ // The user chose same tile
tries--;
cout << "Cannot pick same tile again" << endl;
continue;
}
if (move6[locX][locY].getVal() == move6[holderX][holderY].getVal()){ // tiles match
showGrid6();
cout << "You matched 2 tiles" << endl;
remaining -= 2;
}
else{ // tiles don't match
showGrid6();
cout << "The tiles are different" << endl;
move6[locX][locY].turnTile(); // flip again
move6[holderX][holderY].turnTile();
}
while (1){ // This is a pause, the user can read the text until he presses an arrow key
if (remaining == 0){ // Or if there aren't cards left
break;
}
if (GetAsyncKeyState(VK_UP) & 1){ // UP
if ((locY - 1) < 0){ // If out of bounds
locY = 5;
}
else{
locY--;
}
break;
}
if (GetAsyncKeyState(VK_DOWN) & 1){ // DOWN
if ((locY + 1) > 5){ // If out of bounds
locY = 0;
}
else{
locY++;
}
break;
}
if (GetAsyncKeyState(VK_LEFT) & 1){ // LEFT
if ((locX - 1) < 0){ // If out of bounds
locX = 5;
}
else{
locX--;
}
break;
}
if (GetAsyncKeyState(VK_RIGHT) & 1){ // RIGHT
if ((locX + 1) > 5){ // If out of bounds
locX = 0;
}
else{
locX++;
}
break;
}
}
for (int y = 0; y<6; y++){
for (int x = 0; x<6; x++){
select6[x][y] = 0; // Clear array
}
}
select6[locX][locY] = 1; // set array
showGrid6();
}
else{ // If this is the first tile flipped, stores coords
holderX = locX;
holderY = locY;
secondTurn = true;
}
} while (remaining > 0);
cout << "You won in " << tries << " moves" << endl;
do{ // check replay
cout << "Do you want to play again? (y/n): ";
cin >> again;
if (again == 'n' || again == 'N'){
return 0;
}
else if (again != 'n' && again != 'N' && again != 'y' && again != 'Y'){
cout << "Invalid character, try again." << endl << endl;
}
} while (again != 'y' && again != 'Y');
// Reset varables if player wants to play again
remaining = 36;
tries = 0;
locX = 0;
locY = 0;
for (int y = 0; y<6; y++){
for (int x = 0; x<6; x++){
move6[x][y].turnTile(); // flips tiles face down
select6[x][y] = 0; // Clear array
}
}
select6[0][0] = 1; // Set array
}
}
//end 6X6 grid
} move6[6][6]; Project move4[4][4]; Project move8[8][8];
|