C++面向对象五子棋_c++initwindow

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 {
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+i3)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+i3)return ChessBoardInformation[x][y];

//4.判断副对角线是否满足条件
t=0;
for(int i =1;i<5;i++)
{
	if (y-i>-1 && x+i-1 && y+i3)
	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 {
for(int j=0;j {
if(ChessBoardInformation[i][j]==1)
cout<<" ●";

你可能感兴趣的:(c++,开发语言)