当前位置:   article > 正文

基于C++实现五子棋_c++五子棋

c++五子棋

一、五子棋项目介绍

1.游戏规则

五子棋是我国古代传统的黑白棋种之一,黑白双方依次落子,任意一方在棋盘上形成横向、纵向、斜向的连续相同颜色的五颗棋子的一方为胜。

二、棋子对象


1.棋子类


关于棋子类我们可以做如下几点思考:
由于黑棋和白棋的显示是不同的,棋子类的显示方法是不知道如何实现的,所以它是一个抽象类。每棵棋子在棋盘的不同位置存放,所以它应该包含棋子在屏幕上面的具体坐标。
每颗棋子都有自己的颜色,所以它应该包含颜色。

  1. #ifndef CHESS_HPP
  2. #define CHESS_HPP
  3. #include <iostream>
  4. #include <cstdio>
  5. using namespace std;
  6. class Chess
  7. {
  8. public:
  9. Chess(int x,int y,char color) : x(x),y(y),color(color)
  10. {
  11. }
  12. virtual ~Chess() {}
  13. virtual void show() = 0;
  14. int getX() const
  15. {
  16. return x;
  17. }
  18. int getY() const
  19. {
  20. return y;
  21. }
  22. char getColor() const
  23. {
  24. return color;
  25. }
  26. private:
  27. int x;
  28. int y;
  29. char color;
  30. };
  31. #endif

2.黑棋类

黑棋类应该继承棋子类,然后实现棋子类的void show(void) const函数,在这个函数中显示黑色棋子。

黑棋类代码如下:

  1. #ifndef BLACKCHESS_HPP
  2. #define BLACKCHESS_HPP
  3. #include "Chess.hpp"
  4. #include "Config.hpp"
  5. class BlackChess : public Chess
  6. {
  7. public:
  8. BlackChess(int x,int y) : Chess(x,y,BLACK)
  9. {
  10. }
  11. void show()
  12. {
  13. printf("\033[%d;%dH%s",getY(),getX()-1,BLACK_COLOR);
  14. printf("\033[%d;%dH",getY(),getX());//光标归位
  15. }
  16. };
  17. #endif

3.白棋类

 白棋类应该继承棋子类,然后实现棋子类的void show(void) const函数,在这个函数中显示白色棋子。

白棋类代码如下:

  1. #ifndef WHITE_HPP
  2. #define WHITE_HPP
  3. #include "Chess.hpp"
  4. #include "Config.hpp"
  5. class WhiteChess : public Chess
  6. {
  7. public:
  8. WhiteChess(int x,int y) : Chess(x,y,WHITE)
  9. {
  10. }
  11. void show()
  12. {
  13. printf("\033[%d;%dH%s",getY(),getX()-1,WHITE_COLOR);
  14. printf("\033[%d;%dH",getY(),getX());//光标归位
  15. }
  16. };
  17. #endif

三、棋盘对象

棋盘对象只有—个,我们可以使用单例模式来设计

1.存放棋子

整个棋盘有15行和15列构成,我们把每个棋子都看成是一个对象,我们只需要记录这些对象的首地址就可以了,可以用一个二维的指针数组存放。

2.落子位置是否有效

如果落子位置已经有棋子了则返回false ,如果落子位置没有棋子则返回true 。

