I'm trying to write a battleship program that reads from a file. Here is what the file looks like that I am trying to get the program to read from.
##~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~###~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~#####~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~#~~~~~~~~~~~~~~~~~~~~~
~~~#~~~~~~~~~~~~~~~~~~~~~
~~~#~~~~~~~~~~~~~~~~~~~~~
~~~#~~~~~~~~~~~~~~~~~~~~~
~~~#~~~~~~######~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~#~~~~~~~~~~~~~~~~~~~~
~~~~#~~~~~~~~~~~~~~~~~~~~
~~~~#~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~#~~~~~~~~~
~~~~~~~~~~~~~~~#~~~~~~~~~
~~~~~~~~~~~~~~~#~~~~~~~~~
~~~~~~~~~~~~~~~#~~~~~~~~~
~~~~~~~~~~~~~~~#~~~~~~~~~
~~~~~~~~~~~~~~~#~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~######~~~~~~~~~~~~~~~~
My main problem is that I cannot update the file when I hit a ship from "~" to "H". I have a feeling that my program is not reading the file but I could be wrong... because when I un-comment the for loops that I use to initialize the gameBoard it does not run properly.. everything else seems to be coded logically. Here is the rest of my code...
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
|
//Include statements
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
ifstream inputFile;
//Global declarations: Constants and type definitions only -- no variables
const int Width = 25;
const int Length = 25;
//Function prototypes
bool FIRE(const int Width, const int Length, int x, int y, char gameBoard[25][25]);
void FleetSunk(const int Width, const int Length, int x, int y, char gameBoard[25][25], bool &GAMEOVER);
int main()
{
//In cout statement below substitute your name and lab number
cout << "Tyler Warren -- Lab 9: Battleship" << endl << endl;
inputFile.open("BoardText.txt");
//Variable declarations
char gameBoard[25][25];
int x, y;
bool GAMEOVER = false;
//Program logic
if (!inputFile)
{
cout << "Cannot open file." << endl;
}
cout << "BattleShip!" << endl;
//reads gameboard from file
/*for (int i = 0; i < 25; i++)
{
for (int j = 0; j = 25; j++)
inputFile >> gameBoard[i][j];
}
for (int i = 0; i < 25; i++)
{
for (int j = 0; j < 25; j++)
inputFile >> gameBoard[i][j];
}*/
while (!GAMEOVER)
{
cout << "Enter co-ordinates for attack! (X,Y): ";
cin >> x >> y;
cout << endl;
FIRE(Width, Length, x, y, gameBoard);
FleetSunk(Width, Length, x, y,gameBoard, GAMEOVER);
}
inputFile.close();
//Closing program statements
system("pause");
return 0;
}
//Function definitions
bool FIRE(const int Width, const int Length, int x, int y, char gameBoard[25][25])
{
if (gameBoard[x][y] == '#')
{
cout << "HIT" << endl;
gameBoard[x][y] = 'H';
return true;
}
else if (gameBoard[x][y] == 'H')
{
cout << "HIT AGIAN!" << endl;
return true;
}
else
{
cout << "MISS!" << endl;
return false;
}
}
void FleetSunk(const int Width, const int Length, int x, int y, char gameBoard[25][25], bool &GAMEOVER)
{
bool NoShip;
NoShip = false;
for (int i = 0; i < 25; i++)
{
for (int j = 0; j < 25; j++)
if (gameBoard[x][y] == '~' || gameBoard[x][y] == 'H')
NoShip = true;
else
{
NoShip = false;
break;
}
if (NoShip == false)
break;
}
if (NoShip == true)
{
GAMEOVER = true;
cout << "Fleet has been destroyed!!" << endl;
cout << "GAMEOVER" << endl << endl;
}
}
|
Any help would be appreciated!