0

私は gridworld でコネクト フォー ゲームを作成していますが、ご存知であれば、先生はマウス クリックについて何も教えてくれませんでした。コネクト フォー ピースは、クリックした列に配置する必要があります。マウスリスナーを追加するためにJframeを作成しましたが、それを機能させることができません。

したがって、マウスクリックを機能させてクリックのx座標とy座標を出力するにはどうすればよいかを知る必要があります。

public class ConnectFourWorld extends World<Piece> implements MouseListener
{
   private String whosTurn;
   private boolean gameOver;
   private String winner;
   Piece X = new Piece("ex", Color.WHITE, Color.RED);
   Piece O = new Piece("oh", Color.YELLOW, Color.BLUE);
   Location column1 = new Location(5, 0);
   Location column2 = new Location(5, 1);
   Location column3 = new Location(5, 2);
   Location column4 = new Location(5, 3);
   Location column5 = new Location(5, 4);
   Location column6 = new Location(5, 5);
   Location column7 = new Location(5, 6);

   public ConnectFourWorld()
   {
    super(new BoundedGrid<Piece>(6,7));
    JFrame frame = new JFrame();
    frame.setSize(new Dimension(350, 300));
    frame.addMouseListener((MouseListener) this);
    frame.pack();
    frame.setVisible(true);
    winner="no winner";
    whosTurn="X";
    gameOver=false;
      setMessage("Welcome to Connect Four World!  - -  Click a spot - "+whosTurn+"'s                     turn.");           
   }

   public boolean locationClicked(Location loc)
   {
      Grid<Piece> grid = getGrid();
      if(grid == null)
        return false;
  ArrayList<Location> reset = new ArrayList<Location>();
  reset  = grid.getOccupiedLocations();


  if(grid.get(loc)!=null)
    return false;

  if(gameOver == true)
  {
      for(int i=0; i<reset.size(); i++){
          grid.remove(reset.get(i));
      }

     gameOver = false;
     winner = "no winner";
       setMessage("Click a spot - " + whosTurn + "'s turn.");
    return true;
  }
  // Create a new location variable = add a piece
  Location place = new Location(0,0);
  place = addPiece(loc);
  if(place.equals(null))
      return false;
  //if location variable is = to null return false
  //call the get winner method and step method
  getWinner(loc);
  step();


  return true;  
   }

   public Location addPiece(Location loc)
   {
    Grid<Piece> grid = getGrid();
   if( grid == null)
  return null;  



 int col = loc.getCol();
 int row = grid.getNumRows() - 1;
 Location spot = new Location(row, col);
 ArrayList<Location> occ = new ArrayList<Location>();
 boolean taken = false;
 occ = grid.getOccupiedLocations();
 for (int i=0; i<occ.size(); i++){
     if(spot.equals(occ.get(i)))
         taken=true;
 }
 //loop that runs as long as location is valid and location is not null
    // move up in the row and add the new Location to that row and col
 while((!spot.equals(null))&&taken==false){
     for (int i=0; i<occ.size(); i++){
         if(spot.equals(occ.get(i)))
             taken=true;
     }
     row++;
 }
 //add a piece to that location and return the location
 if(whosTurn.equals("X"))
     grid.put(spot, X);
 else if(whosTurn.equals("O"))
     grid.put(spot, O);
 return spot;
   }

   //This method is done
public void step()
   {  
    Grid<Piece> grid = getGrid();
    if (grid == null)
       return;
      if(!winner.equals("no winner"))
        {
          setMessage("And the winner is..... " + winner + "\n Click anywhere on board to play again.");
               gameOver = true;

   }
}

    //This one is done   
   public boolean isWorldFull()
   {
    Grid<Piece> grid = getGrid();
       if(grid == null)
       return false;

    ArrayList <Location> list = grid.getOccupiedLocations();
     return (grid.getNumCols() * grid.getNumRows() == list.size());
  }


   public void resetWorld()
   {

   }

   public String getWinner(Location loc)
   {
    return "";
   }
@Override
public void mouseClicked(MouseEvent e) {
    int x=e.getX();
        int y=e.getY();
    System.out.println(x+","+y);
...

これを実行する別のメインクラスがあります

4

2 に答える 2