VSCode +CMake +Gtest实现象棋功能

一、工程目录

.
├── build
├── CMakeLists.txt
├── code
│   ├── inc
│   └── src
├── googletest-release-1.8.1
│   ├── appveyor.yml
│   ├── build
│   ├── BUILD.bazel
│   ├── ci
│   ├── CMakeLists.txt
│   ├── configure.ac
│   ├── CONTRIBUTING.md
│   ├── .gitignore
│   ├── googlemock
│   ├── googletest
│   ├── LICENSE
│   ├── Makefile.am
│   ├── README.md
│   ├── .travis.yml
│   └── WORKSPACE
├── googletest-release-1.8.1.zip
└── test_case
    └── test.cpp

Code目录如下:

.
├── inc
│   ├── base_types.h
│   ├── chess_board.h
│   ├── chess.h
│   ├── swap.h
│   └── test.h
└── src
    ├── chess_borad.cpp
    ├── chess.cpp
    └── main.cpp

VSCode配置参考我的另一篇文章:vscode工程调试

头文件相关文件:

base_types.h

#ifndef HEFCEBEDE_3450_4FF2_977F_E04D0CA5C7CD
#define HEFCEBEDE_3450_4FF2_977F_E04D0CA5C7CD
#include <iostream>

typedef unsigned char BYTE;

typedef unsigned short WORD16;
typedef short SWORD16;

typedef WORD16 WORD;
typedef SWORD16 SWORD;

typedef unsigned int WORD32;
typedef int SWORD32;

typedef WORD32 DWORD;
typedef SWORD32 SDWORD;

typedef void VOID;
typedef bool BOOL;



#endif

chess_board.h

#ifndef HC59A80A3_5653_4088_BE0C_9B9C896FFE25
#define HC59A80A3_5653_4088_BE0C_9B9C896FFE25
#include "base_types.h"
#include "chess.h"

const SWORD32 START_AXIS_SIZE = 0;
const SWORD32 MAX_X_AXIS_SIZE = 9;
const SWORD32 MAX_Y_AXIS_SIZE = 10;

class ChessBoard
{
public:
    ChessBoard();
    ~ChessBoard();
    VOID Init();
    Chess* GetChessByPostion(SWORD32 x, SWORD32 y);
    SWORD32 GetChessId(SWORD32 x, SWORD32 y);
    BOOL Move(SWORD32 startx,SWORD32 starty,SWORD32 aimx,SWORD32 aimy);

    static BOOL end;
    static SWORD32 player;
private:
	BOOL IsChessInBoard(SWORD32 startx, SWORD32 starty, SWORD32 aimx, SWORD32 aimy);
private:
    Chess *chess[MAX_Y_AXIS_SIZE][MAX_X_AXIS_SIZE];
};



#endif

chess.h

#ifndef HD43E1ECD_C5C6_4BAB_805A_3C9B010B79D8
#define HD43E1ECD_C5C6_4BAB_805A_3C9B010B79D8

#include "base_types.h"

const SWORD32 SOLDIER_ID = 1;
const SWORD32 HORSE_ID = 2;
const SWORD32 ROOK_ID = 3;
const SWORD32 GUARD_ID = 4;
const SWORD32 CANNON_ID = 5;
const SWORD32 ELEPHANT_ID = 6;
const SWORD32 GENERAL_ID = 7;

class ChessBoard;
class Chess
{
public:
    Chess(SWORD32 i);
    virtual ~Chess(){};
    SWORD32 GetChessId();
    virtual BOOL JudgeMove(ChessBoard &board, SWORD32 startx, SWORD32 starty, SWORD32 aimx, SWORD32 aimy) = 0;

private:
	SWORD32 chessId;
};

class Horse : public Chess
{
public:
    Horse(SWORD32 i);
    BOOL JudgeMove(ChessBoard &board, SWORD32 startx, SWORD32 starty, SWORD32 aimx, SWORD32 aimy);
};

