C++面向对象五子棋

#include
#include<Windows.h>
#include<conio.h>
using namespace std;

const int N = 15;//15*15的棋盘
int ChessBoardInformation[N][N];

class ChessBoard
{
public:
ChessBoard()
{
InitWindow();
DrawChessBoard(N/2,N/2);
}
void InitWindow();//初始化窗口
void DrawChessBoard(int x,int y);//绘制棋盘
};

class Game
{
public:
Game()
{
Init();
}
void Init();
void play();
int Put();
int Judge(int x,int y);
int g_x,g_y;
int g_currentGamer;

};

//初始化对局
void Game::Init()
{
memset(ChessBoardInformation,0,sizeof(ChessBoardInformation));//棋盘数据
g_x=N/2;//光标居中
g_y=N/2;
g_currentGamer=1;//当前玩家:黑子;
}

//落子
int Game::Put()
{
if (ChessBoardInformation[g_x][g_y]==0)//判断是否可以落子
{
ChessBoardInformation[g_x][g_y]=g_currentGamer;//记录当前玩家所下的棋子
return 1;
}
else
return 0;
}

//判断行是否胜利
int Game::Judge(int x,int y)
{
//1.判断行是否满足条件
int t=0;
for(int i=1;i<5;i++)
{
if (x-i>-1)
{
if(ChessBoardInformation[x-i][y]==ChessBoardInformation[x][y])
t++;
else
break;
}
else
break;
}
for(int i=1;i<5;i++)
{
if (x+i<N)
{
if(ChessBoardInformation[x+i][y]==ChessBoardInformation[x][y])
t++;
else
break;
}
else
break;
}
if(t>3)return ChessBoardInformation[x][y];

//2.判断列是否满足条件
t=0;
for(int i =1;i<5;i++)
{		
	if (y-i>-1)
		{
			if(ChessBoardInformation[x][y-i]==ChessBoardInformation[x][y])
				t++;
			else
				break;
		}
	else
		break;
}
for(int i =1;i<5;i++)
{
	if (y+i<N)
		{
			if(ChessBoardInformation[x][y+i]==ChessBoardInformation[x][y]) 
				t++;
			else
				break;
		}
	else
		break;
}
if(t>3)return ChessBoardInformation[x][y];

//3.判断主对角线是否满足条件
t=0;
for(int i =1;i<5;i++)
{
	if (y-i>-1 && x-i>-1)
		{
			if(ChessBoardInformation[x-i][y-i]==ChessBoardInformation[x][y])
				t++;
			else
				break;
		}
	else 
		break;
}
for(int i =1;i<5;i++)
{
	if (x+i<N && y+i<N)
		{
			if(ChessBoardInformation[x+i][y+i]==ChessBoardInformation[x][y]) 
				t++;
			else
				break;
		}
	else
		break;
}
if(t>3)return ChessBoardInformation[x][y];

//4.判断副对角线是否满足条件
t=0;
for(int i =1;i<5;i++)
{
	if (y-i>-1 && x+i<N)
		{
			if(ChessBoardInformation[x+i][y-i]==ChessBoardInformation[x][y])
				t++;
			else
				break;
		}
	else
		break;
}
for(int i =1;i<5;i++)
{
	if (x-i>-1 && y+i<N)
	{
		if(ChessBoardInformation[x-i][y+i]==ChessBoardInformation[x][y]) 
			t++;
		else
			break;
	}
else
	break;
}
if(t>3)
	return ChessBoardInformation[x][y];
else
	return 0;

}

void Game::play()
{
int p=0;
while(1)
{
ChessBoard().DrawChessBoard(g_x,g_y);
if(p>0)
{
if(p==1)
MessageBox(GetForegroundWindow(),“黑棋获胜”,“五子棋”,1);
else
MessageBox(GetForegroundWindow(),“白棋获胜”,“五子棋”,1);
break;
}

	char input=getch();
	switch(input)
	{
		case 32://空格
			if(1==Put())
			{
				if (Judge(g_x,g_y)==0)					
					g_currentGamer=3-g_currentGamer;//交换玩家 
				else		
					p=Judge(g_x,g_y);
			} 	
			break;
		case 72://上键
			g_x--;if(g_x<0) g_x=N-1;
			break;
		case 80://下键
			g_x++;if(g_x>N-1) g_x=0;
			break;
		case 75://左键 
			g_y--;if(g_y<0) g_y=N-1;
			break;
		case 77://右键
			g_y++;if(g_y>N-1) g_y=0;
			break;
	}
}

}

