gpt4 book ai didi

c++ - 巧克力游戏 : input loops without inserting loop or printing game stats

转载 作者:太空宇宙 更新时间:2023-11-04 16:06:23 26 4
gpt4 key购买 nike

<分区>

我一直在尝试做一个巧克力经理游戏(更具体地说是一个控制台测试),它在我输入命令后立即循环!忽略命令值 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

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com