added main_driver implementation and using visualizer

This commit is contained in:
MarcUs7i 2025-01-20 08:56:16 +01:00
parent e92d64b59c
commit e0158dd89d
2 changed files with 30 additions and 4 deletions

View file

@ -15,10 +15,13 @@
#include "config.h"
#include "toh_disk.h"
#include "toh_board.h"
#include "toh_visualizer.h"
/* ========================================================= */
/* Private functions */
static int moveCount = 0;
/**
* Moves a single disk from the top of the 'source' rod to the 'target' rod.
* The move shall only be performed if it is allowed.
@ -38,8 +41,14 @@ static bool ts_move_disk(RodName source, RodName target) {
if (!td_is_valid(disk)) {
return false;
}
bool success = tb_push_disk(tb_get_board(), target, disk);
return tb_push_disk(tb_get_board(), target, disk);
if (success) {
moveCount++;
toh_visualize(tb_get_board(), moveCount);
}
return success;
}
/**
@ -55,8 +64,6 @@ static bool ts_move_disk(RodName source, RodName target) {
* @return True if the move was successful and according to the rules, false otherwise.
*/
static bool ts_move_stack(unsigned short size, RodName source, RodName intermediate, RodName target) {
// inefficient implementation as it makes lots of unnecessary moves, if the number of disks is large
// but it's easier to implement
if (size == 1) {
return ts_move_disk(source, target);
}
@ -114,6 +121,9 @@ void ts_init(unsigned short disk_count) {
Disk disk = td_get_disk(i);
tb_push_disk(board, LEFT, disk);
}
moveCount = 0;
toh_init_visualizer(disk_count);
}
/**