class Soldier : public Chess
{
public:
    Soldier(SWORD32 i);
    bool JudgeMove(ChessBoard &board,SWORD32 startx,SWORD32 starty, SWORD32 aimx,SWORD32 aimy);
};

class General : public Chess
{
public:
    General(SWORD32 i);
    ~General();
    BOOL JudgeMove(ChessBoard &board,SWORD32 startx,SWORD32 starty,SWORD32 aimx,SWORD32 aimy);
};

class Elephant : public Chess
{
public:
    Elephant(SWORD32 i);
    BOOL JudgeMove(ChessBoard &board,SWORD32 startx,SWORD32 starty,SWORD32 aimx,SWORD32 aimy);
};

class Cannon : public Chess
{
public:
    Cannon(SWORD32 i);
    BOOL JudgeMove(ChessBoard &board,SWORD32 startx,SWORD32 starty,SWORD32 aimx,SWORD32 aimy);
};

class Guard : public Chess
{
public:
    Guard(SWORD32 i);
    BOOL JudgeMove(ChessBoard &board,SWORD32 startx,SWORD32 starty,SWORD32 aimx,SWORD32 aimy);
};

class Rook : public Chess
{
public:
    Rook(SWORD32 i);
    BOOL JudgeMove(ChessBoard &board,SWORD32 startx,SWORD32 starty,SWORD32 aimx,SWORD32 aimy);
};


#endif

swap.h

#ifndef _SWAP_H_
#define _SWAP_H_

void swap(int &a, int &b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
}

#endif

test.h

#ifndef H091464F0_FC53_4F7E_A9DE_C372F5BABF75
#define H091464F0_FC53_4F7E_A9DE_C372F5BABF75

#include "gtest/gtest.h"
#include "chess_board.h"

class ChessTest:public testing::Test
{
public:
	ChessBoard chessBoard;
protected:
	virtual VOID SetUp();
	virtual VOID TearDown();

};

#endif

chess_borad.cpp

#include "chess_board.h"
#include <memory.h>
#include <cmath>
#include <cstdio>
#include "chess.h"

const WORD32 RED_PLAYER = 0;
const WORD32 BLACK_PLAYER = 1;

BOOL ChessBoard::end = true;
SWORD32 ChessBoard::player = -1;

ChessBoard::ChessBoard()
{
	memset(chess, 0, sizeof(chess));
};

BOOL ChessBoard::Move(SWORD32 startx, SWORD32 starty, SWORD32 aimx, SWORD32 aimy)
{
	if(IsChessInBoard(startx, starty, aimx, aimy) &&
			GetChessId(startx, starty) && GetChessId(startx, starty) * player > 0 &&
			chess[startx][starty]->JudgeMove(*this, startx, starty, aimx, aimy))
    {
		printf("Player is %d\n", player);
        if(chess[aimx][aimy] != NULL)
		{
			delete chess[aimx][aimy];
		}
        chess[aimx][aimy] = chess[startx][starty];
        chess[startx][starty] = NULL;
        printf("Player is %d\n", player);
        player *= -1;
    	return true;
    }
    printf("走法错误,不符合规则\n\n");
    return false;
}

BOOL ChessBoard::IsChessInBoard(SWORD32 startx, SWORD32 starty, SWORD32 aimx, SWORD32 aimy)
{
    if(startx >= START_AXIS_SIZE && startx < MAX_X_AXIS_SIZE &&
    		starty >= START_AXIS_SIZE && starty < MAX_Y_AXIS_SIZE &&
    		aimx >= START_AXIS_SIZE && aimx < MAX_X_AXIS_SIZE &&
			aimy >= START_AXIS_SIZE && aimy < MAX_Y_AXIS_SIZE)
    {
    	return true;
    }
    return false;
}

Chess* ChessBoard::GetChessByPostion(SWORD32 x, SWORD32 y)
{
	return chess[x][y];
}

SWORD32 ChessBoard::GetChessId(SWORD32 x, SWORD32 y)
{
	if(chess[x][y]!=NULL)
	{
		return chess[x][y]->GetChessId();
	}
	return 0;
}

