Commit 67f226a4 authored by Matt Tower's avatar Matt Tower

RandomPlayer class at 8:34 Nov 11

parent 4d538c31
......@@ -7,6 +7,7 @@
*/
package model.player;
import java.util.ArrayList;
import java.util.Random;
import model.Board;
......@@ -15,14 +16,26 @@ import model.Mark;
/**
* @author mjt023
*
* This is the RandomPlayer class that is an AI player that chooses
* moves randomly.
*/
public class RandomPlayer extends Player {
public class RandomPlayer extends Player implements AutoPlayable {
/*
* declares the board, AI mark, Random object, random number variable,
* Arraylists to store rows and columns of free spaces on the current board
*/
private Board theBoard;
private Mark myMark;
private Random rand = new Random();
private int randNum;
private ArrayList<Integer> rowOptions = new ArrayList<Integer>();
private ArrayList<Integer> colOptions = new ArrayList<Integer>();
/**
* This is the constructor for the RandomPlayer class that takes in the
* current board and the mark of the AI player.
*
* @param theBoard
* @param myMark
*/
......@@ -30,6 +43,15 @@ public class RandomPlayer extends Player {
super(theBoard, myMark);
}
/**
* returns the board
*
* @return - the board
*/
public Board getBoard() {
return this.theBoard;
}
/**
* generates a random number within board dimensions, used for choosing
* random coordinates
......@@ -37,17 +59,32 @@ public class RandomPlayer extends Player {
* @return - randomly generated number within board dimensions
*/
public int genRandNum() {
return this.rand.nextInt(theBoard.getSize());
}
return this.rand.nextInt(3);
/**
* finds the free spaces in the board currently
*/
private void findFreeSpaces() {
for (int i = 0; i < theBoard.getSize(); i++) {
for (int j = 0; j < theBoard.getSize(); j++) {
if (theBoard.isEmpty(i, j)) {
rowOptions.add(i);
colOptions.add(j);
}
}
}
}
/**
* AI makes random moves until an empty space is found
* RandomPlayer makes a random move
*
* @return
* @return - true if move was made successfully, false otherwise
*/
public boolean setMark() {
@Override
public boolean decideMove() {
findFreeSpaces();
randNum = rand.nextInt(theBoard.getSize());
return move(rowOptions.get(randNum), colOptions.get(randNum));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment