Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. import java.awt.Point;
  2.  
  3.  
  4. // Array functions
  5. Square[][] transpose(Square[][] arr) {
  6. Square[][] nArr = new Square[arr[0].length][arr.length];
  7. int i, j;
  8. for (i = 0; i < squares[0].length; i++)
  9. for (j = 0; j < squares.length; j++)
  10. nArr[i][j] = arr[j][i];
  11. return nArr;
  12. }
  13. String[] moveBack(String[] arr) { for (int ii = 0; ii < arr.length-1; ii++) { arr[ii] = arr[ii+1]; } return arr; }
  14. boolean atLeastOne(Square[] arr) { for (Square element : arr) { if (element.moveable == true) { return true; } } return false; }
  15. int farthestLeftSquare() { for (int i = 0; i < squares.length; i++) { if (atLeastOne(squares[i])) { return i+1; } } return 1; }
  16. int farthestRightSquare() { for (int i = squares.length-1; i >= 0; i--) { if (atLeastOne(squares[i])) { return i+1; } } return squares.length; }
  17. int highestSquare() { Square[][] transposed = transpose(squares); for (int i = 0; i < transposed.length; i++) {if (atLeastOne(transposed[i])) { return i+1; } } return 1; }
  18. int lowestSquare() { Square[][] transposed = transpose(squares); for (int i = transposed.length-1; i >= 0; i--) { if (atLeastOne(transposed[i])) { return i+1; } } return squares.length; }
  19.  
  20. void addX(Point p, int add) { if (p.x >= 1 || p.x < BOARD_WIDTH) { p.x += add; } }
  21. void addY(Point p, int add) { if (p.y >= 1 || p.y < BOARD_HEIGHT) { p.y += add; } }
  22. Dimensions dimensions(float w, float h, float rad) { return new Dimensions(w, h, rad); }
  23. class Dimensions { public float w, h, rad; Dimensions(float w, float h, float rad) { this.w = w; this.h = h; this.rad = rad; } }
  24.  
  25. void txt(String text, float size, float x, float y) { fill(0); textSize(size); textAlign(CENTER, CENTER); text(text, x, y); noFill(); }
  26.  
  27.  
  28.  
  29.  
  30.  
  31. class Square {
  32. public boolean moveable;
  33. public color c;
  34. public int number;
  35. Square(color c, int n) { this.c = c; this.number = n; this.moveable = true; }
  36.  
  37. public void show(Point p, float w) {
  38. if (this.moveable) {
  39. fill(this.c);
  40. rect(p.x, p.y, w, w);
  41. noFill();
  42. txt(Integer.toString(this.number), 32, p.x + w/2, p.y + w/2);
  43. }
  44. }
  45.  
  46. public void show(Point p, float w, float rad) {
  47. if (this.moveable) {
  48. fill(this.c);
  49. rect(p.x, p.y, w, w, rad);
  50. noFill();
  51. txt(Integer.toString(this.number), 32, p.x + w/2, p.y + w/2);
  52. }
  53. }
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60. void keyPressed() {
  61. // The user cannot go past the farthest visible number in any direction
  62. if ((key == 119 || (key == CODED && keyCode == UP)) && pos.y > highestSquare()) { addY(pos, -1); LEGAL_KEY = true; } // W
  63. if ((key == 97 || (key == CODED && keyCode == LEFT)) && pos.x > farthestLeftSquare()) { addX(pos, -1); LEGAL_KEY = true; } // A
  64. if ((key == 115 || (key == CODED && keyCode == DOWN)) && pos.y < lowestSquare()) { addY(pos, 1); LEGAL_KEY = true; } // S
  65. if ((key == 100 || (key == CODED && keyCode == RIGHT)) && pos.x < farthestRightSquare()) { addX(pos, 1); LEGAL_KEY = true; } // D
  66.  
  67.  
  68. if (LEGAL_KEY && squares[pos.x-1][pos.y-1].moveable == true) {
  69. // Performs operator on previous value and current value
  70. if (operations[0] == "+") { value += squares[pos.x-1][pos.y-1].number; }
  71. else if (operations[0] == "-") { value -= squares[pos.x-1][pos.y-1].number; }
  72. else if (operations[0] == "*") { value *= squares[pos.x-1][pos.y-1].number; }
  73. else if (operations[0] == "/") {
  74. // Don't want to divide by zero
  75. try { value /= squares[pos.x-1][pos.y-1].number; }
  76. catch (ArithmeticException e) { background(255); txt("YOU DIVIDED BY ZERO!\n Your final score: Undefined", 32, (width-800)/2, height/2); noLoop(); }
  77. }
  78.  
  79. // Change current operator every time a move happens
  80. moveBack(operations);
  81. operations[operations.length-1] = POSSIBLE_OPERATIONS[(int)random(POSSIBLE_OPERATIONS.length)];
  82. }
  83. squares[pos.x-1][pos.y-1].moveable = false;
  84.  
  85.  
  86.  
  87. // Check if there are anymore numbers left
  88. if (LEGAL_KEY) {
  89. boolean cont = false;
  90. for (Square[] xlist : squares) { for (Square sqr : xlist) { if (sqr.moveable == true) { cont = true; break; } } }
  91. // No more squares left
  92. if (cont == false) {
  93. background(255);
  94. txt("There are no more squares.\n Your final score: " + value, 32, (width-800)/2, height/2);
  95. noLoop();
  96. }
  97. }
  98. }
  99.  
  100.  
  101.  
  102.  
  103. void draw() {
  104. background(255);
  105.  
  106. // Text with current operations and current value
  107. txt("Operations: (" + join(operations, ", ") + "). Current Operation: \"" + operations[0] + "\"", 32, (width-800)/2, 32);
  108. txt("Value: " + value, 32, (width-800)/2, 64);
  109.  
  110. // Draws one square around the current position in every direction
  111. int x=0, y=TEXT_SPACE;
  112. for (int xi = pos.x-1; xi <= pos.x+1; xi++) {
  113. for (int yi = pos.y-1; yi <= pos.y+1; yi++) {
  114. // FIXME Square farthest to the (left, right, top left)
  115. try { squares[xi-1][yi-1].show(new Point(x, y), (width-800)/3, 10); }
  116. catch (ArrayIndexOutOfBoundsException e) { }
  117. y += (height-TEXT_SPACE)/3;
  118. }
  119. x += (width-800)/3;
  120. y = TEXT_SPACE;
  121. }
  122.  
  123.  
  124. // Draws the map on the right with the squares already traversed
  125. stroke(0);
  126. int w = (width-800)/BOARD_HEIGHT, h = (height-TEXT_SPACE)/BOARD_HEIGHT;
  127. for (int i = 0; i < squares.length; i++) {
  128. for (int ii = 0; ii < squares[0].length; ii++) {
  129. if (pos.x-1 == i && pos.y-1 == ii) { fill(0); rect(i*w+800, ii*h+TEXT_SPACE, w, h); noFill(); }
  130. else if (squares[i][ii].moveable == false) { fill(102, 255, 102); rect(i*w+800, ii*h+TEXT_SPACE, w, h); noFill(); }
  131. else { fill(255, 51, 51); rect(i*w+800, ii*h+TEXT_SPACE, w, h); noFill(); }
  132. }
  133. }
  134. noFill();
  135. }
  136.  
  137.  
  138.  
  139.  
  140. // Primitive variables
  141. Point pos;
  142. short MIN_RAND = 0;
  143. short MAX_RAND = 15;
  144. short BOARD_WIDTH = 10;
  145. short BOARD_HEIGHT = 10;
  146. short TEXT_SPACE = 100;
  147. int prev_value;
  148. int value;
  149. boolean LEGAL_KEY;
  150. // Object variables
  151. Square[][] squares = new Square[BOARD_WIDTH][BOARD_HEIGHT];
  152. String[] POSSIBLE_OPERATIONS = new String[] {"+", "-", "/", "*"};
  153. String[] operations = new String[4];
  154.  
  155. void settings() { size(1600, 800+TEXT_SPACE); }
  156. void setup() {
  157. pos = new Point(round(random(1, BOARD_WIDTH+0.5)), round(random(1, BOARD_HEIGHT+0.5)));
  158.  
  159. // Populates the board with random numbers
  160. for (int i = 0; i < BOARD_WIDTH; i++) {
  161. for (int ii = 0; ii < BOARD_HEIGHT; ii++) {
  162. // No square on the starting position
  163. // FIX ME Change the 1 to a zero
  164. if (pos.x-1 == i && pos.y-1 == ii) { squares[i][ii] = new Square(color(240, 128, 128), 1); squares[i][ii].moveable = false; }
  165. else { squares[i][ii] = new Square(color(240, 128, 128), (int)random(MIN_RAND, MAX_RAND+1)); }
  166. }
  167. }
  168.  
  169. // Determines the first four operations
  170. for (int ii = 0; ii < 4; ii++) {
  171. operations[ii] = POSSIBLE_OPERATIONS[(int)random(POSSIBLE_OPERATIONS.length)];
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement