1. import java.util.Arrays;
  2. 
  3. import javax.xml.stream.events.Characters;
  4. 
  5. public class Board {
  6. private static final int SIZE = 10;
  7. private char[][] board;
  8. public Board(){
  9. board = new char[SIZE][SIZE];
  10. }
  11. /**
  12. * Sets all the cells of the board to c.
  13. * @param c
  14. */
  15. public void setAll(char c){
  16. for (int row = 0; row < SIZE; row++) {
  17. for (int col = 0; col < SIZE; col++) {
  18. board[row][col] = c;
  19. }
  20. }
  21. }
  22. 
  23. /**
  24. * Set every character in the board to one of the chars in w, all chosen at random.
  25. * That is, every character in board is set to one of the letters word.
  26. * We pick the letter randomly, with Math.random()
  27. * @param w the word.
  28. *///lab 1
  29. public void setToWord(String w){
  30. char[] c = w.toCharArray();
  31. for (int row = 0; row < SIZE; row++) {
  32. for (int col = 0; col < SIZE; col++) {
  33. board[row][col] = w.charAt((int)(Math.random() * w.length()));
  34. }
  35. }
  36. }
  37. 
  38. /**
  39. * Fill a rectangle bounded by topRow, bottomRow and topCol,
  40. * bottomCol (inclusive) with fillChar. That is, we set all
  41. * the row,col pair positions that are within the given rectangle to fillChar
  42. * @param firstRow
  43. * @param firstCol
  44. * @param lastRow
  45. * @param lastCol
  46. * @param fillChar
  47. *///lab 2
  48. public void setRectangle(int firstRow, int firstCol, int lastRow, int lastCol, char fillChar){
  49. for (int row = firstRow; row <= lastRow; row++) {
  50. for (int col = firstCol; col <= lastCol; col++) {
  51. board[row][col] = fillChar;
  52. }
  53. }
  54. }
  55. 
  56. /**
  57. * Set row i+1 in board to be identical to row i for all even i.
  58. * That is, row 1 is set to be identical to row 0, row 3 is set
  59. * identical to row , and so on for all the rows in board.
  60. *///lab 3
  61. public void setToRowPairs(){
  62. if (SIZE <= 1) return; //just one row, nothing to do.
  63. for (int row = 1; row < SIZE; row+=2) {
  64. for (int col = 0; col < SIZE; col++) {
  65. board[row][col] = board[row-1][col];
  66. }
  67. }
  68. }
  69. /**
  70. * Set all the characters in board that are digits to c.
  71. * TIP: Character.isDigit() will come in handy.
  72. * @param c the character that will be placed in all the
  73. * selected locations.
  74. *///lab 4
  75. public void setDigitsTo(char c){
  76. if (SIZE <= 1) return; //just one row, nothing to do.
  77. for (int row = 0; row < SIZE; row++) {
  78. for (int col = 0; col < SIZE; col++) {
  79. if (Character.isDigit(board[row][col])){
  80. board[row][col] = c;
  81. }
  82. }
  83. }
  84. }
  85. /**
  86. * Determine if a contains c
  87. * @param a
  88. * @param c
  89. * @return
  90. */
  91. private static boolean contains(char[] a, char c){
  92. for (char i: a){
  93. if (i == c) return true;
  94. }
  95. return false;
  96. }
  97. /**
  98. * Find all the unique characters in the board and return a array
  99. * that contains all these characters. The returned array does not
  100. * contain any repeated character.
  101. * @return an array with all the distinct characters in board.
  102. * It has no repeats.
  103. *///lab 1
  104. public char[] getAllChars(){
  105. //there can be at most SIZE*SIZE unique chars.
  106. char[] allTheChars = new char[SIZE*SIZE];
  107. int nextIndex = 0;
  108. //add all unique chars to allTheChars
  109. for (int row = 0; row < SIZE; row++) {
  110. for (int col = 0; col < SIZE; col++) {
  111. if ( ! contains(allTheChars, board[row][col])){
  112. allTheChars[nextIndex++] = board[row][col];
  113. }
  114. }
  115. }
  116. //now, make a copy of it without all the empty spots
  117. char[] result = Arrays.copyOf(allTheChars, nextIndex);
  118. return result;
  119. //Another way to implement this method is to first Array.sort(board) and then
  120. // do one pass over the sorted array. Which one is faster? (take 146 and find out)
  121. }
  122. /**
  123. * Find the longest sequence of the same char in a (horizontal) row and
  124. * return the char in question. If there is a tie, return a char in a
  125. * sequence of maximal length (For example, if 'aaaa' and 'cccc' are the
  126. * longest, return either 'a' or 'c', it does not matter which).
  127. * Only checks horizontal (in the same row) sequences.
  128. * @return the char in the longest sequence.
  129. *///lab 2
  130. public char getLongestSequenceRow(){
  131. if (SIZE <= 1) return 1;
  132. int maxLength = 0;
  133. char maxChar = ' ';
  134. for (int row = 0; row < SIZE; row++) {
  135. char last = board[row][0];
  136. int length = 1;
  137. for (int col = 0; col < SIZE; col++) {
  138. if (board[row][col] == last){
  139. length++;
  140. if (length > maxLength){
  141. maxLength = length;
  142. maxChar = last;
  143. }
  144. }
  145. else {
  146. length = 1;
  147. }
  148. last = board[row][col];
  149. }
  150. };
  151. return maxChar;
  152. }
  153. /**
  154. * Determine if word appears on the board, written horizontally from
  155. * left to right (that is, like a word in a book).
  156. * TIP: String.toCharArray() might be useful.
  157. * @param word the word we are looking for.
  158. * @return true if word appears written horizontally somewhere in board,
  159. * false otherwise
  160. *///lab 3
  161. public boolean appears(String word){
  162. if (word.length() > SIZE) return false; //too big to fit
  163. char[] w = word.toCharArray();
  164. for (int row = 0; row < SIZE; row++) {
  165. for (int col = 0; col <= SIZE - w.length; col++) {
  166. boolean match = true;
  167. for (int i=0; i < w.length; i++){
  168. if (w[i] != board[row][col+i]){
  169. match = false;
  170. break;
  171. }
  172. }
  173. if (match){
  174. return true;
  175. }
  176. }
  177. }
  178. return false;
  179. }
  180. /**
  181. * Find out if the character c appears stacked in a column that contains
  182. * exactly 3 instances of it. For example, if c is * then, this is a match:
  183. * rtyii
  184. * 8*des
  185. * d*dfq
  186. * i*dkk
  187. * 998dd
  188. *
  189. * but, this is not a match (because it has 4 *s):
  190. * 9*ddk
  191. * d*dkd
  192. * i*dkd
  193. * i*kdk
  194. * djdjd
  195. *
  196. * Returns true is somewhere on the board there are
  197. * exactly 3 c's stacked in a column, but no more.
  198. * @param c
  199. * @return
  200. *///lab 4
  201. public boolean colOfThree(char c){
  202. if (SIZE < 3) return false; //too small
  203. for (int col = 0; col < SIZE; col++) {
  204. for (int row = 0; row <= SIZE-3; row++) {
  205. if ((row == 0 || board[row-1][col] != c) && //we are either at the first row, or the char at row-1 is not c, and
  206. (board[row][col] == c && board[row+1][col] == c && board[row+2][col] == c) && // there are 3 in a col, and
  207. (row + 3 == SIZE || board[row+3][col] != c)){ //we are at the last row or the next char at row+3 is not c
  208. return true;
  209. }
  210. }
  211. }
  212. return false;
  213. }
  214. public String toString(){
  215. String result = "";
  216. for (int row = 0; row < SIZE; row++) {
  217. for (int col = 1; col < SIZE; col++) {
  218. result += board[row][col];
  219. }
  220. result += "\n";
  221. }
  222. return result;
  223. }
  224. 
  225. /** For testing purposes. */
  226. public void set(int row, int col, char c){
  227. board[row][col] = c;
  228. }
  229. 
  230. /**
  231. * @param args
  232. */
  233. public static void main(String[] args) {
  234. //All labs
  235. Board b = new Board();
  236. b.setAll('R');
  237. System.out.println(b);
  238. //lab 1
  239. b.setToWord("Synchronicity");
  240. System.out.println(b);
  241. //lab 2
  242. b.setToWord("thep0l1c3");
  243. System.out.println(b);
  244. b.setDigitsTo('*');
  245. System.out.println(b);
  246. 
  247. //lab 3
  248. b.setRectangle(2,2,4,4,'P');
  249. System.out.println(b);
  250. //lab 4
  251. b.setToRowPairs();
  252. System.out.println(b);
  253. 
  254. //lab 1
  255. char[] unique = b.getAllChars();
  256. System.out.println("unique=" + Arrays.toString(unique));
  257. //lab 2
  258. System.out.println("Longest char=" + b.getLongestSequenceRow());
  259. b.setToWord("Everybreathyoutake");
  260. System.out.println(b);
  261. System.out.println("Longest char=" + b.getLongestSequenceRow());
  262. //lab 3
  263. System.out.println(b);
  264. System.out.println("Does e appear? " + b.appears("e"));
  265. System.out.println("Does ee appear? " + b.appears("ee"));
  266. System.out.println("Does eee appear? " + b.appears("eee"));
  267. System.out.println("Does eeee appear? " + b.appears("eeee"));
  268. //lab 4
  269. System.out.println("");
  270. b.setAll('*');
  271. b.set(1, 3, 'T');
  272. b.set(2, 3, 'T');
  273. b.set(3, 3, 'T');
  274. System.out.println(b);
  275. System.out.println("Col of three T: " + b.colOfThree('T'));
  276. b.set(4, 3, 'T');
  277. System.out.println(b);
  278. System.out.println("Col of three T: " + b.colOfThree('T'));
  279. }
  280. 
  281. }