#include #include #include int getComputerChoice() { int choices[] = {1, 2, 3}; // 1代表石头,2代表剪刀,3代表布 int randomIndex = rand() % 3; return choices[randomIndex]; } int getPlayerChoice() { int choice; printf("请输入你的选择(1-石头,2-剪刀,3-布):"); scanf("%d", &choice); while (choice < 1 || choice > 3) { printf("输入无效,请重新输入(1-石头,2-剪刀,3-布):"); scanf("%d", &choice); } return choice; } void determineWinner(int playerChoice, int computerChoice) { if (playerChoice == computerChoice) { printf("平局!\n"); } else if ((playerChoice == 1 && computerChoice == 2) || (playerChoice == 2 && computerChoice == 3) || (playerChoice == 3 && computerChoice == 1)) { printf("你赢了!\n"); } else { printf("你输了!\n"); } } int main() { srand(time(NULL)); // 初始化随机数种子 int playerChoice, computerChoice; do { playerChoice = getPlayerChoice(); computerChoice = getComputerChoice(); printf("电脑选择了:"); switch (computerChoice) { case 1: printf("石头\n"); break; case 2: printf("剪刀\n"); break; case 3: printf("布\n"); break; } determineWinner(playerChoice, computerChoice); printf("再玩一轮?(1-是,0-否):"); int playAgain; scanf("%d", &playAgain); } while (playAgain == 1); printf("游戏结束。\n"); return 0; }