Created PlayerObject and implemented into game
Signed-off-by: Malte Tammena <malte.tammena@gmx.de>
This commit is contained in:
parent
be0540a879
commit
b96e0e2799
|
@ -56,8 +56,8 @@ public class Game {
|
|||
* @param p2 Player Two
|
||||
*/
|
||||
public Game(Player p1, Player p2) {
|
||||
this.p1 = new PlayerObject(p1);
|
||||
this.p2 = new PlayerObject(p2);
|
||||
this.p1 = new PlayerObject(1, p1);
|
||||
this.p2 = new PlayerObject(2, p2);
|
||||
this.board = new int [GAME_COLUMNS][GAME_ROWS];
|
||||
this.gameOn = false;
|
||||
}
|
||||
|
@ -66,24 +66,22 @@ 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;
|
||||
lastPlayer = this.p1;
|
||||
} else {
|
||||
first = this.p2;
|
||||
second = this.p1;
|
||||
lastPlayer = this.p2;
|
||||
}
|
||||
// Start the game
|
||||
this.gameOn = true;
|
||||
while(gameOn) {
|
||||
if(lastPlayer == second){
|
||||
lastPlayer = first;
|
||||
makeMove(first);
|
||||
if(lastPlayer == p1){
|
||||
lastPlayer = p2;
|
||||
makeMove();
|
||||
} else {
|
||||
lastPlayer = second;
|
||||
makeMove(second);
|
||||
lastPlayer = p1;
|
||||
makeMove();
|
||||
}
|
||||
if(gameOn) {
|
||||
logGame();
|
||||
|
@ -99,7 +97,8 @@ public class Game {
|
|||
/**
|
||||
* 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!");
|
||||
// Get a choice from the player, while only giving him a copy of the game.
|
||||
int choice = p.move(copyBoard());
|
||||
|
@ -116,7 +115,7 @@ public class Game {
|
|||
pos++;
|
||||
}
|
||||
// Change the board accordingly.
|
||||
this.board[choice][pos] = p.getNr();
|
||||
this.board[choice][pos] = lastPlayer.getID();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
package player;
|
||||
package game;
|
||||
|
||||
import player.Player;
|
||||
|
||||
/**
|
||||
* Wrapper for the player.
|
||||
|
|
Loading…
Reference in a new issue