Commits

Alex Hixon committed 77e621b

Working, playable Console UI. Also make sure we actually check for a winner!

  • Participants
  • Parent commits 17659e1

Comments (0)

Files changed (11)

File src/model/BoardCoordinate.java

         }
     }
 
+    public String toNotationString () {
+        return (char)(this.x + 'a') + ((this.y + 1) + "");
+    }
+
     public BoardCoordinate add (Direction d) {
         return new BoardCoordinate (super.add(d));
     }

File src/model/Coordinate.java

     public Coordinate subtract (Coordinate other) {
         return new Coordinate(this.x - other.x, this.y - other.y);
     }
-
-
 }
 

File src/model/Game.java

                 "starting position for " + brain);
         }*/
 
+        player.setGame(this);
         players.add (player);
     }
 
      */
     public AbstractPlayer playGame() {
         while (!this.hasWinner ()) {
-            for (AbstractPlayer p : this.players) {
-                Move m = p.getMove();
+            Move m = this.currentPlayer.getMove();
 
-                if (m == null || !this.moveIsValid(m)) {
-                    return null;
-                }
+            if (m == null || !this.moveIsValid (m)) {
+                return null;
+            }
 
-                this.makeMove(m);
-                this.endTurn();
-            }
+            this.makeMove (m);
+            this.endTurn ();
         }
 
         return this.getWinner();
     }
 
