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
|
// Mine Sweeper Testing.cpp : main project file.
#include <iostream>
#include <string>
#include <Windows.h>
using std::string;
using std::cout;
using std::cin;
using std::endl;
void clear(int Grid[][10], int col, int row);
void Show_Grid(int Grid[][10]);
void gotoXY(int x, int y);
int main()
{
gotoXY(30, 5);
cout << " Mine Sweeper Testing";
int Grid[10][10] = {
{ 1, 1, 0, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1, 0, 1, 1, 1, 1 },
{ 1, 0, 1, 1, 0, 1, 1, 0, 0, 1 },
{ 1, 0, 1, 0, 0, 1, 1, 1, 0, 1 },
{ 1, 0, 1, 1, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
// A '1' = mine present, '0' = clear
gotoXY(0, 6);
Show_Grid(Grid);
clear(Grid, 1,1);
gotoXY(0, 18);
Show_Grid(Grid);
gotoXY(25, 25);
return 0;
}
void Show_Grid(int Grid[][10])
{
int x, y;
for (y = 0; y < 10; y++)
{
for (x = 0; x < 10; x++)
{
if (Grid[y][x] == -1)
cout << "0 ";
else
cout << "? ";
}
cout << endl;
}
}
void clear(int Grid[][10], int r, int c)
{
int found = 0, x, y;
int sqr[10][10];
for (x = 0; x < 10; x++)
{
for (y = 0; y < 10; y++)
{
sqr[x][y] = Grid[x][y];
}
}
sqr[r][c] = -1;
do
{
found = 0;
if (sqr[r - 1][c] == 0)
{
found++;
sqr[r - 1][c] = -1;
}
if (sqr[r][c - 1] == 0)
{
found++;
sqr[r][c - 1] = -1;
}
if (sqr[r][c + 1] == 0)
{
found++;
sqr[r][c + 1] = -1;
}
if (sqr[r + 1][c] == 0)
{
found++;
sqr[r + 1][c] = -1;
}
if (sqr[r - 1][c] == -1)
{
if(r-1> 0)
r--;
}
else if (sqr[r + 1][c] == -1)
{
if (r + 1 < 9)
r++;
}
else if (sqr[r][c - 1] == -1)
{
if (c - 1 > 0)
c--;
}
else if (sqr[r][c + 1] == -1)
{
if (c + 1 < 9)
c++;
}
Sleep(1000);// This section just to be able to view what the r & c values are
gotoXY(1, 1);
cout << "r = "<< r;
gotoXY(1, 2);
cout << "c = " << c;
} while (found);
for (x = 0; x < 10; x++) // Transfer values to Grid array
{
for (y = 0; y < 10; y++)
{
if (sqr[x][y] == -1)
Grid[x][y] = -1;
}
}
}
void gotoXY(int x, int y)
{
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console, CursorPosition);
}
|