2012-03-21 101 views
-3

我要設計一個batleships遊戲週五,但過程中即時通訊做似乎已經忽略了一些東西,因爲雖然我管理的所有其他任務這最後的項目是令人難以置信我上面的把握,但我必須做一些事情。Java的戰艦問題

我有以下GUI代碼,給了我我玩網格,但我絕對不知道如何做以下事情

  1. 指定船舶在一定的細胞 - 和顏色這些細胞以反映此
  2. 怎麼做的,真實命中,命中,擊沉和更新電網

我想如果我能至少要做到這些,我可以複製CPU的代碼,但IM sooooooo卡住所以任何幫助是非常讚賞請大家工作一些神奇的:)

/** 
* BattleGui: 
* 
*/ 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.*; 
import java.io.*; 

public class BattleGui implements ActionListener 
{ 
    // Default filename to use for saving and loading files 
    // Possible improvement: replace with a FileChooser 
    private final static String DEFAULT_FILENAME = "battlegui.txt"; 
    private int GRID_SIZE = 8; 
    private JButton [] buttonArray; 

    public JMenuBar createMenu() 
    { 
     JMenuBar menuBar = new JMenuBar();; 
     JMenu menu = new JMenu("Battle Menu"); 
     JMenuItem menuItem; 

     menuBar.add(menu); 

     // A group of JMenuItems. You can create other menu items here if desired 
     menuItem = new JMenuItem("New Game"); 
     menuItem.addActionListener(this); 
     menu.add(menuItem); 

     menuItem = new JMenuItem("Load Game"); 
     menuItem.addActionListener(this); 
     menu.add(menuItem); 

     menuItem = new JMenuItem("Save Game"); 
     menuItem.addActionListener(this); 
     menu.add(menuItem); 

     menuItem = new JMenuItem("Quit"); 
     menuItem.addActionListener(this); 
     menu.add(menuItem);   

     //a submenu 
     menu.addSeparator(); 
     return menuBar; 
    } 

    public Container createContentPaneCPU() 
    { 
     int numButtons = GRID_SIZE * GRID_SIZE; 
     JPanel grid = new JPanel(new GridLayout(GRID_SIZE,GRID_SIZE)); 
     buttonArray = new JButton[numButtons]; 


     for (int i=0; i<numButtons; i++) 
     { 
      buttonArray[i] = new JButton(" "); 

      // This label is used to identify which button was clicked in the action listener 
      buttonArray[i].setActionCommand("" + i); // String "0", "1" etc. 
      buttonArray[i].addActionListener(this); 
      grid.add(buttonArray[i]); 

     } 
     return grid; 
    } 

    public Container createContentPane() 
    { 
     int numButtons = GRID_SIZE * GRID_SIZE; 
     JPanel grid = new JPanel(new GridLayout(GRID_SIZE,GRID_SIZE)); 
     buttonArray = new JButton[numButtons]; 

     for (int i=0; i<numButtons; i++) 
     { 
      buttonArray[i] = new JButton(" "); 

      // This label is used to identify which button was clicked in the action listener 
      //buttonArray[i].setActionCommand("" + i); // String "0", "1" etc. 
      // buttonArray[i].addActionListener(this); 
      grid.add(buttonArray[i]); 
     } 
     return grid; 
    }  

    /** 
    * This method handles events from the Menu and the board. 
    * 
    */ 
    public void actionPerformed(ActionEvent e) 
    { 
     String classname = getClassName(e.getSource()); 
     JComponent component = (JComponent)(e.getSource()); 

     if (classname.equals("JMenuItem")) 
     { 
      JMenuItem menusource = (JMenuItem)(e.getSource()); 
      String menutext = menusource.getText(); 

      // Determine which menu option was chosen 
      if (menutext.equals("Load Game")) 
      { 
       /* BATTLEGUI Add your code here to handle Load Game **********/ 
       LoadGame(); 
      } 
      else if (menutext.equals("Save Game")) 
      { 
       /* BATTLEGUI Add your code here to handle Save Game **********/ 
       SaveGame(); 
      } 
      else if (menutext.equals("New Game")) 
      { 
       /* BATTLEGUI Add your code here to handle Save Game **********/ 
       NewGame(); 
      } 
     } 
     // Handle the event from the user clicking on a command button 
     else if (classname.equals("JButton")) 
     { 
      JButton button = (JButton)(e.getSource()); 
      int bnum = Integer.parseInt(button.getActionCommand()); 
      int row = bnum/GRID_SIZE; 
      int col = bnum % GRID_SIZE; 
      System.out.println(e.getSource()); 

      /* BATTLEGUI Add your code here to handle user clicking on the grid ***********/ 
      button.setBackground(Color.GREEN); 
      fireShot(row, col); 
     } 
    } 



    /** 
    * Returns the class name 
    */ 
    protected String getClassName(Object o) 
    { 
     String classString = o.getClass().getName(); 
     int dotIndex = classString.lastIndexOf("."); 
     return classString.substring(dotIndex+1); 
    } 

