Example #1
0
  void newGame() {
    ptm = null;
    NewGame ngd = new NewGame(this);
    ngd.setVisible(true);
    if (ngd.gst == null) {
      if (firstrun) {
        bst = new BoardSettings();
        gst = new GameSettings();
        bst.setOrder(13);
        gst.setType(0);
        gst.swap = false;
        firstrun = false;
      } else return;
    } else {
      bst = ngd.bst;
      gst = ngd.gst;
      firstrun = false;
    }

    bst.findScale(getSize());
    x0 = bst.scale * r3 * (bst.getOrder() + 4) / 2;
    y0 = bst.scale * r3 * 2 + 50;

    data = new GameData(bst.getOrder());
    getCoordinates();
    /*if(gst.ai!=null)
    {
        gst.ai.flush();
        gst.ai.start();
    }*/
    gameActive = true;
    count = 0;
    lmv = null;
    AIMethods.init(bst.getOrder());
    if (gst.getType() == GameSettings.HUMAN_VS_AI) {
      if (gst.ai == null) gst.ai = new MiniMax(bst.getOrder(), (cst == 1) ? 2 : 1);
      gst.ai.init(this);
      userMove = true;
    }
    AePlayWave.q.clear();
    if (gst.audio) AePlayWave.play(AePlayWave.START);
    repaint();
  }
Example #2
0
  void place(int mx, int my) {
    if (drme) {
      drme = false;
      return;
    }

    int n = data.a.length;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (((mx - data.a[i][j].x) * (mx - data.a[i][j].x)
                    + (my - data.a[i][j].y) * (my - data.a[i][j].y)
                < bst.scale * bst.scale * .75)
            && data.a[i][j].getState() == 0) {
          if (count == 1 && gst.swap) {
            int ch =
                JOptionPane.showConfirmDialog(
                    this, "Swap moves?", "Swap", JOptionPane.YES_NO_OPTION);
            if (ch == JOptionPane.YES_OPTION) {
              lmv.setState(cst);
              switch (cst) {
                case 1:
                  data.a[i][j].setState(2);
                  break;
                case 2:
                  data.a[i][j].setState(1);
                  break;
              }
              repaint(
                  (int) (lmv.x - 0.8 * bst.scale),
                  (int) (lmv.y - 0.8 * bst.scale),
                  (int) (1.6 * bst.scale),
                  (int) (1.6 * bst.scale));
              if (AIMethods.gameOver(this, lmv.i, lmv.j, count)) {
                newGame();
              }
              if (gst.ai != null) gst.ai.updateAI(lmv.i, lmv.j, lmv.getState(), count);

            } else data.a[i][j].setState(cst);

          } else data.a[i][j].setState(cst);
          lmv = data.a[i][j];
          repaint(
              (int) (data.a[i][j].x - 0.8 * bst.scale),
              (int) (data.a[i][j].y - 0.8 * bst.scale),
              (int) (1.6 * bst.scale),
              (int) (1.6 * bst.scale));
          switch (cst) {
            case 1:
              cst = 2;
              break;
            case 2:
              cst = 1;
              break;
          }

          if (AIMethods.gameOver(this, lmv.i, lmv.j, count)) {
            System.out.println("Game Over");
            firstrun = true;
            String win = "";
            if (lmv.getState() == Block.RED_BEAD) {
              switch (gst.theme) {
                case 0:
                  win = "Red";
                  break;
                case 1:
                  win = "Black";
                  break;
                case 2:
                  win = "Gold";
                  break;
              }

            } else {
              switch (gst.theme) {
                case 0:
                  win = "Blue";
                  break;
                case 1:
                  win = "Green";
                  break;
                case 2:
                  win = "Silver";
                  break;
              }
            }
            if (gst.audio) AePlayWave.play(AePlayWave.END);
            JOptionPane.showMessageDialog(
                this, win + " wins", "Game Over", JOptionPane.INFORMATION_MESSAGE);
            newGame();
            return;
          }

          if (gst.audio) {
            if (gst.ai == null) AePlayWave.play(AePlayWave.PLACE);
            else if (userMove) AePlayWave.play(AePlayWave.AIMOVE);
          }
          count++;
          if (gst.ai != null && userMove) {
            userMove = false;
            gst.ai.nextMove(lmv.i, lmv.j, lmv.getState(), count);
          } else userMove = true;
          return;
        }
      }
    }
    if (gst.audio) AePlayWave.play(AePlayWave.BLOCK);
  }