实用C ++ - 我是否正确使用单独的文件和类?

时间:2015-08-15 08:28:10

标签: c++

我已经完成了一个程序作为学习经验,以确保我已经理解了这些概念。我在网上找到了练习项目并决定尝试一下。该程序编译并运行应该没有任何问题。

该程序是一款名为Artillery的游戏。你必须一次向一个敌人发射一门大炮直到你击中他。玩家输入射击大炮的角度。

我的问题是:我使用正确的编码方法吗?例如,如果我是在一个人的团队中工作,那么我对类和对象的使用是否最有效?

此外,我想了解如何改进此程序的代码或任何提示,以便更轻松地编写代码的任何部分。代码如下:

distang.h

#pragma once
#ifndef DISTANG_H
#define DISTANG_H

// Calculates angle and distance of shot
class DistAng
{
public:
    double distAng(double);
private:
    double velocity = 200;
    double gravity = 32.2;
    double shotDistance;
    double timeInAir;
    double pi = 3.1415;
};

#endif

game.h

#pragma once
#ifndef GAME_H
#define GAME_H

// The main loop for playing the game
class Game
{
public:
    void game();
private:
    int killed = 0;
    int enemyDistance = 0;
    double inAngle;
    char done;
    int shots = 0;
    bool killedEnemy = false;
};

#endif

highscore.h

#pragma once
#ifndef HIGHSCORE_H
#define HIGHSCORE_H

// Reads/saves high score from/to file
class HighScore
{
public:
    void highScore(int);
private:
    int intHighScore;
};

#endif

startup.h

#pragma once
#ifndef STARTUP_H
#define STARTUP_H

// Displays the game introduction
class Startup
{
public:
    void startup();
private:
};

#endif

DistAng.cpp

#include <iostream>
#include <math.h>
#include "distang.h"

using namespace std;

// Calculates distance and angle of shot
double DistAng::distAng(double inAngle)
{
    inAngle = (inAngle * pi) / 180; // Converts angle from std::cin into radians

    // A bit of simple physics to calculate the distance of shot
    timeInAir = (2.0 * velocity * sin(inAngle)) / gravity;
    shotDistance = round((velocity * cos(inAngle)) * timeInAir);

    return shotDistance;
}

Game.cpp

#include <iostream>
#include "game.h"
#include <time.h>
#include "distang.h"
#include "highscore.h"

using namespace std;

// Main game loop
void Game::game()
{
    do
    {
        // RNG to generate enemy distance each round :: least distance can be 200, max distance can be 900 :: and std::cout the enemey distance
        srand(time(0));
        enemyDistance = 200 + (rand() % 900);
        cout << "\nThe enemy is " << enemyDistance << " feet away!\n";

        // Creating a couple class object here
        DistAng calculate;
        HighScore submit;

        // Determines if enemy was killes, resets to false each round
        killedEnemy = false;

        // Loop for user input until enemy is hit or runs out of shots
        do
        {
            cout << "What angle? ";

            // Input check
            while (!(cin >> inAngle))
            {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "Invalid input.  Try again: ";
            }

            // Keeps track of shots
            shots++;

            // Hit condition counts kills and displays shots taken
            if (enemyDistance == calculate.distAng(inAngle))
            {
                killed++;
                killedEnemy = true;
                cout << "\nYou hit him!\n";
                cout << "It took you " << shots << " shots.\n";
                break;
            }

            // Allows for shot correction if missed
            else if (enemyDistance < calculate.distAng(inAngle))
                cout << "\nYou over shot by " << calculate.distAng(inAngle) - enemyDistance << endl;

            else if (enemyDistance > calculate.distAng(inAngle))
                cout << "\nYou under shot by " << enemyDistance - calculate.distAng(inAngle) << endl;

        } while (shots != 10);

        // End of round if/else conditions for enemy killed or out of shots
        if (shots == 10 && killedEnemy == false)
        {
            shots = 0;
            cout << "You ran out of cannon balls!\n";
            cout << "You've killed " << killed << " of the enemy.\n";
            submit.highScore(killed); // Record enemies killed for duration of game
            killed = 0;

            // Input/New game check
            do
            {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "Play again?(Y/N) ";
                cin >> done;
            } while (done != 'y' && done != 'n');

        }

        else
        {
            shots = 0;
            cout << "You've killed " << killed << " of the enemy.\n";

            // Input/Next round check
            do
            {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "I see another one, care to shoot again?(Y/N) ";
                cin >> done;
            } while (done != 'y' && done != 'n');
        }

    } while (done != 'n');
}

HighScore.cpp

#include <iostream>
#include <fstream>
#include "highscore.h"

using namespace std;

// Reads/saves high score from/to file
void HighScore::highScore(int score)
{
    ifstream readScoreSheet("HighScore.art");

    // Checks current high score first
    if (readScoreSheet.is_open())
    {
        readScoreSheet >> intHighScore;
        readScoreSheet.close();
    }

    else // Condition for first game played
        cout << "No current high score.\n\n"; 

    // Player did not beat high score
    if (intHighScore > score)
        cout << "The current high score is " << intHighScore << " kills!\n\n";

    // If player beat high score
    else if (intHighScore < score)
    {
        ofstream writeScoreSheet("HighScore.art");

        // Replace old score with new score
        if (writeScoreSheet.is_open())
        {
            cout << "New high score!\n\n";
            writeScoreSheet << score;
            writeScoreSheet.close();
        }

        else cout << "Unable to open file"; // Never happens
    }
}

Main.cpp的

#include <iostream>
#include "game.h"
#include "startup.h"
#include <fstream>

using namespace std;

// Main function, obviously
int main()
{
    // Class objects
    Startup run;
    Game begin;


    run.startup(); // Call startup function
    begin.game(); // Call main game loop function

    return 0;
}

Startup.cpp

#include <iostream>
#include "startup.h"

using namespace std;

// Displays introductory text
void Startup::startup()
{
    cout << "Welcome to Artillery!\n\n";
    cout << "You are in the middle of a war and being charged by thousands of enemies!\n";
    cout << "You have one cannon, which you can shoot at any angle.\n";
    cout << "You only have 10 cannonballs for this target.\n\n";
}

0 个答案:

没有答案