求用C++解答
1.现有两人玩猜拳游戏,每人可用拳头表示3种物体(石头、剪刀、布)中的一种,两人同时出拳,游戏胜负规则如下:
(1)石头vs剪刀:石头胜利。
(2)剪刀vs布:剪刀胜利。
(3)布vs石头:石头胜利。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(NULL)); // 初始化随机数生成器
int player1_win = 0, player2_win = 0;
while (player1_win < 2 && player2_win < 2) { // 连续进行3次游戏,直到一方获胜
system("cls"); // 清空屏幕
cout << "Round " << player1_win + player2_win + 1 << endl;
cout << "Player 1: ";
int player1 = 0;
cin >> player1;
cout << "Player 2: ";
int player2 = 0;
cin >> player2;
cout << endl;
// 根据规则判断胜负
if (player1 == player2) {
cout << "Draw!" << endl;
} else if ((player1 == 1 && player2 == 2) || (player1 == 2 && player2 == 3) || (player1 == 3 && player2 == 1)) {
cout << "Player 1 wins!" << endl;
player1_win++;
} else {
cout << "Player 2 wins!" << endl;
player2_win++;
}
cout << "Player 1: " << player1 << endl;
cout << "Player 2: " << player2 << endl;
cout << "Press enter to continue...";
cin.ignore();
cin.get();
}
if (player1_win >= 2) {
cout << "Player 1 wins the game!" << endl;
} else {
cout << "Player 2 wins the game!" << endl;
}
return 0;
}