Skip to content

Commit

Permalink
style: lint project
Browse files Browse the repository at this point in the history
  • Loading branch information
SverreNystad committed Jul 17, 2024
1 parent bcd02cf commit d3e61bd
Show file tree
Hide file tree
Showing 51 changed files with 876 additions and 802 deletions.
7 changes: 3 additions & 4 deletions backend/src/main/java/board/master/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
10 changes: 5 additions & 5 deletions backend/src/main/java/board/master/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins(allowedOrigins)
.allowedMethods(allowedMethods)
.allowedHeaders(allowedHeaders)
.allowCredentials(allowCredentials)
.maxAge(maxAge);
.allowedOrigins(allowedOrigins)
.allowedMethods(allowedMethods)
.allowedHeaders(allowedHeaders)
.allowCredentials(allowCredentials)
.maxAge(maxAge);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package board.master.controller;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.http.ResponseEntity;

import java.util.List;

import board.master.model.agents.AgentFactory;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class AgentController {

/**
* Endpoint to get the list of available agents
*
*
* @return a list of available agents
*/
@GetMapping("/agents")
Expand Down
37 changes: 16 additions & 21 deletions backend/src/main/java/board/master/controller/GameController.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package board.master.controller;

import java.util.logging.Logger;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;

import board.master.model.communication.GameId;
import board.master.model.communication.GameResponse;
import board.master.model.communication.GameStartRequest;
import board.master.model.communication.MoveRequest;
import board.master.service.GameService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class GameController {
Expand All @@ -27,7 +28,7 @@ public GameController(GameService gameService) {

/**
* Endpoint to start a new game.
*
*
* @param request - GameStartRequest for what type of game to start, who is the first player and what bot to fight against
* @return GameResponse with the game id, status and board
*/
Expand All @@ -36,18 +37,16 @@ public ResponseEntity<GameResponse> startGame(@RequestBody GameStartRequest requ
try {
GameResponse response = gameService.startGame(request);
return ResponseEntity.ok(response);
}
catch (IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
}
catch (Exception e) {
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}

/**
* Endpoint to make a move in the game specified with the same gameId.
*
*
* @param request - PlayerMoveRequest for what game to make a move in and what move to make
* @return GameResponse with the game id, status and board
*/
Expand All @@ -56,19 +55,17 @@ public ResponseEntity<GameResponse> playerMove(@RequestBody MoveRequest request)
try {
GameResponse response = gameService.playerMove(request);
return ResponseEntity.ok(response);
}
catch (IllegalArgumentException | IllegalStateException e) {
} catch (IllegalArgumentException | IllegalStateException e) {
return ResponseEntity.badRequest().build();

}
catch (Exception e) {
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}

/**
* Endpoint to getting bots move in the game.
*
*
* @param gameId - GameId with the id of the game to make a move in
* @return GameResponse with the game id, status and board
*/
Expand All @@ -78,12 +75,10 @@ public ResponseEntity<GameResponse> botMove(@RequestBody GameId gameId) {
String id = gameId.getGameId();
GameResponse response = gameService.botMove(id);
return ResponseEntity.ok(response);
}
catch (IllegalArgumentException | IllegalStateException e) {
} catch (IllegalArgumentException | IllegalStateException e) {
logger.warning("Bot move request: id:" + gameId + " error:" + e.getMessage());
return ResponseEntity.badRequest().build();
}
catch (Exception e) {
} catch (Exception e) {
logger.severe("Bot move request: id:" + gameId + " error:" + e.getMessage());
return ResponseEntity.internalServerError().build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package board.master.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.ResponseEntity;

@RestController
@RequestMapping("/api")
Expand Down
4 changes: 2 additions & 2 deletions backend/src/main/java/board/master/model/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

/**
* Abstract class representing a generic action or move in a game.
*
*
* This class serves as a base for defining specific actions in different games.
* Actions can include things like moving a piece in a board game, selecting an
* option in a menu,
* or any other form of interaction within the game's rules and mechanics.
*
*
* Subclasses of this class should implement specific details and behaviors
* relevant to the particular
* action they represent. This could include, for example, the starting and
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/java/board/master/model/StateHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public interface StateHandler {

/**
* Returns the board object associated with the current state.
*
*
* @return the board object associated with the current state.
*/
public Board getBoard();
Expand Down
5 changes: 2 additions & 3 deletions backend/src/main/java/board/master/model/agents/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
/**
* The Agent interface represents an agent that can play a game.
* It shall choose the best move to make in a given game state.
*
*
* The Agent follows the strategy pattern:
* https://en.wikipedia.org/wiki/Strategy_pattern
*/
public interface Agent {

/**
* Calculates the best move to make in a given game state.
*
*
* @param state The current state of the game.
* @return The best action to take in the current state.
*/
public Action getAction(StateHandler state);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
*/
public class AgentFactory {

private final static HashMap<String, Agent> agents = initializeAvailableAgents();

private static final HashMap<String, Agent> agents = initializeAvailableAgents();

/**
* Creates an instance of a game-playing agent based on the specified strategy.
*
*
* Supported agent types are listed in the {@link #getAvailableAgentTypesList()} method.
*
*
* @param strategyType - the strategy type for which to create an agent
* @return Agent instance implementing the specified strategy
* @throws IllegalArgumentException if the specified strategy type is unsupported
Expand All @@ -30,7 +29,7 @@ public static Agent createAgent(String agentStrategy) throws IllegalArgumentExce

/**
* Checks if a given strategy type is supported for agent creation.
*
*
* @param strategyType - the strategy type to check
* @return true if the strategy type is supported, false otherwise
*/
Expand All @@ -40,7 +39,7 @@ public static boolean isValidAgentType(String strategyType) {

/**
* Retrieves a list of supported strategy types for game-playing agents.
*
*
* @return List of supported strategy types
*/
public static List<String> getAvailableAgentTypesList() {
Expand All @@ -49,7 +48,7 @@ public static List<String> getAvailableAgentTypesList() {

/**
* Initializes and returns a map of strategy types to their corresponding Agent instances.
*
*
* @return HashMap mapping strategy types to Agent instances
*/
private static HashMap<String, Agent> initializeAvailableAgents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class AlphaBetaPruningMinimax implements Agent {

private int agentPlayerId;

/**
* Returns the best action to take in the current state.
* It evaluates possible actions and chooses the one with the highest value
Expand All @@ -33,7 +33,6 @@ public Action getAction(StateHandler state) {
if (currentBestMaximizer < value) {
currentBestMaximizer = value;
currentBestAction = action;

}
}

Expand All @@ -58,25 +57,23 @@ private float evaluateState(StateHandler state, float alpha, float beta, Boolean

float value;
if (isMaximizingPlayer) {
value = alpha;
for (Action action : state.getActions()) {
value = Math.max(value, evaluateState(state.result(action), value, beta, !isMaximizingPlayer));
if (value >= beta) {
break;
}
value = alpha;
for (Action action : state.getActions()) {
value = Math.max(value, evaluateState(state.result(action), value, beta, !isMaximizingPlayer));
if (value >= beta) {
break;
}
return value;
}
else {
value = beta;
for (Action action : state.getActions()) {
value = Math.min(value, evaluateState(state.result(action), alpha, value, !isMaximizingPlayer));
if (alpha >= value) {
break;
}
}
return value;
} else {
value = beta;
for (Action action : state.getActions()) {
value = Math.min(value, evaluateState(state.result(action), alpha, value, !isMaximizingPlayer));
if (alpha >= value) {
break;
}
return value;
}
return value;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Comparator;

import board.master.model.Action;
import board.master.model.StateHandler;

/**
* An implementation of the Iterative Deepening Alpha-Beta Pruning Minimax algorithm.
* This class functions as an agent in turn-based board games. It employs the
* Iterative Deepening technique to incrementally deepen the search depth,
* This class functions as an agent in turn-based board games. It employs the
* Iterative Deepening technique to incrementally deepen the search depth,
* combined with Alpha-Beta pruning to optimize the Minimax search process.
* The agent selects the best move based on the current state of the game.
*/
Expand All @@ -23,10 +23,9 @@ public class IterativeDeepeningAlphaBetaPruningMinimax implements Agent {
private long startTime;

public IterativeDeepeningAlphaBetaPruningMinimax(long maxTime) {
this.maxTime = maxTime*1000;
this.maxTime = maxTime * 1000;
}


/**
* Determines the best action to take in the given game state.
* This method iteratively deepens the search depth and uses alpha-beta pruning
Expand All @@ -51,13 +50,14 @@ public Action getAction(StateHandler state) {
List<Action> priorityActions = actions.stream()
.sorted(Comparator.comparingInt(action -> state.result(action).utility(agentPlayerId)))
.collect(Collectors.toList());

Collections.reverse(priorityActions);

while (!isTimeUp()) {
//Find the best move ordering
// Find the best move ordering
for (Action action : priorityActions) {
float value = evaluateState(state.result(action), currentBestMaximizer, currentBestMinimizer, depth, false);
float value =
evaluateState(state.result(action), currentBestMaximizer, currentBestMinimizer, depth, false);
if (currentBestMaximizer < value) {
currentBestMaximizer = value;
currentBestAction = action;
Expand Down Expand Up @@ -85,18 +85,19 @@ private float evaluateState(StateHandler state, float alpha, float beta, int dep
Collections.reverse(priorityActions);
value = alpha;
for (Action action : priorityActions) {
value = Math.max(value, evaluateState(state.result(action), value, beta, minusDepth, !isMaximizingPlayer));
value = Math.max(
value, evaluateState(state.result(action), value, beta, minusDepth, !isMaximizingPlayer));
if (value >= beta) {
break;
}
}

return value;
}
else {
} else {
value = beta;
for (Action action : priorityActions) {
value = Math.min(value, evaluateState(state.result(action), alpha, value, minusDepth, !isMaximizingPlayer));
value = Math.min(
value, evaluateState(state.result(action), alpha, value, minusDepth, !isMaximizingPlayer));
if (alpha >= value) {
break;
}
Expand All @@ -108,5 +109,4 @@ private float evaluateState(StateHandler state, float alpha, float beta, int dep
private boolean isTimeUp() {
return Calendar.getInstance().getTimeInMillis() - startTime >= maxTime;
}

}
Loading

0 comments on commit d3e61bd

Please sign in to comment.