Android Open Source - abalone-android Scenario






From Project

Back to project page abalone-android.

License

The source code is released under:

GNU General Public License

If you think the Android project abalone-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
* Copyright (c) 2010-2011 Yaroslav Geryatovich, Alexander Yakushev
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*//*from  w  ww  . j  a va 2  s.c  o m*/
package com.bytopia.abalone;

import android.util.Log;

import com.bytopia.abalone.mechanics.Board;
import com.bytopia.abalone.mechanics.Group;
import com.bytopia.abalone.mechanics.Move;

public class Scenario {

  final static int PAUSE = 1000;
  final static int HALF_PAUSE = 500;
  final static int NONE = 0;

  static Object monitor = new Object();

  private Action[] actions;
  private boolean active = false;
  private Thread thread;

  TutorialBoardView boardView;
  private Board initial;

  public class Action {

    protected Object parameters;
    public int pause;

    public Action() {
    }

    public Action construct(Object parameters, int pause) {
      this.parameters = parameters;
      this.pause = pause;
      return this;
    }

    public void perform() {
    }
  }

  public class RestoreBoard extends Action {

    @Override
    public void perform() {
      Log.d("restore", "Board" + initial);
      boardView.setBoard(initial.clone());
    }
  }

  public class MakeMove extends Action {

    @Override
    public void perform() {
      boardView.setSelected(null);
      boardView.setHighlighted(null);
      boardView.makeMove((Move) parameters);
    }
  }

  public class SelectGroup extends Action {

    @Override
    public void perform() {
      boardView.setSelected((Group) parameters);
      boardView.postInvalidate();
    }
  }
  
  public class HighlightGroup extends Action {

    @Override
    public void perform() {
      boardView.setHighlighted((Group) parameters);
      boardView.postInvalidate();
    }
  }

  public Scenario(TutorialBoardView boardView, Board initialPosition) {
    this.boardView = boardView;
    initial = initialPosition;
  }

  public void SetActions(final Action... actions) {
    this.actions = actions;
  }

  public void playScenario(final Action[] actions) {
    final Scenario scenario = this;
    thread = new Thread() {
      @Override
      public void run() {
        synchronized (monitor) {
          try {
            synchronized (scenario) {
              scenario.notify();
            }
            boardView.setHighlighted(null);
            boardView.setSelected(null);
            Log.d("thread", "Called setBoard from scenarion thread, board" + initial);
            boardView.setBoard(initial.clone());
            Thread.sleep(PAUSE);
            while (active && actions != null) {
              for (Action a : actions) {
                if (!active) {
                  return;
                }
                a.perform();
                Thread.sleep(a.pause);
              }
            }
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    };
    thread.start();
  }

  public void start() {
    active = true;
    synchronized (this) {
      playScenario(actions);
      try {
        this.wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  public void stop() {
    active = false;
  }
}




Java Source Code List

com.bytopia.abalone.BoardRenderer.java
com.bytopia.abalone.BoardView.java
com.bytopia.abalone.GameActivity.java
com.bytopia.abalone.GameOptionsActivity.java
com.bytopia.abalone.LoseBallsView.java
com.bytopia.abalone.MainMenuActivity.java
com.bytopia.abalone.Options.java
com.bytopia.abalone.Scenario.java
com.bytopia.abalone.SelectLayoutActivity.java
com.bytopia.abalone.SplashAcitvity.java
com.bytopia.abalone.TutorialActivity.java
com.bytopia.abalone.TutorialBoardView.java
com.bytopia.abalone.mechanics.AiAnn.java
com.bytopia.abalone.mechanics.AiBeatrice.java
com.bytopia.abalone.mechanics.AiCharlotte.java
com.bytopia.abalone.mechanics.AiDeborah.java
com.bytopia.abalone.mechanics.ArtificialIntilligence.java
com.bytopia.abalone.mechanics.BelgianLayout.java
com.bytopia.abalone.mechanics.Board.java
com.bytopia.abalone.mechanics.Cell.java
com.bytopia.abalone.mechanics.ClassicLayout.java
com.bytopia.abalone.mechanics.ConsoleWatcher.java
com.bytopia.abalone.mechanics.Debug.java
com.bytopia.abalone.mechanics.Direction.java
com.bytopia.abalone.mechanics.EmptyLayout.java
com.bytopia.abalone.mechanics.Game.java
com.bytopia.abalone.mechanics.GermanLayout.java
com.bytopia.abalone.mechanics.Group.java
com.bytopia.abalone.mechanics.Layout.java
com.bytopia.abalone.mechanics.MoveType.java
com.bytopia.abalone.mechanics.Move.java
com.bytopia.abalone.mechanics.Player.java
com.bytopia.abalone.mechanics.Side.java
com.bytopia.abalone.mechanics.TestLayout.java
com.bytopia.abalone.mechanics.Watcher.java