-
Notifications
You must be signed in to change notification settings - Fork 0
/
VierGewinnt.java
232 lines (196 loc) · 7.79 KB
/
VierGewinnt.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* ************************************************************************* *\
* Programmierung 1 HS 2018 - Serie 5-1
*. Raphaela Seeger 16-113-441
* Anastasija Novikova 16-825-390
\* ************************************************************************* */
import java.util.Arrays;
import java.util.Scanner;
public class VierGewinnt
{
public static final int COLS = 7;
public static final int ROWS = 6;
private Token[][] board = new Token[ COLS ][ ROWS ]; // 7 columns with 6 fields each
private IPlayer[] players = new IPlayer[ 2 ]; // two players
/** initialize board and players and start the game */
public void play()
{
// initialize the board
for ( Token[] column : this.board ) {
Arrays.fill( column, Token.empty );
}
/* initialize players */
players[ 0 ] = new HumanPlayer();
System.out.print( "Play against a human opponent? (y / n) " );
String opponent = new Scanner( System.in ).nextLine().toLowerCase();
while ( ( 1 != opponent.length() ) || ( -1 == ( "yn".indexOf ( opponent ) ) ) ) {
System.out.print( "Can't understand your answer. Play against a human opponent? (y / n) " );
opponent = new Scanner( System.in ).nextLine().toLowerCase();
}
if ( opponent.equals( "y" ) ) {
players[ 1 ] = new HumanPlayer();
} else {
players[ 1 ] = new ComputerPlayer();
}
players[ 0 ].setToken( Token.player1 );
players[ 1 ].setToken( Token.player2 );
/* play... */
boolean solved = false;
int currentPlayer = ( new java.util.Random() ).nextInt( 2 ); //choose randomly who begins
System.out.println( "current player: " + currentPlayer );
int insertCol, insertRow; // starting from 0
while ( !solved && !this.isBoardFull() ) {
// get player's next "move"
// note that we pass only a copy of the board as an argument,
// otherwise the player would be able to manipulate the board and cheat!
insertCol = players[ currentPlayer ].getNextColumn( getCopyOfBoard() );
// insert the token and get the row where it landed
insertRow = this.insertToken( insertCol, players[ currentPlayer ].getToken() );
// check if the game is over
solved = this.checkVierGewinnt( insertCol, insertRow );
//switch to other player
if ( !solved )
currentPlayer = ( currentPlayer + 1 ) % 2;
}
System.out.println( displayBoard( this.board ) );
if ( solved )
System.out.println( "Player " + players[ currentPlayer ].getToken() + " wins!" );
else
System.out.println( "Draw! Game over." );
}
/**
* Inserts the token at the specified column (if possible)
* @param column the column to insert the token
* @param token the players token
* @return the row where the token landed
*/
private int insertToken( int column, Token tok )
{
//TODO: Your code goes here
int depth = ROWS -1;
while ( depth >= 0 && board[column][depth] == Token.empty)
{depth--;}
board [column][depth+ 1] = tok;
return depth +1; //TODO: Replace this linec
}
/**
* Checks if every position is occupied
* @returns true, if the board is full.
*/
private boolean isBoardFull()
{
//TODO: Your code goes here
int topRow = board [0].length -1;
for (int column = 0; column < COLS ; column ++)
{
if (board[column][topRow] == Token.empty)
{
return false;
}
}
return true;
}
/**
* Checks for at least four equal tokens in a row in
* either direction, starting from the given position.
*/
private boolean checkVierGewinnt( int col, int row )
{
//TODO: Your code goes here
//column 4 equals
for (int depth = 0; depth < ROWS-3 ; depth ++) {
for (int column = 0; column < COLS ; column ++)
{
if (board[column][depth] != Token.empty &&
board [column][depth] == board [column][depth +1]&&
board [column][depth] == board [column][depth +2]&&
board [column][depth] == board [column][depth +3]
)
{
return true;
}
}
}
//rows 4 equals
for (int column = 0; column < COLS -4 ; column ++) {
for (int depth = 0; depth < ROWS ; depth ++)
{
if (board[column][depth] != Token.empty &&
board [column][depth] == board [column +1][depth]&&
board [column][depth] == board [column +2][depth]&&
board [column][depth] == board [column +3][depth]
)
{
return true;
}
}
}
//one way vertical
for (int column=0; column < COLS-4; column++)
{
for (int depth =0; depth < ROWS -3; depth++)
{
if (board[column][depth] != Token.empty &&
board [column][depth] == board [column+1][depth +1]&&
board [column][depth] == board [column+2][depth +2]&&
board [column][depth] == board [column+3][depth +3]
)
{
return true;
}
}
for(int depth = ROWS -3; depth < ROWS; depth++)
{
if (board[column][depth] != Token.empty &&
board [column][depth] == board [column +1][depth -1]&&
board [column][depth] == board [column +2][depth -2]&&
board [column][depth] == board [column +3][depth -3]
)
{
return true;
}
}
}
return false;
//ana return sol
//TODO: Replace this line!
}
/** Returns a (deep) copy of the board array */
private Token[][] getCopyOfBoard()
{
Token[][] copiedBoard = new Token[ COLS ][ ROWS ];
for ( int i = 0; i < copiedBoard.length; i++ ) {
for ( int j = 0; j < copiedBoard[ i ].length; j++ ) {
copiedBoard[ i ][ j ] = this.board[ i ][ j ];
}
}
return copiedBoard;
}
/** returns a graphical representation of the board */
public static String displayBoard( Token[][] myBoard )
{
String rowDelimiter = "+";
String rowNumbering = " ";
for ( int col = 0; col < myBoard.length; col++ ) {
rowDelimiter += "---+";
rowNumbering += " " + ( col + 1 ) + " ";
}
rowDelimiter += "\n";
String rowStr;
String presentation = rowDelimiter;
for ( int row = myBoard[ 0 ].length - 1; row >= 0; row-- ) {
rowStr = "| ";
for ( int col = 0; col < myBoard.length; col++ ) {
rowStr += myBoard[ col ][ row ].toString() + " | ";
}
presentation += rowStr + "\n" + rowDelimiter;
}
presentation += rowNumbering;
return presentation;
}
/** main method, starts the program */
public static void main( String args[] )
{
VierGewinnt game = new VierGewinnt();
game.play();
}
}