標題:
猜拳遊戲 (二)
[打印本頁]
作者:
tonyh
時間:
2012-9-29 14:19
標題:
猜拳遊戲 (二)
本帖最後由 tonyh 於 2012-9-29 15:16 編輯
讓使用者輸入0可選擇退出遊戲,
同時顯示戰果統計,
包括了共玩了幾次, 贏幾次, 輸幾次, 平手幾次, 猜贏的機率為幾%.
#include<iostream>
using namespace std;
int main()
{
int computer;
int player;
int win=0, lose=0, draw=0;
start:
cout<<"請輸入你想出的拳, 或輸入0退出遊戲: (1=剪刀 2=石頭 3=布) ";
cin>>player;
srand(time(NULL));
computer=rand()%3+1;
if(computer==1)
{
if(player==1)
{
cout<<"電腦出剪刀, 你也出剪刀, 平手!";
draw++;
}else if(player==2)
{
cout<<"電腦出剪刀, 你出石頭, 你贏了!";
win++;
}else if(player==3)
{
cout<<"電腦出剪刀, 你出布, 你輸了!";
lose++;
}else if(player==0)
{
goto over;
}else
{
cout<<"你輸入錯誤!";
}
}
if(computer==2)
{
if(player==1)
{
cout<<"電腦出石頭, 你出剪刀, 你輸了!";
lose++;
}else if(player==2)
{
cout<<"電腦出石頭, 你也出石頭, 平手!";
draw++;
}else if(player==3)
{
cout<<"電腦出石頭, 你出布, 你贏了!";
win++;
}else if(player==0)
{
goto over;
}else
{
cout<<"你輸入錯誤!";
}
}
if(computer==3)
{
if(player==1)
{
cout<<"電腦出布, 你出剪刀, 你贏了!";
win++;
}else if(player==2)
{
cout<<"電腦出布, 你出石頭, 你輸了!";
lose++;
}else if(player==3)
{
cout<<"電腦出布, 你也出布, 平手!";
draw++;
}else if(player==0)
{
goto over;
}else
{
cout<<"你輸入錯誤!";
}
}
cout<<endl<<endl;
goto start;
over:
float total=win+lose+draw;
cout<<endl<<"***戰績統計***"<<endl;
cout<<"總共玩了"<<total<<"次, 贏了"<<win<<"次, 輸了"
<<lose<<"次, 平手"<<draw<<"次."<<endl;
float rate=win/total*100;
cout<<"你贏的機率為"<<rate<<"%"<<endl;
int level=rate/10;
switch(level)
{
case 8: case 9: case 10:
cout<<"太神了!"<<endl;
break;
case 5: case 6: case 7:
cout<<"運氣超好的呦!"<<endl;
break;
case 4:
cout<<"運氣很不錯!"<<endl;
break;
case 3:
cout<<"今天運氣在正常範圍內!"<<endl;
break;
case 2:
cout<<"運氣有點背!"<<endl;
break;
case 1: case 0:
cout<<"你今天肯定衰到爆!"<<endl;
break;
}
cout<<endl;
system("pause");
return 0;
}
複製代碼
作者:
黃博鴻
時間:
2012-9-29 15:16
#include<iostream>
using namespace std;
int main()
{
int computer;
int player;
int win=0, lose=0, even=0;
start:
cout<<"輸入你想出的拳, 或輸入0結束遊戲:(1=剪刀; 2=石頭; 3=布) ";
cin>>player;
srand(time(NULL));
computer=rand()%3+1;
if(computer==1)
{
if(player==1)
{
cout<<"電腦出剪刀,你也出剪刀,平手!";
even++;
}else if(player==2)
{
cout<<"電腦出剪刀,你出石頭,你贏了!";
win++;
}else if(player==3)
{
cout<<"電腦出剪刀,你出布,你輸了!";
lose++;
}else if(player==0)
{
goto over;
}else
{
cout<<"不要亂打,小心我打你!";
}
}else if(computer==2)
{
if(player==1)
{
cout<<"電腦出石頭,你出剪刀,你輸了!";
lose++;
}else if(player==2)
{
cout<<"電腦出石頭,你也出石頭,平手!";
even++;
}else if(player==3)
{
cout<<"電腦出石頭,你出布,你贏了!";
win++;
}else if(player==0)
{
goto over;
}else
{
cout<<"不要亂打,小心我打你!";
}
}else if(computer==3)
{
if(player==1)
{
cout<<"電腦出布,你出剪刀,你贏了!";
win++;
}else if(player==2)
{
cout<<"電腦出布,你出石頭,你輸了!";
lose++;
}else if(player==3)
{
cout<<"電腦出布,你也出布,平手!";
even++;
}else if(player==0)
{
goto over;
}else
{
cout<<"不要亂打,小心我打你!";
}
}
cout<<endl<<endl;
goto start;
over:
float totoal=win+lose+even;
cout<<endl<<"***戰果統計***"<<endl;
cout<<"總共玩了"<<totoal<<"次, 贏了"<<win<<"次, 輸了"
<<lose<<"次, 平手"<<even<<"次."<<endl;
float rate=(win/totoal)*100;
cout<<"猜贏的機率為"<<rate<<"%.";
int level=rate/10;
switch(level)
{
case 10: case 9: case 8:
cout<<"你太神了!";
break;
case 7: case 6: case 5:
cout<<"你好厲害!";
break;
case 4:
cout<<"你運還不錯!";
break;
case 3:
cout<<"你運氣在正常範圍內!";
break;
case 2:
cout<<"你有一點背!";
break;
case 1:
cout<<"你去轉轉運吧!";
break;
case 0:
cout<<"你是不是天天踩到狗屎啊?我不知道要如何說你了...";
break;
}cout<<endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2