Commit 11a093a9 authored by mva021's avatar mva021
Browse files

Added comments to the Player classes

parent 3f7ae9f0
......@@ -7,6 +7,8 @@ import inf101.v20.sem2.grid.Location;
* Since many of the students had tried implementing such an AI I have included one in this solution.
* But such a level of AI is not expected at the level of INF101.
*
* This player will work both for TicTacToe and for FourInARow
*
* This player uses a minimax strategy to search for the best move.
* That means that it tries all possible moves and gives a score to each move.
* The score is given by calling the method recursively for the other player to
......@@ -17,7 +19,7 @@ import inf101.v20.sem2.grid.Location;
* In this case we give score 1 for win, score for draw and score -1 for loosing.
* Note that the sum of the scores for both players always is 0.
*
* @author mva021
* @author Martin Vatshelle - martin.vatshelle@uib.no
*
*/
public class MiniMaxPlayer extends AbstractPlayer {
......
......@@ -2,6 +2,20 @@ package inf101.v20.sem2.games;
import inf101.v20.sem2.grid.Location;
/**
* Players has only one method that is specific to the game they play.
* namely getMove(), this method contains the logic of the player.
* There are normally differences in how a player chooses a move, e.g.
* in TicTacToe the player chooses x and y while in FourInARow the player
* chooses a column. Therefore one could imagine that different classes
* are needed for each type of game.
* However when possible one should make implement general
* playing strategies that can be reused in several games.
* That is done in this solution.
*
* @author Martin Vatshelle - martin.vatshelle@uib.no
*
*/
public interface Player {
/**
......
......@@ -5,6 +5,15 @@ import java.util.List;
import inf101.v20.sem2.grid.Location;
/**
* This Player chooses a random move among all the possible moves.
* This player can play any game which implements the possibleMoves() method.
*
* If a game where no possible moves exist is given, the player will throw an Exception
*
* @author Martin Vatshelle - martin.vatshelle@uib.no
*
*/
public class RandomPlayer extends AbstractPlayer {
public RandomPlayer(String piece, String name) {
......
......@@ -9,6 +9,18 @@ import inf101.v20.sem2.games.AbstractPlayer;
import inf101.v20.sem2.games.FourinRow;
import inf101.v20.sem2.games.IGame;
/**
* This Player enables the user to play the game by using the keyboard.
* There is in reality two different players here,
* one for TicTacToe and one for FourInARow
* If you check out the makeMove method it is only an if else choosing between the two types
*
* I thought still it was good to keep all this code in one class as then
* all players can be used in both games.
*
* @author Martin Vatshelle - martin.vatshelle@uib.no
*
*/
public class ConsolePlayer extends AbstractPlayer {
/**
......@@ -52,6 +64,9 @@ public class ConsolePlayer extends AbstractPlayer {
return getLocation(game);
}
/**
* Reads input for the FourInARow game
*/
private Location getColumn(FourinRow game) {
System.out.println("Player "+this.toString()+" type [Col] to make a move.");
boolean done = false;
......@@ -77,6 +92,11 @@ public class ConsolePlayer extends AbstractPlayer {
return readName(sc,"Name");
}
/**
* Can read input for any game where input is an x,y location
* @param game
* @return
*/
private Location getLocation(IGame game){
System.out.println("Player "+this.toString()+" type [Row] [Col] to make a move.");
boolean done = false;
......
......@@ -7,7 +7,8 @@ import inf101.v20.sem2.games.Player;
/**
* A class containing helper methods for reading input from a Scanner
* @author mva021
*
* @author Martin Vatshelle - martin.vatshelle@uib.no
*/
public class TerminalInput {
......@@ -16,11 +17,35 @@ public class TerminalInput {
return read(sc,f,name);
}
/**
* This method reads a name and makes sure the name read is not empty.
* The name parameter could e.g. be "Player name" and would be used to
* print an error message when invalid input is given.
*
* @param sc - the Scanner to read from
* @param name - The type of name
* @return a non empty name
*/
public static String readName(Scanner sc, String name) {
Function<String,String> f = Player::validateName;
return read(sc,f,name);
}
public static String readName(Scanner sc) {
return readName(sc, "Name");
}
/**
* Reads in a specified input from a Scanner
* (typically the keyboard but not always)
* A Function is given as input to check if the input found in the Scanner
* is valid and convert it from String to the right return type.
* The converter should throw an Exception if input is invalid.
*
* @param <T> - the return type of the reading
* @param sc - the Scanner to read from
* @param converter - Function to check and convert input
* @param name - This String is used to print error message
* @return
*/
private static <T> T read(Scanner sc, Function<String,T> converter, String name) {
T out = null;
while(out == null) {
......
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