棋盘类代码如下:

  1. #ifndef CHESSBOARD_HPP
  2. #define CHESSBOARD_HPP
  3. #include "Chess.hpp"
  4. #include "Config.hpp"
  5. #include <errno.h>
  6. #include <cstring>
  7. class ChessBoard
  8. {
  9. public:
  10. static ChessBoard* getInstance()
  11. {
  12. return cb;
  13. }
  14. void show()
  15. {
  16. char buf[1024] = {0};
  17. FILE *fp = fopen(CHESS_BOARD_FILE,"r");
  18. if(fp == NULL)
  19. {
  20. fprintf(stderr,"Fail to open ChessBoard.txt: %s\n",strerror(errno));
  21. return ;
  22. }
  23. cout << "\033[1;1H" << CHESS_BOARD_COLOR;
  24. while(fgets(buf,sizeof(buf),fp) != NULL)
  25. {
  26. fputs(buf,stdout);
  27. }
  28. fclose(fp);
  29. cout << "\033[0m";
  30. }
  31. bool isValidPostion(int line,int column)
  32. {
  33. if(line >= LINE || line < 0 || column >= COLUMN || column < 0)
  34. {
  35. return false;
  36. }
  37. return chess[line][column] == NULL ? true : false;
  38. }
  39. bool placeChess(Chess *chess)
  40. {
  41. int line = (chess->getY() - 1)/LINE_INTERVAL;
  42. int column = (chess->getX() - 1)/COLUMN_INTERVAL;
  43. if(isValidPostion(line,column))
  44. {
  45. this->chess[line][column] = chess;
  46. chess->show();
  47. curLine = line;
  48. curColumn = column;
  49. return true;
  50. }
  51. return false;
  52. }
  53. bool isValidColorChess(int line,int column,char color)
  54. {
  55. if(line < 0 || line >= LINE || column < 0 || column >= COLUMN)
  56. {
  57. return false;
  58. }
  59. Chess *ch = chess[line][column];
  60. if(ch == NULL)
  61. {
  62. return false;
  63. }
  64. if(ch->getColor() != color)
  65. {
  66. return false;
  67. }
  68. return true;
  69. }
  70. int getCurLine() const
  71. {
  72. return curLine;
  73. }
  74. int getCurColumn() const
  75. {
  76. return curColumn;
  77. }
  78. private:
  79. ChessBoard() : curLine(-1),curColumn(-1)
  80. {
  81. for(int i = 0; i < LINE; ++i)
  82. {
  83. for(int j = 0; j < COLUMN; ++j)
  84. {
  85. chess[i][j] = NULL;
  86. }
  87. }
  88. }
  89. ~ChessBoard()
  90. {
  91. }
  92. static ChessBoard *cb;
  93. Chess *chess[LINE][COLUMN];
  94. int curLine;
  95. int curColumn;
  96. };
  97. ChessBoard *ChessBoard::cb = new ChessBoard;
  98. #endif

四、棋手对象

五子棋游戏中有两个玩家,一个玩家下黑旗,一个玩家下白旗。每个玩家至少包含以下成员:

1.玩家的名字 2.玩家下棋的棋子颜色  3.在棋盘落子的方法

1.玩家类

  1. #ifndef PLAYER_HPP
  2. #define PLAYER_HPP
  3. #include<iostream>
  4. using namespace std;
  5. class Player
  6. {
  7. public:
  8. Player(const string &name,char color) : name(name),color(color)
  9. {
  10. }
  11. virtual ~Player() {}
  12. char getColor() const
  13. {
  14. return color;
  15. }
  16. string getName() const
  17. {
  18. return name;
  19. }
  20. virtual bool placeChess(int x, int y) = 0;
  21. private:
  22. string name;
  23. char color;
  24. };
  25. #endif

2.黑棋玩家类

  1. #ifndef BLACKPLAYER_HPP
  2. #define BLACKPLAYER_HPP
  3. #include "Player.hpp"
  4. #include "Config.hpp"
  5. #include "ChessBoard.hpp"
  6. #include "BlackChess.hpp"
  7. class BlackPlayer : public Player
  8. {
  9. public:
  10. BlackPlayer(const string &name) : Player(name,BLACK)
  11. {
  12. }
  13. bool placeChess(int x,int y)
  14. {
  15. BlackChess *bc = new BlackChess(x,y);
  16. if(!ChessBoard::getInstance()->placeChess(bc))
  17. {
  18. delete bc;
  19. return false;
  20. }
  21. return true;
  22. }
  23. };
  24. #endif

3.白棋玩家类

  1. #ifndef WHITEPLAYER_HPP
  2. #define WHITEPLAYER_HPP
  3. #include "Player.hpp"
  4. #include "Config.hpp"
  5. #include "ChessBoard.hpp"
  6. #include "WhiteChess.hpp"
  7. class WhitePlayer : public Player
  8. {
  9. public:
  10. WhitePlayer(const string &name) : Player(name,WHITE)
  11. {
  12. }
  13. bool placeChess(int x,int y)
  14. {
  15. WhiteChess *bc = new WhiteChess(x,y);
  16. if(!ChessBoard::getInstance()->placeChess(bc))
  17. {
  18. delete bc;
  19. return false;
  20. }
  21. return true;
  22. }
  23. };
  24. #endif

