001package ball.game.ant.taskdefs; 002/*- 003 * ########################################################################## 004 * Game Applications and Utilities 005 * %% 006 * Copyright (C) 2010 - 2022 Allen D. Ball 007 * %% 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 * ########################################################################## 020 */ 021import ball.game.life.Board; 022import ball.game.life.Game; 023import ball.util.ant.taskdefs.AnnotatedAntTask; 024import ball.util.ant.taskdefs.AntTask; 025import ball.util.ant.taskdefs.ClasspathDelegateAntTask; 026import ball.util.ant.taskdefs.ConfigurableAntTask; 027import java.math.BigInteger; 028import lombok.Getter; 029import lombok.NoArgsConstructor; 030import lombok.Setter; 031import lombok.ToString; 032import lombok.experimental.Accessors; 033import org.apache.tools.ant.BuildException; 034import org.apache.tools.ant.Task; 035import org.apache.tools.ant.util.ClasspathUtils; 036 037import static org.apache.commons.lang3.StringUtils.EMPTY; 038 039/** 040 * {@link.uri http://ant.apache.org/ Ant} {@link Task} to start {@link Game} 041 * of Life simulation. 042 * 043 * {@ant.task} 044 * 045 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball} 046 */ 047@AntTask("life") 048@NoArgsConstructor @ToString 049public class LifeTask extends Task implements AnnotatedAntTask, ClasspathDelegateAntTask, ConfigurableAntTask { 050 @Getter @Setter @Accessors(chain = true, fluent = true) 051 private ClasspathUtils.Delegate delegate = null; 052 @Getter @Setter 053 private int height = 0; 054 @Getter @Setter 055 private int width = 0; 056 @Getter 057 private BigInteger state0 = BigInteger.ZERO; 058 059 public void setState0(String state0) { this.state0 = parse(state0); } 060 061 public void addText(String text) { setState0(text); } 062 063 private BigInteger parse(String string) { 064 BigInteger state = null; 065 066 try { 067 state = new BigInteger(string); 068 } catch (NumberFormatException exception) { 069 state = BigInteger.ZERO; 070 string = string.replaceAll("[\\p{Space}]+", EMPTY); 071 072 for (int i = 0, n = string.length(); i < n; i += 1) { 073 switch (string.charAt(i)) { 074 case '+': 075 state = state.setBit(i); 076 break; 077 078 case '-': 079 default: 080 state = state.clearBit(i); 081 break; 082 } 083 } 084 } 085 086 return state; 087 } 088 089 @Override 090 public void init() throws BuildException { 091 super.init(); 092 ClasspathDelegateAntTask.super.init(); 093 ConfigurableAntTask.super.init(); 094 } 095 096 @Override 097 public void execute() throws BuildException { 098 super.execute(); 099 AnnotatedAntTask.super.execute(); 100 101 try { 102 var game = new Game(getHeight(), getWidth(), getState0()); 103 var board = new Board(game); 104 105 for (;;) { 106 log(); 107 log("Generation #" + String.valueOf(game.size() - 1)); 108 log(board); 109 110 var state = game.automata().next(game.getLast()); 111 112 if (! game.contains(state)) { 113 game.addLast(state); 114 continue; 115 } else { 116 log("Steady state: Returned to Generation #" + String.valueOf(game.indexOf(state))); 117 break; 118 } 119 } 120 } catch (BuildException exception) { 121 throw exception; 122 } catch (Throwable throwable) { 123 throwable.printStackTrace(); 124 throw new BuildException(throwable); 125 } 126 } 127}