2012-03-21 102 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