五、按键方向类

  1. #ifndef KEYHANDLE_HPP
  2. #define KEYHANDLE_HPP
  3. #include "Cursor.hpp"
  4. #include "Player.hpp"
  5. #include <termios.h>
  6. #include <unistd.h>
  7. #include <cstdlib>
  8. class KeyHandle
  9. {
  10. public:
  11. KeyHandle()
  12. {
  13. #if 0
  14. struct termios attr;
  15. tcgetattr(0,&attr);
  16. attr.c_lflag &= ~(ICANON | ECHO);
  17. tcsetattr(0,TCSANOW,&attr);
  18. #else
  19. system("stty -icanon");
  20. system("stty -echo");
  21. #endif
  22. }
  23. ~KeyHandle()
  24. {
  25. #if 0
  26. struct termios attr;
  27. tcgetattr(0,&attr);
  28. attr.c_lflag |= (ICANON | ECHO);
  29. tcsetattr(0,TCSANOW,&attr);
  30. #else
  31. system("stty icanon");
  32. system("stty echo");
  33. #endif
  34. }
  35. bool waitPlayerPlaceChess(Player *player)
  36. {
  37. char key = '\0';
  38. bool ret = false;
  39. while(1)
  40. {
  41. key = getchar();
  42. switch(key)
  43. {
  44. case 'w':
  45. case 'W':
  46. cursor.moveUp();
  47. break;
  48. case 's':
  49. case 'S':
  50. cursor.moveDown();
  51. break;
  52. case 'a':
  53. case 'A':
  54. cursor.moveLeft();
  55. break;
  56. case 'd':
  57. case 'D':
  58. cursor.moveRight();
  59. break;
  60. case ' ':
  61. ret = player->placeChess(cursor.getX(),cursor.getY());
  62. break;
  63. }
  64. if(ret)
  65. {
  66. break;
  67. }
  68. }
  69. }
  70. private:
  71. Cursor cursor;
  72. };
  73. #endif

六、裁判类

  1. #ifndef ARBITRATION_HPP
  2. #define ARBITRATION_HPP
  3. #include "ChessBoard.hpp"
  4. class Arbitration
  5. {
  6. public:
  7. bool isWin(char color)
  8. {
  9. ChessBoard *cb = ChessBoard::getInstance();
  10. int curLine = cb->getCurLine();
  11. int curColumn = cb->getCurColumn();
  12. bool ret = false;
  13. ret = isDirectionWin(curLine,curColumn,color,1,0);
  14. if(ret)
  15. {
  16. return true;
  17. }
  18. ret = isDirectionWin(curLine,curColumn,color,0,1);
  19. if(ret)
  20. {
  21. return true;
  22. }
  23. ret = isDirectionWin(curLine,curColumn,color,1,1);
  24. if(ret)
  25. {
  26. return true;
  27. }
  28. ret = isDirectionWin(curLine,curColumn,color,1,-1);
  29. if(ret)
  30. {
  31. return true;
  32. }
  33. return false;
  34. }
  35. bool isDirectionWin(int line,int column,char color,int x,int y)
  36. {
  37. ChessBoard *cb = ChessBoard::getInstance();
  38. int count = 1;
  39. for(int i = 1; i < 5; ++i)
  40. {
  41. bool ret = cb->isValidColorChess(line + (i * y),column + (i * x),color);
  42. if(!ret)
  43. {
  44. break;
  45. }
  46. ++count;
  47. }
  48. for(int i = 1; i < 5; ++i)
  49. {
  50. bool ret = cb->isValidColorChess(line - (i * y),column - (i * x),color);
  51. if(!ret)
  52. {
  53. break;
  54. }
  55. ++count;
  56. }
  57. if(count >= 5)
  58. {
  59. return true;
  60. }
  61. return false;
  62. }
  63. };
  64. #endif

七、mian.c

  1. #include "BlackPlayer.hpp"
  2. #include "WhitePlayer.hpp"
  3. #include "ChessBoard.hpp"
  4. #include "KeyHandle.hpp"
  5. #include "Arbitration.hpp"
  6. int main(int argc, const char *argv[])
  7. {
  8. cout << "\033[2J";
  9. ChessBoard::getInstance()->show();
  10. KeyHandle KeyHandle;
  11. Arbitration arb;
  12. Player *player[2];
  13. player[0] = new BlackPlayer("张三");
  14. player[1] = new WhitePlayer("李四");
  15. bool isWin = false;
  16. while(1)
  17. {
  18. for(int i = 0; i < 2; ++i)
  19. {
  20. KeyHandle.waitPlayerPlaceChess(player[i]);
  21. isWin = arb.isWin(player[i]->getColor());
  22. if(isWin)
  23. {
  24. cout << "\033[32;0H" << player[i]->getName() << "获得胜利";
  25. break;
  26. }
  27. }
  28. if(isWin)
  29. {
  30. break;
  31. }
  32. }
  33. delete player[0];
  34. delete player[1];
  35. cout << "\033[0m\033[35;0H";
  36. return 0;
  37. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/243683
推荐阅读
相关标签
  

闽ICP备14008679号