Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. /*
  2. * rubik.c : Camillo J. Taylor - Oct. 18, 2018
  3. */
  4.  
  5. #include "lc4libc.h"
  6. #include "rle.h"
  7.  
  8. #define NCOLS 128
  9. #define NROWS 124
  10.  
  11. /*************** GAME STATE VARIABLES *******************/
  12.  
  13. /*
  14. * Arrays that define the state of each of the 6 faces.
  15. * The entries in these arrays are color values
  16. */
  17.  
  18. lc4uint back_left_face[3][3];
  19. lc4uint front_left_face[3][3];
  20. lc4uint front_right_face[3][3];
  21. lc4uint back_right_face[3][3];
  22.  
  23. lc4uint top_face[3][3];
  24. lc4uint bottom_face[3][3];
  25.  
  26.  
  27. /** Array that records the moves made since the last reset **/
  28. #define MAX_MOVES 200
  29.  
  30. lc4int nmoves;
  31. lc4char moves[MAX_MOVES];
  32.  
  33.  
  34. void draw_cube()
  35. {
  36. lc4uint LUT[31];
  37. int row, start_col, end_col, lut_idx, rle_idx;
  38.  
  39. // Construct LUT from indices to colors
  40. LUT[0] = 0x0000; // bg color black
  41.  
  42. LUT[1] = 0x2108U; // left face bg color
  43. LUT[11] = 0x4210U; // right face bg color
  44. LUT[21] = 0x6318U; // top face bg color
  45.  
  46. // Front Left Face
  47. LUT[2] = front_left_face[0][0];
  48. LUT[3] = front_left_face[0][1];
  49. LUT[4] = front_left_face[0][2];
  50.  
  51. LUT[5] = front_left_face[1][0];
  52. LUT[6] = front_left_face[1][1];
  53. LUT[7] = front_left_face[1][2];
  54.  
  55. LUT[8] = front_left_face[2][0];
  56. LUT[9] = front_left_face[2][1];
  57. LUT[10] = front_left_face[2][2];
  58.  
  59. // Front Right Face
  60. LUT[12] = front_right_face[0][0];
  61. LUT[13] = front_right_face[0][1];
  62. LUT[14] = front_right_face[0][2];
  63.  
  64. LUT[15] = front_right_face[1][0];
  65. LUT[16] = front_right_face[1][1];
  66. LUT[17] = front_right_face[1][2];
  67.  
  68. LUT[18] = front_right_face[2][0];
  69. LUT[19] = front_right_face[2][1];
  70. LUT[20] = front_right_face[2][2];
  71.  
  72. // Top Face
  73. LUT[22] = top_face[0][0];
  74. LUT[23] = top_face[0][1];
  75. LUT[24] = top_face[0][2];
  76.  
  77. LUT[25] = top_face[1][0];
  78. LUT[26] = top_face[1][1];
  79. LUT[27] = top_face[1][2];
  80.  
  81. LUT[28] = top_face[2][0];
  82. LUT[29] = top_face[2][1];
  83. LUT[30] = top_face[2][2];
  84.  
  85. // Go through rle array reading entries 2 at a time
  86. for (row=0, rle_idx=0; row < NROWS; ++row) {
  87. start_col=0;
  88. do {
  89. lut_idx = rle[rle_idx++];
  90. end_col = rle[rle_idx++];
  91.  
  92. if (lut_idx) { // Don't draw 0 entries
  93. lc4_draw_hline(row,start_col, end_col-start_col, LUT[lut_idx]);
  94. }
  95.  
  96. start_col = end_col;
  97. } while (end_col < NCOLS);
  98. }
  99. }
  100.  
  101. void redraw ()
  102. {
  103. // This function assumes that PennSim is being run in double buffered mode
  104. // In this mode we first clear the video memory buffer with lc4_reset_vmem,
  105. // then draw the scene, then call lc4_blt_vmem to swap the buffer to the screen
  106. // NOTE that you need to run PennSim with the following command:
  107. // java -jar PennSim.jar -d
  108.  
  109. lc4_reset_vmem();
  110.  
  111. draw_cube();
  112.  
  113. lc4_blt_vmem();
  114. }
  115.  
  116. void reset_game ()
  117. {
  118.  
  119. /// YOUR CODE HERE
  120.  
  121. // Reset all of the faces to original colors
  122. int i;
  123. for(i = 0; i < 0; i = i+1){
  124. int j;
  125. for(j = 0; j < 0; j = j+1){
  126. back_left_face[i][j] = 0x001FU; //blue
  127.  
  128. }
  129. }
  130.  
  131.  
  132. for(i = 0; i < 0; i = i+1){
  133. for(j = 0; j < 0; j = j+1){
  134. back_right_face[i][j] = 0x7FF0U; //yellow
  135.  
  136. }
  137. }
  138.  
  139. for(i = 0; i < 0; i = i+1){
  140. for(j = 0; j < 0; j = j+1){
  141. front_left_face[i][j] = 0x7C00U; //red
  142.  
  143. }
  144. }
  145.  
  146. for(i = 0; i < 0; i = i+1){
  147. for(j = 0; j < 0; j = j+1){
  148. front_right_face[i][j] = 0xF600U; //orange
  149.  
  150. }
  151. }
  152.  
  153. for(i = 0; i < 0; i = i+1){
  154. for(j = 0; j < 0; j = j+1){
  155. top_face[i][j] = 0x3E00U; //green
  156.  
  157. }
  158. }
  159.  
  160. for(i = 0; i < 0; i = i+1){
  161. for(j = 0; j < 0; j = j+1){
  162. bottom_face[i][j] = 0x0770U; //cyan
  163.  
  164. }
  165. }
  166.  
  167.  
  168. nmoves = 0;
  169.  
  170. }
  171.  
  172. // function to copy one 3x3 array to another
  173. void copy_face (int copy_type, lc4uint src[][3], lc4uint dst[][3])
  174. {
  175.  
  176.  
  177.  
  178. }
  179.  
  180.  
  181.  
  182. void rotate_x (int direction)
  183. {
  184.  
  185. /// YOUR CODE HERE
  186.  
  187. }
  188.  
  189.  
  190. void rotate_y (int direction)
  191. {
  192.  
  193. /// YOUR CODE HERE
  194.  
  195. }
  196.  
  197. void rotate_z (int direction)
  198. {
  199.  
  200. /// YOUR CODE HERE
  201.  
  202. }
  203.  
  204. void rotate_top_slice(int direction)
  205. {
  206.  
  207. /// YOUR CODE HERE
  208.  
  209. }
  210.  
  211. void solve_cube ()
  212. {
  213. lc4char move;
  214.  
  215. while (nmoves) {
  216. move = moves[--nmoves];
  217.  
  218. /// YOUR CODE HERE
  219.  
  220. redraw();
  221.  
  222. // wait for timer to go off
  223. while (lc4_check_timer() >= 0);
  224. }
  225.  
  226. lc4_puts ((lc4uint*)"Solved the cube!!\n");
  227. }
  228.  
  229.  
  230. /*
  231. * ############# MAIN PROGRAM ######################
  232. */
  233.  
  234. int main ()
  235. {
  236. lc4int c, record;
  237.  
  238. lc4_puts ((lc4uint*)"!!! Welcome to Rubiks cube !!!\n");
  239. lc4_puts ((lc4uint*)"Press r to reset\n");
  240. lc4_puts ((lc4uint*)"Press s to solve\n");
  241.  
  242. lc4_puts ((lc4uint*)"Press d to rotate cube +90 degrees around x axis\n");
  243. lc4_puts ((lc4uint*)"Press f to rotate cube -90 degrees around x axis\n");
  244.  
  245. lc4_puts ((lc4uint*)"Press g to rotate cube +90 degrees around y axis\n");
  246. lc4_puts ((lc4uint*)"Press h to rotate cube -90 degrees around y axis\n");
  247.  
  248. lc4_puts ((lc4uint*)"Press j to rotate cube +90 degrees around z axis\n");
  249. lc4_puts ((lc4uint*)"Press k to rotate cube -90 degrees around z axis\n");
  250.  
  251. lc4_puts ((lc4uint*)"Press b to rotate top slice +90 degrees around z axis\n");
  252. lc4_puts ((lc4uint*)"Press n to rotate top slice -90 degrees around z axis\n");
  253.  
  254.  
  255. lc4_set_timer ((lc4uint) 150);
  256.  
  257. reset_game ();
  258.  
  259. redraw ();
  260.  
  261. while (1) {
  262.  
  263. c = lc4_getc();
  264.  
  265. if (c > 0) {
  266. record = 0;
  267.  
  268. if (c == 'r') reset_game();
  269. if (c == 's') solve_cube();
  270.  
  271. if (nmoves < MAX_MOVES) {
  272.  
  273. if (c == 'd') { rotate_x( 90); record=1; };
  274. if (c == 'f') { rotate_x(-90); record=1; };
  275.  
  276. if (c == 'g') { rotate_y( 90); record=1; };
  277. if (c == 'h') { rotate_y(-90); record=1; };
  278.  
  279. if (c == 'j') { rotate_z( 90); record=1; };
  280. if (c == 'k') { rotate_z(-90); record=1; };
  281.  
  282. if (c == 'b') { rotate_top_slice( 90); record=1; };
  283. if (c == 'n') { rotate_top_slice(-90); record=1; };
  284.  
  285. if (record) moves[nmoves++] = c;
  286.  
  287. } else {
  288. lc4_puts ((lc4uint*)"Already entered max number of moves\n");
  289. }
  290.  
  291. redraw();
  292. }
  293. }
  294.  
  295. return 0;
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement