[英]Rock Paper Scissors in C++
我正在嘗試完成使用C++的編程原理和實踐練習10第4章中的一個練習。該練習即將編寫一個剪刀石頭布游戲,不使用隨機函數讓計算機在石頭剪刀布之間“選擇” ,所以你必須想辦法讓電腦選擇隨機。 順便說一句,這不是重點,重點是我的程序會跟蹤玩家和計算機得分。 現在的問題是我不知道如何分配分數。 我想出了這個:
enum types {ROCK, PAPER, SCISSORS}
.
.
.
.
.
/* this is the part where i check the score */
// all cases the player would lose
if (player == ROCK && computer == PAPER) // paper beats rock
++computer_score;
else if (player == PAPER && computer == SCISSORS) // scissors beat paper
++computer_score;
else if (player == SCISSORS && computer == ROCK) // rock beats scissors
++computer_score;
else // all other cases the player would win
++player_score;
問題是我不認為這段代碼是好的。 有沒有更聰明的方法來做到這一點?
在互聯網上,我找到了數學家尼克麥克拉倫制作的版本。 這是他的代碼:
vector<string> words;
words.push_back("Rock");
words.push_back("Paper");
words.push_back("Scissors");
string guess;
cout << "Make a guess - Rock, Paper or Scissors\n";
while (cin >> guess) {
int yours, mine; //-your- is the user choice. -mine- is the computer choice
if (guess == "Rock")
yours = 0;
else if (guess == "Paper")
yours = 1;
else if (guess == "Scissors")
yours = 2;
else {
cout << "Invalid guess - Rock used\n";
yours = 0;
}
seed = (13*seed)%256;
mine = seed%3;
if (mine == yours) // draw
cout << "Both guesses were " << words[yours] << " - no winner\n";
else if ((3+mine-yours)%3 == 1) // computer wins
cout << words[mine] << " beats " << words[yours] << " - I win\n";
else // player wins
cout << words[yours] << " beats " << words[mine] << " - you win\n";
}
順便說一下, 這里是他制作的完整代碼的鏈接。 如果您查看整個代碼,也許您會了解更多。
所以他使用了另一種方法,但我不明白他的代碼的這一部分:
else if ((3+mine-yours)%3 == 1)//<-- I don't understand this conditional expression
cout << words[mine] << " beats " << words[yours] <<
" - I win\n";
我知道如果該表達式的結果為 1,則計算機獲勝,否則獲勝者是玩家,但我不明白其中的邏輯。
你想看的是 mine-yours mod 3,如果 mine-yours == 1 mod 3 那么我的贏了,如果你不相信看這三種情況。
(3+mine-yours)%3
這着眼於差異 mod 3,+3 是為了確保 3+mine-yours 在被 % 評估之前是正數
這段代碼提醒我為什么聰明是善良的敵人。
我建議您查看此處發布的類似問題的答案: Rock, Paper, Scissors Game Java
基本上最好為選擇提供一個抽象類的接口,為不同的有效選擇提供實現,並讓類決定它是否從另一個實現中獲勝。
//First time using the stack overflow, it just happend that I got a same
//homework as the question,so here is my code for reference
#include <iostream>
#include <math.h>
#include <iomanip>
#include <time.h>
#include <string>
using namespace std;
//function prototype
int computerChoice();
int userChoice();
int result(int user, int computer);
string choiceName(int choice);
int main()
{
//declare variable
int user, computer, winner;
//keep playing if there is a tie
do {
user = userChoice();
computer = computerChoice();
winner = result(user, computer);
cout << "User choice : " << choiceName(user) << "\tComputer Choice : " << choiceName(computer) << endl;
if (winner == 1)
{
cout << "User win" << endl;
}
else if (winner == 2)
{
cout << "Computer win" << endl;
}
else
{
cout << "Tie" << endl;
}
} while (winner == 0);
//exit system
return 0;
}
//random generate a computer's choice 1-3
int computerChoice() {
srand(time(0));
return rand() % 3 + 1;
}
//get user input 1-3
int userChoice() {
int userChoice;
while (true)
{
cout << "Enter your choice" << endl;
cout << "1.Rock" << endl;
cout << "2.Paper" << endl;
cout << "3.Scissors" << endl;
cin >> userChoice;
if (userChoice > 3 || userChoice < 1) {
cout << "Invalid choice, try again" << endl;
continue;
}
break;
}
return userChoice;
}
// 0 - tie 1- user win 2 - computer win
//rules
int result(int userChoice, int computerChoice) {
if (userChoice == computerChoice)
{
return 0;
}
else if (userChoice == 3 || computerChoice == 3)
{
return userChoice < computerChoice ? 1 : 2;
}
else
{
return userChoice > computerChoice ? 1 : 2;
}
}
//match the choice name
string choiceName(int choice) {
if (choice == 1)
{
return "Rock";
}
else if (choice == 2)
{
return "Paper";
}
else
{
return "Scissors";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.