Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class MyAgent extends Agent
- {
- Random r;
- /**
- * Constructs a new agent, giving it the game and telling it whether it is Red or Yellow.
- *
- * @param game The game the agent will be playing.
- * @param iAmRed false if the agent is Red, False if the agent is Yellow.
- */
- public MyAgent(Connect4Game game, boolean iAmRed)
- {
- super(game, iAmRed);
- r = new Random();
- }
- /**
- * The move method is run every time it is this agent's turn in the game. You may assume that
- * when move() is called, the game has at least one open slot for a token, and the game has not
- * already been won.
- *
- * By the end of the move method, the agent should have placed one token into the game at some
- * point.
- *
- * After the move() method is called, the game engine will check to make sure the move was
- * valid. A move might be invalid if:
- * - No token was place into the game.
- * - More than one token was placed into the game.
- * - A previous token was removed from the game.
- * - The color of a previous token was changed.
- * - There are empty spaces below where the token was placed.
- *
- * If an invalid move is made, the game engine will announce it and the game will be ended.
- *
- */
- public void move()
- {
- if (canWin(iAmRed) >= 0)
- {
- moveOnColumn(canWin(iAmRed));
- }
- else
- {
- moveOnColumn(randomMove());
- }
- }
- /**
- * Drops a token into a particular column so that it will fall to the bottom of the column.
- * If the column is already full, nothing will change.
- *
- * @param columnNumber The column into which to drop the token.
- */
- public void moveOnColumn(int columnNumber)
- {
- int lowestEmptySlotIndex = getLowestEmptyIndex(myGame.getColumn(columnNumber)); // Find the top empty slot in the column
- // If the column is full, lowestEmptySlot will be -1
- if (lowestEmptySlotIndex >= -1) // if the column is not full
- {
- Connect4Slot lowestEmptySlot = myGame.getColumn(columnNumber).getSlot(lowestEmptySlotIndex); // get the slot in this column at this index
- if (iAmRed) // If the current agent is the Red player...
- {
- lowestEmptySlot.addRed(); // Place a red token into the empty slot
- }
- else // If the current agent is the Yellow player (not the Red player)...
- {
- lowestEmptySlot.addYellow(); // Place a yellow token into the empty slot
- }
- }
- }
- /**
- * Returns the index of the top empty slot in a particular column.
- *
- * @param column The column to check.
- * @return the index of the top empty slot in a particular column; -1 if the column is already full.
- */
- public int getLowestEmptyIndex(Connect4Column column ) {
- int lowestEmptySlot = -1;
- for (int i = 0; i < column.getRowCount(); i++)
- {
- if (!column.getSlot(i).getIsFilled())
- {
- lowestEmptySlot = i;
- }
- }
- return lowestEmptySlot;
- }
- /**
- * Returns a random valid move. If your agent doesn't know what to do, making a random move
- * can allow the game to go on anyway.
- *
- * @return a random valid move.
- */
- public int randomMove()
- {
- int i = r.nextInt(myGame.getColumnCount());
- while (getLowestEmptyIndex(myGame.getColumn(i)) == -1)
- {
- i = r.nextInt(myGame.getColumnCount());
- }
- return i;
- }
- /**
- * Returns the column index, i, which contains the winning move for either red or yellow.
- * If no winning move is found, then it returns -1, allowing the agent to place the token randomly.
- *
- * @param isRed Checks to see if the token is red or yellow; true = red, false = yellow.
- * @return the column index, i, containing the winning move for either red or yellow OR
- * @return -1, if no winning move is found.
- */
- public int canWin(boolean isRed)
- {
- for (int i = 0; i < myGame.getColumnCount(); i++)
- {
- if (isHorizontalWinningColumn(i, isRed))
- {
- return i;
- }
- else if (isVerticalWinningColumn(i, isRed))
- {
- return i;
- }
- else if (isDiagonalWinningColumn(i, isRed))
- {
- return i;
- }
- }
- return -1;
- }
- /**
- * Checks to see if there are three tokens of the same color in a row vertically. If the condition returns
- * true, it is called by the canWin method.
- *
- * @param columnIndex Passes column index in order to check every index for vertical wins.
- * @param isRed Checks to see if the token is red or yellow; true = red, false = yellow.
- * @return true if conditions are met, false if conditons are not met.
- */
- public boolean isVerticalWinningColumn(int columnIndex, boolean isRed)
- {
- int vertColSlot = 0;
- vertColSlot = getLowestEmptyIndex(myGame.getColumn(columnIndex));
- int pieces = 0;
- if (vertColSlot < 3 && vertColSlot > -1)
- {
- for (int i = 0; i < 6; i++)
- {
- if (myGame.getColumn(columnIndex).getSlot(i).getIsFilled() == true
- && myGame.getColumn(columnIndex).getSlot(i).getIsRed() == isRed)
- {
- pieces++;
- }
- }
- for (int i = 0; i > -1; i--)
- {
- if (myGame.getColumn(columnIndex).getSlot(i).getIsFilled() == true
- && myGame.getColumn(columnIndex).getSlot(i).getIsRed() == isRed)
- {
- pieces++;
- }
- }
- if (pieces > 2)
- {
- System.out.println("Winner Vertical: " + columnIndex);
- return true;
- }
- }
- return false;
- }
- /**
- * Checks to see if there are three tokens of the same color in a row horizontally. If the condition returns
- * true, it is called by the canWin method.
- *
- * @param columnIndex Passes column index in order to check every index for horizontal wins.
- * @param isRed Checks to see if the token is red or yellow; true = red, false = yellow.
- * @return true if conditions are met, false if conditons are not met.
- */
- public boolean isHorizontalWinningColumn(int columnIndex, boolean isRed)
- {
- int horizColSlot = 0;
- horizColSlot = getLowestEmptyIndex(myGame.getColumn(columnIndex));
- int pieces = 0;
- if (horizColSlot > -1)
- {
- for (int i = columnIndex; i < 7; i++)
- {
- if (myGame.getColumn(i).getSlot(horizColSlot).getIsFilled() == true
- && myGame.getColumn(i).getSlot(horizColSlot).getIsRed() == isRed)
- {
- pieces++;
- }
- }
- for (int i = columnIndex; i > -1; i--)
- {
- if (myGame.getColumn(i).getSlot(horizColSlot).getIsFilled() == true
- && myGame.getColumn(i).getSlot(horizColSlot).getIsRed() == isRed)
- {
- pieces++;
- }
- }
- if (pieces > 2)
- {
- System.out.println("Winner Horizontal: " + columnIndex);
- return true;
- }
- }
- return false;
- }
- /**
- * Checks to see if there are three tokens of the same color diagonally (streching upwards). If the condition returns
- * true, it is called by the canWin method.
- *
- * @param columnIndex Passes column index in order to check every index for upward diagonal wins.
- * @param isRed Checks to see if the token is red or yellow; true = red, false = yellow.
- * @return true if conditions are met, false if conditons are not met.
- */
- public boolean isDiagonalWinningColumn(int columnIndex, boolean isRed)
- {
- int diagColSlot = 0;
- diagColSlot = getLowestEmptyIndex(myGame.getColumn(columnIndex));
- int pieces = 0;
- if (diagColSlot >= 0)
- {
- for (int i = 0; i < 4; i++)
- {
- if (columnIndex < 4 && diagColSlot > 2)
- {
- if (myGame.getColumn(columnIndex + i).getSlot(diagColSlot - i).getIsFilled() == true
- && myGame.getColumn(columnIndex + i).getSlot(diagColSlot - i).getIsRed() == isRed)
- {
- pieces++;
- }
- }
- if (columnIndex > 2 && diagColSlot < 3)
- {
- if (myGame.getColumn(columnIndex - i).getSlot(diagColSlot + i).getIsFilled() == true
- && myGame.getColumn(columnIndex - i).getSlot(diagColSlot + i).getIsRed() == isRed)
- {
- pieces++;
- }
- }
- if (columnIndex > 2 && diagColSlot > 2)
- {
- if (myGame.getColumn(columnIndex - i).getSlot(diagColSlot - i).getIsFilled() == true
- && myGame.getColumn(columnIndex - i).getSlot(diagColSlot - i).getIsRed() == isRed)
- {
- pieces++;
- }
- }
- if (columnIndex < 4 && diagColSlot < 3)
- {
- if (myGame.getColumn(columnIndex + i).getSlot(diagColSlot + i).getIsFilled() == true
- && myGame.getColumn(columnIndex + i).getSlot(diagColSlot + i).getIsRed() == isRed)
- {
- pieces++;
- }
- }
- }
- }
- if (pieces > 2)
- {
- System.out.println("Diagonal Winner");
- return true;
- }
- return false;
- }
- /**
- * Returns the name of this agent.
- *
- * @return the agent's name
- */
- public String getName()
- {
- return "My Agent";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement