Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. package lab4.data;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Observable;
  5.  
  6.  
  7. /**
  8. * Represents the 2-d game grid
  9. */
  10.  
  11. public class GameGrid extends Observable{
  12.  
  13. ArrayList<int[]> myMoves = new ArrayList<int[]>();
  14. public static int EMPTY = 0, ME = 1, OTHER = 2;
  15. public final int INROW = 5;
  16. int size;
  17.  
  18. int[][] grid;
  19.  
  20. /**
  21. * Constructor
  22. *
  23. * @param size The width/height of the game grid
  24. */
  25. public GameGrid(int size){
  26.  
  27. grid = new int[size][size];
  28. clearGrid();
  29. this.size = size;
  30. }
  31.  
  32. /**
  33. * Reads a location of the grid
  34. *
  35. * @param x The x coordinate
  36. * @param y The y coordinate
  37. * @return the value of the specified location
  38. */
  39. public int getLocation(int x, int y){
  40.  
  41. return grid[x][y];
  42.  
  43. }
  44.  
  45. /**
  46. * Returns the size of the grid
  47. *
  48. * @return the grid size
  49. */
  50. public int getSize(){
  51. return size;
  52. }
  53.  
  54. /**
  55. * Enters a move in the game grid
  56. *
  57. * @param x the x position
  58. * @param y the y position
  59. * @param player
  60. * @return true if the insertion worked, false otherwise
  61. */
  62. public boolean move(int x, int y, int player){
  63. if (getLocation(x, y) == EMPTY){
  64. grid[x][y] = player;
  65. setChanged();
  66. notifyObservers();
  67. return true;
  68. }
  69. return false;
  70. }
  71.  
  72. /**
  73. * Clears the grid of pieces
  74. */
  75. public void clearGrid(){
  76. for (int i = 0; i < size; i++){
  77. for (int j = 0; j < size; j++) {
  78. grid[i][j] = EMPTY;
  79. }
  80. }
  81. setChanged();
  82. notifyObservers();
  83. }
  84.  
  85. /**
  86. * Check if a player has 5 in row
  87. *
  88. * @param player the player to check for
  89. * @return true if player has 5 in row, false otherwise
  90. */
  91. public boolean isWinner(int player){
  92. //for (int i = 0; i < 8; i++)
  93.  
  94. return true;
  95. }
  96.  
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement