2015-05-21 193 views
0

在我的跳棋代碼中,我試圖讓每當一塊棋子落在能夠跳躍的地方時,它就被迫跳起來。在我的代碼中,它突出了你可以跳躍的地方。我已經做到了,所以你可以跳過其他棋子等,但我還沒有找到強制玩家跳躍的方法。使用布爾和方法比掃描棋盤進行跳轉並禁用所有棋子,但跳躍棋子和跳躍棋子更合適。跳棋強制跳躍

主類

import javax.swing.*; 

import java.awt.*; 
import java.awt.event.*; 

public class Main extends JFrame implements ActionListener 
{ 
public Tile board[][]; 
private Container contentPane; 
private JButton reset; 
private static final Icon b = new ImageIcon("Black.png"); 
private static final Icon r = new ImageIcon("red.png"); 
private boolean turn1; 
private boolean turn2; 
private boolean jumpAvailable; 
private int count = 0; 

public Main() 
{ 
    try 
    { 
     // Set cross-platform Java L&F (also called "Metal") 
     UIManager.setLookAndFeel(
       UIManager.getCrossPlatformLookAndFeelClassName()); 
    } 
    catch (UnsupportedLookAndFeelException e) { 
     // handle exception 
    } 
    catch (ClassNotFoundException e) { 
     // handle exception 
    } 
    catch (InstantiationException e) { 
     // handle exception 
    } 
    catch (IllegalAccessException e) { 
     // handle exception 
    } 


    contentPane = getContentPane(); 
    contentPane.setLayout(null); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setTitle("Checkers"); 
    setSize(1430,1280); 
    setResizable(false); 
    contentPane.setBackground(Color.cyan.darker()); 

    board = new Tile[8][8]; 
    for(int x = 0; x < 8; x++) 
    { 
     for(int y = 0; y < 8; y++) 
     { 
      board[x][y] = new Tile(x,y); 
      board[x][y].setBounds(25 + (x*150), 25 + (y*150),150,150); 
      board[x][y].addActionListener(this); 
      board[x][y].setOpaque(true); 
      if(x%2 == y%2) 
      { 
       board[x][y].setBackground(Color.BLACK); 
      } 
      else 
      { 
       board[x][y].setBackground(Color.RED); 
      } 
      contentPane.add(board[x][y]); 
      if(x%2 == y/1 - 1) 
      { 
       board[x][y].setIcon(b); 
      } 
      if(x%2 == y/1 + 1) 
      { 
       board[x][y].setIcon(b); 
      } 
      if(x%2 == y/1 - 7) 
      { 
       board[x][y].setIcon(r); 
      } 
      if(x%2 == y/1 - 5) 
      { 
       board[x][y].setIcon(r); 
      } 
      turn1=true; 
     } 
    } 

    reset = new JButton("Reset"); 
    reset.setBounds(1235,150,100,50); 
    reset.addActionListener(this); 
    contentPane.add(reset); 

} 

public static void main(String args[]) 
{ 
    Main frame = new Main(); 
    frame.setVisible(true); 
} 

public void actionPerformed(ActionEvent e) 
{ 
    if(e.getSource()instanceof Tile) 
    { 
     jumpCheck(); 
     Tile clicked = (Tile)e.getSource(); 
     if(clicked==(Tile)e.getSource()) 
     { 
      System.out.println(clicked.getRow() + " " + clicked.getColumn()); 
      count++; 
     } 
     for(int x = 0; x < 8; x++) 
     { 
      for(int y = 0; y < 8; y++) 
      { 
       if(turn1) 
       { 
        if(clicked==board[x][y] && board[x][y].getIcon()==r && board[x][y].getBackground()==Color.red) 
        { 
         if(clicked==board[x][y] && board[x][y].getRow()!=0 && board[x-1][y-1].getIcon()==r &&board[x+1][y-1].getIcon()==r) 
         { 
          JOptionPane.showMessageDialog(null, "This piece cannot move. Please select another."); 
         } 
         else if(clicked==board[x][y] && board[x][y].getRow()==0 && board[x+1][y-1].getIcon()==r) 
         { 
          JOptionPane.showMessageDialog(null, "This piece cannot move. Please select another."); 
         } 
         else if(clicked==board[x][y] && board[x][y].getRow()==7 && board[x-1][y-1].getIcon()==r) 
         { 
          JOptionPane.showMessageDialog(null, "This piece cannot move. Please select another."); 
         } 

         else if(clicked==board[x][y] && board[x][y].getIcon()==r && board[x+1][y-1].getIcon()==b && board[x+2][y-2].getIcon()==null||clicked==board[x][y] && board[x][y].getIcon()==r && board[x-1][y-1].getIcon()==b && board[x-2][y-2].getIcon()==null) 
         { 
          if(clicked==board[x][y] && board[x][y].getIcon()==r && board[x+1][y-1].getIcon()==b && board[x+2][y-2].getIcon()==null) 
          { 
           board[x][y].setIcon(null); 
           board[x+2][y-2].setBackground(Color.white); 
          } 
          else if(clicked==board[x][y] && board[x][y].getIcon()==r && board[x-1][y-1].getIcon()==b && board[x-2][y-2].getIcon()==null) 
          { 
           board[x][y].setIcon(null); 
           board[x-2][y-2].setBackground(Color.white); 
          } 
         } 

         else 
         { 
          board[x][y].setIcon(null); 
          if(clicked==board[x][y] && clicked.getRow()==0) 
          { 
           board[x+1][y-1].setBackground(Color.white); 
          } 
          else if(clicked==board[x][y] && clicked.getRow()==7) 
          { 
           board[x-1][y-1].setBackground(Color.white); 
          } 
          else 
          { 
           board[x-1][y-1].setBackground(Color.white); 
           board[x+1][y-1].setBackground(Color.white); 
          } 
         } 
        } 


        else if(clicked==board[x][y] && board[x][y].getIcon()==null) 
        { 
         if(clicked==board[x][y] && board[x][y].getBackground()==Color.white) 
         { 
          if(clicked==board[x][y] && board[x][y].getIcon()==null && board[x][y].getBackground()==Color.black) 
          { 
           JOptionPane.showMessageDialog(null, "Please choose a valid place to jump to."); 
          } 



          else if(clicked==board[x][y] && board[x+1][y+1].getIcon()==b) 
          { 
           board[x][y].setIcon(r); 
           turn1=false; 
           turn2=true; 
           board[x][y].setBackground(Color.red); 
           board[x+1][y+1].setIcon(null); 
          } 

          else if(clicked==board[x][y] && board[x-1][y+1].getIcon()==b) 
          { 
           board[x][y].setIcon(r); 
           turn1=false; 
           turn2=true; 
           board[x][y].setBackground(Color.red); 
           board[x-1][y+1].setIcon(null); 
          } 

          else if(clicked==board[x][y] && board[x][y].getBackground()==Color.red||board[x][y].getBackground()==Color.white) 
          { 
           if(count == 1) 
           { 
            JOptionPane.showMessageDialog(null, "You really should click on a piece if you want to move."); 
            count = 0; 
           } 
           else 
           { 
            board[x][y].setIcon(r); 
            turn1=false; 
            turn2=true; 
            board[x][y].setBackground(Color.red); 
            board[x+2][y].setBackground(Color.red); 
            board[x-2][y].setBackground(Color.red); 
            count = 0; 
           } 
          } 
         } 
         else 
         { 
          JOptionPane.showMessageDialog(null, "Please choose a valid place to jump to."); 
         } 
        } 


       } 


       if(turn2) 
       { 
        if(clicked==board[x][y] && board[x][y].getIcon()==b && board[x][y].getBackground()==Color.red) 
        { 
         if(clicked==board[x][y] && board[x][y].getRow()!=0 && board[x-1][y+1].getIcon()==b &&board[x+1][y+1].getIcon()==b) 
         { 
          JOptionPane.showMessageDialog(null, "This piece cannot move. Please select another."); 
         } 
         else if(clicked==board[x][y] && board[x][y].getRow()==0 && board[x+1][y-1].getIcon()==b) 
         { 
          JOptionPane.showMessageDialog(null, "This piece cannot move. Please select another."); 
         } 
         else if(clicked==board[x][y] && board[x][y].getRow()==7 && board[x-1][y+1].getIcon()==b) 
         { 
          JOptionPane.showMessageDialog(null, "This piece cannot move. Please select another."); 
         } 

         else if(clicked==board[x][y] && board[x][y].getIcon()==r && board[x+1][y-1].getIcon()==b && board[x+2][y-2].getIcon()==null||clicked==board[x][y] && board[x][y].getIcon()==r && board[x-1][y-1].getIcon()==b && board[x-2][y-2].getIcon()==null) 
         { 
          if(clicked==board[x][y] && board[x][y].getIcon()==r && board[x+1][y-1].getIcon()==b && board[x+2][y-2].getIcon()==null) 
          { 
           board[x][y].setIcon(null); 
           board[x+2][y-2].setBackground(Color.white); 
          } 
          else if(clicked==board[x][y] && board[x][y].getIcon()==r && board[x-1][y-1].getIcon()==b && board[x-2][y-2].getIcon()==null) 
          { 
           board[x][y].setIcon(null); 
           board[x-2][y-2].setBackground(Color.white); 
          } 
         } 

         else 
         { 
          board[x][y].setIcon(null); 
          if(clicked==board[x][y] && clicked.getRow()==0) 
          { 
           board[x-1][y+1].setBackground(Color.white); 
          } 
          else if(clicked==board[x][y] && clicked.getRow()==7) 
          { 
           board[x+1][y+1].setBackground(Color.white); 
          } 
          else 
          { 
           board[x+1][y+1].setBackground(Color.white); 
           board[x-1][y+1].setBackground(Color.white); 
          } 
         } 
        } 

        else if(clicked==board[x][y] && board[x][y].getIcon()==null) 
        { 

         if(clicked==board[x][y] && board[x][y].getBackground()==Color.white) 
         { 
          if(clicked==board[x][y] && board[x][y].getIcon()==null && board[x][y].getBackground()==Color.black) 
          { 
           JOptionPane.showMessageDialog(null, "Please choose a valid place to jump to."); 
          } 

          else if(clicked==board[x][y] && board[x-1][y-1].getIcon()==r) 
          { 
           board[x][y].setIcon(b); 
           turn2=false; 
           turn1=true; 
           board[x][y].setBackground(Color.red); 
           board[x-1][y-1].setIcon(null); 
          } 

          else if(clicked==board[x][y] && board[x+1][y-1].getIcon()==r) 
          { 
           board[x][y].setIcon(b); 
           turn2=false; 
           turn1=true; 
           board[x][y].setBackground(Color.red); 
           board[x+1][y-1].setIcon(null); 
          } 

          else if(clicked==board[x][y] && board[x][y].getBackground()==Color.red||board[x][y].getBackground()==Color.white) 
          { 
           if(count == 1) 
           { 
            JOptionPane.showMessageDialog(null, "You really should click on a piece if you want to move."); 
            count = 0; 
           } 
           else 
           { 
            board[x][y].setIcon(r); 
            turn2=false; 
            turn1=true; 
            board[x][y].setBackground(Color.red); 
            board[x+2][y].setBackground(Color.red); 
            board[x-2][y].setBackground(Color.red); 
            count = 0; 
           } 
          } 
         } 

         else 
         { 
          JOptionPane.showMessageDialog(null, "Please choose a valid place to jump to."); 
         } 
        } 
       } 
      } 
     } 
    } 
} 

private void jumpCheck()//Where I would like to do force jumping. 
{ 
    for(int x = 0; x < 8; x++) 
    { 
     for(int y = 0; y < 8; y++) 
     { 

     } 
    } 
} 
} 

