For this week's drills we will be playing a game called "Kyrel".
In this game, you will be given a row and asked to transform it into a different row. For instance, you might be given a row with five empty cells and asked to draw a blue dot in the right-most cell. Since you always start in the left-most cell (the default starting position), you'll need to move before you can draw.
The following special methods are available for traversing/transforming the row:
moveRight();
moveLeft();
useGreen();
useBlue();
draw();
erase();
onGreen();
onBlue();
Write a program which draws a green circle at the end of the row.
moveRight();
moveRight();
moveRight();
moveRight();
useGreen();
draw();
This would result in the following transformation:
['.', '.', '.', '.', '.'] // start row (initial state)
['.', '.', '.', '.', 'g'] // end row (end state)
- 'b' means a "blue" dot
- 'g' means a "green" dot
- '.' means an empty cell
- You must always begin in the left-most cell. This is your default starting position.
- You cannot fall off the board / row. If you try to move past the last cell, you just remain in the same cell.
- You must choose a color before you can draw.
- You do not need to erase before drawing -- you can overwrite a cell's color.
- Abstract the problem. Does your solution work for only a specific case, or does it work for all cases?
- "Don't repeat yourself" -- See a lot of repetition in your code? Maybe it's time to refactor.
- Be efficient. How many steps do your instructions take? How does the number of steps compare to the number of cells in the row? (What if the row got longer!?)
-
turn_3rd_cell_blue - solution
start: ['.', '.', '.', '.', '.']
finish: ['.', .', 'b', '.', '.'] -
erase_3rd_cell - solution
start: ['b', 'b', 'b', 'b', 'b']
finish: ['b', 'b', '.', 'b', 'b']
- every_other_erase - solution
start: ['b', 'b', 'b', 'b', 'b']
finish: ['b', '.', 'b', '.', 'b']
- every_other_blue - solution
start: ['g', 'g', 'g', '.', '.']
finish: ['g', 'b', 'g', 'b', '.']
- move_start_to_finish - solution
start: ['b', '.', '.', '.', '.']
finish: ['.', '.', '.', '.', 'b']
start: ['g', '.', '.', '.', '.']
finish: ['.', '.', '.', '.', 'g']
start: ['.', '.', '.', '.', '.']
finish: ['.', '.', '.', '.', '.']
- all_blue - solution
start: ['.', '.', '.', '.', '.']
finish: ['b', 'b', 'b', 'b', 'b']
- all_first_color - solution
start: ['b', '.', '.', '.', '.']
finish: ['b', 'b', 'b', 'b', 'b']
start: ['g', '.', '.', '.', '.']
finish: ['g', 'g', 'g', 'g', 'g']
- n_in_a_row - solution
start: ['.', '.', '.', '.', '.']
finish: ['b', 'b', '.', '.', '.'] (given n is 2)
finish: ['b', 'b', 'b', '.', '.'] (given n is 3)
- every_even_erase - solution
start: ['b', 'b', 'b', 'b', 'b']
finish: ['.', 'b', '.', 'b', '.']
- every_odd_erase - solution
start: ['b', 'b', 'b', 'b', 'b']
finish: ['b', '.', 'b', '.', 'b']
Before you get started on today's exercises, please download the Kyrel App, either by cloning it, or downloading the zip file.
- every_even_erase - solution
start: ['b', 'b', 'b', 'b', 'b']
finish: ['.', 'b', '.', 'b', '.']
- every_n_erase - solution
start: ['b', 'b', 'b', 'b', 'b']
finish: ['b', 'b', 'b', '.', 'b'] (given n is 4)
finish: ['b', 'b', 'b', 'b', '.'] (given n is 5)
- move_the_blue_dot_one_to_the_right - solution
start: ['.', '.', 'b', '.', '.']
finish: ['.', '.', '.', 'b', '.']
start: ['g', 'b', '.', '.', 'g']
finish: ['g', '.', 'b', '.', 'g']
- delete_before - solution
start: [ 'g', 'b', '.', '.', '.' ]
finish: [ '.', 'b', '.', '.', '.' ]
start: [ 'g', 'b', 'b', '.', 'b' ]
finish: [ '.', '.', 'b', '.', 'b' ]
- bleed_right - solution
start: ['.', '.', 'b', '.', '.']
finish: ['.', '.', 'b', 'b', 'b']
- invert_colors - solution
start: ['b', 'g', 'g', 'b', 'b']
finish: ['g', 'b', 'b', 'g', 'g']
start: ['b', '.', 'g', '.', 'b']
finish: ['g', '.', 'b', '.', 'g']
- pull_blues_right - solution
start: ['b', '.', 'b', '.', '.']
finish: ['.', '.', '.', 'b', 'b']
- pull_blues_left - solution
start: ['b', 'g', 'g', 'b', 'b']
finish: ['b', 'b', 'b', 'g', 'g']
- reverse_row - solution
start: ['b', 'g', 'g', '.', 'g']
finish: ['g', '.', 'g', 'g', 'b']