Package control

Source Code of control.Util

package control;

import model.Constants;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;

import view.MemoryCard;
import view.resources.Images;

public class Util {

  private static int iX;
  private static int iY;
  private static int iWidth;
  private static int iHeight;

  public static void centerFrame(Shell shell) {

    Monitor primary = shell.getDisplay().getPrimaryMonitor();

    iHeight = primary.getClientArea().height;
    iWidth = primary.getClientArea().width;
    iX = (iWidth / 2) - (shell.getBounds().width / 2);
    iY = (iHeight / 2) - (shell.getBounds().height / 2);

    shell.setLocation(iX, iY);
  }

  public static void checkCards() {

    if (Storage.getInstance().getCards().size() == 2) {
      Storage.getInstance().increaseTurn();
      endTurn();
    }
  }

  public static void closeWindow(Shell shell) {
    shell.dispose();

  }

  private static void endTurn() {
    Storage.getInstance().setTurnPause(true);
    if (validateCards()) {
      if (Storage.getInstance().isPlayerTurn()) {
        Storage.getInstance().increasePlayerPair();
        for (MemoryCard element : Storage.getInstance().getCards()) {
          element.getButton()
              .setBackground(
                  Display.getCurrent().getSystemColor(
                      SWT.COLOR_BLUE));
        }
      } else {
        Storage.getInstance().increaseEnemyPair();
        for (MemoryCard element : Storage.getInstance().getCards()) {
          element.getButton().setBackground(
              Display.getCurrent().getSystemColor(SWT.COLOR_RED));
        }
      }
    } else {

      if (Storage.getInstance().isPlayerTurn()) {
        Storage.getInstance().setPlayerTurn(false);
        Storage.getInstance()
            .getCurrentPlayer()
            .setText(
                Constants.CURRENTPLAYER
                    + Storage.getInstance().getEnemyName());
      } else {
        Storage.getInstance().setPlayerTurn(true);
        Storage.getInstance()
            .getCurrentPlayer()
            .setText(
                Constants.CURRENTPLAYER
                    + Storage.getInstance().getPlayerName());
      }

      for (MemoryCard element : Storage.getInstance().getCards()) {

        element.getButton().setImage(Images.DEFAULT.createImage());
        element.getButton().update();
        element.setbActive(false);
        try {
          Thread.sleep(500);
        } catch (InterruptedException e) {
        }

      }
    }

    Storage.getInstance().getCards().clear();
    Storage.getInstance().setTurnPause(false);
    Storage.getInstance()
        .getTurnCount()
        .setText(
            Constants.CURRENTTURNSCOUNT
                + Storage.getInstance().getTurnsCount());
    if (isGameDone()) {
      String winner;
      if (Storage.getInstance().getEnemyPairCount() < Storage
          .getInstance().getPlayerPairCount()) {
        winner = Storage.getInstance().getPlayerName();
      } else if (Storage.getInstance().getEnemyPairCount() == Storage
          .getInstance().getPlayerPairCount()) {
        winner = Constants.BOTHWIN;
      } else {
        winner = Storage.getInstance().getEnemyName();
      }

      MessageBox winnerMessage = new MessageBox(Display.getCurrent()
          .getActiveShell(), SWT.OK | SWT.APPLICATION_MODAL
          | SWT.ICON_INFORMATION);

      winnerMessage.setText(Constants.WINNERTITLE);
      if (winner.equals(Constants.BOTHWIN)) {
        winnerMessage.setMessage(Constants.BOTHWIN);
      } else {
        winnerMessage.setMessage(Constants.WINNERIS + winner);
      }
      winnerMessage.open();

    }
  }

  public static void resetStats() {
    Storage.getInstance().clearEnemyTurns();
    Storage.getInstance().clearPlayerTurns();
    Storage.getInstance().clearTurns();

  }

  public static int getRandomNumber(int low, int high) {

    return (int) ((Math.random() * (high - low)) + low);

  }

  private static boolean isGameDone() {
    int x = Storage.getInstance().getSize();
    if ((Storage.getInstance().getPlayerPairCount() + Storage.getInstance()
        .getEnemyPairCount()) == ((x * x) / 2)) {
      return true;
    }
    return false;
  }

  private static boolean validateCards() {
    MemoryCard one = Storage.getInstance().getCards().get(0);
    MemoryCard two = Storage.getInstance().getCards().get(1);

    if (one.getImageNumber() == two.getImageNumber()) {
      return true;
    }
    return false;

  }

  public static boolean validateInput(String text) {
    if (text.trim().isEmpty() || (text == null)) {
      return false;
    }
    return true;

  }

  private Util() {

  }

}
TOP

Related Classes of control.Util

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.