Commit d2017dac authored by Stefano Cobelli's avatar Stefano Cobelli
Browse files

Reredid Random player.

Added random movement to blockerplayer if no available moves.
Fixed winning message so it displays teh correct turn
parent 8afa716a
...@@ -160,8 +160,7 @@ public class TTTController implements ActionListener { ...@@ -160,8 +160,7 @@ public class TTTController implements ActionListener {
.decideMove(); .decideMove();
boardPanel.getButtonBoard()[aiMove[0]][aiMove[1]] boardPanel.getButtonBoard()[aiMove[0]][aiMove[1]]
.setText(theGame.getPlayerTurn()); .setText(theGame.getPlayerTurn());
theGame.switchTurn();
statsPanel.getPlayerTurn().setText(theGame.getPlayerTurn());
if (!theGame.isPlayable()) { if (!theGame.isPlayable()) {
// performs relative actions if game is over // performs relative actions if game is over
if (theGame.getState() == State.WIN) { if (theGame.getState() == State.WIN) {
...@@ -188,6 +187,8 @@ public class TTTController implements ActionListener { ...@@ -188,6 +187,8 @@ public class TTTController implements ActionListener {
return; return;
} }
} }
theGame.switchTurn();
statsPanel.getPlayerTurn().setText(theGame.getPlayerTurn());
} }
} }
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
*/ */
package model.player; package model.player;
import java.util.Random;
import model.Board; import model.Board;
import model.Mark; import model.Mark;
...@@ -80,7 +82,23 @@ public class BlockerPlayer extends Player implements AutoPlayable { ...@@ -80,7 +82,23 @@ public class BlockerPlayer extends Player implements AutoPlayable {
(theBoard.getSize() - 1) - checkSpots(marks) }; (theBoard.getSize() - 1) - checkSpots(marks) };
return answer; return answer;
} }
return null;
Integer[][] coors = new Integer[theBoard.getSize() * theBoard.getSize()][2];
int index = 0;
for (int row = 0; row < theBoard.getSize(); row++) {
for (int col = 0; col < theBoard.getSize(); col++) {
if (theBoard.isEmpty(row, col)) {
coors[index][0] = row;
coors[index][1] = col;
index++;
}
}
}
Random rand = new Random();
int randSelection = rand.nextInt(index);
move(coors[randSelection][0], coors[randSelection][1]);
Integer[] answer = { coors[randSelection][0], coors[randSelection][1] };
return answer;
} }
/** /**
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
*/ */
package model.player; package model.player;
import java.util.ArrayList;
import java.util.Random; import java.util.Random;
import model.Board; import model.Board;
...@@ -26,8 +25,6 @@ public class RandomPlayer extends Player implements AutoPlayable { ...@@ -26,8 +25,6 @@ public class RandomPlayer extends Player implements AutoPlayable {
* Arraylists to store rows and columns of free spaces on the current board * Arraylists to store rows and columns of free spaces on the current board
*/ */
private Random rand = new Random(); private Random rand = new Random();
private ArrayList<Integer> rowOptions = new ArrayList<Integer>();
private ArrayList<Integer> colOptions = new ArrayList<Integer>();
/** /**
* This is the constructor for the RandomPlayer class that takes in the * This is the constructor for the RandomPlayer class that takes in the
...@@ -47,23 +44,21 @@ public class RandomPlayer extends Player implements AutoPlayable { ...@@ -47,23 +44,21 @@ public class RandomPlayer extends Player implements AutoPlayable {
*/ */
@Override @Override
public Integer[] decideMove() { public Integer[] decideMove() {
int row = rand.nextInt(rowOptions.size()); Integer[][] coors = new Integer[theBoard.getSize() * theBoard.getSize()][2];
int col = rand.nextInt(rowOptions.size()); int index = 0;
for (int count = 0; count < theBoard.getSize(); count++) { for (int row = 0; row < theBoard.getSize(); row++) {
if (this.move(row, col)) { for (int col = 0; col < theBoard.getSize(); col++) {
Integer[] answer = { rowOptions.get(row), colOptions.get(col) }; if (theBoard.isEmpty(row, col)) {
return answer; coors[index][0] = row;
coors[index][1] = col;
index++;
}
} }
row++;
if (row > theBoard.getSize()) {
row = 0;
col++;
}
if (col > theBoard.getSize()) {
col = 0;
}
} }
return null; int randSelection = rand.nextInt(index);
move(coors[randSelection][0], coors[randSelection][1]);
Integer[] answer = { coors[randSelection][0], coors[randSelection][1] };
return answer;
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment