Commit d2017dac authored by Stefano Cobelli's avatar Stefano Cobelli

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 {
.decideMove();
boardPanel.getButtonBoard()[aiMove[0]][aiMove[1]]
.setText(theGame.getPlayerTurn());
theGame.switchTurn();
statsPanel.getPlayerTurn().setText(theGame.getPlayerTurn());
if (!theGame.isPlayable()) {
// performs relative actions if game is over
if (theGame.getState() == State.WIN) {
......@@ -188,6 +187,8 @@ public class TTTController implements ActionListener {
return;
}
}
theGame.switchTurn();
statsPanel.getPlayerTurn().setText(theGame.getPlayerTurn());
}
}
......
......@@ -7,6 +7,8 @@
*/
package model.player;
import java.util.Random;
import model.Board;
import model.Mark;
......@@ -80,7 +82,23 @@ public class BlockerPlayer extends Player implements AutoPlayable {
(theBoard.getSize() - 1) - checkSpots(marks) };
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 @@
*/
package model.player;
import java.util.ArrayList;
import java.util.Random;
import model.Board;
......@@ -26,8 +25,6 @@ public class RandomPlayer extends Player implements AutoPlayable {
* Arraylists to store rows and columns of free spaces on the current board
*/
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
......@@ -47,23 +44,21 @@ public class RandomPlayer extends Player implements AutoPlayable {
*/
@Override
public Integer[] decideMove() {
int row = rand.nextInt(rowOptions.size());
int col = rand.nextInt(rowOptions.size());
for (int count = 0; count < theBoard.getSize(); count++) {
if (this.move(row, col)) {
Integer[] answer = { rowOptions.get(row), colOptions.get(col) };
return answer;
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++;
}
}
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