Хорошо, что в данный момент я работаю над этой программой линкоров, и у меня есть две проблемы, которые я не мог найти за последние несколько часов.C++ Header/Dowhile loop
- Переменная из файла заголовка (shipsDestroyed, torpedoesLeft) не будет распознана в моем цикле while.
- Я не уверен, что вы можете проверить, был ли корабль уничтожен, а не добавлять 1 каждый раз при проверке.
Задача 1 Имеет место в Battleship.cpp, игра продолжает цикл, даже если торпеды хитов 0 или корабли попали 5.
Проблема 2 Происходит в game.cpp, он у меня установлен каждый раз, когда к +1, когда состояние корабля равно потоку, но оно добавляет 1 каждый раз, когда цикл проходит.
main.cpp
#include <iostream>
#include "battleship.h"
using namespace std;
//Start of main
int main()
{
battleship B1;
B1.newBattleship();
}//End of main
BATTLESHIP.H
#include <iostream>
using namespace std;
class battleship
{
protected:
int shipsDestroyed;
int torpedoesLeft;
public:
void constructGame(int sd, int tL)
{
shipsDestroyed = sd;
torpedoesLeft = tL;
}
void setShipsDestroyed(int sd)
{
shipsDestroyed = sd;
}
void setTorpedoesLeft(int tL)
{
torpedoesLeft = tL;
}
int getShipsDestroyed()
{
return shipsDestroyed;
}
int getTorpedoesLeft()
{
return torpedoesLeft;
}
void newBattleship();
void newGame();
};
BATTLESHIP.CPP
#include <iostream>
#include "battleship.h"
#include "game.h"
using namespace std;
void battleship::newBattleship()
{
cout << "Welcome to the game of Battleship!\n\n";
battleship game;
game.newGame();
}
void battleship::newGame()
{
Game G1;
G1.constructGame(0,45);
G1.setupBoard();
G1.outputBoard();
do
{
G1.torpedoes();
G1.outputBoard();
}while((shipsDestroyed != 5) && (torpedoesLeft != 0));
}
GAME.H
class Game: public battleship
{
protected:
public:
string gameBoard[10][10];
void setupBoard();
void outputBoard();
void torpedoes();
};
GAME.CPP
#include <iostream>
#include <stdlib.h>
#include "battleship.h"
#include "game.h"
using namespace std;
string gameBoard[10][10];
//Start of setupBoard
void Game::setupBoard()
{
for(int x = 0; x < 10; x ++)
{
for(int y = 0; y < 10; y ++)
{
gameBoard[x][y] = "?";
}//End of Y for loop
}//End of X for loop
}//End of setupBoard
void Game::outputBoard()
//Start of outputBoard
{
cout << "? = Unknown\n* = Miss\nX = Hit\n\n";
cout << " A B C D E F G H I J\n";
cout << " | | | | | | | | | " << endl;
cout << "1 " << gameBoard[0][0] << " | " << gameBoard[0][1] << " | " << gameBoard[0][2] << " | " << gameBoard[0][3] << " | " << gameBoard[0][4] << " | " << gameBoard[0][5] << " | " << gameBoard[0][6] << " | " << gameBoard[0][7] << " | " << gameBoard[0][8] << " | " << gameBoard[0][9] << endl;
cout << " _____|_____|_____|_____|_____|_____|_____|_____|_____|_____" << endl;
cout << " | | | | | | | | | " << endl;
cout << "2 " << gameBoard[1][0] << " | " << gameBoard[1][1] << " | " << gameBoard[1][2] << " | " << gameBoard[1][3] << " | " << gameBoard[1][4] << " | " << gameBoard[1][5] << " | " << gameBoard[1][6] << " | " << gameBoard[1][7] << " | " << gameBoard[1][8] << " | " << gameBoard[1][9] << endl;
cout << " _____|_____|_____|_____|_____|_____|_____|_____|_____|_____" << endl;
cout << " | | | | | | | | | " << endl;
cout << "3 " << gameBoard[2][0] << " | " << gameBoard[2][1] << " | " << gameBoard[2][2] << " | " << gameBoard[2][3] << " | " << gameBoard[2][4] << " | " << gameBoard[2][5] << " | " << gameBoard[2][6] << " | " << gameBoard[2][7] << " | " << gameBoard[2][8] << " | " << gameBoard[2][9] << endl;
cout << " _____|_____|_____|_____|_____|_____|_____|_____|_____|_____" << endl;
cout << " | | | | | | | | | " << endl;
cout << "4 " << gameBoard[3][0] << " | " << gameBoard[3][1] << " | " << gameBoard[3][2] << " | " << gameBoard[3][3] << " | " << gameBoard[3][4] << " | " << gameBoard[3][5] << " | " << gameBoard[3][6] << " | " << gameBoard[3][7] << " | " << gameBoard[3][8] << " | " << gameBoard[3][9] << endl;
cout << " _____|_____|_____|_____|_____|_____|_____|_____|_____|_____" << endl;
cout << " | | | | | | | | | " << endl;
cout << "5 " << gameBoard[4][0] << " | " << gameBoard[4][1] << " | " << gameBoard[4][2] << " | " << gameBoard[4][3] << " | " << gameBoard[4][4] << " | " << gameBoard[4][5] << " | " << gameBoard[4][6] << " | " << gameBoard[4][7] << " | " << gameBoard[4][8] << " | " << gameBoard[4][9] << endl;
cout << " _____|_____|_____|_____|_____|_____|_____|_____|_____|_____" << endl;
cout << " | | | | | | | | | " << endl;
cout << "6 " << gameBoard[5][0] << " | " << gameBoard[5][1] << " | " << gameBoard[5][2] << " | " << gameBoard[5][3] << " | " << gameBoard[5][4] << " | " << gameBoard[5][5] << " | " << gameBoard[5][6] << " | " << gameBoard[5][7] << " | " << gameBoard[5][8] << " | " << gameBoard[5][9] << endl;
cout << " _____|_____|_____|_____|_____|_____|_____|_____|_____|_____" << endl;
cout << " | | | | | | | | | " << endl;
cout << "7 " << gameBoard[6][0] << " | " << gameBoard[6][1] << " | " << gameBoard[6][2] << " | " << gameBoard[6][3] << " | " << gameBoard[6][4] << " | " << gameBoard[6][5] << " | " << gameBoard[6][6] << " | " << gameBoard[6][7] << " | " << gameBoard[6][8] << " | " << gameBoard[6][9] << endl;
cout << " _____|_____|_____|_____|_____|_____|_____|_____|_____|_____" << endl;
cout << " | | | | | | | | | " << endl;
cout << "8 " << gameBoard[7][0] << " | " << gameBoard[7][1] << " | " << gameBoard[7][2] << " | " << gameBoard[7][3] << " | " << gameBoard[7][4] << " | " << gameBoard[7][5] << " | " << gameBoard[7][6] << " | " << gameBoard[7][7] << " | " << gameBoard[7][8] << " | " << gameBoard[7][9] << endl;
cout << " _____|_____|_____|_____|_____|_____|_____|_____|_____|_____" << endl;
cout << " | | | | | | | | | " << endl;
cout << "9 " << gameBoard[8][0] << " | " << gameBoard[8][1] << " | " << gameBoard[8][2] << " | " << gameBoard[8][3] << " | " << gameBoard[8][4] << " | " << gameBoard[8][5] << " | " << gameBoard[8][6] << " | " << gameBoard[8][7] << " | " << gameBoard[8][8] << " | " << gameBoard[8][9] << endl;
cout << " _____|_____|_____|_____|_____|_____|_____|_____|_____|_____" << endl;
cout << " | | | | | | | | | " << endl;
cout << "10 " << gameBoard[9][0] << " | " << gameBoard[9][1] << " | " << gameBoard[9][2] << " | " << gameBoard[9][3] << " | " << gameBoard[9][4] << " | " << gameBoard[9][5] << " | " << gameBoard[9][6] << " | " << gameBoard[9][7] << " | " << gameBoard[9][8] << " | " << gameBoard[9][9] << endl;
cout << " | | | | | | | | |\n" << endl;
}//End of outputBoard
//Start of torpedoes
void Game::torpedoes()
{
//Holds Row
int row = 0;
//Holds column char
char temp;
//Holds Column
int column = 0;
//Start of torpedo and checks if torpedo is valid entry
do
{
cout << "Please choose a row. (1 - 10)\n";
cin >> row;
cout << "\n";
}while((row != 1) && (row != 2) && (row != 3) && (row != 4) && (row != 5) && (row != 6) && (row != 7) && (row != 8) && (row != 9) && (row != 10));
//Checks if torpedos end is valid entry
do
{
cout << "Please choose a column. (A - J)\n";
cin >> temp;
cout << "\n";
}while((temp != 'A') && (temp != 'a') && (temp != 'B') && (temp != 'b') && (temp != 'C') && (temp != 'c') && (temp != 'D') && (temp != 'd') && (temp != 'E') && (temp != 'e') && (temp != 'F') && (temp != 'f') && (temp != 'G') && (temp != 'g') && (temp != 'H') && (temp != 'h') && (temp != 'I') && (temp != 'i') && (temp != 'J') && (temp != 'j'));
if((temp == 'A') || (temp == 'a'))
{
column = 0;
}
else if((temp == 'B') || (temp == 'b'))
{
column = 1;
}
else if((temp == 'C') || (temp == 'c'))
{
column = 2;
}
else if((temp == 'D') || (temp == 'd'))
{
column = 3;
}
else if((temp == 'E') || (temp == 'e'))
{
column = 4;
}
else if((temp == 'F') || (temp == 'f'))
{
column = 5;
}
else if((temp == 'G') || (temp == 'g'))
{
column = 6;
}
else if((temp == 'H') || (temp == 'h'))
{
column = 7;
}
else if((temp == 'I') || (temp == 'i'))
{
column = 8;
}
else if((temp == 'J') || (temp == 'j'))
{
column = 9;
}
//Output
cout << "Row: " << row << endl;
row = row - 1;
cout << "Column: " << temp << endl;
if((row == row) && (column == column))
{
gameBoard[row][column] = "*";
}
//Ship 1
if((row == 0) && (column == 0))
{
//Row: 1 | Column: A
gameBoard[0][0] = "X";
}
if((row == 0) && (column == 1))
{
//Row: 1 | Column: B
gameBoard[0][1] = "X";
}
if((row == 0) && (column == 2))
{
//Row: 1 | Column: C
gameBoard[0][2] = "X";
}
if((row == 0) && (column == 3))
{
//Row: 1 | Column: D
gameBoard[0][3] = "X";
}
if((gameBoard[0][0] == "X") && (gameBoard[0][1] == "X") && (gameBoard[0][2] == "X") && (gameBoard[0][3] == "X"))
{
shipsDestroyed = shipsDestroyed + 1;
}
//Ship 2
if((row == 4) && (column == 6))
{
//Row: 5 | Column: G
gameBoard[4][6] = "X";
}
if((row == 5) && (column == 6))
{
//Row: 6 | Column: G
gameBoard[5][6] = "X";
}
if((row == 6) && (column == 6))
{
//Row: 7 | Column: G
gameBoard[6][6] = "X";
}
if((row == 7) && (column == 6))
{
//Row: 8 | Column: G
gameBoard[7][6] = "X";
}
if((gameBoard[4][6] == "X") && (gameBoard[5][6] == "X") && (gameBoard[6][6] == "X") && (gameBoard[7][6] == "X"))
{
shipsDestroyed = shipsDestroyed + 1;
}
//Ship 3
if((row == 3) && (column == 9))
{
//Row: 4 | Column: J
gameBoard[3][9] = "X";
}
if((row == 4) && (column == 9))
{
//Row: 5 | Column: J
gameBoard[4][9] = "X";
}
if((row == 5) && (column == 9))
{
//Row: 6 | Column: J
gameBoard[5][9] = "X";
}
if((row == 6) && (column == 9))
{
//Row: 7 | Column: J
gameBoard[6][9] = "X";
}
if((gameBoard[3][9] == "X") && (gameBoard[4][9] == "X") && (gameBoard[5][9] == "X") && (gameBoard[6][9] == "X"))
{
shipsDestroyed = shipsDestroyed + 1;
}
//Ship 4
if((row == 9) && (column == 2))
{
//Row: 10 | Column: C
gameBoard[9][2] = "X";
}
if((row == 9) && (column == 3))
{
//Row: 10 | Column: D
gameBoard[9][3] = "X";
}
if((row == 9) && (column == 4))
{
//Row: 10 | Column: E
gameBoard[9][4] = "X";
}
if((row == 9) && (column == 5))
{
//Row: 10 | Column: F
gameBoard[9][5] = "X";
}
if((gameBoard[9][2] == "X") && (gameBoard[9][3] == "X") && (gameBoard[9][4] == "X") && (gameBoard[9][5] == "X"))
{
shipsDestroyed = shipsDestroyed + 1;
}
//Ship 5
if((row == 3) && (column == 1))
{
//Row: 4 | Column: B
gameBoard[3][1] = "X";
}
if((row == 4) && (column == 1))
{
//Row: 5 | Column: B
gameBoard[4][1] = "X";
}
if((row == 5) && (column == 1))
{
//Row: 6 | Column: B
gameBoard[5][1] = "X";
}
if((row == 6) && (column == 1))
{
//Row: 7 | Column: B
gameBoard[6][1] = "X";
}
if((gameBoard[3][1] == "X") && (gameBoard[4][1] == "X") && (gameBoard[5][1] == "X") && (gameBoard[6][1] == "X"))
{
shipsDestroyed = shipsDestroyed + 1;
}
system("cls");
torpedoesLeft = torpedoesLeft - 1;
cout << "Torpedoes: " << torpedoesLeft << endl;
cout << "Ships Destroyed: " << shipsDestroyed << endl << endl;
}//End of torpedoes
Ничего себе, такие простые вещи, которые я всегда пропускаю. Спасибо за это, он работал, чтобы разбить цикл. Все, что мне нужно выяснить, это +1. – Justin