2016-09-09 2 views
0

私は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"; 
} 

} 
+2

を私はここに、グローバル変数を使用しない理由を見ていない、プログラムはかなり小さいと手続きで、これは大きなプログラムだった場合、それは悪い習慣になりますが、一般にその時点では、あなたはあなたのトラッカーをカプセル化することができます(とにかくできますが、私はこのコードのためのポイントを見ません)とにかくより多くのoopスタイルに移動します。 – George

+1

クラスを作成します。 – deW1

+0

@ deW1私はそれを調べなければならないでしょう。応答ありがとう –

答えて

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

私は、特定のオブジェクトに与えられたいくつかのタスクで、よりオブジェクト指向の方法で答えたいと思っていました。

私の思考の決定が最適ではないように、私は望みどおりに行っていませんでした。 私は人間が同じプレーヤークラス(/インターフェイス)から継承したかったと思っていましたが、時間がないために単純化しなければなりませんでした。

あなたの運動/宿題は簡単だとは思わないでください。

コードはOKでなければなりません(code :: blockからコピー/貼り付け後に手動で編集しました)。ご質問がある場合は、お気軽にコメントしてください。次のような結果が

#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. 
関連する問題