예제 #1
0
  @Override
  public String visitGameend(@NotNull DiceGameParser.GameendContext ctx) {
    if (ctx.MULTI != null) {
      return "print('Die Spieler '+','.join([self.name for self in game.players if "
          + ctx.COND.accept(this)
          + "])+' haben gewonnen!')";
    }

    if (ctx.SINGLE != null) {
      return "print([self.name for self in game.players if "
          + ctx.COND.accept(this)
          + "][0]+' hat gewonnen!')";
    }

    if (ctx.MOST != null) {
      return "print(str([player.name for player in game.players if player."
          + ctx.VAR.getText()
          + " == (max([self."
          + ctx.VAR.getText()
          + " for self in game.players]))][0]) +' hat gewonnen!')";
    }

    if (ctx.LEAST != null) {
      return "print(str([player.name for player in game.players if player."
          + ctx.VAR.getText()
          + " == (min([self."
          + ctx.VAR.getText()
          + " for self in game.players]))][0]) +' hat gewonnen!')";
    }

    return "visitGameend";
  }
예제 #2
0
  @Override
  public String visitGameinit(@NotNull DiceGameParser.GameinitContext ctx) {

    if (ctx.ASSN != null) {
      return ctx.ASSN.accept(this) + "\n";
    }

    if (ctx.FROM != null && ctx.TO != null) {
      return "self.min = " + ctx.FROM.getText() + "\nself.max = " + ctx.TO.getText();
    }

    if (ctx.FROM != null) {
      return "self.min = " + ctx.FROM.getText() + "\nself.max = " + ctx.FROM.getText();
    }

    if (ctx.DICEINIT != null) {

      StringBuilder result = new StringBuilder();
      result.append("self.dices = [");
      for (ParseTree diceInit : ctx.children) {
        if (diceInit.getClass() == DiceGameParser.DiceinitContext.class) {
          result.append(diceInit.accept(this) + ", ");
        }
      }
      result.append("]\n");
      return result.toString();
    }
    if (ctx.COND != null) {
      return "def isRunning(self): return (" + ctx.COND.accept(this) + ")";
    }

    return "visitGameinit";
  }
예제 #3
0
  @Override
  public String visitLaw(@NotNull DiceGameParser.LawContext ctx) {
    StringBuilder ifelse = new StringBuilder();

    String condStr = ctx.COND.accept(this); // visitChildren(ctx.COND);
    String thenStr = ctx.THEN.accept(this); // visitChildren(ctx.THEN);

    ifelse.append("if " + condStr + ":\n");
    ifelse.append(indent(thenStr));

    if (ctx.ELSE != null) ifelse.append("else:\n" + indent(ctx.ELSE.accept(this)));

    return ifelse.toString();
  }