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); }
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; }
void Maze::Generate(Coord startPos) { Coord DIRECTIONS[4]; // Lists the possible directions, so we can loop through them DIRECTIONS[0].Set(-1, 0); DIRECTIONS[1].Set(0, 1); DIRECTIONS[2].Set(1, 0); DIRECTIONS[3].Set(0, -1); int currDist; // Keeps track of the current distance from start (is equal to positions.Size()) int maxDist = 0; // Keeps track of the longest distance from start (so we place the end point at the right spot) Coord currPos = startPos; // Start generating from provided coordinate Coord newPos; // Next position to go Coord currDirection; // We need to save the current direction in a variable because we need it more than once (moving, breaking the walls) Stack positions; // Saves all the positions where we go Stack directions(4); // Current possible directions depending on grid boundaries and visited bool hidePortal = (portalPos == playerPos); // 1st time we display the portal, but not 2nd time (if player currently is on portal) positions.Push(currPos); // Add current position to stack because our loop needs the top position in stack 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) // If we are farther than our max maxDist = currDist; // Save the new max directions.Clear(); // Empty the stack for (int count = 0; count < 4; count ++) // Search for all possible directions in directions and add them in stack { newPos = currPos + DIRECTIONS[count]; // Adding the possible direction to where we are, so that we can check if new position is valid 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 ++) // Loop through all directions (up down left right) and depending on index, we know which way we went { 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) // If we just set the max distance to our current distance, then we are the farthest from the start { portalPos = positions.GetRandom(positions.Size() / 2, (positions.Size() * 3) / 4); // Get a random position between 1/2 and 3/4 of all positions finishPos = positions.Pop(); // finish is where we are right now and we remove that spot from stack (we are going back) } else positions.Pop(); // Just go back by removing current position.stop } } if (hidePortal) { portalPos.Set(-1, -1); // Set it outside of boundaries, so it will not be displayed and therefore not reached } }