void ChessBoard::InitWindow()
{
SetConsoleTitleA(“五子棋”);// 设置窗口标题
system(“color E0”);//设置背景颜色
system(“mode con cols=51 lines=18”);//调整窗口大小
}

void ChessBoard::DrawChessBoard(int x,int y)
{
system(“cls”);//清屏
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(ChessBoardInformation[i][j]1)
cout<<" ●";
else if(ChessBoardInformation[i][j]2)
cout<<" ○";
else if(i
x && j
y)
cout<<" ╬ “;
else if(i== 0 && j0)
cout<<" ┏ ";
else if (i
0 && jN-1)
cout<<" ┓ ";
else if (i
N-1 && j0)
cout<<" ┗ ";
else if (i
N-1 && jN-1)
cout<<" ┛ "<<endl;
else if (i
0)
cout<<” ┳ “;
else if(iN-1)
cout<<" ┻ ";
else if(j
0)
cout<<” ┣ “;
else if(j==N-1)
cout<<” ┫ “;
else
cout<<” ┼ “;
}
cout<<”\n";
}
}

int main()
{
Game g;
g.play();
return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个简单的 C++ 面向对象五子棋代码,仅供参考: ``` #include <iostream> using namespace std; class Chessboard { public: Chessboard(); // 构造函数 void print(); // 打印棋盘 bool checkWin(int row, int col); // 检查胜利条件 bool setChess(int row, int col, char chess); // 下棋 private: char board[15][15]; // 存储棋盘 }; Chessboard::Chessboard() { for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { board[i][j] = '+'; } } } void Chessboard::print() { for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { cout << board[i][j] << " "; } cout << endl; } } bool Chessboard::checkWin(int row, int col) { char chess = board[row][col]; int count = 0; // 检查行 for (int i = 0; i < 15; i++) { if (board[row][i] == chess) { count++; } else { count = 0; } if (count == 5) { return true; } } count = 0; // 检查列 for (int i = 0; i < 15; i++) { if (board[i][col] == chess) { count++; } else { count = 0; } if (count == 5) { return true; } } count = 0; // 检查左上到右下斜线 for (int i = row, j = col; i < 15 && j < 15; i++, j++) { if (board[i][j] == chess) { count++; } else { count = 0; } if (count == 5) { return true; } } count = 0; // 检查右上到左下斜线 for (int i = row, j = col; i >= 0 && j < 15; i--, j++) { if (board[i][j] == chess) { count++; } else { count = 0; } if (count == 5) { return true; } } return false; } bool Chessboard::setChess(int row, int col, char chess) { if (board[row][col] == '+') { board[row][col] = chess; return true; } return false; } int main() { Chessboard board; char player1 = 'O'; char player2 = 'X'; bool isPlayer1 = true; int row, col; while (true) { board.print(); char chess = isPlayer1 ? player1 : player2; cout << "Player " << (isPlayer1 ? "1" : "2") << " (" << chess << "), please enter the row and column (e.g. 3 4) to set your chess: "; cin >> row >> col; if (board.setChess(row - 1, col - 1, chess)) { if (board.checkWin(row - 1, col - 1)) { board.print(); cout << "Player " << (isPlayer1 ? "1" : "2") << " (" << chess << ") wins!" << endl; break; } isPlayer1 = !isPlayer1; } else { cout << "Invalid input or the position is already occupied. Please try again." << endl; } } return 0; } ``` 这个程序使用了一个 `Chessboard` 类来表示棋盘,包含了打印棋盘、检查胜利条件以及下棋的方法。在 `main` 函数中,通过交替使用两个玩家的棋子来进行游戏,并检查是否有玩家胜利。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值