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
|
#include <iostream>
#include <ctime>
using namespace std;
void InitBoard(char a[5][6]);
void PrintBoard(char a[5][6]);
void ConvertToRowCol(int input, int &row, int &col);
int MarkSpace(char a[5][6], int row, int col);
void PopulateBombs(char a[5][6]);
const int BOMB = 1;
const int SAFE = 0;
const int MARKED = -1;
int main()
{
char boardGame[5][6];
int input = 0;
int score = 0;
int row = 0;
int col = 0;
int count = 0;
InitBoard(boardGame);
PopulateBombs(boardGame);
do
{
cout << "Enter a value between 1 and 30 to check a space: ";
cin >> input;
ConvertToRowCol(input, row, col);
cout << "You selected row " << row << " and col " << col << endl;
MarkSpace(boardGame, row, col);
if (MarkSpace(boardGame, row, col) == MARKED)
{
count--;
cout << "Youve already guessed this space! That will cost you a point" << endl;
}
else if (MarkSpace(boardGame, row, col) == SAFE)
{
count++;
cout << "Very good that is an open space, your count is now " << count << endl;
}
} while (MarkSpace(boardGame, row, col) != BOMB);
PrintBoard(boardGame);
cout << "Tough luck you hit a bomb, your final count was " << count << endl;
return 0;
}
void InitBoard(char a[5][6])
{
for (int row = 0; row < 5; row++)
for (int col = 0; col < 6; col++)
{
a[row][col] = 'O';
}
}
void PrintBoard(char a[5][6])
{
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 6; col++)
{
cout << a[row][col] << "\t";
}
cout << endl;
}
}
void ConvertToRowCol(int input, int &row, int &col)
{
int rowConverter[31]{ 0,0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4 };
int colConverter[31]{ 0,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5 };
row = rowConverter[input];
col = colConverter[input];
}
void PopulateBombs(char a[5][6])
{
srand((unsigned)time(0));
int bombCount = 0;
do
{
for (int row = 0; row < 5; row++)
for (int col = 0; col < 6; col++)
{
int randomBomb = rand() % 10 + 1;
if (randomBomb > 7 && bombCount < 6)
{
a[row][col] = '*';
bombCount++;
}
}
} while (bombCount < 6);
}
int MarkSpace(char a[5][6], int row, int col)
{
if (a[row][col] == 'X')
{
return MARKED;
}
else if (a[row][col] == 'O')
{
a[row][col] = 'X';
return SAFE;
}
else
{
return BOMB;
}
}
|