ChessBoard::~ChessBoard()
{
    for(SWORD32 i = START_AXIS_SIZE; i < MAX_X_AXIS_SIZE; i++)
    {
        for(SWORD32 j = START_AXIS_SIZE; j < MAX_Y_AXIS_SIZE; j++)
        {
            if(chess[i][j]!=NULL)
            {
                delete chess[i][j];
                chess[i][j]=NULL;
            }
        }
    }
}

VOID ChessBoard::Init()
{
    chess[0][0]=new Rook(RED_PLAYER);        	chess[0][8]=new Rook(RED_PLAYER);
    chess[0][1]=new Horse(RED_PLAYER);       	chess[0][7]=new Horse(RED_PLAYER);
    chess[0][2]=new Elephant(RED_PLAYER);    	chess[0][6]=new Elephant(RED_PLAYER);
    chess[0][3]=new Guard(RED_PLAYER);       	chess[0][5]=new Guard(RED_PLAYER);
    chess[0][4]=new General(RED_PLAYER);     	chess[9][4]=new General(BLACK_PLAYER);
    chess[2][1]=new Cannon(RED_PLAYER);      	chess[2][7]=new Cannon(RED_PLAYER);
    chess[3][0]=new Soldier(RED_PLAYER);     	chess[3][2]=new Soldier(RED_PLAYER);
    chess[3][4]=new Soldier(RED_PLAYER);     	chess[3][6]=new Soldier(RED_PLAYER);
    chess[3][8]=new Soldier(RED_PLAYER);     	chess[6][8]=new Soldier(BLACK_PLAYER);
    chess[6][0]=new Soldier(BLACK_PLAYER);     	chess[6][2]=new Soldier(BLACK_PLAYER);
    chess[6][4]=new Soldier(BLACK_PLAYER);     	chess[6][6]=new Soldier(BLACK_PLAYER);
    chess[7][1]=new Cannon(BLACK_PLAYER);      	chess[7][7]=new Cannon(BLACK_PLAYER);
    chess[9][0]=new Rook(BLACK_PLAYER);        	chess[9][8]=new Rook(BLACK_PLAYER);
    chess[9][1]=new Horse(BLACK_PLAYER);       	chess[9][7]=new Horse(BLACK_PLAYER);
    chess[9][2]=new Elephant(BLACK_PLAYER);    	chess[9][6]=new Elephant(BLACK_PLAYER);
    chess[9][3]=new Guard(BLACK_PLAYER);       	chess[9][5]=new Guard(BLACK_PLAYER);
}

chess.cpp

#include "chess.h"
#include "chess_board.h"
#include <iostream>
#include <memory.h>
#include <cmath>

Chess::Chess(SWORD32 i) : chessId(i)
{

}

SWORD32 Chess::GetChessId()
{
	return chessId;
}

Horse::Horse(SWORD32 i) : Chess(((i == 0) ? -HORSE_ID : HORSE_ID))
{
}

BOOL Horse::JudgeMove(ChessBoard &board, SWORD32 startx, SWORD32 starty, SWORD32 aimx, SWORD32 aimy)
{
	SWORD32 tempx = aimx - startx;
	SWORD32	tempy = aimy - starty;
	SWORD32 startChessId = board.GetChessId(startx, starty);
	SWORD32 aimChessId = board.GetChessId(aimx, aimy);
	if (startChessId * aimChessId <= 0 && (tempx * tempx + tempy * tempy == 5) &&
			!board.GetChessByPostion(startx + tempx / 2, starty + tempy / 2))
	{
		return true;
	}
	return false;
}

Soldier::Soldier(SWORD32 i) : Chess(( i == 0 ? -SOLDIER_ID : SOLDIER_ID))
{

}

