Example #1
0
File: AI.java Project: giurra8/Hex
 private ArrayList<Cell> enemyCells() {
   ArrayList<Cell> enemyCells = new ArrayList<>();
   for (Cell c : Frame.getInstance().getBoard().getCells()) {
     if (c.hasToken())
       if (c.getTok().getOwner() != this) {
         enemyCells.add(c);
       }
   }
   return enemyCells;
 }
Example #2
0
File: AI.java Project: giurra8/Hex
  public CellToken doAction() {
    Cell randomCell = null;
    Token randomToken = null;
    CellToken randomCellToken = null;
    ArrayList<Cell> cells = Frame.getInstance().getBoard().getCells();
    Random r = new Random();
    switch (dif) {
      case EASY:
        {
          int index = r.nextInt(cells.size());
          while (cells.get(index).hasToken()) index = r.nextInt(cells.size());
          randomCell = cells.get(index);
          index = r.nextInt(tokens.size());
          randomToken = tokens.get(index);
          break;
        }

      case HARD:
        {
          int max = 0;
          ArrayList<CellToken> cellToken = new ArrayList<>();
          ArrayList<Cell> enemyCells = enemyCells();
          Map<Cell, ArrayList<Cell>> neighborCells = Frame.getInstance().getNeighborCells();
          for (Cell c : enemyCells) {
            for (Cell nc : neighborCells.get(c)) {
              if (nc != null)
                if (!nc.hasToken()) {
                  for (Token t : tokens) {
                    proveraZaToken(nc, t);
                    for (Integer i : t.getBrojpromena()) {
                      if (max <= i) {
                        max = i;
                        cellToken.add(new CellToken(nc, t, max));
                      }
                    }
                    t.setBrojpromena(new ArrayList<Integer>());
                    Map<Integer, Cell> map = new HashMap<>();
                    t.setNumberToOwn(map);
                  }
                }
            }
            max = 0;
          }

          for (CellToken ct : cellToken) {
            if (max <= ct.brojOkrenutih) max = ct.brojOkrenutih;
          }
          int zbir = 29;
          for (CellToken ct : cellToken) {
            if (ct.brojOkrenutih == max)
              if (ct.token.getZbir() < zbir) {
                zbir = ct.token.getZbir();
                randomCellToken = ct;
              }
          }

          randomCell = randomCellToken.cell;
          randomToken = randomCellToken.token;

          break;
        }
      case MEDIUM:
        {
          int max = 0;

          ArrayList<Cell> enemyCells = enemyCells();
          Map<Cell, ArrayList<Cell>> neighborCells = Frame.getInstance().getNeighborCells();
          for (Cell c : enemyCells) {
            for (Cell nc : neighborCells.get(c)) {
              if (nc != null)
                if (!nc.hasToken()) {
                  for (Token t : tokens) {
                    Map numberToOwn = t.getNumberToOwn();
                    proveraZaToken(nc, t);
                    for (Integer i : t.getBrojpromena()) {
                      if (max <= i) {
                        max = i;
                        randomToken = t;
                        randomCell = (Cell) numberToOwn.get(max);
                      }
                    }
                    t.setBrojpromena(new ArrayList<>());
                    Map<Integer, Cell> map = new HashMap<>();
                    t.setNumberToOwn(map);
                  }
                  // System.out.println(""+max);
                }
            }
            max = 0;
          }

          break;
        }
    }
    return new CellToken(randomCell, randomToken);
  }
