Merge pull request #5 from MalteT/redo-output

Redo output
This commit is contained in:
MalteT 2017-10-15 14:22:29 +02:00 committed by GitHub
commit c5f26606f9
7 changed files with 151 additions and 30 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
doc/

View file

@ -13,6 +13,9 @@ LIBARIES=
CLASSES=\
src/game/Main.java \
src/game/Game.java \
src/game/GameHistory.java \
src/game/GameEntry.java \
src/game/PlayerObject.java \
src/player/Player.java \
src/player/Player1.java \
src/player/Player2.java

View file

@ -22,12 +22,22 @@ public class Game {
/**
* Player One
*/
private final Player p1;
private final PlayerObject p1;
/**
* Player Two
*/
private final Player p2;
private final PlayerObject p2;
/**
* The current player.
*/
private PlayerObject currentP;
/**
* The game's history.
*/
private final GameHistory hist;
/**
* The game's board.
@ -46,10 +56,14 @@ public class Game {
/**
* Constructor.
* Initialize board and save players.
*
* @param p1 Player One
* @param p2 Player Two
*/
public Game(Player p1, Player p2) {
this.p1 = p1;
this.p2 = p2;
this.p1 = new PlayerObject(1, p1);
this.p2 = new PlayerObject(2, p2);
this.hist = new GameHistory();
this.board = new int [GAME_COLUMNS][GAME_ROWS];
this.gameOn = false;
}
@ -58,26 +72,19 @@ public class Game {
* Starts the game.
*/
public void start() {
// Set random starting player.
Random ran = new Random();
Player first;
Player second;
if (ran.nextBoolean()) {
first = this.p1;
second = this.p2;
currentP = this.p1;
} else {
first = this.p2;
second = this.p1;
currentP = this.p2;
}
boolean turn = true;
// Start the game
this.gameOn = true;
while(gameOn) {
if(turn){
makeMove(first,turn);
} else {
makeMove(second,turn);
}
makeMove();
currentP = other(currentP);
if(gameOn) {
turn = !turn;
logGame();
checkState();
} else {
@ -88,18 +95,31 @@ public class Game {
}
}
/**
* Returns the opponent of the given player.
*
* @param p The player.
* @return The opponent of the given player.
*/
private PlayerObject other(PlayerObject p) {
if (p1.equals(p)) {
return p2;
} else {
return p1;
}
}
/**
* Calls a players functions to make a move.
*/
private void makeMove(Player p, boolean turn) {
log(p.getName() + " makes a move!");
private void makeMove() {
// Get a choice from the player, while only giving him a copy of the game.
int choice = p.move(copyBoard());
int choice = currentP.getP().move(copyBoard());
// Check his choice against the current board.
if (choice < 0 || choice > GAME_COLUMNS || this.board[choice][0] != 0) {
// If a player makes a false move, the game punishes him by.
// If a player makes a false move, the game punishes him.
this.gameOn = false;
log(p.getName() + " made an illegal move and lost!");
log(currentP.getP().getName() + " made an illegal move and lost!");
return;
}
// Find the lowest empty field in the board in the choosen column.
@ -108,11 +128,9 @@ public class Game {
pos++;
}
// Change the board accordingly.
if(turn){
this.board[choice][pos] = 1;
} else {
this.board[choice][pos] = 2;
}
this.board[choice][pos] = currentP.getID();
this.hist.add(new GameEntry(currentP, choice, pos));
log(currentP.getP().getName() + " made a move!");
}
/**
@ -327,8 +345,8 @@ public class Game {
* The Player that started the game gets 'X' the other one 'O'.
*/
private void logGame() {
for(int i=0;i<board[0].length;i++){
for(int j=0;j<board.length;j++){
for(int i = 0; i < board[0].length; i++){
for(int j = 0;j < board.length; j++){
if(board[j][i] == 0){
System.out.print("| ");
}

26
src/game/GameEntry.java Normal file
View file

@ -0,0 +1,26 @@
package game;
public class GameEntry {
private final PlayerObject p;
private final int column;
private final int row;
public GameEntry(PlayerObject p, int column, int row) {
this.p = p;
this.column = column;
this.row = row;
}
public PlayerObject getPlayer() {
return this.p;
}
public int getColumn() {
return this.column;
}
public int getRow() {
return this.row;
}
}

31
src/game/GameHistory.java Normal file
View file

@ -0,0 +1,31 @@
package game;
import java.util.List;
import java.util.ArrayList;
public class GameHistory {
private List<GameEntry> hist;
public GameHistory() {
hist = new ArrayList<>();
}
public void add(GameEntry ge) {
this.hist.add(ge);
}
public GameEntry getLast() {
if (this.hist.size() > 0) {
return this.hist.get(this.hist.size() - 1);
}
return null;
}
public PlayerObject lastPlayer() {
if (this.hist.size() > 0) {
return getLast().getPlayer();
}
return null;
}
}

View file

@ -0,0 +1,42 @@
package game;
import player.Player;
/**
* Wrapper for the player.
* This is a wrapper for a player. This guarantees, that no
* player will change his Symbol, nor his id.
*/
public class PlayerObject {
private final Player p;
private final int id;
private String sym;
/**
* Wraps a player.
*
* @param id The players id.
* @param p The player himself.
*/
public PlayerObject(int id, Player p) {
this.p = p;
this.id = id;
}
public void setSymbol(String sym) {
this.sym = sym;
}
public String getSymbol() {
return this.sym;
}
public Player getP() {
return this.p;
}
public int getID() {
return this.id;
}
}

View file

@ -2,7 +2,7 @@ package player;
import java.util.Random;
public class Player1 implements Player{
public class Player1 implements Player {
private String name;
private Random ran;