import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;

/*
 *  Genetic Algorithm Exploration
 *  Model component.  when constructed, loads the world file,
 *  builds the initial candidates,
 *  provides the timer facilities and associated action listener.    
 *  @author Roger Frank
 *  @version 1.1
 */

public class GAmodel implements ActionListener {
    private int[][] myGrid;
    private ArrayList paths, newpaths;
    private GAview myView;
    private Timer timer;
    // private Path solutionPath;
        
    private static int locX = 1;        // for demo/stub only
    private static int locY = 1;        // for demo/stub only
        
    /*
     * reads the initial map and builds initial candidate set
     */

    public GAmodel( GAview view ) {
        myGrid = new int[ GA.GRIDY ][ GA.GRIDX ];
        myView = view;
        /* student code here */
        myView.updateView( myGrid );
    }

    public void pause() {
        timer.stop();
    }
    public void resume() {
        timer.restart();
    }
    public void run() {
        timer = new Timer( GA.TIMER_DELAY_MSEC, this );
        timer.setCoalesce( true );
        timer.start();
    }

    /*
     * trace path, stub code 
     */

    private void tracePath( /*Path p*/ ) {
        myGrid[ locX][ locY ] = 1;
        locX++;
        locY++;
        if ( locX >= GA.GRIDX-1 || locY >= GA.GRIDY-1 ) {
            timer.stop();
        }
    }

    /*
     * get here each time timer fires
     */

    public void actionPerformed( ActionEvent e ) {
        tracePath( /*solutionPath*/ );
        myView.updateView( myGrid );
    }
}
