Title: Game of life - Ants
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Game of Life with Ants

In this video, a program is developed where we will create a simulation game with "simulated ants". This example uses a 2-dimensional array of classes (Ants).

The Solution that is developed in this video is as follows:

Ant.java


package gameOfLife;

public class Ant {
    static final int ISOLATION_DEATH_COUNT = 2;
    private boolean alive = false;
    private int isolationCount = 0;

    public boolean isAlive()
    {
        return alive;
    }
    public void setAlive(boolean setVal)
    {
        alive = setVal;
    }
    public String toString()
    {
        if (alive) return " A ";
        return   "   "; // Dead ant
    }
    void update(int numNeighbors, int row, int col, boolean debug)
    {
        if (alive)
        {
            if (numNeighbors > 3)
            {
                alive = false;
                if (debug)
                    System.out.println(row + " : " + col + " - Dying from crowding");
            }
            else if (numNeighbors == 0)
            {
                if (++isolationCount >= ISOLATION_DEATH_COUNT)
                {
                    alive = false;
                    if (debug)
                        System.out.println(row + " : " + col + " - Dying from isolation");
                }
            }
        }
        else
        {
            if (numNeighbors == 2)
            {
                alive = true;
                if (debug)
                    System.out.println(row + " : "+ col + " Birth");
            }
        }
    }

}



Life.java


package gameOfLife;

import java.util.Scanner;

public class Life {
    static final int NUM_ROWS= 8;
    static final int NUM_COLS = 10;
    Ant[][] world;
    static Scanner keyboard = new Scanner(System.in);

    Life(double density)
    {
        world = new Ant[NUM_ROWS][NUM_COLS];
        for (int i=0; i < NUM_ROWS; i++)
        {
            for (int j=0; j < NUM_COLS; j++)
            {
                Ant ant = new Ant();
                world[i][j] = ant;
                if ( Math.random() < density)
                    ant.setAlive(true);
            }
        }
    }
    void display()
    {
        for (int r=0; r < NUM_ROWS; r++)
        {
            for (int c=0; c < NUM_COLS; c++)
            {
                System.out.print(world[r][c]);
            }
            System.out.println();
        }
    }
    int countNeighbors(int row, int col)
    {
        int count =0;
        for (int r= row -1; r <= row+1; r++)
        {
            for (int c= col-1; c <= col+1; c++)
            {
                if (r == row && c == col) continue;
                if ( r < 0 || c < 0) continue;
                if ( r >= NUM_ROWS  || c >= NUM_COLS) continue;
                if (world[r][c].isAlive())
                    count +=1;
            }
        }
        return count;
    }

    void worldupdate(boolean debug)
    {
        for (int r=0; r < NUM_ROWS; r++)
        {
            for (int c=0; c < NUM_COLS; c++)
            {
                int neighborCount = countNeighbors(r, c);
                world[r][c].update(neighborCount, r, c, debug);
            }
        }
    }

    void playGame(boolean debug)
    {
        String response = null;
        do
        {
            display();
            worldupdate(debug);
            System.out.println("Do you want to keep going (y/n)");
            response = keyboard.next();
        } while ("y".equals(response));
    }

    public static void main(String[] args) {
        System.out.println("Enter in Ant density (number between 0.0 and 1.0");

        double density = keyboard.nextDouble();

        Life life = new Life(density);
        life.playGame(true);

    }

}



Last Modified: September 30, 2013 4:30 AM