C 中的岩石剪刀
Rock Paper Scissors in C++
本文关键字:石剪刀 更新时间:2023-10-16
我正在尝试从书籍编程原理和练习中使用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;
问题是我认为这件代码不好。有什么更聪明的方法吗?
在互联网上,我发现了数学家尼克·麦克拉伦(Nick Maclaren)制作的版本。这是他的代码:
vector<string> words;
words.push_back("Rock");
words.push_back("Paper");
words.push_back("Scissors");
string guess;
cout << "Make a guess - Rock, Paper or Scissorsn";
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 usedn";
yours = 0;
}
seed = (13*seed)%256;
mine = seed%3;
if (mine == yours) // draw
cout << "Both guesses were " << words[yours] << " - no winnern";
else if ((3+mine-yours)%3 == 1) // computer wins
cout << words[mine] << " beats " << words[yours] << " - I winn";
else // player wins
cout << words[yours] << " beats " << words[mine] << " - you winn";
}
顺便说一句,这是他制作的完整代码的链接。如果您查看整个代码,也许您会了解更多。
所以他使用另一种方法,但我不了解他的代码的这一部分:
else if ((3+mine-yours)%3 == 1)//<-- I don't understand this conditional expression
cout << words[mine] << " beats " << words[yours] <<
" - I winn";
我得到的是,如果该表达式的结果是1,那么计算机赢了,否则获胜者是玩家,但我不了解逻辑。
您想看的是我的mod 3,如果我的== 1 mod 3,则我的胜利,如果您不相信这三种情况。
(3+mine-yours)%3
这介绍了差异mod 3, 3是要确保3 矿山是正面的,然后由%
评估如果您不确定%https://www.cprogramming.com/tutorial/modulus.html
或什么是modhttps://en.wikipedia.org/wiki/modular_arithmetic
此代码提醒我为什么聪明是善良的敌人。
我建议您查看此处发布的类似问题的答案:摇滚,纸,剪刀游戏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";
}
}
相关文章:
- 为什么我的石头剪刀布游戏不起作用?
- 石头剪刀布程序
- 石头剪刀布(C++)
- 石头剪刀布的麻烦
- C++中的石头剪刀布循环方法
- 随机在石头剪刀布游戏中在C++
- 从类中查看 rand() 并将用户输入与计算机选择进行比较.石头,纸,剪刀游戏
- C 中的岩石剪刀
- 我正在尝试在 c++ 上制作石头、纸、剪刀游戏,需要一些建议
- 岩石/纸/剪刀游戏无法正常工作
- 石头剪刀布游戏 c++
- VS 2012 上的绒毛石无法编译
- 使用孔时的剪刀库问题
- 卡在 do while 循环C++的石头/纸/剪刀/蜥蜴/斯波克游戏 - 未声明的标识符
- SPOJ拒绝BYTESM2的解决方案(魔法石)
- 石头剪刀布的整数到字符转换
- 尝试在c++中用enum编写一个剪刀石头布程序
- 石头剪刀布错误