当前位置: 首页>>代码示例>>C++>>正文


C++ Stack::GetRandom方法代码示例

本文整理汇总了C++中Stack::GetRandom方法的典型用法代码示例。如果您正苦于以下问题:C++ Stack::GetRandom方法的具体用法?C++ Stack::GetRandom怎么用?C++ Stack::GetRandom使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Stack的用法示例。


在下文中一共展示了Stack::GetRandom方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: MakeAIMove

void Othello::MakeAIMove()
{
	int biggestGain = -4 * BOARD_SIZE * gameType; // Start with a really low value because after many moves, we could end up with biggestGain being negative
	int currGain;
	int depth;
	COORD move;
	Stack bestMoves;
	
	switch (gameType)
	{
	case 1:
		depth = 0; // Evaluate no move (play any valid move)
		break;
	case 2:
		depth = 1; // Evaluate wins by this move only
		break;
	case 3:
		depth = 2; // Evaluate wins by this move and the opponent's move
		break;
	case 4:
		depth = 5; // Evaluate wins by this move, the opponent's move and your next move
		break;
	}

	for (int row = 0; row < BOARD_SIZE; row ++) // Rows
	{
		for (int col = 0; col < BOARD_SIZE; col ++) // Columns
		{
			if (ValidMove(row, col)) // If move is valid
			{
				currGain = MoveValue(row, col, depth);
				move.Y = row;
				move.X = col;
				if (currGain > biggestGain) // If we found a new best move
				{
					biggestGain = currGain;
					bestMoves.Clear(); // Remove all the older moves, because this one is better
					bestMoves.Push(move); // Add this move to the stack
				}
				else if (currGain == biggestGain) // If this move is as good as the best one
					bestMoves.Push(move); // Add it to the stack
			}
		}
	}
	move = bestMoves.GetRandom();
	MakeMove(move.Y, move.X);
}
开发者ID:gumgl,项目名称:C,代码行数:47,代码来源:othello.cpp

示例2: Generate

void Maze::Generate(Coord startPos)
{
	Coord DIRECTIONS[4];
	DIRECTIONS[0].Set(-1, 0);
	DIRECTIONS[1].Set(0, 1);
	DIRECTIONS[2].Set(1, 0);
	DIRECTIONS[3].Set(0, -1);

	int currDist;
	int maxDist = 0;
	Coord currPos = startPos;
	Coord newPos;
	Coord currDirection;
	Stack positions;
	Stack directions(4);
	bool hidePortal = (portalPos == playerPos);

	positions.Push(currPos);
	while (positions.Size() > 0) // While away from start point
	{
		currPos = positions.Pop(); // Get top position
		positions.Push(currPos); // Put it back
		currDist = positions.Size(); // This is our distance from start point
		maze[currPos.Y][currPos.X].visited = true; // We visited this
		if (currDist > maxDist)
			maxDist = currDist;

		directions.Clear();
		for (int count = 0; count < 4; count ++) // Add all possible directions in directions[]
		{
			newPos = currPos + DIRECTIONS[count];
			if (newPos.X >= 0 && newPos.X < gridSize && newPos.Y >= 0 && newPos.Y < gridSize) // If it is in boundaries of maze
				if (!maze[newPos.Y][newPos.X].visited) // If never visited
					directions.Push(DIRECTIONS[count]); // Add this direction to possible directions
		}

		if (directions.Size() > 0) // If we can go somewhere
		{
			currDirection = directions.GetRandom(); // Get a random direction
			newPos = currPos + currDirection; // Calculate new position

			for (int count = 0; count < 4; count ++) // BREAK THE WALLS
			{
				if (currDirection == DIRECTIONS[count])
				{
					switch (count)
					{
					case 0: // going up
						maze[currPos.Y][currPos.X].top = false;
						maze[newPos.Y][newPos.X].bottom = false;
						break;
					case 1: // going right
						maze[currPos.Y][currPos.X].right = false;
						maze[newPos.Y][newPos.X].left = false;
						break;
					case 2: // going down
						maze[currPos.Y][currPos.X].bottom = false;
						maze[newPos.Y][newPos.X].top = false;
						break;
					case 3: // going left
						maze[currPos.Y][currPos.X].left = false;
						maze[newPos.Y][newPos.X].right = false;
						break;
					}
				}
			}
			positions.Push(newPos); // Add new position to stack
		}
		else // no possible direction, go back
		{
			if (currDist == maxDist)
			{
				portalPos = positions.GetRandom(positions.Size() / 2, positions.Size() - 2);
				finishPos = positions.Pop();
			}
			else
				positions.Pop();
		}
	}
	maze[finishPos.Y][finishPos.X].content = FINISH;
	if (hidePortal)
	{
		portalPos.Set(-1, -1);
	}
	else
		maze[portalPos.Y][portalPos.X].content = PORTAL;
}
开发者ID:gumgl,项目名称:C,代码行数:87,代码来源:Maze.cpp


注:本文中的Stack::GetRandom方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。