import java.util.Random;

import java.awt.*;

 

/**

*   Class CAProgram takes various inputs from the user, and prints

*   and draws the evolution of a celluar automaton.  The automaton's

*   state and rules for evolution are specified by the user's input.

*   @author Alan Renaud Fall 2000

*   @version 2.0

*/

public class CAProgram {

     /** Number of rules */

     final static int NUMRULES = 8;

    

     /** The main method */

     public static void main( String[] args )

     {

          /** Initialize text object to read from standard input */

          TokenReader in = new TokenReader( System.in );

          /** Graphics object to draw output */

          CAFrame gui = new CAFrame();

          /** Graphics context for drawing object */

          Graphics easel=gui.getGraphics();

         

          String s;

          do {

             
int length = getInt( "Enter length: ", in);
int numOfOutputLines = getInt( "Enter number of output lines: ", in );

int[] rules = getRules( "Enter eight  0/1 values for rules or -1 for random

rules:\n", in);

boolean isRandom = ( getInt( "Initial state? ", in ) == 1 );

 

/** Instantiate new CA object */

              CA bot = new CA( length, isRandom );

             

for ( int i = 0; i < numOfOutputLines; i++ )

               {

                   bot.printState();

                   bot.drawState( i, easel );

                   bot.updateState( rules );

              }

              s = getString("Do you want to stimulate another CA? (yes/no): ", in);               

          } while ( s.equals("yes") );

     }

    

     /**

     *   Ask user for integer input and return the value entered.

     *   @param msg    query for user

     *   @param in       text object to read from standard input

     */

     private static int getInt ( String msg, TokenReader in )

     {

          System.out.print( msg );

          System.out.flush();

          return in.readInt();

     }

    

     /**

     *   Ask user for String input and return the string entered.

     *   @param msg    query for user

     *   @param in       text object to read from standard input

     */

     private static String getString( String msg, TokenReader in )

     {

          System.out.print( msg );

          System.out.flush();

          return in.readString();

     }

    

     /**

     *   Ask user for rules and return a 1x8 array containing them.

     *   If user enters -1, return an array of random 0-1 values.

     *   @param msg    query for user

     *   @param in       text object to read from standard input

     */

     private static int[] getRules( String msg, TokenReader in )

     {

          /** Create array to hold rules */

int[] rules = new int[ NUMRULES ];

          System.out.print( msg );

          System.out.flush();

          int val = in.readInt();

          if ( val == -1 ) { // i.e. user requested random rules

               Random rand = new Random();

              for( int i = 0; i < NUMRULES; i++ )

                   rules[ i ] = Math.abs( rand.nextInt()%2 );

          }

          else {

              rules[ 0 ] = val;

               for( int i = 1; i < NUMRULES; i++ )

                   rules[ i ] = in.readInt();

          }

          System.out.print("Rules are: ");

          for( int i = 0; i < NUMRULES; i++)

               System.out.print( rules[ i ] + " " );

          System.out.println();

          return rules;

     }

 

}