Redoing players in the game.

Signed-off-by: Malte Tammena <malte.tammena@gmx.de>
This commit is contained in:
Malte Tammena 2017-10-15 13:12:31 +02:00
parent d7074f2a18
commit 049171749d
3 changed files with 17 additions and 12 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
doc/

View file

@ -29,6 +29,11 @@ public class Game {
*/ */
private final Player p2; private final Player p2;
/**
* Player who made the last move.
*/
private Player lastPlayer;
/** /**
* The game's board. * The game's board.
* It's size is GAME_COLUMNS * GAME_ROWS. * It's size is GAME_COLUMNS * GAME_ROWS.
@ -46,6 +51,9 @@ 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 = p1;
@ -68,16 +76,16 @@ public class Game {
first = this.p2; first = this.p2;
second = this.p1; second = this.p1;
} }
boolean turn = true;
this.gameOn = true; this.gameOn = true;
while(gameOn) { while(gameOn) {
if(turn){ if(lastPlayer == second){
makeMove(first,turn); lastPlayer = first;
makeMove(first);
} else { } else {
makeMove(second,turn); lastPlayer = second;
makeMove(second);
} }
if(gameOn) { if(gameOn) {
turn = !turn;
logGame(); logGame();
checkState(); checkState();
} else { } else {
@ -91,7 +99,7 @@ public class Game {
/** /**
* 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(Player p) {
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());
@ -108,11 +116,7 @@ public class Game {
pos++; pos++;
} }
// Change the board accordingly. // Change the board accordingly.
if(turn){ this.board[choice][pos] = p.getNr();
this.board[choice][pos] = 1;
} else {
this.board[choice][pos] = 2;
}
} }
/** /**

View file

@ -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;