Example #3
0
File: AI.java Project: giurra8/Hex
  public void proveraZaToken(Cell c, Token t) {
    int i0 = 0, i1 = 0, i2 = 0;
    Map numberToOwn = t.getNumberToOwn();
    ArrayList<Cell> possibleCellsforToken = t.getPossibleCells();
    possibleCellsforToken.add(c);
    Frame frejm = Frame.getInstance();
    int val = 0;

    for (TokenField field : t.getTcells()) {
      if (field.getId() == 1) {
        if (c.getIdx() % 2 == 0) {
          i0 = t.getNiz()[0];

          if (frejm.getCellById(c.getIdx() - 1, c.getIdy()) != null
              && frejm.getCellById(c.getIdx() - 1, c.getIdy()).getTok() != null) {
            i1 = frejm.getCellById(c.getIdx() - 1, c.getIdy()).getTok().getNiz()[1];
            if (i0 > i1 && i1 != 0) val++;
          }
          if (frejm.getCellById(c.getIdx() - 1, c.getIdy() + 1) != null
              && frejm.getCellById(c.getIdx() - 1, c.getIdy() + 1).getTok() != null) {
            i2 = frejm.getCellById(c.getIdx() - 1, c.getIdy() + 1).getTok().getNiz()[2];
            if (i0 > i2 && i2 != 0) val++;
          }

        } else {
          i0 = t.getNiz()[0];
          if (frejm.getCellById(c.getIdx() - 1, c.getIdy() - 1) != null
              && frejm.getCellById(c.getIdx() - 1, c.getIdy() - 1).getTok() != null) {
            i1 = frejm.getCellById(c.getIdx() - 1, c.getIdy() - 1).getTok().getNiz()[1];
            if (i0 > i1 && i1 != 0) val++;
          }
          if (frejm.getCellById(c.getIdx() - 1, c.getIdy()) != null
              && frejm.getCellById(c.getIdx() - 1, c.getIdy()).getTok() != null) {
            i2 = frejm.getCellById(c.getIdx() - 1, c.getIdy()).getTok().getNiz()[2];

            if (i0 > i2 && i2 != 0) val++;
          }
        }
      } else if (field.getId() == 2) {
        if (c.getIdx() % 2 == 0) {
          i1 = t.getNiz()[1];
          if (frejm.getCellById(c.getIdx() + 1, c.getIdy() + 1) != null
              && frejm.getCellById(c.getIdx() + 1, c.getIdy() + 1).getTok() != null) {
            i0 = frejm.getCellById(c.getIdx() + 1, c.getIdy() + 1).getTok().getNiz()[0];
            if (i1 > i0 && i0 != 0) val++;
          }
          if (frejm.getCellById(c.getIdx(), c.getIdy() + 1) != null
              && frejm.getCellById(c.getIdx(), c.getIdy() + 1).getTok() != null) {
            i2 = frejm.getCellById(c.getIdx(), c.getIdy() + 1).getTok().getNiz()[2];
            if (i1 > i2 && i2 != 0) val++;
          }

        } else {
          i1 = t.getNiz()[1];
          if (frejm.getCellById(c.getIdx() + 1, c.getIdy()) != null
              && frejm.getCellById(c.getIdx() + 1, c.getIdy()).getTok() != null) {
            i0 = frejm.getCellById(c.getIdx() + 1, c.getIdy()).getTok().getNiz()[0];

            if (i1 > i0 && i0 != 0) val++;
          }
          if (frejm.getCellById(c.getIdx(), c.getIdy() + 1) != null
              && frejm.getCellById(c.getIdx(), c.getIdy() + 1).getTok() != null) {
            i2 = frejm.getCellById(c.getIdx(), c.getIdy() + 1).getTok().getNiz()[2];
            if (i1 > i2 && i2 != 0) val++;
          }
        }
      } else if (field.getId() == 3) {
        if (c.getIdx() % 2 == 0) {

          i2 = t.getNiz()[2];
          if (frejm.getCellById(c.getIdx() + 1, c.getIdy()) != null
              && frejm.getCellById(c.getIdx() + 1, c.getIdy()).getTok() != null) {
            i0 = frejm.getCellById(c.getIdx() + 1, c.getIdy()).getTok().getNiz()[0];
            if (i2 > i0 && i0 != 0) val++;
          }
          if (frejm.getCellById(c.getIdx(), c.getIdy() - 1) != null
              && frejm.getCellById(c.getIdx(), c.getIdy() - 1).getTok() != null) {
            i1 = frejm.getCellById(c.getIdx(), c.getIdy() - 1).getTok().getNiz()[1];
            if (i2 > i1 && i1 != 0) val++;
          }

        } else {
          i2 = t.getNiz()[2];
          if (frejm.getCellById(c.getIdx() + 1, c.getIdy() - 1) != null
              && frejm.getCellById(c.getIdx() + 1, c.getIdy() - 1).getTok() != null) {
            i0 = frejm.getCellById(c.getIdx() + 1, c.getIdy() - 1).getTok().getNiz()[0];
            if (i2 > i0 && i0 != 0) val++;
          }
          if (frejm.getCellById(c.getIdx(), c.getIdy() - 1) != null
              && frejm.getCellById(c.getIdx(), c.getIdy() - 1).getTok() != null) {
            i1 = frejm.getCellById(c.getIdx(), c.getIdy() - 1).getTok().getNiz()[1];
            if (i2 > i1 && i1 != 0) val++;
          }
        }
      }
    }
    Integer number = val;
    t.getBrojpromena().add(val);
    numberToOwn.put(number, c);
  }
Example #4
0
/**
 * Tato třída reprezentuje formulář pomocí kterého se vyplňují údaje při změně údajů knihy
 *
 * @see Controler.ConfirmModifyForm
 * @author Suchý Vojtěch
 */
public class ModifyForm {

  private ModifyForm() {}

  private static ModifyForm form = null;
  private static JFrame frame = Frame.getInstance();
  private static JButton OKbt = new JButton("OK");
  public static final JTextField tfAutor = new JTextField(15);
  public static final JTextField tfName = new JTextField(15);
  public static final JTextField tfDate = new JTextField(15);
  private static final JLabel lbAutor = new JLabel("Autor:");
  private static final JLabel lbName = new JLabel("Name:");
  private static final JLabel lbDate = new JLabel("Date:");
  public static JDialog dialog = new JDialog(frame, "Enter the data", true);

  public static ModifyForm getInstance() {
    if (form == null) {
      form = new ModifyForm();
      nastaveni();
      return form;
    } else {
      return form;
    }
  }

  private static void nastaveni() {
    OKbt.addActionListener(new ConfirmModifyForm());
    dialog.add(lbName);
    dialog.add(tfName);
    dialog.add(lbAutor);
    dialog.add(tfAutor);
    dialog.add(lbDate);
    dialog.add(tfDate);
    dialog.add(new JSeparator());
    dialog.add(OKbt);

    dialog.setLayout(new FlowLayout());
    dialog.setSize(200, 250);
    dialog.setLocation(frame.getLocation());
  }

  /**
   * Metoda pro získání Stringu z textového pole tfautor
   *
   * @return String obsahují jména autora knihy
   */
  public String getAutor() {
    return tfAutor.getText();
  }

  /**
   * Metoda pro získání Stringu z textového pole tfnazev
   *
   * @return String obsahují název
   */
  public String getName() {
    return tfName.getText();
  }

  /**
   * Metoda pro získání Stringu z textového pole tfdatum
   *
   * @return String obsahující datum
   */
  public String getDate() {
    return tfDate.getText();
  }
}