From e0158dd89df45af71121add764a499fc5ed814ad Mon Sep 17 00:00:00 2001 From: MarcUs7i Date: Mon, 20 Jan 2025 08:56:16 +0100 Subject: [PATCH] added main_driver implementation and using visualizer --- toh_main_driver.c | 18 +++++++++++++++++- toh_solver.c | 16 +++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/toh_main_driver.c b/toh_main_driver.c index 10b99b9..bc0938b 100644 --- a/toh_main_driver.c +++ b/toh_main_driver.c @@ -31,6 +31,8 @@ unsigned short ui_prompt_for_disk_count(); */ void ui_branch(unsigned short disk_count); +static unsigned short get_disk_count(char* input); + /* Main function evaluates the number of command line arguments. * If the user passed one main switches into test mode, i.e., that * the function test_branch() is called and the command line argument @@ -62,8 +64,13 @@ int main(int argc, char *argv[]) unsigned short ui_prompt_for_disk_count() { /* == YOUT IMPLEMENTATION GOES HERE == */ + char input[64]; + printf("Please enter the number of disks: "); + scanf("%s", input); + unsigned short disk_count = get_disk_count(input); /* Hint: scanf for interactive disk count */ + return disk_count; } void ui_branch(unsigned short disk_count) { @@ -71,7 +78,16 @@ void ui_branch(unsigned short disk_count) { /* == YOUT IMPLEMENTATION GOES HERE == */ /* initialize the solver */ + ts_init(disk_count); /* solve the game */ - + ts_solve(); +} + +static unsigned short get_disk_count(char* input) { + unsigned short disk_count = 0; + if (input != 0) { + disk_count = strtoul(input, 0, 10); + } + return disk_count; } diff --git a/toh_solver.c b/toh_solver.c index e1155e3..c6ba8a2 100644 --- a/toh_solver.c +++ b/toh_solver.c @@ -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); } /**