Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class main {
- public static void main(String[] args)
- {
- game var1,var2;
- var1 = new game();
- var2 = new game();
- var1.init();
- var1.print();
- int i;
- for (i=1;i<5;i++)
- {
- if (i%2!=0)
- { var2.update(var1);
- var2.print();
- }
- else
- {
- var1.update(var2);
- var1.print();
- }
- }
- }
- }
- ///
- public class game {
- private cell arr1[][];
- private final static int size=17;
- game(){
- arr1 = new cell[size][size];
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++)
- arr1[i][j] = new white(i,j);
- }
- }
- void init () {
- arr1[3][8] = new black(3, 8);
- arr1[4][7] = new black(4, 7);
- arr1[5][7] = new black(5, 7);
- arr1[5][8] = new black(5, 8);
- arr1[5][9] = new black(5, 9);
- arr1[10][7] = new black(10, 7);
- arr1[10][8] = new black(10, 8);
- arr1[10][9] = new black(10, 9);
- arr1[11][7] = new black(11, 7);
- arr1[12][8] = new black(12, 8);
- arr1[3][2] = new black(3, 2);
- arr1[4][3] = new black(4, 3);
- arr1[5][1] = new black(5, 1);
- arr1[5][2] = new black(5, 2);
- arr1[5][3] = new black(5, 3);
- arr1[9][1] = new black(9, 1);
- arr1[9][2] = new black(9, 2);
- arr1[9][3] = new black(9, 3);
- arr1[10][3] = new black(10, 3);
- arr1[11][2] = new black(11, 2);
- }
- public void update(game m)
- {
- int i,j;
- for(i=1;i<size-1;i++)
- {
- for (j=1;j<size-1;j++)
- {
- arr1[i][j] = m.arr1[i][j].getNextColor(m.arr1);
- }
- }
- }
- public void print() {
- for (int i=1; i<16; i++) {
- for (int j=1; j<16; j++)
- if(arr1[i][j].who() == 1)
- System.out.print("X" + " ");
- else
- System.out.print("-" + " ");
- System.out.println();
- }
- System.out.println();
- }
- }
- ///
- public abstract class cell {
- public int x, y;
- public cell(int x, int y) {
- this.x = x;
- this.y = y;
- }
- abstract int who();
- abstract cell getNextColor(cell[][] a);
- public int neighbours(cell[][] a) {
- int i, j, count;
- count = 0;
- for (i = -1; i < 2; i++) {
- for (j = -1; j < 2; j++) {
- count += a[x + i][y + j].who();
- }
- }
- count -= a[x][y].who();
- return count;
- }
- }
- ///
- public class black extends cell {
- public black(int x, int y) {
- super(x, y);
- }
- public int who() { return 1; }
- cell getNextColor(cell[][] arr1)
- {
- int count = neighbours(arr1);
- if (count == 2 || count == 3)
- return this;
- return new white(x, y);
- }
- }
- ///
- public class white extends cell {
- public white(int x, int y) {
- super(x,y);
- }
- public int who() { return 0; }
- cell getNextColor(cell[][] arr1)
- {
- int count = neighbours(arr1);
- if (count==3)
- return new black(x,y);
- return this;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement