Skip to content
Snippets Groups Projects
Commit 7ca580bd authored by Goik Martin's avatar Goik Martin
Browse files

Cosmetics

parent dce38440
No related branches found
No related tags found
No related merge requests found
package de.hdm_stuttgart.mi.sd1.tictactoe;
/**
*
*
*/
public class Board {
/**
*
*
*/
public enum Player {
PLAYER1("Jim", 'O'), PLAYER2("Eve", 'X');
/**
*
*/
PLAYER1("Jim", 'O'), /**
*
*/
PLAYER2("Eve", 'X');
/**
*
*/
public final String nickname;
/**
*
*/
public final char representation;
Player(final String nickname, final char representation) {
......@@ -14,6 +34,9 @@ public class Board {
this.representation = representation;
}
/**
* @return The opponent
*/
public Player getOtherPlayer() {
switch (this) {
case PLAYER1:
......@@ -31,7 +54,14 @@ public class Board {
}
}
public final short width = 3, height = 3; // 3 x 3 board
/**
*
*/
public final short width = 3;
/**
*
*/
public final short height = 3; // 3 x 3 board
private short numberOfMoves = 0; // Game will be over after a maximum of 9 moves
......@@ -39,6 +69,9 @@ public class Board {
final Player[][] board = new Player[width][height]; // Allocating a two dimensional array, yet empty.
/**
*
*/
public Board() {
for (int x = 0; x < width; x++) { // Initialize board with null values (still empty).
for (int y = 0; y < height; y++) {
......@@ -108,6 +141,9 @@ public class Board {
}
}
/**
* Provide numbering hint to user, see {@link #nextMove(int)}
*/
public void printNumberingHint() {
System.out.println("Numbering scheme:\n");
for (int y = 0; y < height; y++) {
......@@ -124,7 +160,7 @@ public class Board {
}
/**
* Get the player instance which will do the next move. See {@link #nextMove(short)}
* Get the player instance which will do the next move. See {@link #nextMove(int)}
*
* @return the currently active player.
*/
......
......@@ -13,33 +13,33 @@ public class TicTacToe {
* Unused
*/
public static void main(String[] args) {
// See https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
try (final Scanner scan = new Scanner(System.in)) {
final Scanner scan = new Scanner(System.in);
final Board board = new Board();
final Board board = new Board();
board.printNumberingHint();
board.printNumberingHint();
Board.Player winner;
do {
Board.Player winner;
do {
System.out.print("\n\n" + board.getCurrentActivePlayer().nickname + ", please enter next field's number:");
final short nextField = scan.nextShort();
final String errorMessage = board.nextMove(nextField);
if (null == errorMessage) {
break; // We made it, quit loop.
} else {
System.err.println(errorMessage);
}
} while (true);
board.print();
} while (null == (winner = board.evaluateWinner()) && !board.allMovesFinished());
if (null == winner) {
System.out.println("\n\nGame over: draw");
} else {
System.out.println("\n\nCongratulations, " + winner.nickname);
do {
System.out.print("\n\n" + board.getCurrentActivePlayer().nickname + ", please enter next field's number:");
final short nextField = scan.nextShort();
final String errorMessage = board.nextMove(nextField);
if (null == errorMessage) {
break; // We made it, quit loop.
} else {
System.err.println(errorMessage);
}
} while (true);
board.print();
} while (null == (winner = board.evaluateWinner()) && !board.allMovesFinished());
if (null == winner) {
System.out.println("\n\nGame over: draw");
} else {
System.out.println("\n\nCongratulations, " + winner.nickname);
}
}
scan.close();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment