commit
c5f26606f9
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
doc/
|
3
Makefile
3
Makefile
|
@ -13,6 +13,9 @@ LIBARIES=
|
||||||
CLASSES=\
|
CLASSES=\
|
||||||
src/game/Main.java \
|
src/game/Main.java \
|
||||||
src/game/Game.java \
|
src/game/Game.java \
|
||||||
|
src/game/GameHistory.java \
|
||||||
|
src/game/GameEntry.java \
|
||||||
|
src/game/PlayerObject.java \
|
||||||
src/player/Player.java \
|
src/player/Player.java \
|
||||||
src/player/Player1.java \
|
src/player/Player1.java \
|
||||||
src/player/Player2.java
|
src/player/Player2.java
|
||||||
|
|
|
@ -22,12 +22,22 @@ public class Game {
|
||||||
/**
|
/**
|
||||||
* Player One
|
* Player One
|
||||||
*/
|
*/
|
||||||
private final Player p1;
|
private final PlayerObject p1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Player Two
|
* 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.
|
* The game's board.
|
||||||
|
@ -46,10 +56,14 @@ public class Game {
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
* Initialize board and save players.
|
* Initialize board and save players.
|
||||||
|
*
|
||||||
|
* @param p1 Player One
|
||||||
|
* @param p2 Player Two
|
||||||
*/
|
*/
|
||||||
public Game(Player p1, Player p2) {
|
public Game(Player p1, Player p2) {
|
||||||
this.p1 = p1;
|
this.p1 = new PlayerObject(1, p1);
|
||||||
this.p2 = p2;
|
this.p2 = new PlayerObject(2, p2);
|
||||||
|
this.hist = new GameHistory();
|
||||||
this.board = new int [GAME_COLUMNS][GAME_ROWS];
|
this.board = new int [GAME_COLUMNS][GAME_ROWS];
|
||||||
this.gameOn = false;
|
this.gameOn = false;
|
||||||
}
|
}
|
||||||
|
@ -58,26 +72,19 @@ public class Game {
|
||||||
* Starts the game.
|
* Starts the game.
|
||||||
*/
|
*/
|
||||||
public void start() {
|
public void start() {
|
||||||
|
// Set random starting player.
|
||||||
Random ran = new Random();
|
Random ran = new Random();
|
||||||
Player first;
|
|
||||||
Player second;
|
|
||||||
if (ran.nextBoolean()) {
|
if (ran.nextBoolean()) {
|
||||||
first = this.p1;
|
currentP = this.p1;
|
||||||
second = this.p2;
|
|
||||||
} else {
|
} else {
|
||||||
first = this.p2;
|
currentP = this.p2;
|
||||||
second = this.p1;
|
|
||||||
}
|
}
|
||||||
boolean turn = true;
|
// Start the game
|
||||||
this.gameOn = true;
|
this.gameOn = true;
|
||||||
while(gameOn) {
|
while(gameOn) {
|
||||||
if(turn){
|
makeMove();
|
||||||
makeMove(first,turn);
|
currentP = other(currentP);
|
||||||
} else {
|
|
||||||
makeMove(second,turn);
|
|
||||||
}
|
|
||||||
if(gameOn) {
|
if(gameOn) {
|
||||||
turn = !turn;
|
|
||||||
logGame();
|
logGame();
|
||||||
checkState();
|
checkState();
|
||||||
} else {
|
} 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.
|
* Calls a players functions to make a move.
|
||||||
*/
|
*/
|
||||||
private void makeMove(Player p, boolean turn) {
|
private void makeMove() {
|
||||||
log(p.getName() + " makes a move!");
|
|
||||||
// Get a choice from the player, while only giving him a copy of the game.
|
// 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.
|
// 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.
|
// If a player makes a false move, the game punishes him.
|
||||||
this.gameOn = false;
|
this.gameOn = false;
|
||||||
log(p.getName() + " made an illegal move and lost!");
|
log(currentP.getP().getName() + " made an illegal move and lost!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Find the lowest empty field in the board in the choosen column.
|
// Find the lowest empty field in the board in the choosen column.
|
||||||
|
@ -108,11 +128,9 @@ public class Game {
|
||||||
pos++;
|
pos++;
|
||||||
}
|
}
|
||||||
// Change the board accordingly.
|
// Change the board accordingly.
|
||||||
if(turn){
|
this.board[choice][pos] = currentP.getID();
|
||||||
this.board[choice][pos] = 1;
|
this.hist.add(new GameEntry(currentP, choice, pos));
|
||||||
} else {
|
log(currentP.getP().getName() + " made a move!");
|
||||||
this.board[choice][pos] = 2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -327,8 +345,8 @@ public class Game {
|
||||||
* The Player that started the game gets 'X' the other one '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++){
|
||||||
for(int j=0;j<board.length;j++){
|
for(int j = 0;j < board.length; j++){
|
||||||
if(board[j][i] == 0){
|
if(board[j][i] == 0){
|
||||||
System.out.print("| ");
|
System.out.print("| ");
|
||||||
}
|
}
|
||||||
|
|
26
src/game/GameEntry.java
Normal file
26
src/game/GameEntry.java
Normal 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
31
src/game/GameHistory.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
42
src/game/PlayerObject.java
Normal file
42
src/game/PlayerObject.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,7 +2,7 @@ package player;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class Player1 implements Player{
|
public class Player1 implements Player {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
private Random ran;
|
private Random ran;
|
||||||
|
|
Loading…
Reference in a new issue