Пример #1
0
  public void keyPressed(KeyEvent ke) {
    // Move the cursor around the map
    if (getGame().getPartOfGame() == "Battle") {
      if (ke.getKeyCode() == KeyEvent.VK_UP) {
        if (getCursorLocY() != 0
            && getGrid()[getCursorLocX()][getCursorLocY() - 1].getType() != "Null") {
          setCursorLoc(getCursorLocX(), getCursorLocY() - 1);
        } // end if
        else if (getViewingLocY() != 0) {
          setViewingLoc(getViewingLocX(), getViewingLocY() - 1);
        } // end else if
      } // end if

      if (ke.getKeyCode() == KeyEvent.VK_DOWN) {
        if (getCursorLocY() != GameConstants.GUIWIDTH / GameConstants.TILESIZE - 1
            && getGrid()[getCursorLocX()][getCursorLocY() + 1].getType() != "Null") {
          setCursorLoc(getCursorLocX(), getCursorLocY() + 1);
        } // end if
        else if (getViewingLocY()
            != GameConstants.MAPSIZE - GameConstants.GUIWIDTH / GameConstants.TILESIZE) {
          setViewingLoc(getViewingLocX(), getViewingLocY() + 1);
        } // end else if
      } // end if

      if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
        if (getCursorLocX() != GameConstants.GUIWIDTH / GameConstants.TILESIZE - 1
            && getGrid()[getCursorLocX() + 1][getCursorLocY()].getType() != "Null") {
          setCursorLoc(getCursorLocX() + 1, getCursorLocY());
        } // end if
        else if (getViewingLocX()
            != GameConstants.MAPSIZE - GameConstants.GUIWIDTH / GameConstants.TILESIZE - 1) {
          setViewingLoc(getViewingLocX() + 1, getViewingLocY());
        } // end else if
      } // end if

      if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
        if (getCursorLocX() != 0
            && getGrid()[getCursorLocX() - 1][getCursorLocY()].getType() != "Null") {
          setCursorLoc(getCursorLocX() - 1, getCursorLocY());
        } // end if
        else if (getViewingLocX() != 0) {
          setViewingLoc(getViewingLocX() - 1, getViewingLocY());
        } // end else if
      } // end if
    } // end if

    if (ke.getKeyCode() == KeyEvent.VK_X) {
      System.out.println("Select");
    } // end if

    if (ke.getKeyCode() == KeyEvent.VK_Z) {
      System.out.println("Deselect");
    } // end if

    m_gui.repaint();
  } // end method keyPressed
Пример #2
0
  public void run() {

    while (!stop) {
      c.setFuego(true);
      fg.iniciar();
      c.setFuego(false);
      g.sacarJuego(fg.getGrafico());
      g.repaint();
      fg = null;
      this.detener();
    }
  }
  public static void main(String args[]) {

    GUI gui = new GUI(new GameState());
    Agent smith = new Agent();

    System.out.println("PlayerTurn " + gui.data.gameState.isPlayerTurn());
    System.out.println("Deployed " + gui.data.gameState.isBothPlayerAndAgentShipsDeployed());

    System.out.println("PlayerTurn " + gui.data.gameState.isPlayerTurn());
    System.out.println("Deployed " + gui.data.gameState.isBothPlayerAndAgentShipsDeployed());

    while (!gui.data.gameState.playerHomeGrid.allShipsPlaced()) {
      // PlayerDeploymentPhase, wait for player to place all their ships
    }

    gui.data.gameState.addAgentShips(smith.placeShips());

    gui.data.gameState.setPlayerTurn();
    gui.data.outText.setText(gui.data.gameState.turnToString());

    while (!gui.getGameOver()) {

      while (gui.data.gameState.isPlayerTurn() && !gui.getGameOver()) {
        gui.data.gameState.setShipSunkStates();
        if (gui.data.gameState.areAllAgentShipsSunk()) {
          System.out.println("All sunk");
          gui.data.gameState.SetGameOver();
          gui.data.gameState.PlayerIsTheWinner();
        }
      }
      gui.repaint();

      while (gui.data.gameState.isAgentTurn() && !gui.data.gameState.IsGameOver()) {

        System.out.println("agent turn");
        smith.nextShot(gui.data.gameState.influenceMap, gui.data.gameState.compAtt);
        gui.agentShot(smith.getI(), smith.getJ());
        System.out.println("shot at " + smith.getI() + " " + smith.getJ());
        System.out.println(gui.data.gameState.compAtt.toString());
        // if(gameState.playerHome.get(i,j

        determineIfShotSunkAShip(gui, smith);

        gui.data.gameState.setShipSunkStates();

        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }

        /*
        if(g.getAgentShipsSunk())
        {
        	g.setGameOver();
        	g.setPlayerWins();
        }
        */
        if (gui.data.gameState.getPlayerShipsSunk()) {
          gui.setAgentWins();
          gui.data.gameState.SetGameOver();
          gui.data.gameState.setPlayerTurn();
        }
      }
    }

    System.out.println("Game Over!");
    if (gui.data.gameState.isPlayerWinner()) {
      System.out.println("Player Wins");
      gui.setOut("Game Over! You Win!");
    } else {
      System.out.println("Computer Wins");
      gui.setOut("Game Over! Agent Wins!");
    }
  }