package ttaomae.connectn;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomPlayer implements Player
{
    private Random rand;

    public RandomPlayer()
    {
        this.rand = new Random();
    }

    @Override
    public int getMove(Board board)
    {
        // find all valid moves
        List<Integer> validMoves = new ArrayList<Integer>();
        for (int col = 0; col < board.getWidth(); col++) {
            if (board.isValidMove(col)) {
                validMoves.add(col);
            }
        }

        // there are no valid moves
        if (validMoves.size() == 0) {
            return -1;
        }
        // pick a random valid move
        else {
            return validMoves.get(this.rand.nextInt(validMoves.size()));
        }
    }

}

@@ -569,20 +569,21 @@
  // 
-there are no
+pick a random
  val
@@ -581,33 +581,32 @@
 andom valid move
-s
 %0A        if (val
@@ -624,100 +624,16 @@
 e() 
-=
+!
 = 0) %7B%0A
-            return -1;%0A        %7D%0A        // pick a random valid move%0A        else %7B%0A
     
@@ -706,22 +706,78 @@
 );%0A        %7D
+%0A%0A        // there are no valid moves%0A        return -1;
 %0A    %7D%0A%0A%7D%0A

update RandomPlayer