我是c ++的新手,我需要帮助找出如何跟踪计算机和玩家的胜利,直到玩家退出。我以为我可以在全球范围内声明一个变量,但我不确定这是不是很好的做法。这就是我到目前为止所拥有的。
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
// function declarations
int computerRoll();
int computerGuess();
void winner(int uGuess, int cGuess, int uSum, int cSum);
int main()
{
// variable declaration
int userSum = 0, userGuess = 0, aiGuess = 0, aiSum = 0, play = 0;
while (true) {
// ask user for sum of 3 dice rolls
cout << "Enter the sum of your 3 dice rolls: ";
cin >> userSum;
cout << "\n\n";
// check for correct user input
while (userSum > 18 || userSum < 3)
{
cout << "\a\a\a Please enter a sum between 3 and 18: ";
cin >> userSum;
cout << "\n\n";
}
//computer guess
aiGuess = computerGuess();
cout << "The computer has guessed " << aiGuess << " For your number";
cout << "\n\n";
// computer rolls dice 3 times
aiSum = computerRoll();
// User guesses the amount that was rolled by the computer
cout << "Please guess the sum of the 3 dice rolls by the computer: ";
cin >> userGuess;
cout << "\n\n";
// check for correct user input
while (userGuess > 18 || userGuess < 3)
{
cout << "\a\a\a Please enter a sum between 3 and 18: ";
cin >> userGuess;
cout << "\n\n";
}
// Program determins winner/ keeps score
winner(userGuess, aiGuess, userSum, aiSum);
// ask user to keep playing or quit
cout << "Enter any number to keep playing. Enter -1 to stop: ";
cin >> play;
cout << "\n\n";
if (play == -1)
{
break;
}
else
{
continue;
}
}
return 0;
}
// simulate the computer rolling 3 dice, get sum
int computerRoll()
{
int srand(time(NULL));
int computerSum = 0;
const int MAX = 3;
int computerArray[MAX];
for (int i = 0; i < 3; i++)
{
// Just ignor the array for now, was just trying something out
computerArray[i] = rand() % (6 + 1);
computerSum += computerArray[i];
}
return computerSum;
}
// simulate the computer guessing the sum of the 3 dice player has rolled
int computerGuess()
{
int srand(time(NULL));
int guess = 0;
guess = rand() % (18 + 1);
return guess;
}
// Get the difference in guesses
void winner(int uGuess, int cGuess, int uSum, int cSum)
{
int userDifference = 0;
int compDifference = 0;
int compWins = 0;
int userWins = 0;
if (uGuess >= cSum)
{
userDifference = uGuess - cSum;
}
else if (uGuess <= cSum)
{
userDifference = cSum - uGuess;
}
if (cGuess >= uSum)
{
compDifference = cGuess - uSum;
}
else if (cGuess <= uSum)
{
compDifference = uSum - cGuess;
}
if (userDifference >= compDifference)
{
cout << "Computer wins!\n\n";
compWins = compWins++;
cout << userWins << " - " << compWins;
cout << "\n\n";
}
else // userDifference < compDifference
{
cout << "You win!\n\n";
userWins = userWins++;
cout << userWins << " - " << compWins;
cout << "\n\n";
}
}
答案 0 :(得分:1)
这是一个算法:
unsigned int computer_wins = 0U;
unsigned int player_wins = 0U;
bool quit_the_game = false;
while (!quit_the_game)
{
Prompt the user for input.
if User wants to quit, break out of loop.
Calculate computer's move.
Determine winner.
if (winner == computer) ++computer_wins;
else ++player_wins;
}
Display player and computer wins.
实施留作OP的练习。
答案 1 :(得分:1)
我想以更加面向对象的方式回答,其中一些特定的任务专用于给定的对象。
我没有尽我所能,所以我的概念决定不是最佳的。 我曾希望人类的AI继承自同一个玩家类(/ interface),但由于时间不够,我不得不简化。
不要相信你的练习/家庭作业很容易。
应该没问题的代码(从code :: block复制/粘贴后进行1次手动编辑)。如果您有疑问,请随时发表评论。
#include <iostream>
#include <cstdint>
#include <cstdlib>
#include <ctime>
#include <limits>
#include <math.h>
using namespace std;
typedef uint32_t sum_type;
template<typename T> struct dice
{
dice(T number_of_faces=6u):face_nb(number_of_faces){srand(time(0));};
sum_type roll_Ktimes_n_sum(const uint8_t K)
{
double result;
do { result = 0;
for(uint8_t i=0;i<K;++i) { result += roll();}
} while ( numeric_limits<sum_type>::max() < result);
return result;
}
virtual ~dice() = default;
private:
T face_nb;
T roll() { return (T) (((double)rand()/RAND_MAX)*(face_nb-1)+1.5); }
};
struct player
{
static bool IsGameEnd;
virtual sum_type guess(void) = 0;
virtual void play(void) = 0;
protected:
void referee_decision(double human, double machine, double winval)
{
double human_dist = fabs(human - winval);
double AI_dist = fabs(machine - winval);
if (human_dist < AI_dist) human_points++;
if (AI_dist < human_dist) AI_points++;
cout << "Human: " << (int)human_points << " - AI: " << (int)AI_points << endl;
}
private:
static uint8_t human_points;
static uint8_t AI_points;
};
bool player::IsGameEnd = false;
uint8_t player::human_points = 0;
uint8_t player::AI_points = 0;
struct AI: private dice<uint8_t>, public player
{
AI(uint8_t dices_number = 3):nb_dices(dices_number),human_number(0){};
sum_type guess(void) { return roll_Ktimes_n_sum(nb_dices);}
void play(void) { cout << "Please Human give number" << endl;
cin >> human_number;
if ( -1 == (int)human_number ) {IsGameEnd=true; return;}
sum_type machine_value = guess();
cout << "Machine guessed: " << machine_value << endl;
sum_type win_value = roll_Ktimes_n_sum(nb_dices);
cout << "Win value: " << win_value << endl;
referee_decision(human_number, machine_value, win_value);}
private:
uint8_t nb_dices;
sum_type human_number;
};
int main()
{
cout << "Hello world!" << endl;
AI ai;
while(!player::IsGameEnd) ai.play();
return 0;
}
结果如下:
Hello world!
Please Human give number
10
Machine guessed: 7
Win value: 9
Human: 1 - AI: 0
Please Human give number
7
Machine guessed: 12
Win value: 8
Human: 2 - AI: 0
Please Human give number
3
Machine guessed: 17
Win value: 11
Human: 2 - AI: 1
Please Human give number
-1
Process returned 0 (0x0) execution time : 20.366 s
Press any key to continue.