結束主類

瓷磚類

import javax.swing.*; 

import java.awt.*; 
import java.awt.event.*; 

public class Tile extends JButton 
{ 
public int row; 
public int column; 
public boolean occupied; 
public Icon image; 

public Tile(int x, int y) 
{ 
    row = x; 
    column = y; 
    occupied = false; 
    createTile(column, row, occupied); 
} 

public void createTile(int y, int x, boolean b) 
{ 
    row = x; 
    column = y; 
    occupied = false; 
} 

public int setRow() 
{ 
    switch(row) 
    { 
     case 0: return 1; 
     case 1: return 2; 
     case 2: return 3; 
     case 3: return 4; 
     case 4: return 5; 
     case 5: return 6; 
     case 6: return 7; 
     case 7: return 8; 
     default: return 0; 
    } 
} 

public int getRow() 
{ 
    return row; 
} 

public int setColumn() 
{ 
    switch(column) 
    { 
     case 0: return 1; 
     case 1: return 2; 
     case 2: return 3; 
     case 3: return 4; 
     case 4: return 5; 
     case 5: return 6; 
     case 6: return 7; 
     case 7: return 8; 
     default: return 0; 
    } 
} 

public int getColumn() 
{ 
    return column; 
} 

public void setPiece(Icon piece) 
{ 
    image = piece; 
} 

public Icon getPiece() 
{ 
    return image; 
} 

public void setOccupied(boolean b) 
{ 
    occupied = b; 
} 

public boolean isOccupied() 
{ 
    return occupied; 
} 
} 

