⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wuziqipan.cpp

📁 用vc++编写的小游戏
💻 CPP
字号:
// WuZiQiPan.cpp: implementation of the CWuZiQiPan class.
//
//////////////////////////////////////////////////////////////////////

#include "WuZiQiPan.h"
#include <iostream.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CWuZiQiPan::CWuZiQiPan() //初始化棋盘、下子次序
{
	int x,y;
	for(x=1; x<=WuZiQiPanMaxSize; x++)
	{
		for(y=1; y<=WuZiQiPanMaxSize; y++)
			QiPan[x][y] = 0;
	}
	m_isBlack = true;
}

CWuZiQiPan::~CWuZiQiPan()
{

}

void CWuZiQiPan::ShowQP() //显示棋盘和棋盘上的棋子
{

	int y;
	cout << "     ";
	for(int i=1;i<=WuZiQiPanMaxSize; i++)
			cout << " " << i%10;
	cout << endl;

	for(y=1; y<=WuZiQiPanMaxSize; y++)
		ShowQP_Line(y);
}

void CWuZiQiPan::ShowQP_Top() //显示棋盘的第一行
{
	int x;
	if(! ShowQP_QiZi(1,1)) cout << "┏";
	for(x=2; x<WuZiQiPanMaxSize ; x++)
		if(! ShowQP_QiZi(1,x)) cout << "┯";
	if(! ShowQP_QiZi(1,WuZiQiPanMaxSize)) cout << "┓" << endl;
}

void CWuZiQiPan::ShowQP_Bottom() //显示棋盘的最后一行
{
	int x;
	if(! ShowQP_QiZi(WuZiQiPanMaxSize,1)) cout << "┗";
	for(x=2; x<WuZiQiPanMaxSize ; x++)
		if(! ShowQP_QiZi(WuZiQiPanMaxSize,x)) cout << "┷";
	if(! ShowQP_QiZi(WuZiQiPanMaxSize,WuZiQiPanMaxSize)) cout << "┛" << endl;
}

void CWuZiQiPan::ShowQP_Line(int y) //显示棋盘的第y行
{
	if(y<10)
		cout << "    " << y << "";
	else
		cout << "   " << y << "";

	if(y == 1)
	{
		ShowQP_Top();
		return;
	}
	else if(y == WuZiQiPanMaxSize)
	{
		ShowQP_Bottom();
		return;
	}
	
	int x;
	if(! ShowQP_QiZi(y,1)) cout << "┠";
	for(x=2; x<WuZiQiPanMaxSize ; x++)
		if(! ShowQP_QiZi(y,x)) cout << "┼";
	if(! ShowQP_QiZi(y,WuZiQiPanMaxSize)) cout << "┨" << endl;
}

bool CWuZiQiPan::ShowQP_QiZi(int y,int x) //显示(y,x)处的棋子,返回false表示该处没有棋子
{
	switch (GetQiZi(y,x))
	{
	case 1:
		cout << "●";
		return true;
	case 2:
		cout << "○";
		return true;
	default:
		return false;
	}
}

int CWuZiQiPan::GetQiZi(int y,int x) //获得(y,x)处的棋子(0空白,1黑,2白)
{
	if(y < 1 || y > WuZiQiPanMaxSize || x < 1 || x > WuZiQiPanMaxSize)
		return 0;
	else
		return QiPan[y][x];
}

bool CWuZiQiPan::Put(int y,int x,bool isBlack) //在(y,x)下一子
{
	if(y < 1 || y > WuZiQiPanMaxSize || x < 1 || x > WuZiQiPanMaxSize)
		return false;
	if(QiPan[y][x])
		return false;
	if(isBlack)
		QiPan[y][x] = 1;
	else
		QiPan[y][x] = 2;
	return true;
}

bool CWuZiQiPan::Play(int y, int x) //在(y,x)下一子,黑白自动控制
{
	if(Put(y,x,m_isBlack))
	{
		m_isBlack = ! m_isBlack;
		return true;
	}
	else
		return false;
}

bool CWuZiQiPan::IsWin(int y0, int x0) //判断(y0,x0)处是否获胜
{
	int c;
	if((c = GetQiZi(y0, x0)) == 0) return false;
	
	int x,y,n;
	//向左右搜索
	for(n = 0, x = x0, y = y0; GetQiZi(y, x) == c; x--)
		n++;
	for(x = x0 + 1; GetQiZi(y, x) == c; x++)
		n++;
	if(n >= 5) return true;

	//向上下搜索
	for(n = 0, x = x0, y = y0; GetQiZi(y, x) == c; y--)
		n++;
	for(y = y0 + 1; GetQiZi(y, x) == c; y++)
		n++;
	if(n >= 5) return true;
	
	//向左上-右下搜索
	for(n = 0, x = x0, y = y0; GetQiZi(y, x) == c; y--, x--)
		n++;
	for(x = x0 + 1,y = y0 + 1; GetQiZi(y, x) == c; y++, x++)
		n++;
	if(n >= 5) return true;

	//向右上-左下搜索
	for(n = 0, x = x0, y = y0; GetQiZi(y, x) == c; y--, x++)
		n++;
	for(x = x0 + 1,y = y0 + 1; GetQiZi(y, x) == c; y++, x--)
		n++;
	if(n >= 5) return true;

	return false;
}

bool CWuZiQiPan::IsBlack() //是否黑子下
{
	return m_isBlack;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -