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

📄 finaljudge.cpp

📁 C语言大赛的平台。分为环境模块和策略模块。
💻 CPP
字号:
#include <fstream>
#include <memory>

using namespace std;

#include "FinalJudge.h"


FinalJudge::FinalJudge(char *fileName)
{
	ifstream fin(fileName);
	int player, count;

	memset(mPlayerList, 0, sizeof(mPlayerList));
	memset(mScores, 0, sizeof(mScores));

	for(unsigned char i=0; i<12; i++)
	{
		fin >> player;
		mPlayerList[i] = (unsigned char)player;
	}

	fin >> count;
	mGroupGameCount = count;
	fin >> count;
	mGroupWinCount = count;
	fin >> count;
	mFinalGameCount = count;
	fin >> count;
	mFinalWinCount = count;

	fin.close();
}

FinalJudge::~FinalJudge(void)
{
}

void FinalJudge::loadScores(char *fileName)
{
	ifstream fin(fileName);
	int score1, score2;
	char ch;
	for(int i=0; i<19; i++)
	{
		fin >> score1 >> ch >> score2;
		mScores[i][0] = (unsigned char)score1;
		mScores[i][1] = (unsigned char)score2;
	}
	fin.close();

	update();
}

void FinalJudge::saveScores(char *fileName)
{
	ofstream fout(fileName);
	for(int i=0; i<12; i++)
	{
		fout << (int)mScores[i][0] << ":" << (int)mScores[i][1] << " ";
		if(i%3 == 2)
			fout << endl;
	}
	for(int i=12; i<16; i++)
		fout << (int)mScores[i][0] << ":" << (int)mScores[i][1] << " ";
	fout << endl;
	for(int i=16; i<18; i++)
		fout << (int)mScores[i][0] << ":" << (int)mScores[i][1] << " ";
	fout << endl;
	fout << (int)mScores[18][0] << ":" << (int)mScores[18][1] << " ";
	fout << endl;
	fout << endl;
	fout.close();
}

int FinalJudge::getPlayer(unsigned char index)
{
	if(index < 27)
		return mPlayerList[index];
	else
		return -1;
}

void FinalJudge::getPlayer(unsigned char index, unsigned char &player1, unsigned char &player2)
{
	unsigned char base, offset1, offset2;

	if(index < 12)
	{
		base = index/3*3;
		offset1 = index%3;
		offset2 = (index + 1)%3;
		player1 = mPlayerList[base + offset1];
		player2 = mPlayerList[base + offset2];
	}
	else if(index < 19)
	{
		base = 12;
		offset1 = (index - 12)*2;
		offset2 = (index - 12)*2 + 1;
		player1 = mPlayerList[base + offset1];
		player2 = mPlayerList[base + offset2];
	}
	else
	{
		player1 = 0;
		player2 = 0;
	}
}

void FinalJudge::setScore(unsigned char index, unsigned char score1, unsigned char score2)
{
	if(index < 19)
	{
		mScores[index][0] = score1;
		mScores[index][1] = score2;

		update();
	}
}

void FinalJudge::getScore(unsigned char index, unsigned char &score1, unsigned char &score2)
{
	if(index < 19)
	{
		score1 = mScores[index][0];
		score2 = mScores[index][1];
	}
}

unsigned char FinalJudge::judge(unsigned char index)
{
	if(index < 12)
	{
		if(mScores[index][0] + mScores[index][1] != mGroupGameCount)
		{
			mScores[index][0] = 0;
			mScores[index][1] = 0;
			return JUDGE_UNKNOWN;
		}
		else if(mScores[index][0] >= mGroupWinCount)
			return JUDGE_1_WIN;
		else
			return JUDGE_2_WIN;
	}
	else if(index < 19)
	{
		if(mScores[index][0] + mScores[index][1] > mFinalGameCount)
		{
			mScores[index][0] = 0;
			mScores[index][1] = 0;
			return JUDGE_UNKNOWN;
		}
		else if((mScores[index][0] < mFinalWinCount) &&
				(mScores[index][1] < mFinalWinCount))
		{
			mScores[index][0] = 0;
			mScores[index][1] = 0;
			return JUDGE_UNKNOWN;
		}
		else if(mScores[index][0] >= mFinalWinCount)
			return JUDGE_1_WIN;
		else
			return JUDGE_2_WIN;
	}
	else
		return JUDGE_UNKNOWN;
}