-    private boolean hasWinner() {
+    public boolean hasWinner() {
         return getWinner() != null;
     }
 
-    private AbstractPlayer getWinner() {
-        if (this.winner != null) {
+    public AbstractPlayer getWinner() {
+        if (this.winner == null) {
             // look for one if it's still undecided
             for (AbstractPlayer p : this.players) {
                 if (p.getFinishBaseline().hasCoordinateOnBaseline(p.getCurrentCoordinate())) {

File src/model/GameManager.java

     }
 
     public void makeMove (Move move) {
-        if(currentGame.moveIsValid(move)){
+        /*if(currentGame.moveIsValid(move)){
             games = games.subList(0, games.indexOf(currentGame));
             Game temp = new Game(currentGame);
             currentGame.makeMove(move);
             games.add(new Game(currentGame));
             currentGame = games.get(games.size()-1);
-        }
+        } */
     }
 
 }

File src/model/players/AIPlayer.java

 
     @Override
     public Move getMove() {
-        return null;
+        return new Move (MoveType.MOVE_PLAYER, initializeBuildTree(game));
     }
 
     private BoardCoordinate initializeBuildTree (Game game) {
             }
         }
     }
+
+    public String getType (){
+        return "ai";
+    }
 }

File src/model/players/AbstractPlayer.java

     protected Direction direction;
     protected String icon;
 
-    private int wallCount;
-    private Game game; //TODO initialize this in constructor
+    protected int wallCount;
+    protected Game game; //TODO initialize this in constructor
 
-    private BoardCoordinate currentCoordinate;
-    private final Baseline startBaseline;
-    private final Baseline finishBaseline;
+    protected BoardCoordinate currentCoordinate;
+    protected final Baseline startBaseline;
+    protected final Baseline finishBaseline;
 
     public AbstractPlayer(String name, String icon, Direction direction) {
         this.name = name;
 
     public abstract Move getMove ();
 
+    public abstract String getType();
+
     public void setGame (Game game){
         this.game = game;
     }

File src/model/players/DumbPlayer.java

-package model.players;
-
-import model.Direction;
-import model.Move;
-
-public class DumbPlayer extends AbstractPlayer {
-
-    public DumbPlayer(String name, String icon, Direction direction) {
-        super(name, icon, direction);
-    }
-
-    @Override
-    public Move getMove() {
-        //this.get
-        return null;
-    }
-}

File src/model/players/NullPlayer.java

     public Move getMove() {
         return null;
     }
+
+    public String getType () {
+        return "null";
+    }
 }

File src/view/ConsolePlayer.java

+package view;
+
+import model.BoardCoordinate;
+import model.Direction;
+import model.Game;
+import model.Move;
+import model.players.AbstractPlayer;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.LinkedList;
+
+public class ConsolePlayer extends AbstractPlayer {
+    public ConsolePlayer(String name, String icon, Direction direction) {
+        super(name, icon, direction);
+    }
+
+    @Override
+    public Move getMove () {
+        while (true) {
+            ConsoleUI.printBoard(this.game.getBoard());
+            System.out.print("> ");
+            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
+            String moveStr = null;
+
+            try {
+                moveStr = reader.readLine().toLowerCase();
+                if (moveStr.equals("quit")) {
+                    return null;
+                }
+
+                Move move = Move.translate(moveStr);
+                if (this.game.moveIsValid (move)) {
+                    return move;
+                }
+            } catch (IOException ex) {
+                return null;
+                // FIXME: should we return null?
+            } catch (IllegalArgumentException ex) {
+                System.out.println ("Badly formatted input - expected <col><row>");
+            }
+
+            if (moveStr != null) {
+                System.out.println("Move " + moveStr + " is invalid. Possible moves:");
+                LinkedList<BoardCoordinate> possibleMovements = this.game.getPossibleCoordinates();
+                for (BoardCoordinate coordinate : possibleMovements) {
+                    System.out.println ("\t" + coordinate.toNotationString ());
+                }
+
+                if (this.getWallCount() < Game.MAX_PLAYER_WALL_COUNT) {
+                    System.out.println("or any valid wall placement");
+                }
+            }
+        }
+
+    }
+
+    public String getType () {
+        return "console";
+    }
+}

File src/view/ConsoleUI.java

 package view;
 
 import model.*;
+import model.players.AIPlayer;
 import model.players.AbstractPlayer;
-import model.players.DumbPlayer;
 
 import java.util.InputMismatchException;
 import java.util.LinkedList;
     public static void main (String args[]) {
         Scanner scanner = new Scanner (System.in);
         Game game = new Game();
-        System.out.println ("Quoridor - now with 100% more gameplay!");
+        System.out.println ("Quoridor - now with 100% more gameplay!");
 
         // setup brains
         int numPlayers;
 
         LinkedList<AbstractPlayer> brains = new LinkedList<AbstractPlayer>();
         Direction startPositions[] = {Direction.DOWN, Direction.UP, Direction.LEFT, Direction.RIGHT};
-        String icons[] = {"", "", "", ""};
+        String icons[] = {"A", "B", "C", "D"};
         int aiNum = 1;
         for (int i = 0; i < numPlayers; i++) {
             String type;
             String name = "Computer " + aiNum;
             aiNum++;
             if (type.equals ("ai")) {
-                game.addPlayer(new DumbPlayer(name, icons[i], startPositions[i]));
+                game.addPlayer(new AIPlayer (name, icons[i], startPositions[i]));
             } else {
                 System.out.print("Player name? ");
                 name = scanner.nextLine().toLowerCase();
-                game.addPlayer(new HumanConsoleBrain(name, icons[i], startPositions[i]));
+                game.addPlayer(new ConsolePlayer (name, icons[i], startPositions[i]));
             }
         }
 
         // keep playing until we have a winner
         // playGame stops if someone returned a bad move (eg a human player returnning null)
         while (game.playGame() == null) {
-            System.out.println("WANT TO SAVE AND STUFF?");
+            if (game.getCurrentPlayer().getType () != "console"){
+                System.out.println("AI returned a null move - that's badness!");
+                System.out.println("Quitting");
+                return;
+                // this logic probably belongs in Game?
+            } else {
+                System.out.println("WANT TO SAVE AND STUFF?");
+                break;
+            }
         }
+
+        System.out.println("Congratulations! " + game.getWinner().getName() + " has won the game!");
+
     }
 
     public static void printBoard (Board b) {

File src/view/HumanConsoleBrain.java

-package view;
-
-import model.Direction;
-import model.Move;
-import model.players.AbstractPlayer;
-
-public class HumanConsoleBrain extends AbstractPlayer {
-    public HumanConsoleBrain(String name, String icon, Direction direction) {
-        super(name, icon, direction);
-    }
-
-    @Override
-    public Move getMove () {
-        return null;
-    }
-}