Merge pull request #4 from MalteT/redo-output
[WIP] Did some comments and minor change.
This commit is contained in:
commit
e32eafe3a0
|
@ -30,9 +30,11 @@ public class Game {
|
||||||
private final Player p2;
|
private final Player p2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The board on which is played.
|
* The game's board.
|
||||||
* [GAME_COLUMNS] columns, [GAME_ROWS] rows
|
* It's size is GAME_COLUMNS * GAME_ROWS.
|
||||||
* " " - Empty Space
|
* 0 - Empty Space
|
||||||
|
* 1 - Player One's piece
|
||||||
|
* 2 - Player Two's piece
|
||||||
*/
|
*/
|
||||||
private int[][] board;
|
private int[][] board;
|
||||||
|
|
||||||
|
@ -49,7 +51,7 @@ public class Game {
|
||||||
this.p1 = p1;
|
this.p1 = p1;
|
||||||
this.p2 = p2;
|
this.p2 = p2;
|
||||||
this.board = new int [GAME_COLUMNS][GAME_ROWS];
|
this.board = new int [GAME_COLUMNS][GAME_ROWS];
|
||||||
gameOn = true;
|
this.gameOn = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -67,6 +69,7 @@ public class Game {
|
||||||
second = this.p1;
|
second = this.p1;
|
||||||
}
|
}
|
||||||
boolean turn = true;
|
boolean turn = true;
|
||||||
|
this.gameOn = true;
|
||||||
while(gameOn) {
|
while(gameOn) {
|
||||||
if(turn){
|
if(turn){
|
||||||
makeMove(first,turn);
|
makeMove(first,turn);
|
||||||
|
@ -90,19 +93,21 @@ public class Game {
|
||||||
*/
|
*/
|
||||||
private void makeMove(Player p, boolean turn) {
|
private void makeMove(Player p, boolean turn) {
|
||||||
log(p.getName() + " makes a move!");
|
log(p.getName() + " makes a move!");
|
||||||
|
// Get a choice from the player, while only giving him a copy of the game.
|
||||||
int choice = p.move(copyBoard());
|
int choice = p.move(copyBoard());
|
||||||
|
// Check his choice against the current board.
|
||||||
if (choice < 0 || choice > GAME_COLUMNS || this.board[choice][0] != 0) {
|
if (choice < 0 || choice > GAME_COLUMNS || this.board[choice][0] != 0) {
|
||||||
|
// If a player makes a false move, the game punishes him by.
|
||||||
this.gameOn = false;
|
this.gameOn = false;
|
||||||
log(p.getName() + " made an illegal move and lost!");
|
log(p.getName() + " made an illegal move and lost!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Find the lowest empty field in the board in the choosen column.
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
while (pos < (board[0].length-1) && this.board[choice][pos + 1] == 0) {
|
while (pos < (board[0].length - 1) && this.board[choice][pos + 1] == 0) {
|
||||||
pos++;
|
pos++;
|
||||||
}
|
}
|
||||||
|
// Change the board accordingly.
|
||||||
if(turn){
|
if(turn){
|
||||||
this.board[choice][pos] = 1;
|
this.board[choice][pos] = 1;
|
||||||
} else {
|
} else {
|
||||||
|
@ -111,10 +116,16 @@ public class Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Checks the board for game-ending conditions.
|
||||||
|
* The game is won, if one of the players manages to get four
|
||||||
|
* of his pieces in a row.
|
||||||
|
* The game is drawn when the board is full, that is, when every
|
||||||
|
* one of the 42 pieces are played.
|
||||||
|
* The function changes variables accordingly.
|
||||||
*/
|
*/
|
||||||
private void checkState() {
|
private void checkState() {
|
||||||
// --- CHECK WIN ---
|
// --- CHECK WIN ---
|
||||||
|
// Check rows
|
||||||
outer:for (int i = 0; i < GAME_COLUMNS; i++) {
|
outer:for (int i = 0; i < GAME_COLUMNS; i++) {
|
||||||
int count1 = 0;
|
int count1 = 0;
|
||||||
int count2 = 0;
|
int count2 = 0;
|
||||||
|
@ -141,6 +152,7 @@ public class Game {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Check columns
|
||||||
outer:for (int j = 0; j < GAME_ROWS; j++) {
|
outer:for (int j = 0; j < GAME_ROWS; j++) {
|
||||||
int count1 = 0;
|
int count1 = 0;
|
||||||
int count2 = 0;
|
int count2 = 0;
|
||||||
|
@ -167,6 +179,7 @@ public class Game {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Check diagonals from lower left to upper right.
|
||||||
boolean reverse = false;
|
boolean reverse = false;
|
||||||
int count1 = 0;
|
int count1 = 0;
|
||||||
int count2 = 0;
|
int count2 = 0;
|
||||||
|
@ -218,6 +231,7 @@ public class Game {
|
||||||
break outer;
|
break outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Check diagonals from lower right to upper left.
|
||||||
reverse = false;
|
reverse = false;
|
||||||
count1 = 0;
|
count1 = 0;
|
||||||
count2 = 0;
|
count2 = 0;
|
||||||
|
@ -271,7 +285,6 @@ public class Game {
|
||||||
break outer;
|
break outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- CHECK DRAW ---
|
// --- CHECK DRAW ---
|
||||||
boolean draw = true;
|
boolean draw = true;
|
||||||
outer:for (int i = 0; i < GAME_COLUMNS; i++) {
|
outer:for (int i = 0; i < GAME_COLUMNS; i++) {
|
||||||
|
@ -288,9 +301,9 @@ public class Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deep copies the board.
|
* Creates a deep copy of the board.
|
||||||
*
|
*
|
||||||
* @return A deep copy of the board
|
* @return A deep copy of the board.
|
||||||
*/
|
*/
|
||||||
private int[][] copyBoard() {
|
private int[][] copyBoard() {
|
||||||
int[][] ret = new int[GAME_COLUMNS][GAME_ROWS];
|
int[][] ret = new int[GAME_COLUMNS][GAME_ROWS];
|
||||||
|
@ -310,8 +323,8 @@ public class Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints the current board to stdout. The Player that started the game gets 'X' the other one
|
* Prints the current board to stdout.
|
||||||
* 'O'.
|
* The Player that started the game gets 'X' the other one 'O'.
|
||||||
*/
|
*/
|
||||||
private void logGame() {
|
private void logGame() {
|
||||||
for(int i=0;i<board[0].length;i++){
|
for(int i=0;i<board[0].length;i++){
|
||||||
|
|
Loading…
Reference in a new issue