Skip to content

Commit

Permalink
refactor: make string constant into enum
Browse files Browse the repository at this point in the history
  • Loading branch information
SverreNystad committed Jul 16, 2024
1 parent 79d7fd3 commit 96cf2e1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

public class GameResponse {
private final String gameId;
private final String status;
private final Status status;
private final Board board;

public GameResponse(String gameId, String status, Board board) {
public GameResponse(String gameId, Status status, Board board) {
this.gameId = gameId;
this.status = status;
this.board = board;
Expand All @@ -18,7 +18,7 @@ public String getGameId() {
}

public String getStatus() {
return status;
return status.getMessage();
}

public Board getBoard() {
Expand Down
18 changes: 18 additions & 0 deletions backend/src/main/java/board/master/model/communication/Status.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package board.master.model.communication;

public enum Status {
PLAYER_WIN("You won"),
BOT_WIN("Bot won"),
DRAW("Draw"),
IN_PROGRESS("Game in progress");

private final String message;

private Status(String message) {
this.message = message;
}

public String getMessage() {
return message;
}
}
19 changes: 8 additions & 11 deletions backend/src/main/java/board/master/service/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import board.master.model.communication.GameResponse;
import board.master.model.communication.GameStartRequest;
import board.master.model.communication.MoveRequest;
import board.master.model.communication.Status;
import board.master.model.Action;

import java.util.HashMap;
Expand Down Expand Up @@ -159,21 +160,17 @@ public GameResponse botMove(String gameId) throws IllegalArgumentException, Ille
}
}

private String getBoardStatus(StateHandler stateHandler, boolean player) {
private Status getBoardStatus(StateHandler stateHandler, boolean player) {
if(stateHandler.isTerminal()) {
if (stateHandler.utility(stateHandler.toMove()) >= 1) {
return (player) ? "You won" : "Bot won";
int utility = stateHandler.utility(stateHandler.toMove());
if (utility >= 1 || utility <= -1) {
return (player) ? Status.PLAYER_WIN : Status.BOT_WIN;
}
else if (stateHandler.utility(stateHandler.toMove()) <= -1) {
return (player) ? "You won" : "Bot won";
}
else if (stateHandler.utility(stateHandler.toMove()) == 0) {
return "Draw";
else if (utility == 0) {
return Status.DRAW;
}
}

return "Game in progress";
return Status.IN_PROGRESS;
}

/**
Expand Down

0 comments on commit 96cf2e1

Please sign in to comment.