2013-10-31 27 views
1
import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

import java.awt.event.ActionEvent; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.ActionListener; 

public class Minesweeper extends JFrame implements MouseListener, ActionListener 
{ 
public int gridRows; 
public int gridColumns; 
int mines; 
private JMenuBar menuBar; 
private JMenu Difficulty; 
private JMenu Options; 
private int numberofMines; 
private int row, col; 
JMenuItem Expert, Intermediate, Easy; 
JButton[][] easyGrid; 
Tile[][] squares = new Tile[9][9]; 
Container box = getContentPane(); 
private JLabel flagcounter; 
private JLabel timecounter; 
private Font f1 = new Font("Brush Script", Font.BOLD,20); 
int easyflags = 10; 

public static void main(String args[]) 
{ 

    Minesweeper myGui = new Minesweeper(); 
    myGui.setVisible(true);   
} 
public Minesweeper() 
{ 
    super(); 
    setLayout(null); 
    makeBoard(9, 9); 
    initUI(); 
    setJMenuBar(menuBar); 
    menuBar.add(Difficulty); 
    menuBar.add(Options); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setLocation(800,200); 
    setSize(500, 600); 
    setTitle("Minesweeper"); 
    setVisible(true); 

    flagcounter = new JLabel("0"+easyflags, SwingConstants.CENTER); 
    flagcounter.setBounds(20, 20, 50, 40); 
    flagcounter.setFont(f1); 
    flagcounter.setBackground(Color.black); 
    flagcounter.setForeground(Color.red); 
    flagcounter.setOpaque(true); 
    flagcounter.setBorder(BorderFactory.createLineBorder(Color.black)); 
    flagcounter.setVisible(true); 
    add(flagcounter); 

} 


private void makeBoard(int rowsX, int columnsY) 
{ 

gridRows = rowsX; 
gridColumns = columnsY; 


easyGrid = new JButton[rowsX][columnsY]; // sets the grid size from the parameters given 
squares = new Tile[gridRows][gridColumns]; 

    for (int i = 0; i<rowsX; i++) 
    { 
     for (int j = 0; j<columnsY; j++) 
     { 
      squares[i][j] = new Tile(i, j); 
      squares[i][j].setBounds(25+(i*50),(j*50) + 100,50,50); 
      squares[i][j].setHorizontalAlignment(SwingConstants.CENTER); 
      squares[i][j].setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY)); 
      squares[i][j].addMouseListener(this); 

      box.add(squares[i][j]); 

     } 

    } 

    repaint(); 
} 
public Tile get(int i, int j) 
{ 
    return squares [i][j]; 
} 
final void initUI() 
{ 
    menuBar = new JMenuBar(); 
    Difficulty = new JMenu("Difficulty"); 
    Options = new JMenu("Options"); 
    JMenuItem easy = new JMenuItem("Easy"); 
    easy.addActionListener(this); 
    easy.setToolTipText("New 9x9 game with 10 bombs."); 

    JMenuItem medium = new JMenuItem("Medium"); 
    medium.addActionListener(this); 
    medium.setToolTipText("New 16x16 game with 40 bombs"); 

    JMenuItem hard = new JMenuItem("Hard"); 
    hard.addActionListener(this); 
    hard.setToolTipText("New 30x16 game with 99 bombs"); 

    JMenuItem custom = new JMenuItem("custom"); 
    custom.addActionListener(this); 
    custom.setToolTipText("Set a custom layout for a minesweeper game"); 

    JMenuItem oExit = new JMenuItem("Exit"); 
    oExit.addActionListener(this); 
    oExit.setToolTipText("Exit the game"); 
    Difficulty.add(easy); 
    Difficulty.add(medium); 
    Difficulty.add(hard); 

    Options.add(oExit); 
} 




public void actionPerformed(ActionEvent e) 
{ 
    if(e.getSource() instanceof Tile) 
    { 

    } 
    if(e.getSource() instanceof JMenuItem) 
    { 
     String menu; 
     menu = e.getActionCommand(); 

     if (menu.equals("Exit")) 
     { 
      System.exit(0); 
     } 
     if (menu.equals("Easy")) 
     { 
      setSize(500,600); 
      makeBoard(9,9); 
      // TODO need to reset the board 
     } 
     if (menu.equals("Medium")) 
     { 
      setSize(850,950); 
      //TODO figure out how to change the flag counter... 
      makeBoard(16, 16); 
     } 
     if (menu.equals("Hard")) 
     { 
      setSize(1550,950); 
      makeBoard(30,16); 
     } 

    } 

    } 


public void mouseClicked(MouseEvent e) 
{ 
    if(e.getSource() instanceof Tile) 
    { 
    Tile clicked = (Tile)e.getSource(); 
     //Test Statements 
     System.out.println("Row " + clicked.getColumn() + " Column " + clicked.getRow()); 
     System.out.println("This tile is: " + clicked.getOccupied()); 
     if(e.getButton() == MouseEvent.BUTTON3) 
     { 
      clicked.setForeground(Color.BLUE); 
      clicked.setOccupied('f'); 
      clicked.setBackground(Color.LIGHT_GRAY); 

     } 

    } 

} 


