From 1917de87f451b55deb1bf7404228f76975df31c8 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 19 Jul 2017 21:55:02 +0100 Subject: [PATCH] Wrote a function that simulates the destruction of a geoblock. Pass it the index of the block in state that would be destroyed. The function will return whatever is left in state after triggering the destruction of said block. --- CMakeLists.txt | 2 +- src/disgaea_solve.d | 91 +++++++++++++++++++++++++++++++++++++++++++-- src/main.d | 6 +++ 3 files changed, 95 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fdfc028..50dc1c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 2.6 FATAL_ERROR) -list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake-d) +list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake-d/cmake-d) project(disgaea_solver D) add_executable(${PROJECT_NAME} diff --git a/src/disgaea_solve.d b/src/disgaea_solve.d index f3867df..eb970d5 100644 --- a/src/disgaea_solve.d +++ b/src/disgaea_solve.d @@ -1,8 +1,93 @@ module src.disgaea_solve; -struct Action { +import std.array; +import std.algorithm : remove; +import std.range; +import std.stdio; + +struct GeoPair { char block; - char tile; + char panel; } -Action[] disgaea_solve ( +struct PlyItem { + GeoPair[] geo_pair; + int start_item; +} + +private int find_starting_pair (GeoPair[] parState) { + int[char] seen_panels; + foreach (geo; parState) { + int* seen_panel = geo.panel in seen_panels; + if (seen_panel) + ++*seen_panel; + else + seen_panels[geo.panel] = 1; + } + + return 0; +} + +private int[] find_all_blocks_on_panel (GeoPair[] parState, char parPanel) { + int[] retval; + foreach (z, geo; parState) { + if (geo.panel == parPanel) + retval ~= cast(int)z; + } + return retval; +} + +private void morph (GeoPair[] parState, char parFrom, char parTo) { + foreach (ref geo; parState) { + if (geo.panel == parFrom) + geo.panel = parTo; + } +} + +private GeoPair[] simulate (GeoPair[] parState, int parDestroy) { + assert(parDestroy < parState.length); + int[] destroyed = [parDestroy]; + auto state = parState.dup; + + do { + assert(!destroyed.empty); + const auto current = destroyed.back; + destroyed.popBack(); + auto current_geo = state[current]; + assert(current >= 0 && current < state.length); + state = state.remove(current); + + auto affected = find_all_blocks_on_panel(state, current_geo.panel); + morph(state, current_geo.panel, current_geo.block); + destroyed ~= affected; + } while(!destroyed.empty); + + return state; +} + +private int evaluate_state (GeoPair[] parState, int parStart) { + int score = 0; + //foreach (geo; parState) { + // switch (geo.block) { + // case '@': + // if (geo.panel + //} + + return score; +} + +PlyItem[] disgaea_solve (GeoPair[] parState) { + const int starting_index = find_starting_pair(parState); + auto new_state = simulate(parState, starting_index); + + writeln("Original state:"); + foreach (geo; parState) { + writef("%s/%s, ", geo.block, geo.panel); + } + writeln("\nState after running simulation:"); + foreach (geo; new_state) { + writef("%s/%s, ", geo.block, geo.panel); + } + writeln(); + return new PlyItem[1]; +} diff --git a/src/main.d b/src/main.d index c147928..944edbc 100644 --- a/src/main.d +++ b/src/main.d @@ -13,5 +13,11 @@ int main (string[] parArgs) { return 2; } + disgaea_solve.disgaea_solve([ + GeoPair('b', 'r'), + GeoPair('y', 'b'), + GeoPair('g', 'r'), + GeoPair('@', 'g') + ]); return 0; }