巧克力游戏:输入循环,不插入循环或打印游戏统计数据

Chocolate Game: input loops without inserting loop or printing game stats

本文关键字:循环 游戏 统计数据 插入 打印 输入 巧克力      更新时间:2023-10-16

我一直在尝试做一个Chocolate Manager游戏(更具体地说是一个控制台测试),它在我输入命令后就循环了!忽略命令值cin和幸福。。。我不知道幸福会发生什么,因为它甚至都没有表现出来!

**Main.cpp**
// Include Space
//======
#include <iostream>
#include "timer.cpp"
#include <string>
#include "clrscr.cpp"
//======
using namespace std;
int main() {
    bool exit = false;
    int numchocolate = 15;
    double money = 25;
    string commandname;
    string commandvalue;
    string placeholder;
    double happiness;
    int cmdvl;
    while (exit == false) {
        commandname = "";  //Empties the variables so that they can be reused.
        commandvalue = ""; //
        cout << "Chocolate: " << numchocolate << "nMoney:" << money << "nCommand:"; //stats and initial prompt
        cin >> commandname; //Input for command name
        cout << "nCommand value:"; //Second prompt
        cin >> commandvalue; //Input for command's value
        cmdvl = StringToNumber(&commandvalue); //Converts the string into a int. But this always fails for some reason....

        //Wait! I found the problem... wait, I don't know why it's caused so let's go on find more problems :3
        if (cmdvl == -1) {
            cout << "Something gone wrong in conversion! Exiting...";
            return 1;
        }
        if (commandname == "buy n chocolates") {
             numchocolate += buyChocolate(money, cmdvl);
        }
        else if (commandname == "eat n chocolates") {
            numchocolate -= cmdvl;
            happiness += cmdvl * 2.5;
        }
        else if (commandname == "go work n hours") {
            money += cmdvl * 2;
            happiness -= cmdvl / 3.5;
        }
        else if (commandname == "exit") {
            exit = true;
        }
        else {
            cout << "nInvalid command! Happiness penalty!n";
        }
        if (happiness > 101.0) {
            happiness = 101.0;
        }
        if (happiness > 1.0) {
            happiness -= 1.0;
        }
        cout << "nNow you're " << happiness << "% happy.n";
        cout << "Press Enter to continue to next simulation cycle.";
        placeholder = "";
        getline(cin, placeholder);
        ClearScreen();
    }
    return 0;
}

Timer.cpp(我知道,与定时器无关)

// Include Space
//======
#include <string>
#include <sstream>
//======
using namespace std;
int buyChocolate(double money, int amount) {
    if (money > amount * 3.5) {
        return amount;
    }
    else if (money == 0) {
        return 0;
    }
    else {
        double newAmont = money / 3.5;
        return (int) (newAmont);
    }
    return -1;
}
int StringToNumber ( const string * sometxt ) //Why do you always fail? :(
{
    stringstream ss(*sometxt); //A string stream declarer and initializer. Nothing much.
    int result; //Results are good :)
    return ss >> result ? result : -1; //Returns the int of the string. If it isn't int-compatible returns the error int -1.
}

clrscr.cpp

#include <windows.h>
void ClearScreen(){
    HANDLE                     hStdOut;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD                      count;
    DWORD                      cellCount;
    COORD                      homeCoords = { 0, 0 };
    hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
    if (hStdOut == INVALID_HANDLE_VALUE) return;
    /* Get the number of cells in the current buffer */
    if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
    cellCount = csbi.dwSize.X *csbi.dwSize.Y;
    /* Fill the entire buffer with spaces */
    if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;
    /* Fill the entire buffer with the current colors and attributes */
    if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;
    /* Move the cursor home */
    SetConsoleCursorPosition( hStdOut, homeCoords );
}

它的主要目标是增加幸福感,同时避免破产或失去巧克力。祝你好运(修复和播放)!:3

哦,感谢谁制作了清晰的屏幕代码,因为它不是我的,我忘记了是谁制作的……我想在发布简单游戏的控制台版本之前避免版权问题,我不知道如何:(

代码也更新了,但字符串到int的转换不起作用。我认为调试它不会有帮助,因为它就像一个明显的4行函数,我已经检查了一千次了!

我建议您使用调试器仔细检查代码,并设置断点,以查看它到底在哪里停止以及会导致什么错误。

顺便说一句,你忘了这里有一个分号(;):

if (cmdvl == -1) {
    cout << "Something gone wrong in conversion! Exiting..." <-
    return 1;
}

从使用getline开始,cin读取第一个白色字符:

        cout << "Chocolate: " << numchocolate << "nMoney:" << money << "nCommand:";
        getline(cin,commandname);
        cout << "nCommand value:";
        getline(cin,commandvalue);

但数学似乎也失败了。

Now you're 1.4822e-323% happy.