結束瓷磚類

您有什麼想法或建議嗎?

回答

0

我在這裏看到的最簡單的方法是,您創建一個名爲jump的新Methoc,獲取點擊Tile。在這種方法中,你有你的代碼跳躍,如果你想強制玩家跳到一個點,你可以調用這個方法,你想跳轉到Tile。 下面這個方法你的代碼的PICE,我認爲它的跳躍樣本:

private void jumpTo(Tile tileToJumpTo) 
     { 
      if(tileToJumpTo==board[x][y] && board[x][y].getBackground()==Color.white) 
          { 
           if(tileToJumpTo==board[x][y] && board[x][y].getIcon()==null && board[x][y].getBackground()==Color.black) 
           { 
            JOptionPane.showMessageDialog(null, "Please choose a valid place to jump to."); 
           } 



           else if(tileToJumpTo==board[x][y] && board[x+1][y+1].getIcon()==b) 
           { 
            board[x][y].setIcon(r); 
            turn1=false; 
            turn2=true; 
            board[x][y].setBackground(Color.red); 
            board[x+1][y+1].setIcon(null); 
           } 

           else if(tileToJumpTo==board[x][y] && board[x-1][y+1].getIcon()==b) 
           { 
            board[x][y].setIcon(r); 
            turn1=false; 
            turn2=true; 
            board[x][y].setBackground(Color.red); 
            board[x-1][y+1].setIcon(null); 
           } 

           else if(tileToJumpTo==board[x][y] && board[x][y].getBackground()==Color.red||board[x][y].getBackground()==Color.white) 
           { 
            if(count == 1) 
            { 
             JOptionPane.showMessageDialog(null, "You really should click on a piece if you want to move."); 
             count = 0; 
            } 
            else 
            { 
             board[x][y].setIcon(r); 
             turn1=false; 
             turn2=true; 
             board[x][y].setBackground(Color.red); 
             board[x+2][y].setBackground(Color.red); 
             board[x-2][y].setBackground(Color.red); 
             count = 0; 
            } 
           } 
      } 

我希望這回答了你的問題的幫助你。 :)

Btw。我建議你爲用戶可以製作的每一項操作制定一個自己的方法。