BOOL Soldier::JudgeMove(ChessBoard &board, SWORD32 startx, SWORD32 starty, SWORD32 aimx, SWORD32 aimy)
{
	SWORD32 tempx = aimx - startx;
	SWORD32 tempy = aimy - starty;
	SWORD32 startChessId = board.GetChessId(startx, starty);
	SWORD32 aimChessId = board.GetChessId(aimx, aimy);
	if(startChessId * aimChessId <= 0 && startChessId * tempx <= 0)
	{
		if(std::abs(tempx) == 1 && tempy == 0)
		{
			return true;
		}
		if(std::abs(tempy) == 1 && tempx == 0)
		{
			if((startx / 5 == 0 && startChessId > 0) || (startx / 5 == 1 && startChessId < 0))
			{
				return true;
			}
		}
		return false;
	}
	return false;
}


General::General(SWORD32 i) : Chess(( i == 0 ? -GENERAL_ID : GENERAL_ID))
{

}
General::~General()
{
	ChessBoard::end = false;
}

BOOL General::JudgeMove(ChessBoard &board, SWORD32 startx, SWORD32 starty, SWORD32 aimx, SWORD32 aimy)
{
	SWORD32 tempx = aimx - startx;
	SWORD32	tempy = aimy - starty;
	SWORD32 startChessId = board.GetChessId(startx, starty);
	SWORD32 aimChessId = board.GetChessId(aimx, aimy);
	if(startChessId * aimChessId <= 0 && tempy * tempy + tempx * tempx == 1 &&
			aimx % 7 >= 0 && aimx % 7 <= 2 && aimy >= 3 && aimy <= 5)
	{
		return true;
	}
	return false;
}


Elephant::Elephant(SWORD32 i) : Chess((i == 0 ? -ELEPHANT_ID : ELEPHANT_ID))
{

}

BOOL Elephant::JudgeMove(ChessBoard &board,SWORD32 startx,SWORD32 starty,SWORD32 aimx,SWORD32 aimy)
{
	SWORD32 tempx = aimx - startx;
	SWORD32 tempy = aimy - starty;
	SWORD32 startChessId = board.GetChessId(startx, starty);
	SWORD32 aimChessId = board.GetChessId(aimx, aimy);
	if(startChessId * aimChessId <= 0 && tempy * tempy + tempx * tempx == 8 && startx / 5 == aimx / 5 &&
			!board.GetChessByPostion(startx + tempx / 2,starty + tempy / 2))
	{
		return true;
	}
	return false;
}


Cannon::Cannon(SWORD32 i) : Chess((i == 0 ? -CANNON_ID : CANNON_ID))
{

}

BOOL Cannon::JudgeMove(ChessBoard &board, SWORD32 startx, SWORD32 starty, SWORD32 aimx, SWORD32 aimy)
{
	SWORD32 tempx = aimx - startx;
	SWORD32 tempy = aimy - starty;
	SWORD32 startChessId = board.GetChessId(startx, starty);
	SWORD32 aimChessId = board.GetChessId(aimx, aimy);
	if(startChessId * aimChessId <= 0 && !(tempx && tempy) && (tempx + tempy))
	{
		SWORD32 conut = 0;
		if(tempx != 0)
		{
			SWORD32 sign = tempx > 0 ? 1 : -1;
			for(SWORD32 i = 1; i < std::abs(tempx); i++)
			{
				if(board.GetChessByPostion(startx + sign * i, starty))
				{
					conut++;
				}
			}

		}
		else
		{
			SWORD32 sign = tempy > 0 ? 1 : -1;
			for(SWORD32 i = 1; i < std::abs(tempy); i++)
			{
				if(board.GetChessByPostion(startx, starty + sign * i))
				{
					conut++;
				}
			}

		}
		if(!aimChessId)
		{
			if(!conut)
			{
				return true;
			}
		}
		else
		{
			if(conut == 1)
			{
				return true;
			}
		}
	}
	return false;
}


