標題:
[作業] 猜拳遊戲 (二)
[打印本頁]
作者:
tonyh
時間:
2014-3-8 18:00
標題:
[作業] 猜拳遊戲 (二)
本帖最後由 tonyh 於 2014-3-15 16:24 編輯
需求說明:
1. 為程式加上一個適當的標題 (如: *** 猜拳遊戲 v1.0 ***)
2. 要有判斷輸入錯誤的機制
3. 加上戰果統計, 顯示總共贏了幾次, 輸了幾次, 平手幾次.
[attach]854[/attach]
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int player, computer;
int win=0,lose=0,draw=0;
srand(time(NULL));
string name[]={"剪刀","石頭","布"};
cout<<"☆★☆ 猜拳遊戲 ☆★☆"<<endl;
start:
cout<<endl<<"請出拳! (1)剪刀 (2)石頭 (3)布 (0)結束"<<endl;
cin>>player;
if(player==0)
goto end;
else if(player>=1 && player<=3)
{
computer=rand()%3+1;
cout<<"你出: "<<name[player-1]<<endl;
cout<<"電腦出: "<<name[computer-1]<<endl;
if(player==computer)
{
cout<<"平手!"<<endl;
draw++;
}else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
{
cout<<"你贏了!"<<endl;
win++;
}else
{
cout<<"電腦贏了!"<<endl;
lose++;
}
goto start;
}else
{
cout<<"輸入錯誤!"<<endl;
goto start;
}
end:
cout<<endl<<"☆★☆ 戰果統計 ☆★☆"<<endl;
cout<<"總共贏了"<<win<<"次, 輸了"<<lose<<"次, 平手"<<draw<<"次!"<<endl<<endl;
system("pause");
return 0;
}
複製代碼
作者:
張彥承
時間:
2014-3-8 18:12
本帖最後由 張彥承 於 2014-3-15 17:13 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int player,computer;
srand(time(NULL));
int win=0,lose=0,draw=0;
string name[3]={"剪刀 ","石頭 ","布 "};
re:
cout<<"請猜拳 <1>剪刀 <2>石頭 <3>布 <4>結束 "<<endl;
cin>>player;
if(player==4)
goto end;
computer=rand()%3+1;
cout<<"你出:"<<name[player-1]<<endl;
cout<<"電腦出:"<<name[computer-1]<<endl;
if(player==computer)
{
cout<<"平手"<<endl;
draw++;
}else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
{
cout<<"你贏了!"<<endl;
win++;
}else
{
cout<<"你輸了!"<<endl;
lose++;
}
cout<<endl;
goto re;
end:
cout<<"@@戰果分析@@"<<endl;
cout<<"贏了"<<win<<"次"<<"輸了"<<lose<<"次"<<"平手"<<draw<<"次"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
周雍程
時間:
2014-3-8 18:20
本帖最後由 周雍程 於 2014-3-15 17:21 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
srand(time(NULL));
int player, computer, win=0, lose=0, draw=0;
string name[3]={"剪刀","石頭","布"};
cout<<"******-猜拳遊戲-******"<<endl;
re:
computer=rand()%3+1;
cout<<" <1> 剪刀 <2> 石頭 <3> 布 <4> 結束 ";
cin>>player;
if(player==0)
{
goto aa;
}
else if(player>=1 && player<=3)
{
cout<<"你出:"<<name[player-1]<<endl;
cout<<"電腦出:"<<name[computer-1]<<endl;
if(player == computer)
{
cout<<"平手!"<<endl;
draw++;
}else if((player==1 && computer==3)||(player==2 && computer==3)||(player==3 && computer==2))
{
cout<<"你贏了!"<<endl;
win++;
}else
{
cout<<"電腦贏了!"<<endl;
lose++;
}
goto re;
}
else
{
cout<<"輸入錯誤!"<<endl;
}
aa:
cout<<"---戰果統計---"<<endl;
cout<<"總共贏了"<<win<<"次,"<<"輸了"<<lose<<"次,"<<"平手"<<draw<<"次,"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
張峻瑋
時間:
2014-3-8 18:25
本帖最後由 張峻瑋 於 2014-3-15 17:02 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int player,computer,a=0,b=0,c=0;
srand(time(NULL));
string name[3]={"剪刀","石頭","布"};
re:
cout<<"*** 猜拳遊戲 ***"<<endl;
cout<<"請出拳!(1)剪刀(2)石頭(3)布(0)結束";
cin>>player;
if(player==0)
{
goto end;
}
else if(player>=1&&player<=3)
{
computer=rand()%3+1;
cout<<"你出"<<name[player-1]<<endl;
cout<<"電腦出"<<name[computer-1]<<endl;
if(player==computer)
{
cout<<"平手!"<<endl;
a++;
}
else if((player==1 && computer==3)||
(player==2 && computer==1)||
(player==3 && computer==2))
{
cout<<"你贏了!"<<endl;
b++;
}
else
{
cout<<"電腦贏了!"<<endl;
c++;
}
goto re;
}
else
{
cout<<"輸入錯誤"<<endl;
goto re;
}
cout<<endl;
end:
cout<<"*** 戰果統計 ***"<<endl;
cout<<"總共贏了"<<b<<"次,輸了"<<c<<"次,"<<"平手"<<a<<"次"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
張郁偵
時間:
2014-3-8 18:31
本帖最後由 張郁偵 於 2014-3-15 17:08 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int player,computer,draw=0,win=0,lose=0;
srand(time(NULL));
string name[3]={"剪刀","石頭","布"};
cout<<"※※猜拳遊戲※※"<<endl;
start:
cout<<"請出拳!(1)剪刀(2)石頭(3)布(0)結束"<<endl;
cin>>player;
if(player==0)
goto end;
else if(player>=1 && player<=3)
{
computer=rand()%3+1;
cout<<"你出"<<name[player-1]<<endl;
cout<<"電腦出"<<name[computer-1]<<endl;
if(player==computer)
{
cout<<"平手"<<endl;
draw++;
}else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
{
cout<<"你贏了"<<endl;
win++;
}else if((player==3 && computer==1)||(player==1 && computer==2)||(player==2 && computer==3))
{
cout<<"電腦贏了"<<endl;
lose++;
}
goto start;
}else
{
cout<<"輸入錯誤!"<<endl;
goto start;
}
end:
cout<<"※※戰果統計※※"<<endl;
cout<<"總共贏了"<<win<<"次,"<<"輸了"<<lose<<"次,"<<"平手"<<draw<<"次,"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
張郁庭
時間:
2014-3-8 18:33
本帖最後由 張郁庭 於 2014-3-15 16:55 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int player,computer;
int win=0, lost=0, draw=0;
srand(time(NULL));
cout<<"☆☆☆猜拳遊戲☆☆☆"<<endl<<endl;
re:
string name[3]={"剪刀","石頭","布"};
cout<<"請出拳! (1) 剪刀 (2) 石頭 (3) 布 (4)戰果統計"<<endl;
cin>>player;
if(player>=1 && player<=3)
{
computer=rand()%3+1;
cout<<"你出"<<name[player-1]<<endl;
cout<<"電腦出"<<name[computer-1]<<endl;
if(player==computer)
{
cout<<"平手!"<<endl;
draw++;
goto re;
}else if((player==1 && computer==3)||
(player==2 && computer==1)||
(player==3 && computer==2))
{
cout<<"你贏了!"<<endl;
win++;
goto re;
}else
{
cout<<"電腦贏了!"<<endl;
lost++;
goto re;
}
}else if(player==4)
{
goto end;
}else
{
cout<<"輸入錯誤!"<<endl;
goto re;
}
cout<<endl;
end:
cout<<"※※※戰果統計※※※"<<endl;
cout<<"贏"<<win<<"次"<<endl;
cout<<"輸"<<lost<<"次"<<endl;
cout<<"平手"<<draw<<"次"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
張彥承
時間:
2014-3-14 17:26
[code]#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
int player,computer;
string name[3]={"剪刀 ","石頭 ","布 ",};
cout<<"***
cout<<"請猜拳 <1>剪刀 <2>石頭 <3>布 <4>結束 "<<endl;
cin>>player;
int win=0,lose=0,draw=0;
if(player==4)
g
srand(time(NULL));
computer=rand()%3+1;
cout<<"你出:"<<name[player-1]<<endl;
cout<<"電腦出:"<<name[computer-1]<<endl;
if(player==computer` {
cout<<"平手"<<endl;
draw++;
}else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
{
cout<<"你贏了!"<<endl;
win++;
}else
{
cout<<"你輸了!"<<endl;
lose++;
}
cout<<endl;
goto re;
end:
cout<<"---戰果統計---"<<endl;
cout<<"總共贏了"<<win<"次,輸了"<<lose<<"次,平手"<<draw<<"次"<<endl;
cout<<"@@戰果分析@@"<<endl;
system("pause");
return 0;
}
複製代碼
[/code]
作者:
劉得旗
時間:
2014-3-15 16:28
本帖最後由 劉得旗 於 2014-3-15 16:54 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int player,computer;
int win=0, lost=0, draw=0;
cout<<"@@@猜拳遊戲@@@"<<endl<<endl;
srand(time(NULL));
re:
string name[3]={"剪刀","石頭","布"};
cout<<"請出拳! (1) 剪刀 (2) 石頭 (3) 布 (4)結束並到戰果統計"<<endl;
cin>>player;
if(player>=1 && player<=3)
{
srand(time(NULL));
computer=rand()%3+1;
cout<<"你出"<<name[player-1]<<endl;
cout<<"電腦出"<<name[computer-1]<<endl;
if(player==computer)
{
cout<<"平手!"<<endl;
draw++;
goto re;
}else if((player==1 && computer==3)||
(player==2 && computer==1)||
(player==3 && computer==2))
{
cout<<"你贏了!"<<endl;
win++;
goto re;
}else
{
cout<<"你輸了!"<<endl;
lost++;
goto re;
}
}else if(player==4)
{
goto end;
}else
{
cout<<"輸入錯誤!"<<endl;
goto re;
}
cout<<endl;
end:
cout<<"###戰果統計###"<<endl;
cout<<"你贏"<<win<<"次"<<endl;
cout<<"你輸"<<lost<<"次"<<endl;
cout<<"平手"<<draw<<"次"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
施伯叡
時間:
2014-3-21 16:42
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int player, computer;
int win=0,lose=0,draw=0;
srand(time(NULL));
string name[]={"剪刀","石頭","布"};
cout<<"~~~ 猜拳遊戲 ~~~"<<endl;
start:
cout<<endl<<"請出拳:";
cout<<endl<<"(1)剪刀 (2)石頭 (3)布 (0)結束"<<endl;
cin>>player;
if(player==0)
goto over;
else if(player>=1 && player<=3)
{
computer=rand()%3+1;
cout<<"你出: "<<name[player-1]<<endl;
cout<<"電腦出: "<<name[computer-1]<<endl;
if(player==computer)
{
cout<<"平手!"<<endl;
draw++;
}else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
{
cout<<"你贏了!"<<endl;
win++;
}else
{
cout<<"電腦贏了!"<<endl;
lose++;
}
goto start;
}
else
{
cout<<"輸入錯誤!"<<endl;
goto start;
}
over:
cout<<endl<<"戰果統計: "<<endl;
cout<<"你總共贏了"<<win<<"次, 輸了"<<lose<<"次, 平手"<<draw<<"次!"<<endl<<endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2