Created PlayerObject and implemented into game

Signed-off-by: Malte Tammena <malte.tammena@gmx.de>
This commit is contained in:
Malte Tammena 2017-10-15 13:32:15 +02:00
parent be0540a879
commit b96e0e2799
2 changed files with 17 additions and 16 deletions

View file

@ -56,8 +56,8 @@ public class Game {
* @param p2 Player Two * @param p2 Player Two
*/ */
public Game(Player p1, Player p2) { public Game(Player p1, Player p2) {
this.p1 = new PlayerObject(p1); this.p1 = new PlayerObject(1, p1);
this.p2 = new PlayerObject(p2); this.p2 = new PlayerObject(2, p2);
this.board = new int [GAME_COLUMNS][GAME_ROWS]; this.board = new int [GAME_COLUMNS][GAME_ROWS];
this.gameOn = false; this.gameOn = false;
} }
@ -66,24 +66,22 @@ 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; lastPlayer = this.p1;
second = this.p2;
} else { } else {
first = this.p2; lastPlayer = this.p2;
second = this.p1;
} }
// Start the game
this.gameOn = true; this.gameOn = true;
while(gameOn) { while(gameOn) {
if(lastPlayer == second){ if(lastPlayer == p1){
lastPlayer = first; lastPlayer = p2;
makeMove(first); makeMove();
} else { } else {
lastPlayer = second; lastPlayer = p1;
makeMove(second); makeMove();
} }
if(gameOn) { if(gameOn) {
logGame(); logGame();
@ -99,7 +97,8 @@ public class Game {
/** /**
* Calls a players functions to make a move. * Calls a players functions to make a move.
*/ */
private void makeMove(Player p) { private void makeMove() {
Player p = lastPlayer.getP();
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. // 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());
@ -116,7 +115,7 @@ public class Game {
pos++; pos++;
} }
// Change the board accordingly. // Change the board accordingly.
this.board[choice][pos] = p.getNr(); this.board[choice][pos] = lastPlayer.getID();
} }
/** /**

View file

@ -1,4 +1,6 @@
package player; package game;
import player.Player;
/** /**
* Wrapper for the player. * Wrapper for the player.