package ttaomae.connectn.gui;

import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import ttaomae.connectn.Board;
import ttaomae.connectn.Player;

public class MousePlayer implements Player
{
    private static final int INVALID_MOVE = -2;
    private int move;

    private BoardPanel boardPanel;
    public MousePlayer(BoardPanel boardPanel)
    {
        this.move = INVALID_MOVE;
        this.boardPanel = boardPanel;

        boardPanel.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent me)
            {
                synchronized (MousePlayer.this) {
                    MousePlayer.this.move = MousePlayer.this.boardPanel.getBoardColumn(me.getX());
                    MousePlayer.this.notifyAll();
                }
            }
        });
    }

    @Override
    public int getMove(Board board)
    {
        this.move = INVALID_MOVE;
        while (this.move == INVALID_MOVE) {
            try {
                synchronized (this) {
                    this.wait();
                }
            } catch (InterruptedException e) {
                // do nothing
                }
            }
        int result = this.move;
        this.move = INVALID_MOVE;
        return result;
    }

        }

@@ -436,24 +436,25 @@
 Panel;%0A%0A
+%0A
         
 boardPan
@@ -445,16 +445,21 @@
         
+this.
 boardPan
@@ -465,26 +465,50 @@
 nel.
-setOnMouseClicked(
+addEventHandler(MouseEvent.MOUSE_CLICKED, 
 new 
@@ -950,32 +950,66 @@
 = INVALID_MOVE;%0A
+        synchronized (this) %7B%0A    
         while (t
@@ -1052,49 +1052,15 @@
     
-try %7B%0A                synchronized (this)
+    try
  %7B%0A 
@@ -1108,30 +1108,16 @@
        %7D
-%0A            %7D
  catch (
@@ -1138,24 +1138,28 @@
 eption e) %7B%0A
+    
             
@@ -1200,32 +1200,42 @@
 %7D%0A            %7D%0A
+        %7D%0A
         int resu
@@ -1313,19 +1313,10 @@
 ;%0A    %7D%0A
-%0A        
 %7D%0A

update MousePlayer to allow two human players