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
|
#include <iostream>
#include <cstdlib> // for random numbers
#include <ctime>
using namespace std;
const int SIZE = 4;
const int SYM_SIZE = 16;
struct Position //Position of player to be passed everywhere
{
int row, col, row2, col2;
int points = 0;
Position(int r = 0, int c = 0) : row(r), col(c) { }
};
struct IndexCount
{
int count;
};
Position playerMove;
IndexCount COUNT;
IndexCount getCount()
{
COUNT.count = 0;
}
Position getPos1()
{
cout << "Enter Row: ";
cin >> playerMove.row;
cout << "Enter Column: ";
cin >> playerMove.col;
}
Position getPos2()
{
cout << "Enter Row 2: ";
cin >> playerMove.row2;
cout << "Enter Column 2: ";
cin >> playerMove.col2;
}
ostream& operator <<(ostream& os, const Position& pos);
void createSymArray(int myArray[][SIZE], int SYMBOLS[SYM_SIZE])
{
int symCount = 0;
int symIndex = 0;
for (int i = 0; i < SIZE; i++)
{
//cout << SYMBOLS[i];
for (int j = 0; j < SIZE; j++ )
{
Position RandIndex(rand() % 4, rand() % 4); //Random index and Rand Index
int randSmybolIndex = rand() % 9;
//myArray[i][j] = rand()%8+1;
myArray[RandIndex.row][RandIndex.col] = SYMBOLS[symIndex++];
//*(myArray + i) = *(myArray + randNum); //Assign randNum to Array[i]
}
}
}
bool checkGame(int myArray[][SIZE], Position &playerMove1, Position &playerMove2)
{
for (int r = 0; r < SIZE; r++)
{
for (int c = 0; c < SIZE; c++ )
{
if ((r == playerMove.row)&&(c == playerMove.col))
{
cout << myArray[r][c]<<" ";
}
else if((r == playerMove.row2)&&(playerMove.col2))
{
cout << myArray[r][c]<<" ";
}
else
{
cout<<"* ";
}
}
cout<<endl;
}
cout << "You chose: " << myArray[playerMove.row][playerMove.col];
if (myArray[playerMove.row][playerMove.col] == myArray[playerMove.row2][playerMove.col2])
{
return true;
}
else
{
return false;
}
}
void displaySymbols(int myArray[][SIZE])
{
for (int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE;j++)
{
cout << myArray[i][j] << " ";
}
cout << endl;
}
}
void displayGame(int myArray[][SIZE])
{
cout << endl;
cout<<" 0 1 2 3\n";
cout<<"";
for (int i = 0; i <= 8; i++)
{
cout << "-";
}
cout << endl;
cout<<"0 * * * *\n1 * * * *\n2 * * * *\n3 * * * *\n";
cout<<"";
for (int i = 0; i <= 8; i++)
{
cout << "-";
}
cout<<endl;
/**
for (int r = 0; r < SIZE; r++)
{
for (int c = 0; c < SIZE; c++ )
{
cout<<"* ";
}
cout << endl;
}
cout<<endl;
**/
}
void displayMove(int myArray[][SIZE], Position &playerMove1, Position &playerMove2)
{
for (int r = 0; r < SIZE; r++)
{
for (int c = 0; c < SIZE; c++ )
{
if ((r == playerMove.row) && (c == playerMove.col))
{
cout << myArray[r][c] << " ";
}
else if (( r == playerMove.row2)&&(c == playerMove.col2))
{
cout << myArray[r][c]<<" ";
}
else
{
cout<<"* ";
}
}
cout << endl;
}
cout<<endl;
}
bool checkMove(int myArray[][SIZE], Position &playerMove1, Position &playerMove2)
{
if (myArray[playerMove.row][playerMove.col] == myArray[playerMove.row2][playerMove.col2])
{
return true;
}
return false;
}
bool GAMEOVER(int myArray[][SIZE], IndexCount &COUNT)
{
COUNT.count++;
int countsleft = 16 - COUNT.count;
cout << "You have, " << countsleft << ", tries left." << endl;
if (COUNT.count == 16)
return false;
else
return true;
}
int main()
{
srand((unsigned)time(NULL)); //Seed
int gameArray[SIZE][SIZE] = {};
int SYMBOLS[SYM_SIZE] = {1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8};
createSymArray(gameArray, SYMBOLS);
//createGameArray(gameArray);
cout << "Let's begin!" << endl;
//displayGame(gameArray);
displaySymbols(gameArray);
cout << "Now for the game board" << endl;
IndexCount newCount = getCount();
loop:
displayGame(gameArray);
Position getMove1 = getPos1();
Position getMove2 = getPos2();
newCount;
displayMove(gameArray, getMove1,getMove2);
bool check = checkMove(gameArray, getMove1, getMove2);
bool game = GAMEOVER(gameArray, newCount);
if(check == true)
{
cout << "You found a match!";
if (game == true)
{
playerMove.points++;
cout << "You have " << playerMove.points << " points!";
goto loop;
}
}
else
{
cout << "No match!" << endl;
if (game == false)
{
cout << "You are out of tries, you earned," << playerMove.points << endl;
}
else
{
goto loop;
}
}
return 0;
}
|