C ++ Header / Do While循环

时间:2015-01-22 15:25:23

标签: c++ header-files

好吧,所以现在我正在研究这个战舰计划,我有两个问题,我在过去的几个小时里都没能找到。

  1. 来自头文件的变量(shipsDestroyed,torpedoesLeft)在我的do while循环中无法识别。
  2. 我不确定快速检查船是否被毁坏,每次检查时都不加1。
  3. 问题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
    

2 个答案:

答案 0 :(得分:0)

代码的工作流程有点奇怪...... 但是,至少,我在这里看到错误:

while((shipsDestroyed != 5) && (torpedoesLeft != 0))

原因:您正在创建名为&#34; B1&#34;的第一个战舰对象;在主要功能。在战舰:: newGame()中,您正在创建从战舰继承的游戏对象。你正在循环中使用G1,但是检查字段&#34; torpedoesLeft&#34;和&#34; shipDestroyed&#34;对象B1!

可能的解决方案之一:

while((G1.shipsDestroyed != 5) && (G1.torpedoesLeft != 0))

并将此字段设为公开(但它非常糟糕,最好阅读有关继承和封装并重构此代码的信息,使用构造函数而不是方法constrructGame,newBattleship也会更好)

答案 1 :(得分:0)

这有点乱,所以我只是告诉你发生了什么。

main

battleship B1;
B1.newBattleship();

您正在创建一个battleship个实例,并告诉它启动一个新的&#34;战舰&#34;。

void battleship::newBattleship()
{
    cout << "Welcome to the game of Battleship!\n\n";
    battleship game;
    game.newGame();
}

这会创建一个不同的battleship实例并告诉它开始一个新游戏

void battleship::newGame()
{
    Game G1;

    G1.constructGame(0,45);

    G1.setupBoard();
    G1.outputBoard();

    do
    {
        G1.torpedoes();
        G1.outputBoard();
    }while((shipsDestroyed != 5) && (torpedoesLeft != 0));
}

instace创建Game的实例并使用它来运行游戏。

所有这些实例(此时有三个实例)都有自己的成员变量,所有这些变量都与其他变量不同。

所以

while((shipsDestroyed != 5) && (torpedoesLeft != 0));

检查自己的变量,而不是属于G1的变量。

您可能希望G1.getTorpedoesLeft()G1.getShipsDestroyed(),但它并不完全清楚您的意图是什么。

旁注:

if((row == row) && (column == column))

总是如此。