Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/main/java/controller/Command/GameCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import controller.Command.game.DotsAndBoxesController;
import controller.Command.game.GameController;
import model.DotsAndBoxes.DotsAndBoxes;

public class GameCommands implements ResolveCommand {
private static final GameCommands gameCommands = new GameCommands();
Expand All @@ -23,6 +22,9 @@ public void resolveCommand(String[] tokens) throws Exception {
break;
case "DotsAndBoxes":
DotsAndBoxesCommand.getDotsAndBoxesCommand().execute(tokens);
break;
case "BattleSea":
BattleSeaCommand.getBattleSeaCommand().execute(tokens);
}
}

Expand Down Expand Up @@ -70,29 +72,45 @@ public void execute(String[] tokens) throws Exception {
case "open":
done(GameController.getGameController().open(tokens[3], tokens[1]));
break;
case "join" :
done(DotsAndBoxesController.getDotsAndBoxesController().join(Long.parseLong(tokens[3]),tokens[4]));
case "end of my turn":
case "endofmyturn":
done(DotsAndBoxesController.getDotsAndBoxesController().endOfMyTurn(Long.parseLong(tokens[3])));
break;
case "show score":
case "showscore":
done(DotsAndBoxesController.getDotsAndBoxesController().showScore(Long.parseLong(tokens[3])));
break;
case "show available lines":
case "showavailablelines":
done(DotsAndBoxesController.getDotsAndBoxesController().showAvailableLines(Long.parseLong(tokens[3])));
break;
case "show table":
case "showtable":
done(DotsAndBoxesController.getDotsAndBoxesController().showTable(Long.parseLong(tokens[3])));
break;
case "who is next?":
case "whoisnext?":
done(DotsAndBoxesController.getDotsAndBoxesController().whoIsNext(Long.parseLong(tokens[3])));
break;
case "show result":
case "showresult":
done(DotsAndBoxesController.getDotsAndBoxesController().showResult(Long.parseLong(tokens[3])));
break;
case "end":
done(DotsAndBoxesController.getDotsAndBoxesController().end(Long.parseLong(tokens[3])));
}
}
}

private static class BattleSeaCommand implements ExecuteCommand {
private static final BattleSeaCommand battleSeaCommand = new BattleSeaCommand();

private BattleSeaCommand() {
}

static BattleSeaCommand getBattleSeaCommand() {
return battleSeaCommand;
}

@Override
public void execute(String[] tokens) throws Exception {
switch (tokens[2]) {
//TODO add commands.
}
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/controller/Command/game/BattleSeaController.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
package controller.Command.game;

public class BattleSeaController {
private static final BattleSeaController battleSeaController = new BattleSeaController();

private BattleSeaController() {
}

public static BattleSeaController getBattleSeaController() {
return battleSeaController;
}

public void changeCoordination(char ship, int x, int y) {

}
}
14 changes: 14 additions & 0 deletions src/main/java/controller/Command/game/DotsAndBoxesController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
import exception.game.NotYourTurn;
import model.DotsAndBoxes.DotsAndBoxes;

import model.Game;
import model.GameLog;
import model.Player;

import java.time.LocalDate;
import java.util.Date;

public class DotsAndBoxesController {
private static final DotsAndBoxesController dotsAndBoxesController = new DotsAndBoxesController();

Expand Down Expand Up @@ -148,8 +153,17 @@ public String end(long gameID) throws GameNotFound {
DotsAndBoxes dotsAndBoxes = DotsAndBoxes.getDotsAndBoxes().get(gameID);
if (dotsAndBoxes != null) {
if (dotsAndBoxes.isBoardFull()) {
GameLog.Result result;
if (dotsAndBoxes.judge() == null)
result = GameLog.Result.DRAW;
else if (dotsAndBoxes.judge().equals(dotsAndBoxes.getHost()))
result = GameLog.Result.WIN;
else
result = GameLog.Result.DEFEAT;
new GameLog("dotsandboxes", dotsAndBoxes.getHost(), dotsAndBoxes.getGuest(), LocalDate.now(), result);
return "Back to the game menu";
} else {
new GameLog("dotsandboxes", dotsAndBoxes.getHost(), dotsAndBoxes.getGuest(), LocalDate.now(), dotsAndBoxes.winByForfeit().equals(dotsAndBoxes.getHost())? GameLog.Result.WIN: GameLog.Result.DEFEAT);
return dotsAndBoxes.winByForfeit().getUsername() + "won by forfeit" + '\n' + "Back to the game menu";
}

Expand Down
9 changes: 3 additions & 6 deletions src/main/java/controller/Command/game/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import exception.game.GameNotFound;
import exception.UsernameNotFound;
import exception.game.NotPlayedYet;
import model.DotsAndBoxes.DotsAndBoxes;
import model.Game;
import model.GameLog;
Expand All @@ -23,7 +24,7 @@ public static GameController getGameController() {

public String names() {
StringBuilder tmp = new StringBuilder();
for (String s : Game.getGames()) {
for (String s : Game.getNames()) {
tmp.append(s).append(" • ");
}
return tmp.toString().substring(0, tmp.toString().length() - 1).trim();
Expand All @@ -36,14 +37,10 @@ public String open(String username, String game) throws GameNotFound {
break;
case "DotsAndBoxes":
return Long.toString((new DotsAndBoxes(Player.getPlayers().get(username))).getGameID()); // returns game id for further uses (user game commands)
break;
default:
throw new GameNotFound();
}
}

public String turn(long game) { // changes turn

return null;
}

public void showScoreboard(String gameName) throws GameNotFound {
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/controller/DotsAndBoxesController.java

This file was deleted.

7 changes: 7 additions & 0 deletions src/main/java/exception/game/NotPlayedYet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package exception.game;

public class NotPlayedYet extends Exception {
public NotPlayedYet() {
super("You should play first!");
}
}
Loading