当前位置:   article > 正文

C++实现的五子棋_c++五子棋

c++五子棋

第一步:先创建一个Chessman.h主要作用是用于表示棋盘上的每一个棋子。其实现的cpp文件不需要(内容为空)。

  1. enum ChessmanType{
  2. BLACK,
  3. WHITE,
  4. NONE
  5. };
  6. class Chessman{
  7. public:
  8. ChessmanType type = ChessmanType::NONE;
  9. };

第二部则需要创建一个棋盘类Chessman.h:

  1. #include "Chessman.h"
  2. class CheckBoard
  3. {
  4. private:
  5. /* data */
  6. public:
  7. CheckBoard(int w,int h);
  8. int width = 0;
  9. int height = 0;
  10. Chessman* table = nullptr;//table为棋盘数组。
  11. ~CheckBoard();
  12. Chessman Get(int x,int y);//得到棋盘上(x,y)位置 的棋子类型。
  13. int put(int x,int y,ChessmanType type);//落子
  14. int print();
  15. int IsWin(int x,int y,ChessmanType type);
  16. };

    其具体的实现放在Chessman.cpp里面:

  1. #include "CheckBoard.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. CheckBoard::CheckBoard(int w, int h)
  5. {
  6. width = w;
  7. height = h;
  8. table = (Chessman *)malloc(sizeof(int) * width * height);
  9. for (int i = 0; i < width * height; i++)
  10. {
  11. table[i].type = ChessmanType::NONE;
  12. }
  13. }
  14. CheckBoard::~CheckBoard()
  15. {
  16. if (table != nullptr)
  17. {
  18. free(table);
  19. table = nullptr;
  20. }
  21. }
  22. Chessman CheckBoard::Get(int x, int y)
  23. {
  24. if (x < 0 || x > width)
  25. {
  26. Chessman res;
  27. res.type = ChessmanType::NONE;
  28. return res;
  29. }
  30. if (y < 0 || y > height)
  31. {
  32. Chessman res;
  33. res.type = ChessmanType::NONE;
  34. return res;
  35. }
  36. int index = y * width + x;
  37. return table[index];
  38. }
  39. int CheckBoard::put(int x, int y, ChessmanType type)
  40. {
  41. Chessman chessman = Get(x, y);
  42. if (x < 0 || x > width)
  43. {
  44. return -1;
  45. }
  46. if (y < 0 || y > height)
  47. {
  48. return -1;
  49. }
  50. if (chessman.type != ChessmanType::NONE)
  51. {
  52. return -1;
  53. }
  54. int index = y * width + x;
  55. table[index].type = type;
  56. return 0;
  57. }
  58. int CheckBoard::print()
  59. {
  60. for (int j = 0; j < height; j++)
  61. {
  62. for (int i = 0; i < width; i++)
  63. {
  64. Chessman chessman = Get(i, j);
  65. if (chessman.type == ChessmanType::BLACK)
  66. {
  67. printf("B");
  68. }
  69. else if (chessman.type == ChessmanType::WHITE)
  70. {
  71. printf("W");
  72. }
  73. else
  74. {
  75. printf("-");
  76. }
  77. }
  78. printf("\n");
  79. }
  80. return 0;
  81. }
  82. int CheckBoard::IsWin(int x, int y, ChessmanType type)
  83. {
  84. //横向。
  85. int count = 1;
  86. for (int i = x - 1; i >= 0; i--)
  87. {
  88. Chessman chessman = Get(i, y);
  89. if (chessman.type != type)
  90. {
  91. break;
  92. }
  93. count++;
  94. }
  95. for (int i = x + 1; i < width; i++)
  96. {
  97. Chessman chessman = Get(i, y);
  98. if (chessman.type != type)
  99. {
  100. break;
  101. }
  102. count++;
  103. }
  104. if (count >= 5)
  105. {
  106. return 1;
  107. }
  108. //纵向
  109. count = 1;
  110. for (int j = y - 1; j >= 0; j--)
  111. {
  112. Chessman chessman = Get(x, j);
  113. if (chessman.type != type)
  114. {
  115. break;
  116. }
  117. count++;
  118. }
  119. for (int j = y + 1; j < height; j++)
  120. {
  121. Chessman chessman = Get(x, j);
  122. if (chessman.type != type)
  123. {
  124. break;
  125. }
  126. count++;
  127. }
  128. if (count >= 5)
  129. {
  130. return 1;
  131. }
  132. //左上-右下
  133. count = 1;
  134. for (int i = 1; i < 5; i++)
  135. {
  136. Chessman chessman = Get(x - i, y - i);
  137. if (chessman.type != type)
  138. {
  139. break;
  140. }
  141. count++;
  142. }
  143. for (int i = 1; i < 5; i++)
  144. {
  145. Chessman chessman = Get(x + i, y + i);
  146. if (chessman.type != type)
  147. {
  148. break;
  149. }
  150. count++;
  151. }
  152. if (count >= 5)
  153. {
  154. return 1;
  155. }
  156. //右上-左下
  157. count = 1;
  158. for (int i = 1; i < 5; i++)
  159. {
  160. Chessman chessman = Get(x + i, y - i);
  161. if (chessman.type != type)
  162. {
  163. break;
  164. }
  165. count++;
  166. }
  167. for(int i = 1;i < 5;i ++){
  168. Chessman chessman = Get(x - i, y + i);
  169. if (chessman.type != type)
  170. {
  171. break;
  172. }
  173. count++;
  174. }
  175. if (count >= 5)
  176. {
  177. return 1;
  178. }
  179. return 0;
  180. }

