赞
踩
第一步:先创建一个Chessman.h主要作用是用于表示棋盘上的每一个棋子。其实现的cpp文件不需要(内容为空)。
- enum ChessmanType{
- BLACK,
- WHITE,
- NONE
- };
-
- class Chessman{
-
- public:
- ChessmanType type = ChessmanType::NONE;
- };
第二部则需要创建一个棋盘类Chessman.h:
-
- #include "Chessman.h"
- class CheckBoard
- {
- private:
- /* data */
- public:
- CheckBoard(int w,int h);
- int width = 0;
- int height = 0;
-
- Chessman* table = nullptr;//table为棋盘数组。
-
- ~CheckBoard();
- Chessman Get(int x,int y);//得到棋盘上(x,y)位置 的棋子类型。
- int put(int x,int y,ChessmanType type);//落子
- int print();
- int IsWin(int x,int y,ChessmanType type);
- };
-

其具体的实现放在Chessman.cpp里面:
- #include "CheckBoard.h"
- #include <stdio.h>
- #include <stdlib.h>
- CheckBoard::CheckBoard(int w, int h)
- {
- width = w;
- height = h;
- table = (Chessman *)malloc(sizeof(int) * width * height);
- for (int i = 0; i < width * height; i++)
- {
- table[i].type = ChessmanType::NONE;
- }
- }
- CheckBoard::~CheckBoard()
- {
- if (table != nullptr)
- {
- free(table);
- table = nullptr;
- }
- }
- Chessman CheckBoard::Get(int x, int y)
- {
- if (x < 0 || x > width)
- {
- Chessman res;
- res.type = ChessmanType::NONE;
- return res;
- }
- if (y < 0 || y > height)
- {
- Chessman res;
- res.type = ChessmanType::NONE;
- return res;
- }
- int index = y * width + x;
- return table[index];
- }
- int CheckBoard::put(int x, int y, ChessmanType type)
- {
- Chessman chessman = Get(x, y);
- if (x < 0 || x > width)
- {
- return -1;
- }
- if (y < 0 || y > height)
- {
- return -1;
- }
- if (chessman.type != ChessmanType::NONE)
- {
- return -1;
- }
- int index = y * width + x;
- table[index].type = type;
- return 0;
- }
- int CheckBoard::print()
- {
- for (int j = 0; j < height; j++)
- {
- for (int i = 0; i < width; i++)
- {
- Chessman chessman = Get(i, j);
- if (chessman.type == ChessmanType::BLACK)
- {
- printf("B");
- }
- else if (chessman.type == ChessmanType::WHITE)
- {
- printf("W");
- }
- else
- {
- printf("-");
- }
- }
- printf("\n");
- }
- return 0;
- }
- int CheckBoard::IsWin(int x, int y, ChessmanType type)
- {
- //横向。
- int count = 1;
- for (int i = x - 1; i >= 0; i--)
- {
- Chessman chessman = Get(i, y);
- if (chessman.type != type)
- {
- break;
- }
- count++;
- }
- for (int i = x + 1; i < width; i++)
- {
- Chessman chessman = Get(i, y);
- if (chessman.type != type)
- {
- break;
- }
- count++;
- }
- if (count >= 5)
- {
- return 1;
- }
- //纵向
- count = 1;
- for (int j = y - 1; j >= 0; j--)
- {
- Chessman chessman = Get(x, j);
- if (chessman.type != type)
- {
- break;
- }
- count++;
- }
- for (int j = y + 1; j < height; j++)
- {
- Chessman chessman = Get(x, j);
- if (chessman.type != type)
- {
- break;
- }
- count++;
- }
- if (count >= 5)
- {
- return 1;
- }
- //左上-右下
- count = 1;
- for (int i = 1; i < 5; i++)
- {
- Chessman chessman = Get(x - i, y - i);
- if (chessman.type != type)
- {
- break;
- }
- count++;
- }
- for (int i = 1; i < 5; i++)
- {
- Chessman chessman = Get(x + i, y + i);
- if (chessman.type != type)
- {
- break;
- }
- count++;
- }
- if (count >= 5)
- {
- return 1;
- }
- //右上-左下
- count = 1;
- for (int i = 1; i < 5; i++)
- {
- Chessman chessman = Get(x + i, y - i);
- if (chessman.type != type)
- {
- break;
- }
- count++;
- }
- for(int i = 1;i < 5;i ++){
- Chessman chessman = Get(x - i, y + i);
- if (chessman.type != type)
- {
- break;
- }
- count++;
- }
- if (count >= 5)
- {
- return 1;
- }
- return 0;
- }

最后是main.cpp
- #include "CheckBoard.h"
- #include <stdio.h>
- #include <stdlib.h>
- int main(){
-
- CheckBoard checkboard = CheckBoard(10,10);
- checkboard.print();
-
- while(1){
- int x,y;
- while(1){
- printf("黑子:");
- scanf("%d,%d",&x,&y);
- int ret = checkboard.put(x,y,ChessmanType::BLACK);
- if(ret){
- printf("错误重新输入:");
- continue;
- }
- break;
- }
- checkboard.print();
- if(checkboard.IsWin(x,y,ChessmanType::BLACK)){
- printf("黑子win\n");
- break;
- }
-
- while(1){
- printf("白子:");
- scanf("%d,%d",&x,&y);
- int ret = checkboard.put(x,y,ChessmanType::WHITE);
- if(ret){
- printf("错误重新输入:");
- continue;
- }
- break;
- }
- checkboard.print();
- if(checkboard.IsWin(x,y,ChessmanType::WHITE)){
- printf("白子win\n");
- break;
- }
- }
- return 0;
- }

运行结果展示:
- 黑子:0,0
- B---------
- ----------
- ----------
- ----------
- ----------
- ----------
- ----------
- ----------
- ----------
- ----------
- 白子:4,5
- B---------
- ----------
- ----------
- ----------
- ----------
- ----W-----
- ----------
- ----------
- ----------
- ----------
- 黑子:1,0
- BB--------
- ----------
- ----------
- ----------
- ----------
- ----W-----
- ----------
- ----------
- ----------
- ----------
- 白子:6,9
- BB--------
- ----------
- ----------
- ----------
- ----------
- ----W-----
- ----------
- ----------
- ----------
- ------W---
- 黑子:2,0
- BBB-------
- ----------
- ----------
- ----------
- ----------
- ----W-----
- ----------
- ----------
- ----------
- ------W---
- 白子:8,9
- BBB-------
- ----------
- ----------
- ----------
- ----------
- ----W-----
- ----------
- ----------
- ----------
- ------W-W-
- 黑子:3,0
- BBBB------
- ----------
- ----------
- ----------
- ----------
- ----W-----
- ----------
- ----------
- ----------
- ------W-W-
- 白子:7,9
- BBBB------
- ----------
- ----------
- ----------
- ----------
- ----W-----
- ----------
- ----------
- ----------
- ------WWW-
- 黑子:4,0
- BBBBB-----
- ----------
- ----------
- ----------
- ----------
- ----W-----
- ----------
- ----------
- ----------
- ------WWW-
- 黑子win
- (base) PS E:\VS_Project\C++_Study\wuziqi>

不足之处希望多多指导。时隔3年的第一篇博客。QAQ.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。