新微赢技术网

标题: 猜拳游戏求教 [打印本页]

作者: 地瓜    时间: 2009-11-4 00:00
标题: 猜拳游戏求教
#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
void CQ()
{int i,j;
srand(unsigned(time(0)));
cout<<"输入的数值为1,2,3否则后果自负"<<endl;
cout<<"1-石头 2-剪刀 3-布"<<endl<<"你与对方大喊到:";
cin>>i;
if(i<=3&&i!=0)
{//你出拳
if(i==1)cout<<"石头!"<<endl;
if(i==2)cout<<"剪刀!"<<endl;
if(i==3)cout<<"布!"<<endl;
//它出拳
j=(rand()%3+1);
if(j==1)cout<<"石头!"<<endl;
if(j==2)cout<<"剪刀!"<<endl;
if(j==3)cout<<"布!"<<endl;
//比大小
if(j==i)
{cout<<"你俩不相上下啊!"<<endl;}
if(j==1&&i==2)
{cout<<"你说到:佩服!佩服!还是你高!"<<endl;}
if(i==1&&j==2)
{cout<<"它说到:它NND该死的剪刀!"<<endl;}
if(j==2&&i==3)
{cout<<"你说到佩服!佩服!还是你高!"<<endl;}
if(j==3&&i==2)
{cout<<"它说到:它NND该死的布!"<<endl;}
if(j==1&&i==3)
{cout<<"它说到:它NND该死的石头!"<<endl;}
if(j==3&&i==1)
{cout<<"你说到:佩服!佩服!还是你高!"<<endl;}
}
else
{cout<<"它说道:哎! 就 就 就你不按套路出牌!"<<endl;exit(0);}
}
int main()
{int i,k=0,p=0,q=0;
cout<<"你要来多少局"<<endl;
cin>>i;
while(i!=0)
{CQ();i--;}
return 0;
}
我还想实现 平局 胜利 失败的统计数和当我输入数值不是1,2,3时能够重新输入。
作者: 听妈妈的话    时间: 2009-11-4 00:00
挺有意思的闹
作者: 敬你永远遗忘    时间: 2009-11-4 00:00
小游戏的总结构自己想好 在写出个大概的总程序 多想多改----完美程序
出错的小地方想得实在不明白或者...... 才可以问别人么
你已经画出了轮廓,在帮它补妆,就OK了`

 你写出了该游戏的核心部分,加油噢~~
作者: 风忠人    时间: 2009-11-4 00:00
我写的一个
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;

  4. class Random
  5. {
  6. private:
  7. Random() { srand(time(0)); }
  8. Random(const Random &ri) { }
  9. void operator=(Random &ri) { }
  10. public:
  11. static Random& get() { static Random r; return r; }
  12. int random() { return rand(); }
  13. };

  14. const string& RPS(int rps)
  15. {
  16. const static string rps_str[3] = {"Rock", "Scissors", "Paper"};
  17. switch(rps)
  18. {
  19. case 1: return rps_str[0];
  20. case 2: return rps_str[1];
  21. case 3: return rps_str[2];
  22. default:
  23. cout<<"RPS error"<<endl;
  24. }
  25. }

  26. class PK
  27. {
  28. public:
  29. void run(int n)
  30. {
  31. cout<<"input 1-Rock 2-Scissors 3-Paper"<<endl;
  32. win = loss = draw = 0;
  33. int total = n;
  34. while(n-- != 0)
  35. {
  36. your_act();
  37. computer_act();
  38. judge();
  39. }
  40. cout<<"\nresult\ntotal : "<<total<<"\nyou win : "<<win
  41. <<"\nyou loss : "<<loss<<"\ndraw : "<<draw<<endl;
  42. }

  43. private:
  44. void your_act()
  45. {
  46. cout<<"\nyour turn\n";
  47. char in;
  48. cin>>in;
  49. while(in<'1' || in>'3')
  50. {
  51. cout<<"illegality!over again\n";
  52. cin>>in;
  53. }
  54. switch(in)
  55. {
  56. case '1': you = 1; cout<<"you "<<RPS(1)<<endl;break;
  57. case '2': you = 2; cout<<"you "<<RPS(2)<<endl;break;
  58. case '3': you = 3; cout<<"you "<<RPS(3)<<endl;break;
  59. }
  60. }

  61. void computer_act()
  62. {
  63. computer = (Random::get().random()%3 + 1);
  64. }

  65. void judge()
  66. {
  67. if(you == computer)
  68. {
  69. draw++;
  70. cout<<"computer "<<RPS(computer)<<"\ndraw"<<endl;
  71. }
  72. else if(you - computer == 1 || you - computer == -2)
  73. {
  74. loss++;
  75. cout<<"computer "<<RPS(computer)<<"\nyou loss"<<endl;
  76. }
  77. else
  78. {
  79. win++;
  80. cout<<"computer "<<RPS(computer)<<"\nyou win"<<endl;
  81. }
  82. }

  83. int you, computer;
  84. int win, loss, draw;

  85. };

  86. int main()
  87. {
  88. int n;
  89. cout<<"how many ?"<<endl;
  90. cin>>n;
  91. PK pk;
  92. pk.run(n);
  93. return 0;
  94. }
复制代码





欢迎光临 新微赢技术网 (http://bbs.weiying.cn/) Powered by Discuz! X3.2