C ++保龄球模拟器程序

时间:2015-10-29 18:18:19

标签: c++ random numbers

我最近收到了一个用C ++创建保龄球程序的任务,该程序模拟两个人保龄球并输出每个帧的正确分数。我的程序首先生成每个帧的所有抛出,然后在一个单独的方法中累积得分。当玩家玩一个非完美的游戏和一个完美的游戏时,我能够让程序工作,但是当玩家把所有备件都扔掉时我遇到了问题。我操纵了代码,所以我第一次投掷9次,第二次投掷1次(这是在frame.cpp中)。总数应该是190,但我得到191,我似乎无法找到错误。每个保龄球类包含11帧的阵列。我知道只有10帧,但这是为了说明玩家是否在第10帧获得了一次攻击。任何帮助将不胜感激,谢谢。

这是框架。 h文件

#ifndef FRAME_H
#define FRAME_H
#include<iostream>
using namespace std;

class Frame
{
private: int throw1;
         int throw2;
         int score;
         bool spare;
         bool strike;

public: Frame();
        int genThrow(int size);
        int getFirstThrow();
        int getSecondThrow();
        int getScore();
        void setScore(int);
        void setFirstThrow(int value1);
        void setSecondThrow(int value2);
        void setStrike(bool value);
        void setSpare(bool value);
        bool getStrike();
        bool getSpare();
};
#endif

这是frame.cpp文件

#include "Frame.h"
#include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

Frame::Frame()
{
    spare = false;
    strike = false;
    throw1 = 0;
    throw2 = 0;
    score = 0;
}

//generates a random throw
int Frame::genThrow(int size)
{
    int randomNumber = 0;

    if (size < 10 || throw1 != 10)
    {
        randomNumber = 0 + rand() % (11 - throw1); //generate a number between 0 and 10
    }
    else
    {
        randomNumber = 0 + rand() % (11);
    }

    //cout << randomNumber << endl;

    return randomNumber;
}

//get first throw
int Frame::getFirstThrow()
{
    return throw1;
}

//get second throw
int Frame::getSecondThrow()
{
    return throw2;
}

//get the score of both throws
int Frame::getScore()
{
    return score;
}

//set the score
void Frame::setScore(int value)
{
    score = value;
}

//set the first throw
void Frame::setFirstThrow(int value1)
{
    //throw1 = genThrow(value1); //normal generator
    //throw1 = 10; //strike game rigged
    throw1 = 9; //spare game rigged
}

//set the second throw
void Frame::setSecondThrow(int value2)
{

    //throw2 = genThrow(value2); //normal generator

    throw2 = 1; //spare game rigged
    //throw2 = 10; //strike game rigged
}

//set the strike
void Frame::setStrike(bool value)
{
    strike = value;
}

//set the spare
void Frame::setSpare(bool value)
{
    spare = value;
}

//get the strike
bool Frame::getStrike()
{
    return strike;
}


//get the spare
bool Frame::getSpare()
{
    return spare;
}

这是bowling.h文件

#ifndef BOWLING_H
#define BOWLING_H
#include "Frame.h"
#include<iostream>
using namespace std;

class Bowling 
{
private: Frame a[11];

public: void accumulateScore();
        void bowl();
        void printData();
};
#endif

这是bowling.cpp文件

#include "Bowling.h"
#include<iostream>
using namespace std;

//takes all of the throw values after bowling and accumulates the correct score
void Bowling::accumulateScore()
{
    int totalSum = 0;
    for (int x = 0; x < 10; x++)
    {
        if (a[x].getFirstThrow() + a[x].getSecondThrow() < 10) //not a strike or spare
        {
            totalSum += a[x].getFirstThrow() + a[x].getSecondThrow();
            a[x].setScore(totalSum);
        }
        else if (a[x].getFirstThrow() == 10) //throws a strike
        {
            if (x < 9)
            {
                totalSum += 10 + a[x + 1].getFirstThrow() + a[x + 1].getSecondThrow();
                if (a[x + 2].getStrike() == true)
                {
                    totalSum += 10;
                }
                a[x].setScore(totalSum);
            }
        }
        else if (a[x].getFirstThrow() + a[x].getSecondThrow() == 10) //throws a spare
        {
            if(x < 10)
            {
                totalSum += 10 + a[x + 1].getFirstThrow();
                a[x].setScore(totalSum);
            }
        }
    }

    //player got the 11th frame
    if (a[9].getStrike() == true)
    {
        totalSum += 10 + a[10].getFirstThrow() + a[10].getSecondThrow();
        a[9].setScore(totalSum);
    }
    else if (a[9].getSpare() == true)
    {
        totalSum += 10;
        a[9].setScore(totalSum);
    }
}

