Skip to content

Commit

Permalink
refactor: move the scramble generation
Browse files Browse the repository at this point in the history
  • Loading branch information
RaZeSloth committed Jan 27, 2023
1 parent 710b08f commit 5adcfbe
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions src/rubiks_cube_scrambler/rubiks_cube_scrambler.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <input/input.h>
#include <gui/elements.h>
#include <furi_hal.h>
#include <gui/modules/submenu.h>

#include "scrambler.h"
#include "furi_hal_random.h"
Expand All @@ -23,17 +22,24 @@ static void success_vibration()
furi_hal_vibro_on(false);
return;
}
void split_array(char original[], int size, char first[], char second[]) {
void split_array(char original[], int size, char first[], char second[])
{
int32_t mid = size / 2;
if (size % 2 != 0) {
if (size % 2 != 0)
{
mid++;
}
int32_t first_index = 0, second_index = 0;
for (int32_t i = 0; i < size; i++) {
if (i < mid) {
for (int32_t i = 0; i < size; i++)
{
if (i < mid)
{
first[first_index++] = original[i];
} else {
if (i == mid && (original[i] == '2' || original[i] == '\'')) {
}
else
{
if (i == mid && (original[i] == '2' || original[i] == '\''))
{
continue;
}
second[second_index++] = original[i];
Expand All @@ -42,26 +48,19 @@ void split_array(char original[], int size, char first[], char second[]) {
first[first_index] = '\0';
second[second_index] = '\0';
}

void genScramble()
{
scrambleReplace();
strcpy(scramble_str, printData());
split_array(scramble_str, strlen(scramble_str), scramble_start, scramble_end);
}

static void draw_callback(Canvas *canvas, void *ctx)
{
UNUSED(ctx);
canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 4, 13, "Rubik's Cube Scrambler");

if (scrambleStarted)
{
scrambleReplace();
strcpy(scramble_str, printData());
if (notifications_enabled)
{
success_vibration();
}
split_array(scramble_str, strlen(scramble_str), scramble_start, scramble_end);
scrambleStarted = 0;
}
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 64, 28, AlignCenter, AlignCenter, scramble_start);
canvas_draw_str_aligned(canvas, 64, 38, AlignCenter, AlignCenter, scramble_end);
Expand All @@ -72,6 +71,7 @@ static void draw_callback(Canvas *canvas, void *ctx)

static void input_callback(InputEvent *input_event, void *ctx)
{

furi_assert(ctx);
FuriMessageQueue *event_queue = ctx;
furi_message_queue_put(event_queue, input_event, FuriWaitForever);
Expand Down Expand Up @@ -99,7 +99,11 @@ int32_t rubiks_cube_scrambler_main(void *p)

if (event.key == InputKeyOk && event.type == InputTypeShort)
{
scrambleStarted = true;
genScramble();
if (notifications_enabled)
{
success_vibration();
}
}
if (event.key == InputKeyLeft && event.type == InputTypeShort)
{
Expand All @@ -122,7 +126,7 @@ int32_t rubiks_cube_scrambler_main(void *p)
furi_message_queue_free(event_queue);

gui_remove_view_port(gui, view_port);

view_port_free(view_port);
furi_record_close(RECORD_GUI);
return 0;
Expand Down

0 comments on commit 5adcfbe

Please sign in to comment.