Skip to content

Method: toString()

1: package de.fhdw.gaming.ipspiel22.vierGewinnt.moves.impl;
2:
3: import java.util.Objects;
4:
5: import de.fhdw.gaming.core.domain.GameException;
6: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGAnswerEnum;
7: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGFieldState;
8: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGPlayer;
9: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGPosition;
10: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGState;
11:
12: /**
13: * Represents a move which places a token on the board.
14: * <p>
15: * Note that this move is only allowed if the colour of the token placed belongs to the corresponding player, and if the
16: * move leads to at least one other token to be flipped.
17: */
18: final class VGPlaceTokenMove extends AbstractVGMove {
19:
20: /**
21: * {@code true} if a red token is placed, and {@code false} if a yellow token is placed.
22: */
23: private final boolean placingRedToken;
24: /**
25: * The position of the token placed on the board.
26: */
27: private final VGPosition tokenPosition;
28:
29: /**
30: * Creates a {@link VGPlaceTokenMove} object.
31: *
32: * @param placingRedToken {@code true} if a red token is placed, and {@code false} if a yellow token is placed.
33: * @param tokenPosition The position of the token placed on the board.
34: */
35: VGPlaceTokenMove(final boolean placingRedToken, final VGPosition tokenPosition) {
36: this.placingRedToken = placingRedToken;
37: this.tokenPosition = Objects.requireNonNull(tokenPosition, "tokenPosition");
38: }
39:
40: /**
41: * Returns {@code true} if a red token is placed, and {@code false} if a yellow token is placed.
42: */
43: boolean isPlacingRedToken() {
44: return this.placingRedToken;
45: }
46:
47: /**
48: * Returns the position of the token placed on the board.
49: */
50: VGPosition getTokenPosition() {
51: return this.tokenPosition;
52: }
53:
54: @Override
55: public void applyTo(final VGState state, final VGPlayer player) throws GameException {
56: if (this.isPlacingRedToken() != player.isUsingRedChips()) {
57: throw new GameException(
58: String.format(
59: "Player %s cannot place a %s token.",
60: player,
61: this.isPlacingRedToken() ? VGFieldState.RED : VGFieldState.YELLOW));
62: }
63:
64: state.getBoard().getNextFieldInColumn(this.toAnswerEnum()).placeToken(player.isUsingRedChips());
65: }
66:
67: /**
68: * Translate an integer from 1 to 7 into a VGAnswerEnum.
69: *
70: * @return VGAnswerEnum.
71: */
72: private VGAnswerEnum toAnswerEnum() {
73: if (this.tokenPosition.getColumn() == 0) {
74: return VGAnswerEnum.FIRSTCOLUMN;
75: } else if (this.tokenPosition.getColumn() == 1) {
76: return VGAnswerEnum.SECONDCOLUMN;
77: } else if (this.tokenPosition.getColumn() == 2) {
78: return VGAnswerEnum.THIRDCOLUMN;
79: } else if (this.tokenPosition.getColumn() == 3) {
80: return VGAnswerEnum.FOURTHCOLUMN;
81: } else if (this.tokenPosition.getColumn() == 4) {
82: return VGAnswerEnum.FITFHCOLUMN;
83: } else if (this.tokenPosition.getColumn() == 5) {
84: return VGAnswerEnum.SIXTHCOLUMN;
85: } else {
86: return VGAnswerEnum.SEVENTHCOLUMN;
87: }
88: }
89:
90: @Override
91: public String toString() {
92: return String.format(
93: "Placing %s token on field at %s",
94:• this.placingRedToken ? "red" : "yellow",
95: this.tokenPosition);
96: }
97:
98: @Override
99: public int getColumnInt() {
100: return 0;
101: }
102:
103: }