最后是main.cpp

  1. #include "CheckBoard.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main(){
  5. CheckBoard checkboard = CheckBoard(10,10);
  6. checkboard.print();
  7. while(1){
  8. int x,y;
  9. while(1){
  10. printf("黑子:");
  11. scanf("%d,%d",&x,&y);
  12. int ret = checkboard.put(x,y,ChessmanType::BLACK);
  13. if(ret){
  14. printf("错误重新输入:");
  15. continue;
  16. }
  17. break;
  18. }
  19. checkboard.print();
  20. if(checkboard.IsWin(x,y,ChessmanType::BLACK)){
  21. printf("黑子win\n");
  22. break;
  23. }
  24. while(1){
  25. printf("白子:");
  26. scanf("%d,%d",&x,&y);
  27. int ret = checkboard.put(x,y,ChessmanType::WHITE);
  28. if(ret){
  29. printf("错误重新输入:");
  30. continue;
  31. }
  32. break;
  33. }
  34. checkboard.print();
  35. if(checkboard.IsWin(x,y,ChessmanType::WHITE)){
  36. printf("白子win\n");
  37. break;
  38. }
  39. }
  40. return 0;
  41. }

运行结果展示:
 

  1. 黑子:0,0
  2. B---------
  3. ----------
  4. ----------
  5. ----------
  6. ----------
  7. ----------
  8. ----------
  9. ----------
  10. ----------
  11. ----------
  12. 白子:4,5
  13. B---------
  14. ----------
  15. ----------
  16. ----------
  17. ----------
  18. ----W-----
  19. ----------
  20. ----------
  21. ----------
  22. ----------
  23. 黑子:1,0
  24. BB--------
  25. ----------
  26. ----------
  27. ----------
  28. ----------
  29. ----W-----
  30. ----------
  31. ----------
  32. ----------
  33. ----------
  34. 白子:6,9
  35. BB--------
  36. ----------
  37. ----------
  38. ----------
  39. ----------
  40. ----W-----
  41. ----------
  42. ----------
  43. ----------
  44. ------W---
  45. 黑子:2,0
  46. BBB-------
  47. ----------
  48. ----------
  49. ----------
  50. ----------
  51. ----W-----
  52. ----------
  53. ----------
  54. ----------
  55. ------W---
  56. 白子:8,9
  57. BBB-------
  58. ----------
  59. ----------
  60. ----------
  61. ----------
  62. ----W-----
  63. ----------
  64. ----------
  65. ----------
  66. ------W-W-
  67. 黑子:3,0
  68. BBBB------
  69. ----------
  70. ----------
  71. ----------
  72. ----------
  73. ----W-----
  74. ----------
  75. ----------
  76. ----------
  77. ------W-W-
  78. 白子:7,9
  79. BBBB------
  80. ----------
  81. ----------
  82. ----------
  83. ----------
  84. ----W-----
  85. ----------
  86. ----------
  87. ----------
  88. ------WWW-
  89. 黑子:4,0
  90. BBBBB-----
  91. ----------
  92. ----------
  93. ----------
  94. ----------
  95. ----W-----
  96. ----------
  97. ----------
  98. ----------
  99. ------WWW-
  100. 黑子win
  101. (base) PS E:\VS_Project\C++_Study\wuziqi>

不足之处希望多多指导。时隔3年的第一篇博客。QAQ.

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号