Commit 46995823 authored by mva021's avatar mva021
Browse files

Fixed bugs so Connect four can be played from terminal

parent 450f6cfc
......@@ -46,7 +46,7 @@ public class FourinRow extends Game {
* @param loc
* @return
*/
Location drop(Location loc) {
public Location drop(Location loc) {
return drop(loc.getCol());
}
......
......@@ -6,6 +6,7 @@ import java.util.Scanner;
import inf101.v20.sem2.grid.Location;
import inf101.v20.sem2.games.AbstractPlayer;
import inf101.v20.sem2.games.FourinRow;
import inf101.v20.sem2.games.IGame;
public class ConsolePlayer extends AbstractPlayer {
......@@ -44,24 +45,29 @@ public class ConsolePlayer extends AbstractPlayer {
@Override
public Location getMove(IGame game) {
game.getGameBoard().printBoard();
System.out.println("Player "+this.toString()+" type [Row] [Col] to make a move.");
if(game instanceof FourinRow) {
return getColumn((FourinRow) game);
}
else
return getLocation(game);
}
private Location getColumn(FourinRow game) {
System.out.println("Player "+this.toString()+" type [Col] to make a move.");
boolean done = false;
int row = 0;
int col = 0;
while(!done) {
row = readInt(sc,"row");
col = readInt(sc,"column");
if(game.canPlace(new Location(row, col), this)) {
if(game.canPlace(game.drop(col), this)) {
done = true;
}
else {
System.out.println("Can not place there. Try again");
}
}
return new Location(row, col);
return game.drop(col);
}
/**
* Reads in a player name, only requirement is that name is not empty
* @param sc
......@@ -71,4 +77,21 @@ public class ConsolePlayer extends AbstractPlayer {
return readName(sc,"Name");
}
private Location getLocation(IGame game){
System.out.println("Player "+this.toString()+" type [Row] [Col] to make a move.");
boolean done = false;
int row = 0;
int col = 0;
while(!done) {
row = readInt(sc,"row");
col = readInt(sc,"column");
if(game.canPlace(new Location(row, col), this)) {
done = true;
}
else {
System.out.println("Can not place there. Try again");
}
}
return new Location(row, col);
}
}
......@@ -26,7 +26,7 @@ public class TerminalMenu {
players.add(new ConsolePlayer("O"));
break;
case 2:
players.add(new MiniMaxPlayer("O", 2));
players.add(new MiniMaxPlayer("O", 5));
break;
default:
throw new IllegalArgumentException("Unexpected value: " + multiplayerChoice);
......@@ -42,7 +42,8 @@ public class TerminalMenu {
break;
case 2:
game = new FourinRow();
game = new FourinRow(players);
break;
default:
throw new IllegalArgumentException("Unexpected value: " + choice);
}
......
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