Skip to contentMethod: getState()
      1: /*
2:  * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3:  *
4:  * This file is part of ipspiel24-VierConnects-core.
5:  *
6:  * ipspiel24-VierConnects-core is free software: you can redistribute it and/or modify it under
7:  * the terms of the GNU General Public License as published by the Free Software
8:  * Foundation, either version 3 of the License, or (at your option) any later
9:  * version.
10:  *
11:  * ipspiel24-VierConnects-core is distributed in the hope that it will be useful, but WITHOUT
12:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13:  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14:  * details.
15:  *
16:  * You should have received a copy of the GNU General Public License along with
17:  * ipspiel24-VierConnects-core. If not, see <http://www.gnu.org/licenses/>.
18:  */
19: package de.fhdw.gaming.ipspiel24.VierConnects.core.domain.impl;
20: 
21: import java.util.List;
22: import java.util.Objects;
23: import java.util.Optional;
24: 
25: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsField;
26: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsFieldState;
27: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsRow;
28: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsRowType;
29: 
30: /**
31:  * Implements {@link VierConnectsRow}.
32:  */
33: public final class VierConnectsRowImpl implements VierConnectsRow {
34: 
35:     /**
36:      * The type of this row.
37:      */
38:     private final VierConnectsRowType type;
39: 
40:     /**
41:      * The zero-based index of this row.
42:      */
43:     private final int index;
44: 
45:     /**
46:      * The fields of this row.
47:      */
48:     private final List<? extends VierConnectsField> fields;
49: 
50:     /**
51:      * Creates a row.
52:      *
53:      * @param type   The type of this row.
54:      * @param index  The zero-based index of this row.
55:      * @param fields The fields of this row.
56:      */
57:     public VierConnectsRowImpl(final VierConnectsRowType type, final int index,
58:             final List<? extends VierConnectsField> fields) {
59:         this.type = type;
60:         this.index = index;
61:         this.fields = fields;
62:     }
63: 
64:     @Override
65:     public boolean equals(final Object obj) {
66:         if (obj instanceof VierConnectsRowImpl) {
67:             final VierConnectsRowImpl other = (VierConnectsRowImpl) obj;
68:             return this.type.equals(other.type) && this.index == other.index && this.fields.equals(other.fields);
69:         }
70:         return false;
71:     }
72: 
73:     @Override
74:     public int hashCode() {
75:         return Objects.hash(this.type, this.index, this.fields);
76:     }
77: 
78:     @Override
79:     public String toString() {
80:         return String.format("%s@%d%s", this.type, this.index, this.fields);
81:     }
82: 
83:     @Override
84:     public VierConnectsRowType getType() {
85:         return this.type;
86:     }
87: 
88:     @Override
89:     public int getIndex() {
90:         return this.index;
91:     }
92: 
93:     @Override
94:     public List<? extends VierConnectsField> getFields() {
95:         return this.fields;
96:     }
97: //    Old version of TicTacToe
98: //    @Override
99: //    public Optional<VierConnectsFieldState> getState() {
100: //        VierConnectsFieldState lastState = null;
101: //        for (final VierConnectsField field : this.fields) {
102: //            if (lastState == null) {
103: //                lastState = field.getState();
104: //            } else if (!lastState.equals(field.getState())) {
105: //                return Optional.empty();
106: //            }
107: //        }
108: //        return Optional.ofNullable(lastState); // may be empty if row is
109: //    }
110:     
111:     @Override
112:     public Optional<VierConnectsFieldState> getState() {
113:         VierConnectsFieldState lastState = null;
114:         int count  = 0;
115:•        for (final VierConnectsField field : this.fields) {
116:•            if (lastState == null) {
117:                 lastState = field.getState();
118:•            } else if (!lastState.equals(field.getState())) {
119:                 lastState = field.getState();
120:                 count = 0;
121:             }
122:             count++;
123:•            if (count >= 4 && !lastState.equals(VierConnectsFieldState.EMPTY)) {
124:                 return Optional.ofNullable(lastState);
125:             }
126:         }
127:         return Optional.empty();
128:     }
129: }