void FinalJudge::updateGroup(unsigned char index)
{
	if(index < 4)
	{
		unsigned short score[3] =
		{
			mScores[index*3][0] + mScores[index*3 + 2][1],
			mScores[index*3][1] + mScores[index*3 + 1][0],
			mScores[index*3 + 1][1] + mScores[index*3 + 2][0]
		};
		unsigned char result[3] =
		{
			judge(index*3),
			judge(index*3 + 1),
			judge(index*3 + 2),
		};

		if((result[0] != JUDGE_UNKNOWN) &&
				(result[1] != JUDGE_UNKNOWN) &&
				(result[2] != JUDGE_UNKNOWN))
		{
			if(result[0] == JUDGE_1_WIN)
				score[0] += 1 << 8;
			else
				score[1] += 1 << 8;
			if(result[1] == JUDGE_1_WIN)
				score[1] += 1 << 8;
			else
				score[2] += 1 << 8;
			if(result[2] == JUDGE_1_WIN)
				score[2] += 1 << 8;
			else
				score[0] += 1 << 8;

			if((score[2] < score[0]) && (score[2] < score[1]))
			{
				if(score[0] >= score[1])
				{
					mPlayerList[12 + index*2] = mPlayerList[index*3];
					mPlayerList[12 + (index*2 + 3)%8] = mPlayerList[index*3 + 1];
				}
				else
				{
					mPlayerList[12 + index*2] = mPlayerList[index*3 + 1];
					mPlayerList[12 + (index*2 + 3)%8] = mPlayerList[index*3];
				}
			}
			else if((score[0] < score[1]) && (score[0] < score[2]))
			{
				if(score[1] >= score[2])
				{
					mPlayerList[12 + index*2] = mPlayerList[index*3 + 1];
					mPlayerList[12 + (index*2 + 3)%8] = mPlayerList[index*3 + 2];
				}
				else
				{
					mPlayerList[12 + index*2] = mPlayerList[index*3 + 2];
					mPlayerList[12 + (index*2 + 3)%8] = mPlayerList[index*3 + 1];
				}
			}
			else if((score[1] < score[2]) && (score[1] < score[0]))
			{
				if(score[2] >= score[0])
				{
					mPlayerList[12 + index*2] = mPlayerList[index*3 + 2];
					mPlayerList[12 + (index*2 + 3)%8] = mPlayerList[index*3];
				}
				else
				{
					mPlayerList[12 + index*2] = mPlayerList[index*3];
					mPlayerList[12 + (index*2 + 3)%8] = mPlayerList[index*3 + 2];
				}
			}
		}
	}
}

void FinalJudge::update(void)
{
	for(unsigned char i=12; i<27; i++)
		mPlayerList[i] = 0;

	for(int i=0; i<4; i++)
		updateGroup(i);

	for(int i=20; i<27; i++)
	{
		if((mPlayerList[27 - (27 - i)*2 - 1] != 0) &&
				(mPlayerList[27 - (27 - i)*2] != 0))
		{
			unsigned char result = judge(12 + (i - 20));
			if(result == JUDGE_1_WIN)
				mPlayerList[i] = mPlayerList[27 - (27 - i)*2 - 1];
			else if(result == JUDGE_2_WIN)
				mPlayerList[i] = mPlayerList[27 - (27 - i)*2];
		}
		else
		{
			mScores[12 + (i - 20)][0] = 0;
			mScores[12 + (i - 20)][1] = 0;
		}
	}
}

⌨️ 快捷键说明

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