C 石頭剪刀布的實現

2021-10-24 11:24:37 字數 3487 閱讀 6122

知識儲備

while

(true

)// 不斷重複執行大括號內部的語句

#include

#include

using

namespace std;

main (

)輸入:jkljkljkl

輸出:jkljkljkl

輸入:jkl jfksldfj jklsjfl

輸出:jkl jfksldfj jklsjfl

和cin.

getline

()類似,但是cin.

getline

()屬於istream流,而getline

()屬於string流,是不一樣的兩個函式

#include

#include

#include

#include

using

namespace std;

intmain()

;// 列舉

enum result

; chioce yours, mine;

// 定義列舉變數

result winner;

string s1;

// 定義字串變數

cout <<

"你出的是:"

; cin >> s1;

cout << s1 << endl;

switch

(s1[0]

)// 出錯:產生的隨機數並不是真的隨機數。每次都是布。原因還不知道為啥

int n =

rand()

;// 產生隨機數

cout << n << endl;

switch

(n%3

) cout << mine

if(mine == rock && yours == shear || mine == shear && yours == cloth || mine == cloth && yours == rock)

if(yours == rock && mine == shear || yours == shear && mine == cloth || yours == cloth && mine == rock)

switch

(winner)

return0;

}

問題:

電腦產生的隨機數並不是真的隨機數,而是偽隨機數;

電腦只能執行一次遊戲,無法多次執行

q:標頭檔案#include是什麼?

【c++】關於#include<ctime>與srand(time(null))

from:乙個簡單的剪刀石頭布遊戲(c++實現)

#include

#include

#include

#include

using std::cin;

using std::cout;

using std::endl;

using std::string;

namespace;}

;// 使用者和電腦的選擇

choice player_choice;

choice computer_choice;

// 記錄使用者和電腦的遊戲結果

int player_wins =0;

int computer_wins =0;

string words[3]

=;choice get_computer_choice()

;void

decide_winner()

;string get_msg

(choice winner)

;int

rand0ton1

(int n)

;int

main

(int argc,

char

* ar**)

c = input_str[0]

;if(c ==

'r'|| c ==

'r')

player_choice = choice::rock;

else

if(c ==

'p'|| c ==

'p')

player_choice = choice::*****;

else

if(c ==

's'|| c ==

's')

player_choice = choice::scissors;

else

if(c ==

'e'|| c ==

'e')

break

;else

// 電腦做出隨機選擇,並判斷遊戲結果

computer_choice =

get_computer_choice()

;int p =

(int

)player_choice;

int c =

(int

)computer_choice;

cout <<

"you chose "

<< words[p]

; cout <<

","<< endl;

cout <<

"i chose "

<< words[c]

; cout <<

","<< endl;

decide_winner()

;}// 退出遊戲後,顯示使用者和電腦各自的得分

cout <<

"player wins: "

<< player_wins << endl;

cout <<

"computer wins: "

<< computer_wins << endl;

return exit_success;

}// 電腦等概率地做出選擇

choice get_computer_choice()

// 判斷誰是勝利者

void

decide_winner()

int p =

(int

)player_choice;

int c =

(int

)computer_choice;

if(p - c ==

1|| p - c ==-2

)else

cout << endl;

}// 列印相應的提示資訊

string get_msg

(choice winner)

// 隨機地返回0~n-1中的任一整數

intrand0ton1

(int n)

C 實現石頭剪刀布

c 實現了個石頭剪子布功能具有資料的讀寫功能即顯示遊戲記錄功能 要充分地分析和理解問題本身,弄清要求做什麼。在確定解決方案框架過程中,考慮怎樣使程式結構清晰 合理 簡單和易於除錯,並確定每個函式的簡單功能,以及函式之間的呼叫關係。綜上 新手寫的 歡迎指出問題 以下是computer people類的...

C 實現石頭剪刀布遊戲

using system using system.collections.generic using system.componentmodel using system.data using system.drawing using system.linq using system.text u...

剪刀石頭布

開始學習時寫的剪刀石頭布 雖然是用c 寫的 我感覺思想還是面向過程 慚愧慚愧 include include include include includeusing namespace std enum caiquan class inte cet inte cet inte cet system...