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
|
#include <iostream>
using std::cout;
using std::cin;
using std::endl;;
#include <cstdlib>
using std::rand;
using std::srand;
#include <ctime>
using std::time;
const int arraySize = 4;
char elements[ arraySize ] [ arraySize ] = {{'1','2','3','4'},
{'5','6','7','8'},
{'9','A','B','C'},
{'D','E',' ','F'}};
char chack [ arraySize ] [ arraySize ] = {{'1','2','3','4'},
{'5','6','7','8'},
{'9','A','B','C'},
{'D','E','F',' '}};
int vSP = 3; // vertical space Position
int hSP = 2; // h space Position
void moveUp();
void moveDown();
void moveRight();
void moveLeft();
void randomise(); // randomize the array
int winer(); // chacks if player have solved the puzzel
int main()
{
srand(time(0));
randomise();
bool quite(false);
do
{
for(int i = 0; i < arraySize; i++){
for(int j = 0; j < arraySize; j++)
cout << " " << elements[i][j];
cout << endl << endl;
}
char a;
cout << "w - Up, z - Down, a - Left, s - Right" << endl;
cin >> a;
switch(a)
{
case 'W':
case 'w':
moveUp();
break;
case 'Z':
case 'z':
moveDown();
break;
case 's':
case 'S':
moveRight();
break;
case 'a':
case 'A':
moveLeft();
break;
default:
cout << "Wrong character, pleas type again!" << endl;
break;
}
int c = winer();
if(c == 1){
cout << "Bravo! You solved the puzle!" << endl;
quite = true;
}
system("cls");
}while(quite == false);
return 0;
}
void moveUp()
{
int vP = vSP;
if(vP + 1 < 4 && vP >= 0){
elements[vSP][hSP] = elements[vSP + 1][hSP];
elements[vSP + 1][hSP] = ' ';
vSP += 1;
}
}
void moveDown()
{
int vP = vSP;
if(vP + 1 <= 4 && vP > 0){
elements[vSP][hSP] = elements[vSP - 1][hSP];
elements[vSP - 1][hSP] = ' ';
vSP -= 1;
}
}
void moveRight()
{
int hP = hSP;
if(hP + 1 <= 4 && hP > 0){
elements[vSP][hSP] = elements[vSP][hSP - 1];
elements[vSP][hSP - 1] = ' ';
hSP -= 1;
}
}
void moveLeft()
{
int hP = hSP;
if(hP + 1 < 4 && hP >= 0){
elements[vSP][hSP] = elements[vSP][hSP + 1];
elements[vSP][hSP + 1] = ' ';
hSP += 1;
}
}
void randomise()
{
for(int i = 0; i < 20000; i++)
{
int a = 1 + rand() % 4;
switch(a)
{
case 1:
moveUp();
break;
case 2:
moveDown();
break;
case 3:
moveRight();
break;
case 4:
moveLeft();
break;
}
}
}
int winer()
{
int ans;
for(int i = 0; i < arraySize; i++){
for(int j = 0; j < arraySize; j++){
if(elements[i][j] == chack[i][j])
ans = 1;
else
return -1;
}
}
return 1;
}
|