求一套完整代码

xnsbb19x 2010-10-21 12:31:32
猜颜色游戏
从红、绿、黄、白中随机生成四个颜色,分别用R,G,Y,W表示(例如黄绿绿红用YGGR表示),这个结果不要显示,游戏开始后参与者通过依次输入所猜颜色的代码进行猜色,每次猜色后,软件提示nAmB,A表示位置和颜色都正确,n表示A的数目,B表示颜色正确位置不正确,m表示B的数目,游戏参与者根据提示继续猜色,直到全部正确或者次数超过8次程序结束。
基本要求1:完成基本游戏,达到8次未猜出结果的时候要给出答案
基本要求2:画出程序流程图,必须使用类完成本次作业
高级要求1:任何时候输入E立即给出答案并结束本轮游戏;输入X,经确认后直接退出游戏
高级要求2:总颜色增加紫色和橙色,用P和O表示,允许的猜测次数增加到12次
高级要求3:增加游戏排行榜
高级要求4:增加你觉得有意义的功能
...全文
87 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
manytao 2010-10-21
  • 打赏
  • 举报
回复

1. #include <iostream>
2. #include <algorithm>
3. #include <iterator>
4. #include <string>
5. #include <time.h>
6. using namespace std;
7.
8. class GuessColor{
9. private:
10. typedef string colorlist_t;
11. colorlist_t m_cl;
12. public:
13. // 从字符串初始化颜色表
14. void fromString(const string &str){
15. m_cl = str;
16. }
17.
18. // 从gc随机取颜色
19. void sampleFrom( const GuessColor& gc ) {
20. m_cl.resize(gc.m_cl.size());
21. for(colorlist_t::iterator i=m_cl.begin(); i!=m_cl.end(); ++i){
22. *i = gc.m_cl[ random( m_cl.size() ) ];
23. }
24. }
25. // 显示颜色表
26. friend ostream& operator<< (ostream& os, const GuessColor& gc){
27. copy(gc.m_cl.begin(), gc.m_cl.end(),
28. ostream_iterator<char>(os));
29. return os;
30. }
31. // 对比另一个GuessColor,得到nA和mB(other会被改变)
32. void nAmB(GuessColor& other, int *nA, int *nB) const{
33. *nA = 0, *nB = 0;
34.
35. colorlist_t A = other.m_cl;
36. colorlist_t B = m_cl;
37.
38. colorlist_t::iterator itr,itrOth;
39. for(itr=A.begin(), itrOth = B.begin();
40. itr!=A.end() && itrOth!=B.end(); ++itr,++itrOth)
41. if(*itr == *itrOth) {
42. ++*nA;
43. *itrOth = '\0';
44. *itr = '\0';
45. }
46.
47. for(itr=A.begin();itr!=A.end(); ++itr)
48. {
49. if(*itr != '\0')
50. {
51. colorlist_t::iterator itrFind=find(B.begin(),B.end(), *itr);
52. if(itrFind != B.end()){
53. ++*nB;
54. *itrFind = '\0';
55. }
56. }
57. }
58. }
59. public:
60. static void randomize(){
61. srand( (unsigned)time(NULL));
62. }
63. static size_t random(size_t max){
64. return (double)rand() / (RAND_MAX + 1) * max;
65. }
66. };
67.
68. int main(int argc, char* argv[])
69. {
70. const char szAllColor[] = "RGYW";
71. const size_t nColorNumber = sizeof(szAllColor)-1;
72.
73. GuessColor::randomize();
74. GuessColor gcAll;
75. GuessColor gcRand;
76. GuessColor gcInput;
77.
78. gcAll.fromString(szAllColor);
79.
80. gcRand.sampleFrom(gcAll);
81. for(int gt=0; gt<8; gt++) {
82. string s;
83. cout << endl << "输入颜色组合["<<gcAll<<"]:";
84. getline(cin, s);
85. transform(s.begin(), s.end(), s.begin(), (int (*)(int))toupper);
86. if(s == "X"){
87. char c;
88. cout << endl << "确认退出吗?(Y/N)";
89. cin >> c;
90. if(c == 'Y' || c == 'y')
91. break;
92. else{
93. gt--; //这次不算次数
94. continue;
95. }
96. }
97. else if(s == "E"){
98. break;
99. }
100.
101. s.resize(nColorNumber, '\0');
102. gcInput.fromString(s);
103. int nA, nB;
104. gcRand.nAmB(gcInput, &nA, &nB);
105.
106. cout << endl << nA << 'A' << nB << 'B';
107. if(nA == 4){
108. cout << endl << "猜对啦~~聪明!!!";
109. break;
110. }
111. }
112.
113. cout << endl << "答案:" << gcRand << endl;
114. system("pause");
115. return 0;
116. }


g++环境编译运行的 lz试试吧
xnsbb19x 2010-10-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 manytao 的回复:]

C/C++ code

1. #include <iostream>
2. #include <algorithm>
3. #include <iterator>
4. #include <string>
5. #include <time.h>
6. using namespace std;
7.
8. class……
[/Quote]

只有一个警告 能运行 哈哈!

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