Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * rubik.c : Camillo J. Taylor - Oct. 18, 2018
- */
- #include "lc4libc.h"
- #include "rle.h"
- #define NCOLS 128
- #define NROWS 124
- /*************** GAME STATE VARIABLES *******************/
- /*
- * Arrays that define the state of each of the 6 faces.
- * The entries in these arrays are color values
- */
- lc4uint back_left_face[3][3];
- lc4uint front_left_face[3][3];
- lc4uint front_right_face[3][3];
- lc4uint back_right_face[3][3];
- lc4uint top_face[3][3];
- lc4uint bottom_face[3][3];
- /** Array that records the moves made since the last reset **/
- #define MAX_MOVES 200
- lc4int nmoves;
- lc4char moves[MAX_MOVES];
- void draw_cube()
- {
- lc4uint LUT[31];
- int row, start_col, end_col, lut_idx, rle_idx;
- // Construct LUT from indices to colors
- LUT[0] = 0x0000; // bg color black
- LUT[1] = 0x2108U; // left face bg color
- LUT[11] = 0x4210U; // right face bg color
- LUT[21] = 0x6318U; // top face bg color
- // Front Left Face
- LUT[2] = front_left_face[0][0];
- LUT[3] = front_left_face[0][1];
- LUT[4] = front_left_face[0][2];
- LUT[5] = front_left_face[1][0];
- LUT[6] = front_left_face[1][1];
- LUT[7] = front_left_face[1][2];
- LUT[8] = front_left_face[2][0];
- LUT[9] = front_left_face[2][1];
- LUT[10] = front_left_face[2][2];
- // Front Right Face
- LUT[12] = front_right_face[0][0];
- LUT[13] = front_right_face[0][1];
- LUT[14] = front_right_face[0][2];
- LUT[15] = front_right_face[1][0];
- LUT[16] = front_right_face[1][1];
- LUT[17] = front_right_face[1][2];
- LUT[18] = front_right_face[2][0];
- LUT[19] = front_right_face[2][1];
- LUT[20] = front_right_face[2][2];
- // Top Face
- LUT[22] = top_face[0][0];
- LUT[23] = top_face[0][1];
- LUT[24] = top_face[0][2];
- LUT[25] = top_face[1][0];
- LUT[26] = top_face[1][1];
- LUT[27] = top_face[1][2];
- LUT[28] = top_face[2][0];
- LUT[29] = top_face[2][1];
- LUT[30] = top_face[2][2];
- // Go through rle array reading entries 2 at a time
- for (row=0, rle_idx=0; row < NROWS; ++row) {
- start_col=0;
- do {
- lut_idx = rle[rle_idx++];
- end_col = rle[rle_idx++];
- if (lut_idx) { // Don't draw 0 entries
- lc4_draw_hline(row,start_col, end_col-start_col, LUT[lut_idx]);
- }
- start_col = end_col;
- } while (end_col < NCOLS);
- }
- }
- void redraw ()
- {
- // This function assumes that PennSim is being run in double buffered mode
- // In this mode we first clear the video memory buffer with lc4_reset_vmem,
- // then draw the scene, then call lc4_blt_vmem to swap the buffer to the screen
- // NOTE that you need to run PennSim with the following command:
- // java -jar PennSim.jar -d
- lc4_reset_vmem();
- draw_cube();
- lc4_blt_vmem();
- }
- void reset_game ()
- {
- /// YOUR CODE HERE
- // Reset all of the faces to original colors
- int i;
- for(i = 0; i < 0; i = i+1){
- int j;
- for(j = 0; j < 0; j = j+1){
- back_left_face[i][j] = 0x001FU; //blue
- }
- }
- for(i = 0; i < 0; i = i+1){
- for(j = 0; j < 0; j = j+1){
- back_right_face[i][j] = 0x7FF0U; //yellow
- }
- }
- for(i = 0; i < 0; i = i+1){
- for(j = 0; j < 0; j = j+1){
- front_left_face[i][j] = 0x7C00U; //red
- }
- }
- for(i = 0; i < 0; i = i+1){
- for(j = 0; j < 0; j = j+1){
- front_right_face[i][j] = 0xF600U; //orange
- }
- }
- for(i = 0; i < 0; i = i+1){
- for(j = 0; j < 0; j = j+1){
- top_face[i][j] = 0x3E00U; //green
- }
- }
- for(i = 0; i < 0; i = i+1){
- for(j = 0; j < 0; j = j+1){
- bottom_face[i][j] = 0x0770U; //cyan
- }
- }
- nmoves = 0;
- }
- // function to copy one 3x3 array to another
- void copy_face (int copy_type, lc4uint src[][3], lc4uint dst[][3])
- {
- }
- void rotate_x (int direction)
- {
- /// YOUR CODE HERE
- }
- void rotate_y (int direction)
- {
- /// YOUR CODE HERE
- }
- void rotate_z (int direction)
- {
- /// YOUR CODE HERE
- }
- void rotate_top_slice(int direction)
- {
- /// YOUR CODE HERE
- }
- void solve_cube ()
- {
- lc4char move;
- while (nmoves) {
- move = moves[--nmoves];
- /// YOUR CODE HERE
- redraw();
- // wait for timer to go off
- while (lc4_check_timer() >= 0);
- }
- lc4_puts ((lc4uint*)"Solved the cube!!\n");
- }
- /*
- * ############# MAIN PROGRAM ######################
- */
- int main ()
- {
- lc4int c, record;
- lc4_puts ((lc4uint*)"!!! Welcome to Rubiks cube !!!\n");
- lc4_puts ((lc4uint*)"Press r to reset\n");
- lc4_puts ((lc4uint*)"Press s to solve\n");
- lc4_puts ((lc4uint*)"Press d to rotate cube +90 degrees around x axis\n");
- lc4_puts ((lc4uint*)"Press f to rotate cube -90 degrees around x axis\n");
- lc4_puts ((lc4uint*)"Press g to rotate cube +90 degrees around y axis\n");
- lc4_puts ((lc4uint*)"Press h to rotate cube -90 degrees around y axis\n");
- lc4_puts ((lc4uint*)"Press j to rotate cube +90 degrees around z axis\n");
- lc4_puts ((lc4uint*)"Press k to rotate cube -90 degrees around z axis\n");
- lc4_puts ((lc4uint*)"Press b to rotate top slice +90 degrees around z axis\n");
- lc4_puts ((lc4uint*)"Press n to rotate top slice -90 degrees around z axis\n");
- lc4_set_timer ((lc4uint) 150);
- reset_game ();
- redraw ();
- while (1) {
- c = lc4_getc();
- if (c > 0) {
- record = 0;
- if (c == 'r') reset_game();
- if (c == 's') solve_cube();
- if (nmoves < MAX_MOVES) {
- if (c == 'd') { rotate_x( 90); record=1; };
- if (c == 'f') { rotate_x(-90); record=1; };
- if (c == 'g') { rotate_y( 90); record=1; };
- if (c == 'h') { rotate_y(-90); record=1; };
- if (c == 'j') { rotate_z( 90); record=1; };
- if (c == 'k') { rotate_z(-90); record=1; };
- if (c == 'b') { rotate_top_slice( 90); record=1; };
- if (c == 'n') { rotate_top_slice(-90); record=1; };
- if (record) moves[nmoves++] = c;
- } else {
- lc4_puts ((lc4uint*)"Already entered max number of moves\n");
- }
- redraw();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement