2013-10-02 143 views
2

這是我的滑塊拼圖遊戲。到目前爲止,它只能做3x3遊戲。當我嘗試傳遞板的變量l和w(長度和寬度)時,它不起作用。它只適用於當我設置變量ROWS和COLS爲決賽。當我嘗試改變它時,我收到錯誤。我不知道該怎麼做,任何幫助,將不勝感激。滑塊拼圖問題

目前用戶可以輸入目前無法做任何事情的值。遊戲開始時,會生成一個3x3的棋盤。用戶可以使用不同的加擾板重新開始遊戲,但解決棋盤上的按鈕和將板重置爲原始狀態的按鈕不起作用。

public class SlidePuzzle { 

public static void main(String[] args) 
{ 

    JFrame window = new JFrame("Slide Puzzle"); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    String length = JOptionPane.showInputDialog("Length"); 
    String width = JOptionPane.showInputDialog("Width"); 
    int l = Integer.parseInt(length); 
    int w = Integer.parseInt(width); 
    window.setContentPane(new SlidePuzzleGUI()); 
    window.pack(); 
    window.show(); 
    window.setResizable(false); 

} 
} 

public class SlidePuzzleGUI extends JPanel 
{ 

private GraphicsPanel _puzzleGraphics; 
private SlidePuzzleModel _puzzleModel = new SlidePuzzleModel(); 

這個類包含了拼圖

public SlidePuzzleGUI() { 

    JButton newGameButton = new JButton("New Game"); 
    JButton resetButton = new JButton("Reset"); 
    JButton solveButton = new JButton("I GIVE UP :("); 

    resetButton.addActionListener(new ResetAction()); 
    newGameButton.addActionListener(new NewGameAction()); 


    JPanel controlPanel = new JPanel(); 
    controlPanel.setLayout(new FlowLayout()); 
    controlPanel.add(newGameButton); 
    controlPanel.add(resetButton); 
    controlPanel.add(solveButton); 


    _puzzleGraphics = new GraphicsPanel(); 

    this.setLayout(new BorderLayout()); 
    this.add(controlPanel, BorderLayout.NORTH); 
    this.add(_puzzleGraphics, BorderLayout.CENTER); 
} 

這是圖形面板

class GraphicsPanel extends JPanel implements MouseListener { 
    private static final int ROWS = 3; 
    private static final int COLS = 3; 

    private static final int CELL_SIZE = 80; 
    private Font _biggerFont; 



    public GraphicsPanel() { 
     _biggerFont = new Font("SansSerif", Font.BOLD, CELL_SIZE/2); 
     this.setPreferredSize(
       new Dimension(CELL_SIZE * COLS, CELL_SIZE*ROWS)); 
     this.setBackground(Color.black); 
     this.addMouseListener(this); 
    } 



    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     for (int r=0; r<ROWS; r++) { 
      for (int c=0; c<COLS; c++) { 
       int x = c * CELL_SIZE; 
       int y = r * CELL_SIZE; 
       String text = _puzzleModel.getFace(r, c); 
       if (text != null) { 
        g.setColor(Color.gray); 
        g.fillRect(x+2, y+2, CELL_SIZE-4, CELL_SIZE-4); 
        g.setColor(Color.black); 
        g.setFont(_biggerFont); 
        g.drawString(text, x+20, y+(3*CELL_SIZE)/4); 
       } 
      } 
     } 
    } 



    public void mousePressed(MouseEvent e) { 

     int col = e.getX()/CELL_SIZE; 
     int row = e.getY()/CELL_SIZE; 

     if (!_puzzleModel.moveTile(row, col)) { 

      Toolkit.getDefaultToolkit().beep(); 
     } 

     this.repaint(); 
    } 



    public void mouseClicked (MouseEvent e) {} 
    public void mouseReleased(MouseEvent e) {} 
    public void mouseEntered (MouseEvent e) {} 
    public void mouseExited (MouseEvent e) {} 
} 

public class NewGameAction implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     _puzzleModel.reset(); 
     _puzzleGraphics.repaint(); 
    } 
} 
public class ResetAction implements ActionListener { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     _puzzleModel.tryAgain(); 
     _puzzleGraphics.repaint(); 

    } 

} 

public class solveAction implements ActionListener 
{ 

    @Override 
    public void actionPerformed(ActionEvent e) 
     { 
      _puzzleModel.solve(); 
      _puzzleGraphics.repaint(); 

     } 

    } 
} 
public class SlidePuzzleModel { 
private static final int ROWS = 3; 
private static final int COLS = 3; 

private Tile[][] _contents; 
private Tile[][] _solved; 
private Tile  _emptyTile; 



public SlidePuzzleModel() { 
    _contents = new Tile[ROWS][COLS]; 
    reset(); 
} 



String getFace(int row, int col) { 
    return _contents[row][col].getFace(); 
} 



public void reset() { 
    for (int r=0; r<ROWS; r++) { 
     for (int c=0; c<COLS; c++) { 
      _contents[r][c] = new Tile(r, c, "" + (r*COLS+c+1)); 
     } 
    } 

    _emptyTile = _contents[ROWS-1][COLS-1]; 
    _emptyTile.setFace(null); 


    for (int r=0; r<ROWS; r++) { 
     for (int c=0; c<COLS; c++) { 
      exchangeTiles(r, c, (int)(Math.random()*ROWS) 
           , (int)(Math.random()*COLS)); 
     } 
    } 
} 

public void tryAgain() 
{ 

} 

public void solve() 
{ 
    for (int i = 1; i < ROWS+1;i++) 
    { 
     for(int j = 1; j < COLS+1;j++) 
     { 
      exchangeTiles(i, j, i, j); 
     } 
    } 
} 


public boolean moveTile(int r, int c) { 

    return checkEmpty(r, c, -1, 0) || checkEmpty(r, c, 1, 0) 
     || checkEmpty(r, c, 0, -1) || checkEmpty(r, c, 0, 1); 
} 

private boolean checkEmpty(int r, int c, int rdelta, int cdelta) { 
    int rNeighbor = r + rdelta; 
    int cNeighbor = c + cdelta; 

    if (isLegalRowCol(rNeighbor, cNeighbor) 
       && _contents[rNeighbor][cNeighbor] == _emptyTile) { 
     exchangeTiles(r, c, rNeighbor, cNeighbor); 
     return true; 
    } 
    return false; 
} 

public boolean isLegalRowCol(int r, int c) { 
    return r>=0 && r<ROWS && c>=0 && c<COLS; 
} 



private void exchangeTiles(int r1, int c1, int r2, int c2) { 
    Tile temp = _contents[r1][c1]; 
    _contents[r1][c1] = _contents[r2][c2]; 
    _contents[r2][c2] = temp; 
} 

public boolean isGameOver() { 
    for (int r=0; r<ROWS; r++) { 
     for (int c=0; c<ROWS; c++) { 
      Tile trc = _contents[r][c]; 
      return trc.isInFinalPosition(r, c); 
     } 
    } 


    return true; 
} 
} 

class Tile { 

private int _row;  
private int _col;  
private String _face; 

public Tile(int row, int col, String face) { 
    _row = row; 
    _col = col; 
    _face = face; 
} 
public void setFace(String newFace) { 
    _face = newFace; 
} 
public String getFace() { 
    return _face; 
} 
public boolean isInFinalPosition(int r, int c) { 
    return r==_row && c==_col; 
} 
} 

我將如何讓這個用戶可以指定遊戲的尺寸GUI板?

編輯 公共類SlidePuzzle {

public static void main(String[] args) 
{ 

    JFrame window = new JFrame("Slide Puzzle"); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    String length = JOptionPane.showInputDialog("Length"); 
    String width = JOptionPane.showInputDialog("Width"); 
    int l = Integer.parseInt(length); 
    int w = Integer.parseInt(width); 
    window.setContentPane(new SlidePuzzleGUI()); 
    window.pack(); 
    window.show(); 
    window.setResizable(false); 

} 

} 公共類SlidePuzzleGUI擴展JPanel {

private GraphicsPanel _puzzleGraphics; 
private SlidePuzzleModel _puzzleModel = new SlidePuzzleModel(); 




public SlidePuzzleGUI(int l, int w) { 

    JButton newGameButton = new JButton("New Game"); 
    JButton resetButton = new JButton("Reset"); 
    JButton solveButton = new JButton("I GIVE UP :("); 

    resetButton.addActionListener(new ResetAction()); 
    newGameButton.addActionListener(new NewGameAction()); 


    JPanel controlPanel = new JPanel(); 
    controlPanel.setLayout(new FlowLayout()); 
    controlPanel.add(newGameButton); 
    controlPanel.add(resetButton); 
    controlPanel.add(solveButton); 


    _puzzleGraphics = new GraphicsPanel(l,w); 

    this.setLayout(new BorderLayout()); 
    this.add(controlPanel, BorderLayout.NORTH); 
    this.add(_puzzleGraphics, BorderLayout.CENTER); 
} 

class GraphicsPanel extends JPanel implements MouseListener { 
    private int ROWS; 
    private int COLS; 

    private static final int CELL_SIZE = 80; 
    private Font _biggerFont; 



    public GraphicsPanel(int l, int w) { 
     _biggerFont = new Font("SansSerif", Font.BOLD, CELL_SIZE/2); 
     this.setPreferredSize(
       new Dimension(CELL_SIZE * COLS, CELL_SIZE*ROWS)); 
     this.setBackground(Color.black); 
     this.addMouseListener(this); 
     ROWS = l; 
     COLS = w; 
    } 



    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     for (int r=0; r<ROWS; r++) { 
      for (int c=0; c<COLS; c++) { 
       int x = c * CELL_SIZE; 
       int y = r * CELL_SIZE; 
       String text = _puzzleModel.getFace(r, c); 
       if (text != null) { 
        g.setColor(Color.gray); 
        g.fillRect(x+2, y+2, CELL_SIZE-4, CELL_SIZE-4); 
        g.setColor(Color.black); 
        g.setFont(_biggerFont); 
        g.drawString(text, x+20, y+(3*CELL_SIZE)/4); 
       } 
      } 
     } 
    } 



    public void mousePressed(MouseEvent e) { 

     int col = e.getX()/CELL_SIZE; 
     int row = e.getY()/CELL_SIZE; 

     if (!_puzzleModel.moveTile(row, col)) { 

      Toolkit.getDefaultToolkit().beep(); 
     } 

     this.repaint(); 
    } 



    public void mouseClicked (MouseEvent e) {} 
    public void mouseReleased(MouseEvent e) {} 
    public void mouseEntered (MouseEvent e) {} 
    public void mouseExited (MouseEvent e) {} 
} 

public class NewGameAction implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     _puzzleModel.reset(); 
     _puzzleGraphics.repaint(); 
    } 
} 
public class ResetAction implements ActionListener { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     _puzzleModel.tryAgain(); 
     _puzzleGraphics.repaint(); 

    } 

} 

public class solveAction implements ActionListener 
{ 

    @Override 
    public void actionPerformed(ActionEvent e) 
     { 
      _puzzleModel.solve(); 
      _puzzleGraphics.repaint(); 

     } 

    } 
} 
+0

你得到了什麼錯誤,在哪裏? –

+0

好吧,現在你輸入的是長度和寬度,但不對它們做任何事情,並且在'GraphicsPanel'中對行和列使用常量。可能將長度和寬度參數添加到'SlidePuzzleGUI'構造函數和'GraphicsPanel'構造函數中?然後,主程序在構造'SlidePuzzleGUI'時使用它們,並且在構造'GraphicsPanel'時使用'SlidePuzzleGUI' ......或者還有其他一些問題嗎? – ajb

回答

3

你重點班有沒有setter方法或構造函數的參數,使您可以通過長和寬度給他們。如果你想改變他們國家的這一方面,那麼他們必須有一個機制來做到這一點。目前你的GraphicsPanel被硬編碼以使用ROWS和COLS常量,這將很難改變。相反,將所有這些硬編碼替換爲變量,並確保它們默認爲默認構造函數中的ROWS和COLS常量,但也提供了一個非默認構造函數,允許程序員通過不同的行和列設置。


編輯
你得

  • 使用構造函數的參數來設定領域,使整個類可以使用這些值,而不僅僅是構造函數。
  • 致電正確的構造函數,需要參數!

public MyConstructor(int x, int y) { 
    this.x = x; 
    this.y = y; 
} 
+0

我試圖把它們放在構造函數的參數中,但是這給了我錯誤,如果你給我一分鐘我可以重新鍵入我之前嘗試的內容 – user2776808

+0

@ user2776808:將更改後的代碼添加到當前文章的底部,但保留當前代碼也到位。 –

+0

添加了一些編輯 – user2776808