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

📄 wzq.cpp

📁 五子棋核心算法,一级完整的应用程序演示
💻 CPP
字号:
// Wzq.cpp: implementation of the CWzq class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LinkF.h"
#include "Wzq.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

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

CWzq::CWzq()
{
	m_nOver = 0;
}

CWzq::~CWzq()
{
	delete [] PlayerScore;
	delete [] ComputerScore;

	for (int i = 0;i < m_nWidth;i++)
		delete [] Board[i];
	delete [] Board;

	for (i = 0;i < m_nWidth;i++)
	{
		
		for (int j = 0;j < m_nHeight;j++)
		{
			delete [] PlayerTable[i][j];
			delete [] ComputerTable[i][j];
		}

		delete [] PlayerTable[i];
		delete [] ComputerTable[i];
	}
	delete [] PlayerTable;
	delete [] ComputerTable;
}
void CWzq::ComputerThinking(int &o_nX, int &o_nY)
{
	int PlayerGrade=0;
	int ComputerGrade=0;
	int ComputerX;
	int ComputerY;
	int i,j,k,mat,nat,mde,nde;

	int** PlayerGrades;//[NUM][NUM],[NUM][NUM];
	int** ComputerGrades;
	PlayerGrades = (int**) new int*[m_nWidth];
	for ( i = 0; i < m_nWidth;i++)
		PlayerGrades[i] = new int[m_nHeight];

	ComputerGrades = (int**) new int*[m_nWidth];
	for (i = 0; i < m_nWidth;i++)
		ComputerGrades[i] = new int[m_nHeight];

	for (i = 0;i < m_nWidth;i++)
		for ( j = 0;j < m_nHeight;j++)
			ComputerGrades[i][j] = PlayerGrades[i][j] = 0;

	for(i=0;i<19;i++)
		for(j=0;j<19;j++)
		{
			PlayerGrades[i][j]=0;
			if(Board[i][j]==0)
				for(k=0;k<1020;k++)
					if(PlayerTable[i][j][k])
					{
						switch(PlayerScore[k])
						{
						case 1:
							PlayerGrades[i][j]+=5;
							break;
						case 2:
							PlayerGrades[i][j]+=50;
							break;
						case 3:
							PlayerGrades[i][j]+=500;
							break;
						case 4:
							PlayerGrades[i][j]+=5000;
							break;
						}
					}
		}
	for(i=0;i<19;i++)
		for(j=0;j<19;j++)
		{
			ComputerGrades[i][j]=0;
			if(Board[i][j]==0)
				for(k=0;k<1020;k++)
					if(ComputerTable[i][j][k])
					{
						switch(ComputerScore[k])
						{
						case 1:
							ComputerGrades[i][j]+=5;
							break;
						case 2:
							ComputerGrades[i][j]+=50;
							break;
						case 3:
							ComputerGrades[i][j]+=500;
							break;
						case 4:
							ComputerGrades[i][j]+=500000;
							break;
						}
					}
		}
	for(i=0;i<19;i++)
			for(j=0;j<19;j++)
				if(Board[i][j]==0)
				{
					if(ComputerGrades[i][j]>=ComputerGrade)
					{
						ComputerGrade=ComputerGrades[i][j];
						mat=i;
						nat=j;
					}
					if(PlayerGrades[i][j]>=PlayerGrade)
					{
						PlayerGrade=PlayerGrades[i][j];
						mde=i;
						nde=j;
					}
				}
	if(ComputerGrade>=PlayerGrade)
		{
			ComputerX=mat;
			ComputerY=nat;
		}
		else
		{
			ComputerX=mde;
			ComputerY=nde;
		}

	o_nX = ComputerX;
	o_nY = ComputerY;
	for (i = 0;i < m_nWidth;i++)
	{
		delete [] PlayerGrades[i];
		delete [] ComputerGrades[i];
	}
	delete [] PlayerGrades;
	delete [] ComputerGrades;

}

void CWzq::PlayerDo(const int &i_nX, const int i_nY)
{
	int PlayerX = i_nX;
	int PlayerY = i_nY;
	Board[PlayerX][PlayerY]=1;

	int i;
	for(i=0;i<m_nGood;i++)
	{
		if(PlayerTable[PlayerX][PlayerY][i]&&PlayerScore[i]!=7)
			PlayerScore[i]++;
		if(ComputerTable[PlayerX][PlayerY][i])
		{
			ComputerTable[PlayerX][PlayerY][i]=false;
			ComputerScore[i]=7;
		}
	}

	for(i=0;i<m_nGood;i++)
		if(PlayerScore[i]==5)
		{
			m_nOver=1;
		}
}

void CWzq::ComputerDo(int &o_nX, int &o_nY)
{
	int i;

	int ComputerX, ComputerY;
	ComputerX = o_nX;
	ComputerY = o_nY;
	ComputerThinking(ComputerX, ComputerY);
	Board[ComputerX][ComputerY]=2;

	for(i=0;i<m_nGood;i++)
	{
		if(ComputerTable[ComputerX][ComputerY][i]&&ComputerScore[i]!=7)
			ComputerScore[i]++;
		if(PlayerTable[ComputerX][ComputerY][i])
		{
			PlayerTable[ComputerX][ComputerY][i]=false;
			PlayerScore[i]=7;
		}
	}

	for(i=0;i<m_nGood;i++)
		if(ComputerScore[i]==5)
		{
			m_nOver=2;
		}
}

int CWzq::IsGameWin()
{
	return m_nOver;
}

void CWzq::New(const int &i_nWidth, const int &i_nHeight)
{
	m_nWidth = i_nWidth;
	m_nHeight = i_nHeight;
	int nWidhtCount = (m_nWidth - 4) * m_nHeight;
	int nHeightCount = (m_nHeight - 4) * m_nWidth;
	int nSide = (m_nWidth - 4) * (m_nHeight - 4) + (-1) * (m_nWidth - 4) * (m_nWidth - 5) / 2;
	nSide *= 4;
	nSide -= 2 * (m_nHeight - 4);
	m_nGood = nSide + nWidhtCount + nHeightCount;

	PlayerScore = new int[m_nGood];
	ComputerScore = new int[m_nGood];
	for (int i = 0;i < m_nGood;i++)
	{
		PlayerScore[i] = ComputerScore[i] = 0;
	}


	Board = (int**) new int*[m_nWidth];
	for (i = 0;i < m_nWidth;i++)
		Board[i] = new int[m_nHeight];
	for (i = 0;i < m_nWidth;i++)
		for (int j = 0;j < m_nHeight;j++)
			Board[i][j] = 0;
	PlayerTable = (int***) new int**[m_nWidth];
	ComputerTable = (int***) new int**[m_nWidth];
	for (i = 0;i < m_nWidth;i++)
	{
		PlayerTable[i] = (int**) new int*[m_nHeight];
		ComputerTable[i] = (int**) new int*[m_nHeight];
		for (int j = 0;j < m_nHeight;j++)
		{
			PlayerTable[i][j] = new int[m_nGood];
			ComputerTable[i][j] = new int[m_nGood];
		}
	}
	for (i = 0;i < m_nWidth;i++)
		for (int j = 0;j < m_nHeight;j++)
			for (int k = 0;k < m_nGood;k++)
				ComputerTable[i][j][k] = PlayerTable[i][j][k] = 0;


}

⌨️ 快捷键说明

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