標題:
猜拳遊戲 (二)
[打印本頁]
作者:
周政輝
時間:
2018-4-14 15:10
標題:
猜拳遊戲 (二)
請簡化第一種版本的寫法
試著讓程式碼大幅減少
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
// 1:剪刀 2:石頭 3:布
srand(time(NULL));
int player =0;
int com =0;
string mora[] = {"剪刀","石頭","布"};
cout << "請選擇想要出的拳 (1:剪刀 2:石頭 3:布)"<< endl;
cin >> player;
cout << "電腦出拳中..." << endl;
com = (rand()%3)+1;
cout << "你出:" << mora[player-1] << endl;
cout << "電腦出:" << mora[com-1] << endl;
if(player == 1 && com==3) {
cout << "你贏了" << endl;
}
else if(player == 2 && com==1) {
cout << "你贏了" << endl;
}
else if(player == 3 && com==2) {
cout << "你贏了" << endl;
}
else if(player == com) {
cout << "平手" << endl;
}
else {
cout << "你輸了" << endl;
}
system("pause");
return 0;
}
複製代碼
作者:
林峻安
時間:
2018-4-14 15:15
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main()
{
int player=0;
int com=0;
string mora[]={"剪刀","石頭","布"};
cout<<"輸入你要的拳(1:剪刀,2:石頭,3:布)"<<endl;
cin>>player;
cout<<"電腦出拳中"<<endl;
srand(time(NULL));
com=(rand()%3)+1;
cout << "你出:" << mora[player-1] << endl;
cout << "電腦出:" << mora[com-1] << endl;
if(player==com)
{
cout<<"平手"<<endl;
}
else if(player==1&&com==3)
{
cout<<"贏"<<endl;
}
else if(player==2&&com==1)
{
cout<<"贏"<<endl;
}
else if(player==3&&com==2)
{
cout<<"贏"<<endl;
}
else
{
cout<<"輸"<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
黃安立
時間:
2018-4-14 15:15
#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
int main()
{
srand(time(NULL));
int player =0;
int com =0;
string mora [] = {"剪刀","石頭","布"};
cout << "請選擇想要出的拳 (1:剪刀 2:石頭 3:布)"<< endl;
cin >> player;
cout << "電腦出拳中..." << endl;
com = (rand()%3)+1;
cout << "你出:" << mora[player-1] << endl;
cout << "電腦出:" << mora[com-1] << endl;
{
cout<<"電腦出布"<<endl;
}
if(player==1&&com==1)
{
cout<<"平手"<<endl;
}
else if(player==1&&com==2)
{
cout<<"你輸了"<<endl;
}
else if(player==1&&com==3)
{
cout<<"你贏了"<<endl;
}
else if(player==2&&com==1)
{
cout<<"你贏了"<<endl;
}
else if(player==2&&com==2)
{
cout<<"平手"<<endl;
}
else if(player==2&&com==3)
{
cout<<"你輸了"<<endl;
}
else if(player==3&&com==1)
{
cout<<"你輸了"<<endl;
}
else if(player==3&&com==2)
{
cout<<"你贏了"<<endl;
}
else if(player==3&&com==3)
{
cout<<"平手"<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
王駿愷
時間:
2018-4-14 15:15
#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
int main()
{
int player=0;
int com=0;
string mora[]={"剪刀","石頭","布"};
cout<<"請出拳(1剪刀.2石頭3布)"<<endl;
cin>>player;
cout<<"電腦出拳中。。。"<<endl;
srand(time(NULL));
com=rand()%3+1;
cout<<"電腦出"<<mora[com+1]<<endl ;
if((player==1&&com==3))
{
cout<<"你贏了"<<endl;
}
else if((player==2&&com==1))
{
cout<<"你贏了"<<endl;
}
else if((player==3&&com==1))
{
cout<<"你贏了"<<endl;
}
else if(player=com)
{
cout<<"平手"<<endl;
}
else
{
cout<<"你輸了"<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
顏詢
時間:
2018-4-14 15:19
#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
int main()
{
int player=0;
int com=0;
string mora[]={"剪刀","石頭","布"}
cout<<"請輸入要出的拳(1:剪刀 2:石頭 3:布)"<<endl;
cin>>a;
cout<<"電腦出拳中.."<<endl;
srand(time(NULL));
com=rand3+1
cout << "電腦出:" << mora[com-1] << endl;
cout << "你出:" << mora[player-1] << endl;
if(player == 1 && com==3) {
cout << "你贏了" << endl;
}
else if(player == 2 && com==1) {
cout << "你贏了" << endl;
}
else if(player == 3 && com==2) {
cout << "你贏了" << endl;
}
else if(player == com) {
cout << "平手" << endl;
}
else {
cout << "你輸了" << endl;
}
system("pause");
return 0;
}
複製代碼
作者:
湯東緯
時間:
2018-4-14 15:24
#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
int main()
{
srand(time(NULL));
int player=0;
int com=0;
string mora[] = {"剪刀","石頭","布"};
cout<<"請出拳(1)剪刀(2)石頭(3)布"<<endl;
cin>>player;
cout<<"電腦出拳中..."<<endl;
com=(rand()%3)+1;
cout <<"你出:"<< mora[player-1] << endl;
cout <<"電腦出:"<< mora[com-1] << endl;
if(player == 1&&com==3) {
cout << "你贏了" << endl;
}
else if(player ==2&&com==1) {
cout <<"你贏了"<< endl;
}
else if(player == 3&&com==2) {
cout <<"你贏了"<< endl;
}
else if(player == com) {
cout <<"平手"<< endl;
}
else {
cout <<"你輸了"<< endl;
}
system("pause");
return 0;
}
複製代碼
作者:
吳秉翰
時間:
2018-4-14 15:26
#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
int main()
{
srand(time(NULL));
int player =0;
int com =0;
string mora[] = {"剪刀","石頭","布"};
cout << "選擇要出的拳 ((1)剪刀(2)石頭(3)布)"<< endl;
cin >> player;
com = (rand()%3)+1;
cout << "你出" << mora[player-1] << endl;
cout << "電腦出" << mora[com-1] << endl;
if(player==com)
{
cout<<"平手"<<endl;
}
else if(player==1&&com==3)
{
cout<<"贏"<<endl;
}
else if(player==2&&com==1)
{
cout<<"贏"<<endl;
}
else if(player==3&&com==2)
{
cout<<"贏"<<endl;
}
else
{
cout<<"輸"<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
戴嘉禾
時間:
2018-4-14 15:30
#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
int main()
{
re:
system("cls");
srand(time(NULL));
int player=0;
int com=0;
string mora[]={"剪刀","石頭","布"};
cout<< "請輸入你想要出的拳 (1)剪刀(2)石頭(3)布" <<endl;
cin>>player;
cout<< "電腦出拳中..." <<endl;
com=(rand()%3)+1;
cout<< "電腦出" << mora[com-1] <<endl;
if(player==1&&com==3)
cout<<"你贏了"<<endl;
else if(player==2&&com==1)
cout<<"你贏了"<<endl;
else if(player==3&&com==2)
cout<<"你贏了"<<endl;
else if(player==com)
cout<<"平手"<<endl;
else
cout<<"你輸了"<<endl;
system("pause");
goto re;
return 0;
}
複製代碼
作者:
鄭楀諺
時間:
2018-4-21 14:40
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main()
{
//剪刀石頭布
string win[3]={"你贏","電腦贏","平手"};
int player=0;
int com=0;
cout<<"輸入你要的拳(1.剪刀2.石頭3.布)"<<endl;
cin>>player;
cout<<"電腦出拳中..."<<endl;
srand(time(NULL));
com=(rand()%3)+1;
if(player==1&&com==3)
{
cout<<win[0]<<endl;
}
if(player==1&&com==2)
{
cout<<win[1]<<endl;
}
if(player==2&&com==1)
{
cout<<win[0]<<endl;
}
if(player==2&&com==3)
{
cout<<win[1]<<endl;
}
if(player==3&&com==2)
{
cout<<win[0]<<endl;
}
if(player==2&&com==3)
{
cout<<win[1]<<endl;
}
else
{
cout<<win[2]<<endl;
}
system("PAUSE");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2