[ create a new paste ] login | about

Link: http://codepad.org/V2z1QuoP    [ raw code | fork ]

C, pasted on Oct 10:
#include "graphics.h"
#include "misc.h"

UCHAR *gBoard = 0;
BOOLEAN gbIsPlaying = FALSE;
PCHESS_PLAYING_STEP glpLinkHead = 0, glpLinkTail = 0;
POS gtComputerPosLast;

BOOLEAN GmGetIndexByMouseMsg(MOUSEMSG tMsg, PPOS lpPos)
{
	ULONG LenX = CELL_WIDTH / 2, LenY = CELL_HEIGHT / 2;
	lpPos->x = (tMsg.x - LenX) / CELL_WIDTH;
	lpPos->y = (tMsg.y - LenY) / CELL_HEIGHT;
	if (lpPos->x <= CELL_COUNT && lpPos->y <= CELL_COUNT) {
		return TRUE;
	}
	return FALSE;
}

inline UCHAR GmGetBoard(ULONG x, ULONG y)
{
	if (x <= CELL_COUNT && x >= 0 && y <= CELL_COUNT && y >= 0 ) {
		return gBoard[x * (CELL_COUNT + 1) + y];
	}
	return CHESS_BAD;
}

inline VOID GmSetBoard(ULONG x, ULONG y, UCHAR ucData)
{
	if (x <= CELL_COUNT && y <= CELL_COUNT) {
		gBoard[x * (CELL_COUNT + 1) + y] = ucData;
	}
	return;
}

VOID GmInsertTailLink(PCHESS_PLAYING_STEP lpLinkHead, PCHESS_PLAYING_STEP lpLinkCurrent)
{
	PCHESS_PLAYING_STEP lpLinkTail = lpLinkHead->BLink;
	lpLinkTail->FLink = lpLinkCurrent;
	lpLinkCurrent->BLink = lpLinkTail;
	lpLinkCurrent->ulStepNumber = lpLinkTail->ulStepNumber + 1;
	lpLinkHead->BLink = lpLinkCurrent;
	lpLinkCurrent->FLink = lpLinkHead;
	return;
}

PCHESS_PLAYING_STEP GmInitializePlayingStep(PCHESS_PLAYING_STEP lpLinkHead, UCHAR ucChess, POS tPos)
{
	PCHESS_PLAYING_STEP lpLinkCurrent = (PCHESS_PLAYING_STEP)malloc(sizeof(CHESS_PLAYING_STEP));
	lpLinkCurrent->ucChess = ucChess;
	lpLinkCurrent->tPos.x = tPos.x;
	lpLinkCurrent->tPos.y = tPos.y;

	// 如果已有链表头,则插入链表尾
	if (lpLinkHead != 0) {
		GmInsertTailLink(lpLinkHead, lpLinkCurrent);
	} else {   // 否则就建立链表
		lpLinkCurrent->ulStepNumber = 0;
		lpLinkCurrent->BLink = lpLinkCurrent->FLink = lpLinkCurrent;
	}

	return lpLinkCurrent;
}

VOID GmRefreshGraph()
{
	ULONG x, y;
	UCHAR ucData = CHESS_NONE;
	ULONG ulColor = NULL;
	for (x = 0; x <= CELL_COUNT; x++) {
		for (y = 0; y <= CELL_COUNT; y++) {
			ucData = GmGetBoard(x, y);
			if (ucData != CHESS_NONE && ucData != CHESS_BAD) {
				ulColor = (ucData == CHESS_BLACK? BLACK: WHITE);
				setcolor(ulColor);
				setfillstyle(ulColor);
				fillellipse(CELL_WIDTH + x * CELL_WIDTH, CELL_HEIGHT + y * CELL_HEIGHT, CELL_WIDTH / 2 * GRAPH_SCALE, CELL_HEIGHT / 2 * GRAPH_SCALE);
			}
		}
	}
	return;
}

GAME_STATUS GmComputerTurn(POS tHumanPos)
{
	GAME_STATUS bStatus;
	LONG ulScore;
	POS tPos = AiGetBestPos(gBoard, CHESS_BLACK, &ulScore, AI_LEVEL);
	//POS tPos;
	//AiGetRandomPos(gBoard, &tPos);
	gtComputerPosLast.x = tPos.x; gtComputerPosLast.y = tPos.y;
	GmSetBoard(tPos.x, tPos.y, CHESS_BLACK);
	if (!AiIsPlayerWin(gBoard, tPos, CHESS_BLACK)) {
		bStatus = STATUS_PLAYING;
		glpLinkTail = GmInitializePlayingStep(glpLinkHead, CHESS_BLACK, tPos);
	} else {
		bStatus = STATUS_COMPUTER_WIN;
		gbIsPlaying = FALSE;
	}

	return bStatus;
}

GAME_STATUS GmHumanTurn(POS tPos)
{
	GAME_STATUS bStatus = STATUS_PLAYING;
	UCHAR ucData = GmGetBoard(tPos.x, tPos.y);
	if (ucData == CHESS_NONE) {
		GmSetBoard(tPos.x, tPos.y, CHESS_WHITE);
		if (!AiIsPlayerWin(gBoard, tPos, CHESS_WHITE)) {
			glpLinkTail = GmInitializePlayingStep(glpLinkHead, CHESS_WHITE, tPos);
			bStatus = GmComputerTurn(tPos);
		} else {
			gbIsPlaying = FALSE;
			bStatus = STATUS_HUMAN_WIN;
		}
		GmRefreshGraph();
	}
	return bStatus;
}

VOID GmDeinitialize()
{
	free(gBoard);
	gBoard = 0;

	PCHESS_PLAYING_STEP lpLinkCurrent = glpLinkHead->FLink;
	do {
		free(lpLinkCurrent->BLink);
		lpLinkCurrent = lpLinkCurrent->FLink;
	} while (lpLinkCurrent != glpLinkHead);

	return;
}

BOOLEAN GmInitialize()
{
	// 初始化棋盘
	setinitmode(0);
	initgraph((CELL_COUNT + 2)* CELL_WIDTH, (CELL_COUNT + 2) * CELL_HEIGHT);
	cleardevice();
	setbkcolor(GREEN);
	setcolor(BLACK);

	for (ULONG i = 1; i <= CELL_COUNT + 1; i++) {
		line(i * CELL_WIDTH, 0 + CELL_HEIGHT, i * CELL_WIDTH, CELL_COUNT * CELL_HEIGHT + CELL_HEIGHT);
		line(0 + CELL_WIDTH, i * CELL_HEIGHT, CELL_COUNT * CELL_WIDTH + CELL_WIDTH, i * CELL_HEIGHT);
	}

	// 初始化棋盘数据
	gBoard = (PUCHAR)malloc((CELL_COUNT + 1) * (CELL_COUNT + 1) * sizeof(UCHAR));
	memset(gBoard, 0, (CELL_COUNT + 1) * (CELL_COUNT + 1) * sizeof(UCHAR));

	POS tPos;
	tPos.x = 7; tPos.y = 7;   // 天元(棋盘中心)
	gtComputerPosLast.x = 7; gtComputerPosLast.y = 7;
	GmSetBoard(7, 7, CHESS_BLACK);

	// 初始化落子链表
	glpLinkHead = glpLinkTail = GmInitializePlayingStep(0, CHESS_BLACK, tPos);

	GmRefreshGraph();
	gbIsPlaying = TRUE;

	return TRUE;
}


Create a new paste based on this one


Comments: