標題:
猜拳遊戲 (一)
[打印本頁]
作者:
tonyh
時間:
2015-11-6 20:32
標題:
猜拳遊戲 (一)
本帖最後由 tonyh 於 2015-11-6 21:16 編輯
運用產生隨機亂數的技巧, 設計一個猜拳遊戲,
使用者可以選擇出剪刀石頭或布, 電腦隨機出拳.
[attach]1453[/attach]
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
srand(time(NULL));
int player,computer;
cout<<"請出拳! (1)剪刀(2)石頭(3)布 ";
cin>>player;
computer=rand()%3+1;
cout<<"你出";
if(player==1)
cout<<"剪刀!"<<endl;
else if(player==2)
cout<<"石頭!"<<endl;
else
cout<<"布!"<<endl;
cout<<"電腦出";
if(computer==1)
cout<<"剪刀!"<<endl;
else if(computer==2)
cout<<"石頭!"<<endl;
else
cout<<"布!"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
陳思惟
時間:
2015-11-6 20:47
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
int player, computer;
string name[]={"剪刀","石頭","布"};
cout<<"請出拳:(1)剪刀 (2)石頭 (3)布 ";
cin>>player;
srand(time(NULL));
computer=rand()%3+1;
cout<<"你出"<<name[player-1]<<endl;
cout<<"電腦出"<<name[computer-1]<<endl;
if(computer==player)
{
cout<<"平手!"<<endl;
}
else if((player==1 && computer==3)||
(player==2 && computer==1)||
(player==3 && computer==2))
{
cout<<"你贏了"<<endl;
}
else
{
cout<<"電腦贏了"<<endl;
}
cout<<endl;
goto re;
system("pause");
return 0;
}
複製代碼
作者:
黃璽安
時間:
2015-11-6 20:51
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int computer;
int player;
int tryagain;
start:
srand(time(NULL));
computer=rand()%3+1;
cout<<"請輸入你要出的拳! (1) 剪刀 (2) 石頭 (3) 布"<<endl;
cin>>player;
if(computer==1)
{
if(player==1)
{
cout<<"你出剪刀, 電腦也出剪刀!"<<endl<<"平手!";
}else if(player==2)
{
cout<<"你出石頭, 電腦出剪刀!"<<endl<<"你贏了!";
}else if(player==3)
{
cout<<"你出布, 電腦出剪刀!"<<endl<<"你輸了!";
}else
{
cout<<"輸入錯誤!";
}
}
if(computer==2)
{
if(player==1)
{
cout<<"你出剪刀, 電腦出石頭!"<<endl<<"你輸了!";
}else if(player==2)
{
cout<<"你出石頭, 電腦也出石頭!"<<endl<<"平手!";
}else if(player==3)
{
cout<<"你出布, 電腦出石頭!"<<endl<<"你贏了!";
}else
{
cout<<"輸入錯誤!";
}
}
if(computer==3)
{
if(player==1)
{
cout<<"你出剪刀, 電腦出布!"<<endl<<"你贏了!";
}else if(player==2)
{
cout<<"你出石頭, 電腦出布!"<<endl<<"你輸了!";
}else if(player==3)
{
cout<<"你出布, 電腦也出布!"<<endl<<"平手!";
}else
{
cout<<"輸入錯誤!";
}
}
cout<<endl;
if(tryagain==1)
{
goto start;
}else
{
goto end;
}
end:
system("pause");
return 0;
}
複製代碼
作者:
曾挺桂
時間:
2015-11-6 20:54
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string x,n[3]={"剪刀","石頭","布"};
cout<<"請出拳! <1>剪刀 <2>石頭 <3>布"<<endl;
cin>>x;
if(x==1)
cout<<"你出剪刀"<<endl;
if(x==2)
cout<<"你出石頭"<<endl;
if(x==3)
cout<<"你出布"<<endl;
else
cout<<"1到3,你眼殘啊"<<endl;
srand(time(NULL));
cout<<"電腦出"<<n[rand()%3+1]<<endl;;
system("pause");
return 0;
}
複製代碼
作者:
沈子耕
時間:
2015-11-6 21:04
本帖最後由 沈子耕 於 2015-11-6 21:16 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
re:
int pla,com;
srand(time(NULL));
cout<<"請出拳! <1>剪刀 <2>石頭 <3>布 <0>不想玩了 ";
cin>>pla;
com=rand()%3+1;
if(pla==1)
cout<<"你出剪刀!"<<endl;
else if(pla==2)
cout<<"你出石頭!"<<endl;
else if(pla==3)
cout<<"你出布!"<<endl;
else if(pla==0)
goto end;
else{
cout<<"輸入錯誤"<<endl;
goto re;
}
if(com==1)
cout<<"電腦出剪刀!"<<endl;
else if(com==2)
cout<<"電腦出石頭!"<<endl;
else if(com==3)
cout<<"電腦出布!"<<endl;
cout<<"結果: ";
if(pla==com)
cout<<"平手"<<endl;
else if(pla==1&&com==3 || pla==2&&com==1 || pla==3&&com==2)
cout<<"恭喜您贏了"<<endl;
else if(pla==1&&com==2 || pla==2&&com==3 || pla==3&&com==1)
cout<<"恭喜電腦贏了"<<endl;
goto re;
end:
system("pause");
return 0;
}
複製代碼
作者:
陳思惟
時間:
2015-11-13 20:14
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
srand(time(NULL));
int player,computer;
cout<<"請出拳! (1)剪刀(2)石頭(3)布 ";
cin>>player;
computer=rand()%3+1;
cout<<"你出";
if(player==1)
cout<<"剪刀!"<<endl;
else if(player==2)
cout<<"石頭!"<<endl;
else
cout<<"布!"<<endl;
cout<<"電腦出";
if(computer==1)
cout<<"剪刀!"<<endl;
else if(computer==2)
cout<<"石頭!"<<endl;
else
cout<<"布!"<<endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2