Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1.  
  2. public class AIPlayer implements Player{
  3. //do not change the line above
  4.  
  5. //Define your fields here
  6. private int playerID;
  7. private int col;
  8. private int row;
  9. private int[][] tokenLocation;
  10. private int lastMoveValue;
  11. private int lastCol;
  12. Board connectFourBoard;
  13.  
  14. //constructor takes the player id for this player, and the number of
  15. // rows and columns of the board we're playing on
  16. public AIPlayer(int playerID, int row, int col){
  17. this.playerID = playerID;
  18. this.lastMoveValue = -1;
  19. this.lastCol = 0;
  20. this.row = row;
  21. this.col = col;
  22. tokenLocation = new int [row][col];
  23. }
  24.  
  25. //used to notify your AI player of the other players last move
  26. public void lastMove(int c) {
  27. setToken(2, findOpenSpace(c), c);
  28. }
  29. private void setToken(int playerNum, int row, int col) {
  30.  
  31. tokenLocation[row][col] = playerNum;
  32. }
  33. private int findOpenSpace(int c) {
  34. int row = -1;
  35. for(int i = row - 1; i >= 0; i--) {
  36. if(tokenLocation[i][c] == 0) {
  37. row = i;
  38. break;
  39. }
  40. }
  41. return row;
  42. }
  43. //returns column of where to play a token
  44. public int playToken(){
  45.  
  46. if(this.lastMoveValue == -1){
  47. this.lastCol = 0;
  48. connectFourBoard.play(this.playerID, this.lastCol);
  49. return this.lastCol;
  50. }
  51. else if(this.lastCol == this.lastMoveValue){
  52. this.lastCol = this.lastMoveValue + 1;
  53. if(this.lastCol < this.col){
  54. if(connectFourBoard.play(this.playerID, this.lastCol)){
  55. return this.lastCol;
  56. }
  57. else{
  58. this.lastCol = 0;
  59. boolean b = false;
  60. while(b){
  61. b = connectFourBoard.play(this.playerID, this.lastCol);
  62. this.lastCol++;
  63. } // end while
  64. return this.lastCol;
  65. } // else of board.play
  66. } // end if myLastCol < this.col
  67. } // end of of lastMoveVal == myLastCol
  68.  
  69. if(connectFourBoard.play(this.playerID, this.lastCol)){
  70. return this.lastCol;
  71. }
  72. else{
  73. this.lastCol = 0;
  74. boolean b = false;
  75. while(b){
  76. b = connectFourBoard.play(this.playerID, this.lastCol);
  77. this.lastCol++;
  78. }
  79. }
  80. return this.lastCol;
  81. //c = min + (int)(Math.random()*((max-min)+1));
  82. //c = (int)(Math.random()*(max/2));
  83. }
  84.  
  85. //get this player's id
  86. public int getPlayerID(){
  87. return playerID;
  88. }
  89.  
  90. //resets the state of the player in preparation for a new game
  91. public void reset(){
  92. for(int i = 0; i < row; i++) {
  93. for(int j = 0; j < col; j++){
  94.  
  95. tokenLocation[i][j] = 0; // reset id to none
  96. }
  97. }
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement