自己寫的一個石頭剪刀布游戲,如果有需要更改的地方請指出
#define _CRT_SECURE_NO_WARNINGS // scanf_s編寫起來太過于麻煩,直接把這個警告關掉,便于編寫。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 定義猜拳選項
#define Rock 1
#define Paper 2
#define Scissors 3
void rules() {printf("歡迎參加剪刀石頭布游戲\n石頭:1,布:2,剪刀:3\n");//先進行游戲介紹printf("提醒:因為srand函數是秒級的,請注意輸入數字的時候不要過快哦,不然就會導致電腦只出一個招式。\n");//提醒srand的注意事項
}
int choice() {int choice;if (scanf("%d", &choice) != 1 || choice < 1 || choice > 3) {printf("輸入無效,請輸入1到3的數字。\n");return -1;}return choice;
}
int comchoice() {srand((unsigned)time(NULL)); // 利用隨機數保證每次電腦出的結果不同return rand() % 3 + 1; // rand % 3 的范圍是0-2, 加1 是 1-3,滿足猜拳范圍。
}
void printChoice(int choice, const char* prefix) {switch (choice) {case Rock:printf("%s石頭", prefix);break;case Paper:printf("%s布", prefix);break;case Scissors:printf("%s剪刀", prefix);break;}
}
int winner(int userChoice, int comChoice) {if (userChoice == comChoice) {printf("平局!你和電腦都出了 ");printChoice(userChoice, "");return 0; // 平局返回0}else if (((userChoice == Rock && comChoice == Scissors) ||(userChoice == Paper && comChoice == Rock) ||(userChoice == Scissors && comChoice == Paper))) {printf("你贏了!你出了 ");printChoice(userChoice, "");printf(",電腦出了 ");printChoice(comChoice, "");return 1; // 用戶贏返回1}else {printf("你輸了!你出了 ");printChoice(userChoice, "");printf(",電腦出了 ");printChoice(comChoice, "");return -1; // 用戶輸返回-1}
}
int main() {int totalgames, win, userwin = 0, comwin = 0;rules();while (1) { // 循環直至獲取有效輸入printf("請輸入比賽局數(總局數必須為整數且為奇數): ");if (scanf("%d", &totalgames) != 1 || totalgames % 2 == 0) { // 檢查輸入是否為整數以及是否為奇數printf("無效輸入,總局數必須為奇數。\n");//輸入無效,循環繼續}break; // 成功獲取有效輸入,退出循環}win = (totalgames / 2) + 1;for (int i = 0; i < totalgames;) {printf("\n第%d局開始:\n", i + 1);int userChoice, computerChoice, result;while ((userChoice = choice()) == -1); // 確保輸入有效computerChoice = comchoice();result = winner(userChoice, computerChoice);if (result == 1) {userwin++;i++; // 只有當結果不是平局時才增加局數計數器}else if (result == -1) {comwin++;i++; // 同上}// 如果一方達到獲勝條件,則提前結束if (userwin >= win || comwin >= win) break;}printf("\n最終結果: ");if (userwin >= win) printf("恭喜你贏得了比賽!");else if (comwin >= win) printf("很遺憾,電腦贏得了比賽。");else printf("比賽結束,未分勝負。");return 0;
}
}
這是運行的結果。
可以自己寫一寫,很鍛煉coding能力。
#define _CRT_SECURE_NO_WARNINGS // scanf_s編寫起來太過于麻煩,直接把這個警告關掉,便于編寫。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 定義猜拳選項
#define Rock 1
#define Paper 2
#define Scissors 3
void rules() {printf("歡迎參加剪刀石頭布游戲\n石頭:1,布:2,剪刀:3\n");//先進行游戲介紹printf("提醒:因為srand函數是秒級的,請注意輸入數字的時候不要過快哦,不然就會導致電腦只出一個招式。\n");//提醒srand的注意事項
}
int choice() {int choice;if (scanf("%d", &choice) != 1 || choice < 1 || choice > 3) {printf("輸入無效,請輸入1到3的數字。\n");return -1;}return choice;
}
int comchoice() {srand((int)time(NULL));// 利用隨機數保證每次電腦出的結果不同return rand() % 3 + 1; // rand % 3 的范圍是0-2, 加1 是 1-3,滿足猜拳范圍。
}
int printAndDecideWinner(int userChoice, int comChoice) {if (userChoice == comChoice) {printf("平局!你和電腦都出了 ");switch (userChoice) {case Rock:printf("石頭");break;case Paper:printf("布");break;case Scissors:printf("剪刀");break;}return 0;//返回0,便于在主函數時操作}else if (((userChoice == Rock && comChoice == Scissors) ||(userChoice == Paper && comChoice == Rock) ||(userChoice == Scissors && comChoice == Paper))) {printf("你贏了!你出了 ");switch (userChoice) {case Rock:printf("石頭");break;case Paper:printf("布");break;case Scissors:printf("剪刀");break;}printf(",電腦出了 ");switch (comChoice) {case Rock:printf("石頭");break;case Paper:printf("布");break;case Scissors:printf("剪刀");break;}return 1;//返回1到主函數操作}else {printf("你輸了!你出了 ");switch (userChoice) {case Rock:printf("石頭");break;case Paper:printf("布");break;case Scissors:printf("剪刀");break;}printf(",電腦出了 ");switch (comChoice) {case Rock:printf("石頭");break;case Paper:printf("布");break;case Scissors:printf("剪刀");break;}return -1;//同理}
}
int main() {int totalgames, win, userwin = 0, comwin = 0;rules();while (true) {//一直循環直到獲得有效輸出printf("請輸入比賽局數(總局數必須為整數且為奇數): ");if (scanf("%d", &totalgames) != 1 || totalgames % 2 == 0) {// 檢查輸入是否為整數以及是否為奇數printf("無效輸入,總局數必須為奇數。\n");//輸入無效,循環繼續}else {break; //成功獲取有效輸入,退出循環}}win = (totalgames / 2) + 1;for (int i = 1; i <= totalgames;) {// 確保輸入有效(如果return的值是-1的話,會一直讓用戶輸入值)printf("\n第%d局開始:\n", i);int userchoice, computerchoice, result;userchoice = choice();computerchoice = comchoice();while (userchoice == -1) {userchoice = choice();computerchoice = comchoice();}result = printAndDecideWinner(userchoice, computerchoice);if (result == 1) {userwin++;i++;// 只有當結果不是平局時才增加局數計數器}else if (result == -1) {comwin++;i++; // 同上}if (userwin >= win || comwin >= win) // 如果一方達到獲勝條件,那就提前結束。break;}printf("\n最終結果: ");if (userwin >= win)printf("恭喜你贏得了比賽!");else if (comwin >= win)printf("很遺憾,電腦贏得了比賽。");elseprintf("比賽結束,未分勝負。");return 0;
}
修改過的第二版方法。更通俗易懂,用switch case 一個函數實現所有