Giter VIP home page Giter VIP logo

emoflon-ibex-examples's People

Contributors

31shubham avatar anthonyanjorin avatar daryazarkalam avatar harihachiko avatar patrickrobrecht avatar shub1989 avatar surbhi2111 avatar surendra2016 avatar vdeppe avatar vipasyan avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

emoflon-ibex-examples's Issues

Refactored Ibex-GT Handbook Example

Below follows a suggestion for refactoring the handbook example using additional intermediate methods.

Maybe, it could be sensible to introduce a dedicated generated "MatchEventSink/Listener" interface that provides suitable unimplemented or default-implemented methods for appearing and disappearing matches (e.g. MoveSokobanUpMatchEventListener::matchAppeared(MoveSokobanUpMatch) and MoveSokobanUpMatchEventListener::matchDisappeared(MoveSokobanUpMatch) )

package org.moflon.tutorial.sokobangamegui.rules;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

import SokobanLanguage.Board;
import SokobanLanguage.Field;
import SokobanLanguage.Figure;
import SokobanLanguage.Sokoban;
import SokobanRules.SokobanValidator;
import SokobanRules.api.SokobanRulesAPI;
import SokobanRules.api.matches.MoveSokobanUpMatch;
import SokobanRules.api.matches.PushBlockUpMatch;

public class SokobanRules
{
   private String allsWell = "Everything seems to be ok...";

   private SokobanRulesAPI api;

   // Keep track of all currently possible moves
   private Map<Field, Supplier<Result>> possibleMoves;

   public SokobanRules(Board board)
   {
      this.api = new SokobanValidator(board).initAPI();

      possibleMoves = new HashMap<>();

      api.moveSokobanUp().subscribeAppearing(m -> registerMoveSokobanUpMatch(m));
      api.moveSokobanUp().subscribeDisappearing(m -> unregisterSokobanMoveUpMatch(m));

      api.pushBlockUp().subscribeAppearing(m -> registerPushBlockUpMatch(m));
      api.pushBlockUp().subscribeDisappearing(m -> unregisterPushBlockUpMatch(m));
   }

   private void unregisterSokobanMoveUpMatch(MoveSokobanUpMatch m)
   {
      possibleMoves.remove(m.getTo());
   }

   private void registerMoveSokobanUpMatch(MoveSokobanUpMatch m)
   {
      Field targetField = m.getTo();
      registerRuleApplication(targetField, targetField, () -> api.moveSokobanUp().apply());
   }

   private void registerPushBlockUpMatch(PushBlockUpMatch m)
   {
      registerRuleApplication(m.getTo(), m.getNext(), () -> api.pushBlockUp().apply());
   }

   private Supplier<Result> unregisterPushBlockUpMatch(PushBlockUpMatch m)
   {
      return possibleMoves.remove(m.getTo());
   }

   // If the required field ("emptyField") is indeed empty, add the potential rule application
   private void registerRuleApplication(Field targetField, Field emptyField, Runnable applyRule)
   {
      if (!isEmpty(emptyField))
         possibleMoves.put(targetField, () -> {
            applyRule.run();
            return new Result(true, "Go Sokoban!");
         });
   }

   private boolean isEmpty(Field field)
   {
      return api.anOccupiedField().bindField(field).hasMatches();
   }

   public Result move(Figure figure, Field field)
   {

      // Refuse to do anything if the chosen figure is not Sokoban
      if (!(figure instanceof Sokoban))
         return new Result(false, "You can only move Sokoban!");

      if (possibleMoves.containsKey(field))
         return possibleMoves.get(field).get();
      else
         return new Result(false, "Sokoban can't move to " + "[" + field.getRow() + "," + field.getCol() + "]");
   }

   public Result validateBoard(Board board)
   {
      final int blockCount = api.oneBlock().countMatches();
      final int endPositionFieldsCount = api.oneEndField().countMatches();

      if (blockCount == 0)
         return new Result(false, "You must have at least one Block!");

      if (blockCount != endPositionFieldsCount)
         return new Result(false, "Number of blocks needs to equal number of end-position fields.");

      if (api.boulderOnEndField().countMatches() != 0)
         return new Result(false, "An end position is blocked by a boulder.");

      if (api.oneSokoban().countMatches() != 1)
         return new Result(false, "You must have exactly one Sokoban!");

      return new Result(true, allsWell);
   }
}

Sokoban: message if game is over

Idea: Pattern findBlockNotOnEndPosition matching a block not on an end position (can be used to introduce NACs?).

Subscribe to appearing/disappearing matches and maintain a set of blocks not on an end position. If the size of a set reaches 0, the game is over.

SokobanGUI: Error message remains after fixing problems

The error message produced by validateBoard remains visible even after fixing the problem in edit mode.

This could be confusing.

Maybe a more reactive validation would be helpful. At least, the validation errors should be removed as soon as the model is modified in edit mode.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.