Guard::Guard(SWORD32 c) : Chess(((c == 0) ? -GUARD_ID : GUARD_ID))
{

}
BOOL Guard::JudgeMove(ChessBoard &board,SWORD32 startx,SWORD32 starty,SWORD32 aimx,SWORD32 aimy)
{
	SWORD32 tempx = aimx - startx;
	SWORD32 tempy =aimy - starty;
	SWORD32 sid = board.GetChessId(startx, starty);
	SWORD32 aid = board.GetChessId(aimx, aimy);
	if(sid * aid <= 0 && tempy * tempy + tempx * tempx == 2 && aimx % 7 >= 0 && aimx %7 <= 2 &&
			aimy >= 3 && aimy<= 5)
	{
		return true;
	}
	return false;
}


Rook::Rook(SWORD32 i) : Chess(((i == 0) ? -ROOK_ID : ROOK_ID))
{

}

BOOL Rook::JudgeMove(ChessBoard &board,SWORD32 startx,SWORD32 starty,SWORD32 aimx,SWORD32 aimy)
{
	SWORD32 tempx = aimx - startx;
	SWORD32 tempy = aimy - starty;
	SWORD32 sid = board.GetChessId(startx, starty);
	SWORD32 aid = board.GetChessId(aimx, aimy);
	if( sid * aid <= 0 && !(tempx && tempy) && (tempx + tempy))
	{
		if(tempx != 0)
		{
			SWORD32 sign = tempx > 0 ? 1 : -1;
			for(SWORD32 i = 1; i < std::abs(tempx); i++)
			{
				if(board.GetChessByPostion(startx + sign * i, starty))
				{
					return false;
				}
			}
		}
		else
		{
			SWORD32 sign = tempy > 0 ? 1 : -1;
			for(SWORD32 i = 1; i < std::abs(tempy); i++)
			{
				if(board.GetChessByPostion(startx,starty + sign * i))
				{
					return false;
				}
			}
		}
			return true;
	}
	return false;
}


main.cpp

#include <iostream>
#include "gtest/gtest.h"

using namespace std;
 
int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

test.cpp

#include "base_types.h"
#include "chess_board.h"
#include "test.h"
#include <cstdio>
#include "swap.h"

VOID ChessTest::SetUp()
{
	printf("ChessTest::SetUp()\n");
	chessBoard.Init();
}

VOID ChessTest::TearDown()
{
	ChessBoard::end = true;
	ChessBoard::player = -1;
	printf("ChessTest::TearDown()\n");
}

TEST_F(ChessTest, test_chess_move_out_board)
{
	SWORD32 startx = 3, starty = 1;
	SWORD32 aimx = 3, aimy = -1;
	BOOL ret = chessBoard.Move(startx, starty, aimx, aimy);

	ASSERT_EQ(ret, false);
}

TEST_F(ChessTest, test_horse_move_in_board_right)
{
	SWORD32 startx = 0, starty = 1;
	SWORD32 aimx = 2, aimy = 0;
	BOOL ret = chessBoard.Move(startx, starty, aimx, aimy);

	ASSERT_EQ(ret, true);
}

TEST_F(ChessTest, test_horse_move_in_board_error)
{
	SWORD32 startx = 0, starty = 1;
	SWORD32 aimx = 1, aimy = 1;
	BOOL ret = chessBoard.Move(startx, starty, aimx, aimy);

	ASSERT_EQ(ret, false);
}

TEST_F(ChessTest, test_soldier_move_in_board_right)
{
	SWORD32 startx = 3, starty = 2;
	SWORD32 aimx = 4, aimy = 2;
	BOOL ret = chessBoard.Move(startx, starty, aimx, aimy);

	ASSERT_EQ(ret, true);
}

TEST_F(ChessTest, test_soldier_move_in_board_error)
{
	SWORD32 startx = 3, starty = 2;
	SWORD32 aimx = 4, aimy = 1;
	BOOL ret = chessBoard.Move(startx, starty, aimx, aimy);

	ASSERT_EQ(ret, false);
}

TEST_F(ChessTest, test_general_move_in_board_right)
{
	SWORD32 startx = 0, starty = 4;
	SWORD32 aimx = 1, aimy = 4;
	BOOL ret = chessBoard.Move(startx, starty, aimx, aimy);

	ASSERT_EQ(ret, true);
}