    /** 
    * Create the GUI and show it. 
    * For thread safety, this method should be invoked from the event-dispatching thread. 
    */ 
    private static void createAndShowGUI() 
    { 
     // Create and set up the window. 
     JFrame frame = new JFrame("Battleships"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     int maxGap = 20; 
     int ButtonWidth = 20; 
     int ButtonHeight = 1; 


     BattleGui battlegui = new BattleGui(); 
     frame.setJMenuBar(battlegui.createMenu()); 
     JPanel gui = new JPanel(new GridLayout(2,2,20,5)); 
     gui.setBorder(new EmptyBorder(5,5,5,5)); 
     //Set up components preferred size 
     JButton b = new JButton("Just fake button"); 
     Dimension buttonSize = b.getPreferredSize(); 

     gui.add(new JButton("Player")); 
     gui.add(new JButton("CPU")); 
     b.setPreferredSize(new Dimension(ButtonWidth, ButtonHeight)); 
     gui.add(battlegui.createContentPane()); 
     gui.add(battlegui.createContentPaneCPU()); 
     frame.setContentPane(gui); 
     // Create and set up the content pane. 
     /* 
     BattleGui battlegui = new BattleGui(); 
     frame.setJMenuBar(battlegui.createMenu()); 
     frame.setContentPane(battlegui.createContentPane()); 
     */ 

     // Display the window, setting the size 
     frame.setSize(800, 600); 
     frame.setVisible(true); 
    } 

    /** 
    * Sets a Gui grid square at row, col to display a character 
    */ 
    public boolean setGuiSquare(int row, int col, char c) 
    { 
     int bnum = row * GRID_SIZE + col; 
     if (bnum >= (GRID_SIZE*GRID_SIZE)) 
     { 
      return false; 
     } 
     else 
     { 
      buttonArray[bnum].setText(Character.toString(c)); 
     } 
     return true; 
    } 

    /** 
    * This is a standard main function for a Java GUI 
    */ 
    public static void main(String[] args) 
    { 
     //Schedule a job for the event-dispatching thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 

       createAndShowGUI(); 
       //Deploy(); 
      } 
     }); 
    } 

    //************************************************************************ 
    //*** BATTLEGUI: Modify the methods below to respond to Menu and Mouse click events 

    /** 
    * This method is called from the Menu event: New Game. 
    * BATTLEGUI 
    */ 
    public void NewGame() 
    { 
     System.out.println("New game selected"); 

    } 


    /** 
    * This method is called from the Menu event: Load Game. 
    * BATTLEGUI 
    */ 
    public void LoadGame() 
    { 
      System.out.println("Load game selected"); 
    } 


    /** 
    * This method is called from the Menu event: Save Game. 
    * BATTLEGUI 
    */ 
    public void SaveGame() 
    { 
      System.out.println("Save game selected"); 
    } 

    /** 
    * This method is called from the Mouse Click event. 
    * BATTLEGUI 
    */ 
    public void fireShot(int row, int col) 
    { 
      System.out.println("Fire shot selected: at (" + row + ", " + col + ")"); 
    } 



} 
+0

https://stackoverflow.com/questions/28400111/battleships-game-android-tutorial – Qgenerator 2015-02-09 01:30:18

回答

4

我會建議,退後一步,思考問題域。

你有一個BattleScene,其中包含BattleSquares。每個battleSquare最多可以有1艘船,並且可以有顏色。你也有Ship對象(可以屬於某個特定的玩家,指示它是否被損壞)...

BattleSquare需要決定它是否是HitMiss,因爲它具有所有信息。它知道它有沒有船。

/**true if had a ship, false if it was a miss 
    */ 
public class BattleSquare{  
     public boolean processHit(){ 
      if (hasShip()){ 
       ship.setState(DESTROYED); 
       return true; 
      } 
      return false; 
     } 
     public void setShip(Ship ship){ .... } 
     public boolean hasShip() { ... } } ... methods for color too 

如果您將代碼隔離爲可管理的代碼片段,其中某些類代表模型,則可以更好地管理事情。你似乎正在混合一堂課的所有內容,因此感到迷茫。

同樣,你BattleScene將包含BattleSquares的List。一旦你開火,你可以單獨尋找一個特定的BattleSquare並告訴它自己處理。如果它是一個命中,你更新狀態。

想法是你的模型類只負責管理狀態。您的控制器類可以觸發由視圖攔截的事件,這些事件更新模型並自行刷新。

希望它有幫助。

+0

它的實際單元格選擇這真的讓我在這裏我可以寫邏輯和周圍,但我不知道如何選擇說單元格行1列2然後着色它,這樣我可以通過選擇一種顏色來處理部署船舶以及遊戲的實際玩法,這取決於座標是否出現在數組中 – JazziJeff 2012-03-21 22:49:00

+0

您需要告訴模型的網格自身着色。 View會自動更新它。如果有意義,也許使用JTable。編寫一個TableModel,您可以使用單元格渲染器根據屬性進行着色。 http://docs.oracle.com/javase/tutorial/uiswing/components/table.html提供了全面的教程。這隻有在使用表格單元而不是您嘗試創建的網格時纔有意義。我懷疑這項任務可以使用它。 – Nasir 2012-03-22 03:11:08