public void mouseEntered(MouseEvent arg0) 
{ 
    if(arg0.getSource() instanceof Tile) 
    { 
     Tile entered = (Tile)arg0.getSource(); 
     entered.setBorder(BorderFactory.createLineBorder(Color.blue)); 
    } 
} 

@Override 
public void mouseExited(MouseEvent arg0) 
{ 

    Tile exited = (Tile)arg0.getSource(); 
    exited.setBorder(BorderFactory.createLineBorder(Color.black)); 
} 

@Override 
public void mousePressed(MouseEvent arg0) 
{ 
    // TODO Auto-generated method stub 
    if(arg0.getSource() instanceof Tile) 
    { 

    } 
} 

@Override 
public void mouseReleased(MouseEvent arg0) 
{ 
    // TODO Auto-generated method stub 

} 

} 

這是主類創建掃雷艇時,如何在更改難度時重置主板。

import java.awt.Color; 
import java.awt.Image; 

import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JLabel; 
import javax.swing.SwingConstants; 
// Tile class for mine sweeper 
public class Tile extends JLabel 
{ 
private char occupied; 
private int column; 
private int row; 

private JLabel OccupiedB; 
private JLabel OccupiedC; 
private JLabel OccupiedE; 
private JLabel OccupiedF; 
//Icon flag = new ImageIcon("flag.ico"); 

/* 
whatever.setIcon(new ImageIcon("filename.ext")); 
or 
Icon myIcon = new ImageIcon("filename.ext"); 
whatever.setIcon(myIcon); 
*/ 

public Tile(int col, int newRow) 
{ 
    super(); 
    setBackground(Color.GRAY); 
    setOpaque(true); 
    occupied = 'g'; 
    column = col; 
    row = newRow; 

} 

public boolean setOccupied(char Ecolumn) 
{ 
    occupied = Ecolumn; 
    if(occupied == 'b')   // If the tile is a bomb 
    { 

    } 
    else if(occupied == 'c') // If the tile is clear 
    { 
     setBackground(Color.WHITE); 
    } 
    else if(occupied == 'e') // If the tile is empty or surrounding a bomb 
    { 

    } 
    else if(occupied == 'f') // If the tile is a flag 
    { 
     setText("F"); 

     //setIcon(new ImageIcon("flag.ico")); 

    } 
    return true; 
} 

public int getColumn() 
{ 
    return column; 
} 

public void setColumn(int column) 
{ 
    this.column = column; 
} 

public int getRow() 
{ 
    return row; 
} 

public JLabel getOccupiedB() 
{ 
    return OccupiedB; 
} 

public void setOccupiedB(JLabel occupiedB) 
{ 
    OccupiedB = occupiedB; 
} 


public JLabel getOccupiedC() 
{ 
    return OccupiedC; 
} 

public void setOccupiedC(JLabel occupiedC) 
{ 
    OccupiedC = occupiedC; 
} 


public JLabel getOccupiedE() 
{ 
    return OccupiedE; 
} 

public void setOccupiedE(JLabel occupiedE) 
{ 
    OccupiedE = occupiedE; 
} 
public JLabel getOccupiedF() 
{ 
    return OccupiedF; 
} 

public void setOccupiedF(JLabel occupiedF) 
{ 
    OccupiedF = occupiedF; 
} 

public void setRow(int row) 
{ 
    this.row = row; 
} 

public char getOccupied() 
{ 
    return occupied; 
} 


} 

這是瓷磚類

我不知道如何基本復位電路板時,即時通訊改變的難度,或者當我計劃設置自定義板選項如何根據給定的磁貼數量自動調整電路板大小。

如果你不能告訴...我很新的代碼。謝謝您的幫助。

+0

爲了更好的幫助更快,發佈[SSCCE](http://sscce.org/)。 –

回答

0

這不是已經做到了大部分?

if (menu.equals("Easy")) 
    { 
     setSize(500,600); 
     makeBoard(9,9); 
     // TODO need to reset the board 
    } 
    if (menu.equals("Medium")) 
    { 
     setSize(850,950); 
     //TODO figure out how to change the flag counter... 
     makeBoard(16, 16); 
    } 
    if (menu.equals("Hard")) 
    { 
     setSize(1550,950); 
     makeBoard(30,16); 
    } 

有人從菜單中選擇一個選項會觸發事件。事件處理程序包含一個if/else塊,可以:退出,設置爲簡單,設置爲中等,設置爲硬。 makeBoard函數處理灰色瓷磚的實際顯示,據我所知,對於所有瓷磚,佔用設置爲'g'(灰色?)。如果你問的是實際生成的炸彈等,我沒有看到有關這方面的任何內容。它要麼照顧你,你沒有提供代碼,或者你必須自己寫。

您需要將int easyFlags設置爲它們爲特定難度獲得的許多值。 您需要將flagCounter標籤設置爲0. 您需要設置timecounter標籤t0 0. 您需要將int礦設置爲儘可能多的難度。

的menu.equals(「易」),如果塊將是這樣的:

if (menu.equals("Easy")) 
    { 
     setSize(500,600); 
     makeBoard(9,9); 
     flagCounter.setText("0"); 
     timecounter.setText("0"); //or "0:00" or w/e 
     mines = 10; 
     easyFlags = 10; 
    } 

有可能略多,但我不想做的顯然是某種形式的學校項目,爲您大聲笑