Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AIPlayer implements Player{
- //do not change the line above
- //Define your fields here
- private int playerID;
- private int col;
- private int row;
- private int[][] tokenLocation;
- private int lastMoveValue;
- private int lastCol;
- Board connectFourBoard;
- //constructor takes the player id for this player, and the number of
- // rows and columns of the board we're playing on
- public AIPlayer(int playerID, int row, int col){
- this.playerID = playerID;
- this.lastMoveValue = -1;
- this.lastCol = 0;
- this.row = row;
- this.col = col;
- tokenLocation = new int [row][col];
- }
- //used to notify your AI player of the other players last move
- public void lastMove(int c) {
- setToken(2, findOpenSpace(c), c);
- }
- private void setToken(int playerNum, int row, int col) {
- tokenLocation[row][col] = playerNum;
- }
- private int findOpenSpace(int c) {
- int row = -1;
- for(int i = row - 1; i >= 0; i--) {
- if(tokenLocation[i][c] == 0) {
- row = i;
- break;
- }
- }
- return row;
- }
- //returns column of where to play a token
- public int playToken(){
- if(this.lastMoveValue == -1){
- this.lastCol = 0;
- connectFourBoard.play(this.playerID, this.lastCol);
- return this.lastCol;
- }
- else if(this.lastCol == this.lastMoveValue){
- this.lastCol = this.lastMoveValue + 1;
- if(this.lastCol < this.col){
- if(connectFourBoard.play(this.playerID, this.lastCol)){
- return this.lastCol;
- }
- else{
- this.lastCol = 0;
- boolean b = false;
- while(b){
- b = connectFourBoard.play(this.playerID, this.lastCol);
- this.lastCol++;
- } // end while
- return this.lastCol;
- } // else of board.play
- } // end if myLastCol < this.col
- } // end of of lastMoveVal == myLastCol
- if(connectFourBoard.play(this.playerID, this.lastCol)){
- return this.lastCol;
- }
- else{
- this.lastCol = 0;
- boolean b = false;
- while(b){
- b = connectFourBoard.play(this.playerID, this.lastCol);
- this.lastCol++;
- }
- }
- return this.lastCol;
- //c = min + (int)(Math.random()*((max-min)+1));
- //c = (int)(Math.random()*(max/2));
- }
- //get this player's id
- public int getPlayerID(){
- return playerID;
- }
- //resets the state of the player in preparation for a new game
- public void reset(){
- for(int i = 0; i < row; i++) {
- for(int j = 0; j < col; j++){
- tokenLocation[i][j] = 0; // reset id to none
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement