Skip to content

Commit

Permalink
house keeping
Browse files Browse the repository at this point in the history
  • Loading branch information
pfichtner committed Nov 5, 2023
1 parent d51238e commit 160918b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
32 changes: 18 additions & 14 deletions core/src/main/java/org/ase/fourwins/board/Board.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.ase.fourwins.board;

import static java.util.Arrays.stream;
import static java.util.Collections.emptyList;
import static java.util.Spliterator.ORDERED;
import static java.util.Spliterators.spliteratorUnknownSize;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static java.util.stream.IntStream.range;
import static java.util.stream.Stream.concat;
import static java.util.stream.StreamSupport.stream;
import static org.ase.fourwins.board.Board.Direction.EAST;
Expand All @@ -22,21 +24,21 @@
import static org.ase.fourwins.board.Board.Score.WIN;
import static org.ase.fourwins.board.Coordinate.xy;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import lombok.Builder;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import lombok.With;

public abstract class Board {

@RequiredArgsConstructor
enum Direction {
NORTH(c -> c.mutateY(-1)), //
SOUTH(c -> c.mutateY(+1)), //
Expand All @@ -49,12 +51,8 @@ enum Direction {

private final Function<Coordinate, Coordinate> mutator;

private Direction(Direction d1, Direction d2) {
this(d1.mutator.andThen(d2.mutator));
}

private Direction(Function<Coordinate, Coordinate> mutator) {
this.mutator = mutator;
private Direction(Direction direction1, Direction direction2) {
this(direction1.mutator.andThen(direction2.mutator));
}

public Coordinate mutate(Coordinate coordinate) {
Expand All @@ -64,6 +62,7 @@ public Coordinate mutate(Coordinate coordinate) {
}

@Value
// fails @RequiredArgsConstructor(staticName = "fromTo")
static class Line {

Direction from, to;
Expand Down Expand Up @@ -208,16 +207,21 @@ public boolean isFull() {

}

private static final List<Line> lines = List.of(fromTo(NORTH, SOUTH), fromTo(WEST, EAST),
fromTo(SOUTHWEST, NORTHEAST), fromTo(NORTHWEST, SOUTHEAST));
private static final List<Line> lines = List.of( //
fromTo(NORTH, SOUTH), //
fromTo(WEST, EAST), //
fromTo(SOUTHWEST, NORTHEAST), //
fromTo(NORTHWEST, SOUTHEAST) //
);

private final BoardInfo boardInfo;
private final Column[] columns;
private GameState gameState = GameState.builder().score(IN_GAME).build();

private PlayableBoard(BoardInfo boardInfo) {
this.boardInfo = boardInfo;
this.columns = IntStream.range(0, boardInfo.getColumns()).mapToObj(i -> new Column(boardInfo.getRows()))
int rowCount = boardInfo.getRows();
this.columns = range(0, boardInfo.getColumns()).mapToObj(__ -> rowCount).map(Column::new)
.toArray(Column[]::new);
}

Expand Down Expand Up @@ -259,12 +263,12 @@ public Board insertToken(Move move, Object token) {
return isDraw(column) ? new DrawBoard(boardInfo()) : this;
}

private boolean isDraw(Column column) {
return column.isFull() && allColumnsFull();
private boolean isDraw(Column tokenPlacedInColumn) {
return tokenPlacedInColumn.isFull() && allColumnsFull();
}

private boolean allColumnsFull() {
return Arrays.stream(columns).allMatch(Column::isFull);
return stream(columns).allMatch(Column::isFull);
}

private Stream<Coordinate> connectedTokens(Coordinate center, Object token, Line line) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,42 @@
import org.ase.fourwins.tournament.listener.TournamentListener;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

public class DefaultTournament implements Tournament {

@Getter
private BoardInfo boardInfo = BoardInfo.sevenColsSixRows;

@RequiredArgsConstructor
static final class CoffeebreakGame implements Game {

static final String COFFEE_BREAK_WIN_MESSAGE = "coffee break";

private static final BoardInfo boardInfo = BoardInfo.builder().columns(0).rows(0).build();

private static final GameState gameState = GameState.builder().score(WIN).reason(COFFEE_BREAK_WIN_MESSAGE)
private static final GameState gameStateTemplate = GameState.builder().score(WIN).reason(COFFEE_BREAK_WIN_MESSAGE)
.build();

private final Player other;

@Getter
private final GameId id;

private final GameState gameState;

private final List<Player> players;

CoffeebreakGame(Player other, GameId id) {
this.id = id;
this.gameState = gameStateTemplate.withToken(other.getToken());
this.players = List.of(other);
}

@Override
public BoardInfo getBoardInfo() {
return boardInfo;
}

@Override
public GameState gameState() {
return gameState.withToken(other.getToken());
return gameState;
}

@Override
Expand All @@ -64,7 +70,7 @@ public Game runGame() {

@Override
public List<Player> getPlayers() {
return List.of(other);
return players;
}
}

Expand Down

0 comments on commit 160918b

Please sign in to comment.