added main_driver implementation and using visualizer
This commit is contained in:
parent
e92d64b59c
commit
e0158dd89d
2 changed files with 30 additions and 4 deletions
|
|
@ -31,6 +31,8 @@ unsigned short ui_prompt_for_disk_count();
|
||||||
*/
|
*/
|
||||||
void ui_branch(unsigned short 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.
|
/* Main function evaluates the number of command line arguments.
|
||||||
* If the user passed one main switches into test mode, i.e., that
|
* If the user passed one main switches into test mode, i.e., that
|
||||||
* the function test_branch() is called and the command line argument
|
* 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() {
|
unsigned short ui_prompt_for_disk_count() {
|
||||||
|
|
||||||
/* == YOUT IMPLEMENTATION GOES HERE == */
|
/* == 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 */
|
/* Hint: scanf for interactive disk count */
|
||||||
|
return disk_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ui_branch(unsigned short disk_count) {
|
void ui_branch(unsigned short disk_count) {
|
||||||
|
|
@ -71,7 +78,16 @@ void ui_branch(unsigned short disk_count) {
|
||||||
/* == YOUT IMPLEMENTATION GOES HERE == */
|
/* == YOUT IMPLEMENTATION GOES HERE == */
|
||||||
|
|
||||||
/* initialize the solver */
|
/* initialize the solver */
|
||||||
|
ts_init(disk_count);
|
||||||
|
|
||||||
/* solve the game */
|
/* 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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
16
toh_solver.c
16
toh_solver.c
|
|
@ -15,10 +15,13 @@
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "toh_disk.h"
|
#include "toh_disk.h"
|
||||||
#include "toh_board.h"
|
#include "toh_board.h"
|
||||||
|
#include "toh_visualizer.h"
|
||||||
|
|
||||||
/* ========================================================= */
|
/* ========================================================= */
|
||||||
/* Private functions */
|
/* Private functions */
|
||||||
|
|
||||||
|
static int moveCount = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Moves a single disk from the top of the 'source' rod to the 'target' rod.
|
* 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.
|
* 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)) {
|
if (!td_is_valid(disk)) {
|
||||||
return false;
|
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.
|
* @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) {
|
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) {
|
if (size == 1) {
|
||||||
return ts_move_disk(source, target);
|
return ts_move_disk(source, target);
|
||||||
}
|
}
|
||||||
|
|
@ -114,6 +121,9 @@ void ts_init(unsigned short disk_count) {
|
||||||
Disk disk = td_get_disk(i);
|
Disk disk = td_get_disk(i);
|
||||||
tb_push_disk(board, LEFT, disk);
|
tb_push_disk(board, LEFT, disk);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
moveCount = 0;
|
||||||
|
toh_init_visualizer(disk_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue