Feb 22, 2018 at 8:36pm UTC
I'm sure the fix is super simple, but I am still really new to this and need some suggestions to fixing my problem. I have created a 2d array game where the game board is printed with '?'s and I have randomly placed 5 pieces of gold 'G' on the board, along with 1 bomb hidden as well. You get 5 guesses to find gold, earning points for each time you do. If you find the bomb then it's game over. At the end of the game, I'm suppose to print the board showing where each hidden item was located, without any of the '?'s that were on the original board. Can someone tell me what I'm doing wrong here?
// BoardGame.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <iomanip> //used this so I could use the setw() function
using namespace std;
const int ROWS = 8;
const int COLS = 8;
const int NO_GUESSES = 5;
const int NO_GOLD = 5;
const int BOMB = 1;
void initBoard(char board[ROWS][COLS]);
void printHidden(char board[ROWS][COLS]);
void hideItems(char board[ROWS][COLS]);
void gameRules();
void guessWhere(char board[ROWS][COLS]);
int main()
{
char gameboard [ROWS][COLS];
char answer;
do
{
initBoard(gameboard);
printHidden(gameboard);
hideItems(gameboard);
gameRules();
guessWhere(gameboard);
cout << endl;
cout << "Play Again? Enter 'y' ";
cin >> answer;
} while (answer == 'Y' || answer == 'y');
return 0;
}
void initBoard(char board[ROWS][COLS])
{
int r, c;
for (r = 0; r < ROWS; r++)
{
for (c = 0; c < COLS; c++)
{
board[r][c] = '?';
}
}
}
void printHidden(char board[ROWS][COLS])
{
int r, c;
cout << endl;
cout << " ";
for (c = 0; c < COLS; c++)
cout << setw(4) << c + 1;
cout << endl;
cout << " " << "--------------------------------" << endl;
for (r = 0; r < ROWS; r++)
{
cout << " " << r + 1 << " |";
for (c = 0; c < COLS; c++)
{
cout << setw(4) << board[r][c];
}
cout << endl;
}
cout << " " << "--------------------------------" << endl;
}
void hideItems(char board[ROWS][COLS])
{
int gold = 0;
int x, y;
int bomb = 0;
srand(time(NULL));
for (gold = 0; gold < NO_GOLD; gold++)
{
x = rand() % ROWS;
y = rand() % COLS;
board[x][y] = 'G';
}
for (bomb = 0; bomb < BOMB; bomb++)
{
x = rand() % ROWS;
y = rand() % COLS;
board[x][y] = 'B';
}
}
void gameRules()
{
cout << endl;
cout << "**" << " ******************************** " << "**" << endl;
cout << "**" << " Find Gold! " << "**" << endl;
cout << "**" << " You have " << NO_GUESSES << " guesses, " << "**" << endl;
cout << "**" << " 5 pieces of gold " << "**" << endl;
cout << "**" << " and 1 bomb " << "**" << endl;
cout << "**" << " Good Luck! " << "**" << endl;
cout << "**" << " ******************************** " << "**" << endl;
cout << endl;
}
void guessWhere(char board[ROWS][COLS])
{
int guessRow, guessCol;
int chancesLeft = NO_GUESSES;
int points = 0;
while (chancesLeft > 0)
{
cout << endl;
cout << "Enter a x-coordinate between 0-" << ROWS - 1 << ": ";
cin >> guessRow;
cout << endl;
cout << "Enter a y-coordinate between 0-" << COLS - 1 << ": ";
cin >> guessCol;
if (board[guessRow][guessCol] == 'G')
{
cout << endl;
cout << "You found GOLD!!";
board[guessRow][guessCol] = 'F';
points = points + 1;
chancesLeft--;
cout << "\t" << "You have " << chancesLeft << " guesses left!!" << endl;
cout << endl;
continue;
}
else if (board[guessRow][guessCol] == 'B')
{
cout << endl;
chancesLeft = 0;
cout << "Bomb!!" << "GAME OVER" << endl;
break;
}
else
cout << endl;
cout << "Too Bad. ";
chancesLeft--;
cout << "\t" << "You have " << chancesLeft << " guesses left!!" << endl;
cout << endl;
}
cout << "You earned " << points << " points!!" << endl;
cout << "Better Luck Next Time!!" << endl;
cout << "Here's your board: " << endl;
cout << endl;
cout << endl;
cout << " ";
for (guessCol = 0; guessCol < COLS; guessCol++)
cout << setw(4) << guessCol + 1;
cout << endl;
cout << " " << "--------------------------------" << endl;
for (guessRow = 0; guessRow < ROWS; guessRow++)
{
cout << " " << guessRow + 1 << " |";
for (guessCol = 0; guessCol < COLS; guessCol++)
{
cout << setw(4) << board[guessRow][guessCol];
}
cout << endl;
}
cout << " " << "--------------------------------" << endl;
}
Feb 22, 2018 at 9:19pm UTC
can you tell us what its doing wrong or not doing or some hint to the problem.
Feb 22, 2018 at 10:28pm UTC
I can get it to print the array exposing where the pieces of gold and bomb are, but can't get rid of the ?s in that same array.
Last edited on Feb 22, 2018 at 10:29pm UTC