TEST_F(ChessTest, test_general_move_in_board_error)
{
	SWORD32 startx = 0, starty = 4;
	SWORD32 aimx = 1, aimy = 3;
	BOOL ret = chessBoard.Move(startx, starty, aimx, aimy);

	ASSERT_EQ(ret, false);
}

TEST_F(ChessTest, test_elephant_move_in_board_right)
{
	SWORD32 startx = 0, starty = 2;
	SWORD32 aimx = 2, aimy = 0;
	BOOL ret = chessBoard.Move(startx, starty, aimx, aimy);

	ASSERT_EQ(ret, true);
}

TEST_F(ChessTest, test_elephant_move_in_board_error)
{
	SWORD32 startx = 0, starty = 2;
	SWORD32 aimx = 1, aimy = 1;
	BOOL ret = chessBoard.Move(startx, starty, aimx, aimy);

	ASSERT_EQ(ret, false);
}

TEST_F(ChessTest, test_cannon_move_in_board_right)
{
	SWORD32 startx = 2, starty = 1;
	SWORD32 aimx = 4, aimy = 1;
	BOOL ret = chessBoard.Move(startx, starty, aimx, aimy);

	ASSERT_EQ(ret, true);
}

TEST_F(ChessTest, test_cannon_move_in_board_error)
{
	SWORD32 startx = 2, starty = 1;
	SWORD32 aimx = 1, aimy = 0;
	BOOL ret = chessBoard.Move(startx, starty, aimx, aimy);

	ASSERT_EQ(ret, false);
}

TEST_F(ChessTest, test_guard_move_in_board_right)
{
	SWORD32 startx = 0, starty = 3;
	SWORD32 aimx = 1, aimy = 4;
	BOOL ret = chessBoard.Move(startx, starty, aimx, aimy);

	ASSERT_EQ(ret, true);
}

TEST_F(ChessTest, test_guard_move_in_board_error)
{
	SWORD32 startx = 0, starty = 3;
	SWORD32 aimx = 1, aimy = 3;
	BOOL ret = chessBoard.Move(startx, starty, aimx, aimy);

	ASSERT_EQ(ret, false);
}

TEST_F(ChessTest, test_rook_move_in_board_right)
{
	SWORD32 startx = 0, starty = 0;
	SWORD32 aimx = 2, aimy = 0;
	BOOL ret = chessBoard.Move(startx, starty, aimx, aimy);

	ASSERT_EQ(ret, true);
}

TEST_F(ChessTest, test_rook_move_in_board_error)
{
	SWORD32 startx = 0, starty = 0;
	SWORD32 aimx = 1, aimy = 1;
	BOOL ret = chessBoard.Move(startx, starty, aimx, aimy);

	ASSERT_EQ(ret, false);
}

TEST(sample_test_case, sample_test)
{
    EXPECT_EQ(1, 1);
}

TEST(sample_test_case, swap_test)
{
    int a = 10, b = 20;
    swap(a, b);
    EXPECT_EQ(a, 20);
    ASSERT_EQ(b, 10);
}


CMakeLists.txt稍微改一下,加上新增的几个源文件

cmake_minimum_required(VERSION 2.6)
project(basic_test)

################################
# GTest
################################
ADD_SUBDIRECTORY (googletest-release-1.8.1)
enable_testing()
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR} code/inc)

################################
# Unit Tests
################################
# Add test cpp file
add_executable( 
    runUnitTests 
    test_case/test.cpp 
    code/src/main.cpp 
    code/src/chess.cpp 
    code/src/chess_borad.cpp
)
# Link test executable against gtest & gtest_main
target_link_libraries(runUnitTests gtest gtest_main)
add_test( runUnitTests runUnitTests )

新建一个build目录,进入build目录,执行

cmake ..
make

运行 ./runUnitTests,即可输出用例运行结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值