void Bowling::bowl()
{
    //generate all throws and store them in the frames
    for (int x = 0; x < 10; x++)
    {
        a[x].setFirstThrow(x);
        if (a[x].getFirstThrow() == 10)
        {
            a[x].setStrike(true);
        }
        if (a[x].getStrike() == false)
        {
            a[x].setSecondThrow(x);
            if (a[x].getFirstThrow() + a[x].getSecondThrow() == 10)
            {
                a[x].setSpare(true);
            }   
        }
        a[x].setScore(a[x].getFirstThrow() + a[x].getSecondThrow());
    }

    //play the 11th frame if they got a strike on the tenth frame

    if(a[9].getStrike() == true)
    {
        a[10].setFirstThrow(10);
        if (a[10].getFirstThrow() == 10)
        {
            a[10].setStrike(true);
        }
        a[10].setSecondThrow(10);
        cout << "The second throw is this value: " << a[10].getSecondThrow() << endl;
        if (a[10].getSecondThrow() == 10)
        {
            a[10].setStrike(true);
        }
        else if (a[10].getFirstThrow() + a[10].getSecondThrow() == 10)
        {
            a[10].setSpare(true);
        }
        a[9].setScore(a[10].getFirstThrow() + a[10].getSecondThrow());

    }
}

void Bowling::printData()
{
    for (int x = 0; x < 10; x++)
    {
        cout << "*****************************" << endl;
        cout << "Frame " << x + 1 << endl;
        cout << "First throw: ";
        if (a[x].getStrike() == true)
        {
            cout << "Strike!" << endl;
        }
        else
        {
            cout << a[x].getFirstThrow() << endl;
        }


        cout << "Second throw: ";
        if (a[x].getStrike() == false)
        {
            if (a[x].getSpare() == true)
            {
                cout << "Spare!" << endl;
            }
            else if(a[x].getSpare() == false)
            {
                cout << a[x].getSecondThrow() << endl;
            }
            else
            {
                cout << endl;
            }
        }
        cout << "Score: " << a[x].getScore();
        cout << endl;
    }

    if (a[9].getStrike() == true)
    {
        cout << "*****************" << endl;
        cout << "Frame 11" << endl;
        cout << "First throw: ";
        if (a[10].getStrike() == true)
        {
            cout << "Strike!" << endl;
        }
        else
        {
            cout << a[10].getFirstThrow() << endl;
        }
        cout << "Second throw: ";

        if (a[10].getStrike() == false)
        {
            if (a[10].getSpare() == true)
            {
                cout << "Spare!" << endl;
            }
            else
            {
                cout << a[10].getSecondThrow() << endl;
            }
        }
        else
        {
            cout << "Strike!" << endl;
        }
        //cout << "Score: " << a[10].getScore();
        cout << endl;
    }
}

这是我在主要

中测试的地方
#include "Bowling.h"
#include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    srand(time(0));
    int dummy = 0;

    //create two players that can bowl
    Bowling player1;
    Bowling player2;
    int player1Score = 0;
    int player2Score = 0;

    //have the players bowl their throws before accumulating score
    player1.bowl();
    player2.bowl();

    //accumulate the score after all of the throws have been done
    player1.accumulateScore();
    player2.accumulateScore();

    //print player 1 data
    cout << "Here are the throws and score for the first player: " << endl;
    player1.printData();

    //spacing
    cout << endl << endl;

    //print player 2 data
    cout << "Here are the throws and score for the second player: " << endl;
    player2.printData();

    cout << "Enter a dummy number:" << endl;
    cin >> dummy;
    return 0;
}

0 个答案:

没有答案