|
| 1 | +rem ============================================ |
| 2 | +rem The Game of Life. |
| 3 | +rem Uses AI to evolve the grid |
| 4 | +rem (c) George P. Afentakis |
| 5 | +rem ============================================ |
| 6 | + |
| 7 | +print "\n\n\nThe Conway's Game of Life." |
| 8 | +print "How many Generations should I'll simulate: "; |
| 9 | +input M |
| 10 | + |
| 11 | +gosub MakeAIRules |
| 12 | + |
| 13 | +let SEED=rnd()*34214*M 'SEED is a random 5 digit number or more |
| 14 | +let i = 0 |
| 15 | +let N = 16 |
| 16 | + |
| 17 | + |
| 18 | +MEMSTREAM GridStream |
| 19 | + |
| 20 | +REM Init random grid |
| 21 | +For i = 1 To N * N |
| 22 | + let gameChar = "." |
| 23 | + SEED = mod(SEED * 16807, 2147483647) |
| 24 | + let rng = mod(SEED * 11, 100) |
| 25 | + if rng < 27 Then |
| 26 | + gameChar = "@" |
| 27 | + EndIf |
| 28 | + SDATA GameScreen gameChar |
| 29 | +Next i |
| 30 | + |
| 31 | +let gen = 0 |
| 32 | + |
| 33 | +For gen = 0 To M - 1 |
| 34 | + |
| 35 | + print "Generation " + str(gen) |
| 36 | + |
| 37 | + RESET GameScreen |
| 38 | + let GameOutput = "" |
| 39 | + let cnt = 0 |
| 40 | + |
| 41 | + FOREACH GameScreen |
| 42 | + GameOutput = GameOutput + [GameScreen.Item] + " " |
| 43 | + cnt = cnt + 1 |
| 44 | + if mod(cnt, N) = 0 Then |
| 45 | + GameOutput = GameOutput + "\n" |
| 46 | + EndIf |
| 47 | + ENDFOREACH GameScreen |
| 48 | + |
| 49 | + print GameOutput |
| 50 | + print "" |
| 51 | + |
| 52 | + if gen = M - 1 Then |
| 53 | + GOTO DoneAll |
| 54 | + EndIf |
| 55 | + |
| 56 | + REM write grid to GridStream |
| 57 | + RESET GameScreen |
| 58 | + let GridText = "" |
| 59 | + FOREACH GameScreen |
| 60 | + GridText = GridText + [GameScreen.Item] |
| 61 | + ENDFOREACH GameScreen |
| 62 | + |
| 63 | + SREWIND GridStream |
| 64 | + STOS GridStream FROM GridText |
| 65 | + |
| 66 | + REM run the AI generated code |
| 67 | + eval RuleProgram |
| 68 | + |
| 69 | + REM read new grid back |
| 70 | + SREWIND GridStream |
| 71 | + STOS GridStream TO GridTextNext |
| 72 | + |
| 73 | + let totalCells = len(GridTextNext) |
| 74 | + let idx = 0 |
| 75 | + For idx = 1 To totalCells |
| 76 | + let ch = mid(GridTextNext, idx, 1) |
| 77 | + SSET GameScreen idx ch |
| 78 | + Next idx |
| 79 | + |
| 80 | +Next gen |
| 81 | + |
| 82 | +DoneAll: |
| 83 | + |
| 84 | +print "\nChange on the rules:\n" |
| 85 | +print RuleChange |
| 86 | +print "\n\n" |
| 87 | + |
| 88 | +HALT |
| 89 | + |
| 90 | + |
| 91 | +REM ============================================ |
| 92 | +REM GenerateRule |
| 93 | +REM Ask AI to generate code based on a template |
| 94 | +REM ============================================ |
| 95 | + |
| 96 | +REM --- Configure AI provider & session --- |
| 97 | +MakeAIRules: |
| 98 | + |
| 99 | +AIPROVIDER MyProvider, CLAUDE, * |
| 100 | +AISESSION RuleSession, MyProvider, "You Generate code in a tiny BASIC-like language called FBASIC. You must output ONLY clear FBASIC code, no markdown marks, no explanations, no markdown, no ``` fences." |
| 101 | +print "Should the Game rule include anything special?" |
| 102 | +print "(E.g. How many New rules should I add? Or just write \"Be Very Creative\" )" |
| 103 | +print "Left empty line, to use the sample code or enter `L` and the last rule will be used: "; |
| 104 | +input suggestionRule |
| 105 | + |
| 106 | +gosub loadSampleCode |
| 107 | +if suggestionRule = "" then |
| 108 | + let RuleProgram = samplecode |
| 109 | + return |
| 110 | +endif |
| 111 | +if ucase(suggestionRule) = "L" Then |
| 112 | + gosub LoadLastRule |
| 113 | + return |
| 114 | +endif |
| 115 | + |
| 116 | +let RuleProgram = "" |
| 117 | +let prompt = "Here is a complete novel programming language for a Game of Life rule:" + "\n" + samplecode |
| 118 | +let prompt = prompt+"\nStudy the syntax and Rewrite this so it follows different rules the rules can be as usual Or as diffrent New And creative as you want. |
| 119 | +Output ONLY the final FBASIC program code without markdown code. No explanations. No code block in the begging Or the end. |
| 120 | +The point here Is playing Conways Game of life. But utilize whatever New rules you want."+ suggestionRule |
| 121 | + |
| 122 | +PRINT "Waiting AI to make a new rule code." |
| 123 | +AIPROMPT RuleSession, RuleProgram, prompt |
| 124 | +AIPROMPT RuleSession, RuleChange, "What exactly did you change? What New rules did you introduce Or what old ones did you delete? Explain in 1 to 10 sentences" |
| 125 | + |
| 126 | + |
| 127 | +gosub SaveGeneratedRule |
| 128 | + |
| 129 | +RETURN |
| 130 | + |
| 131 | +rem |
| 132 | +rem Save the Generated rule |
| 133 | +rem |
| 134 | +SaveGeneratedRule: |
| 135 | +FILESTREAM RuleFile, out, "","Output", "lifeGame.rule" |
| 136 | +STOS RuleFile FROM RuleProgram |
| 137 | + |
| 138 | +FILESTREAM ChangeFile, out, "","Output", "lifeGame.txt" |
| 139 | +let txt=suggestionRule+"\n\n"+RuleChange |
| 140 | +STOS ChangeFile FROM txt |
| 141 | +sclose ChangeFile |
| 142 | +sclose RuleFile |
| 143 | +PRINT "Generated program saved." |
| 144 | +return |
| 145 | + |
| 146 | + |
| 147 | +rem |
| 148 | +rem Load last Generated rule |
| 149 | +rem |
| 150 | +LoadLastRule: |
| 151 | +FILESTREAM oldRuleFile, in, "","Output", "lifeGame.rule" |
| 152 | +STOS oldRuleFile TO RuleProgram |
| 153 | +FILESTREAM oldRuleText, in, "","Output", "lifeGame.txt" |
| 154 | +STOS oldRuleText TO RuleChange |
| 155 | +sclose oldRuleText |
| 156 | +sclose oldRuleFile |
| 157 | +PRINT "Last generated program loaded." |
| 158 | +return |
| 159 | + |
| 160 | +rem |
| 161 | +rem Sample Code |
| 162 | +rem |
| 163 | +loadSampleCode: |
| 164 | + |
| 165 | +let samplecode = "let N = 16 |
| 166 | +SREWIND GridStream |
| 167 | +STOS GridStream TO GridText |
| 168 | +let row = 0 |
| 169 | +let col = 0 |
| 170 | +let qRow = 0 |
| 171 | +let qCol = 0 |
| 172 | +let idx = 0 |
| 173 | +let cellChar = \".\" |
| 174 | +let curCell = \".\" |
| 175 | +let liveNeighbors = 0 |
| 176 | +let newChar = \".\" |
| 177 | +let NextText = \"\" |
| 178 | +
|
| 179 | +For row = 1 To N |
| 180 | +For col = 1 To N |
| 181 | +
|
| 182 | +qRow = row |
| 183 | +qCol = col |
| 184 | +GOSUB GetCell |
| 185 | +curCell = cellChar |
| 186 | +
|
| 187 | +liveNeighbors = 0 |
| 188 | +
|
| 189 | +qRow = row - 1 |
| 190 | +qCol = col - 1 |
| 191 | +GOSUB GetCell |
| 192 | +if cellChar = \"@\" Then |
| 193 | + liveNeighbors = liveNeighbors + 1 |
| 194 | +EndIf |
| 195 | +
|
| 196 | +qRow = row - 1 |
| 197 | +qCol = col |
| 198 | +GOSUB GetCell |
| 199 | +if cellChar = \"@\" Then |
| 200 | + liveNeighbors = liveNeighbors + 1 |
| 201 | +EndIf |
| 202 | +
|
| 203 | +qRow = row - 1 |
| 204 | +qCol = col + 1 |
| 205 | +GOSUB GetCell |
| 206 | +if cellChar = \"@\" Then |
| 207 | + liveNeighbors = liveNeighbors + 1 |
| 208 | +EndIf |
| 209 | +
|
| 210 | +qRow = row |
| 211 | +qCol = col - 1 |
| 212 | +GOSUB GetCell |
| 213 | +
|
| 214 | +if cellChar = \"@\" Then |
| 215 | + liveNeighbors = liveNeighbors + 1 |
| 216 | +EndIf |
| 217 | +
|
| 218 | +qRow = row |
| 219 | +qCol = col + 1 |
| 220 | +GOSUB GetCell |
| 221 | +
|
| 222 | +if cellChar = \"@\" Then |
| 223 | + liveNeighbors = liveNeighbors + 1 |
| 224 | +EndIf |
| 225 | +
|
| 226 | +qRow = row + 1 |
| 227 | +qCol = col - 1 |
| 228 | +GOSUB GetCell |
| 229 | +
|
| 230 | +if cellChar = \"@\" Then |
| 231 | + liveNeighbors = liveNeighbors + 1 |
| 232 | +EndIf |
| 233 | +
|
| 234 | +qRow = row + 1 |
| 235 | +qCol = col |
| 236 | +GOSUB GetCell |
| 237 | +
|
| 238 | +if cellChar = \"@\" Then |
| 239 | + liveNeighbors = liveNeighbors + 1 |
| 240 | +EndIf |
| 241 | +
|
| 242 | +qRow = row + 1 |
| 243 | +qCol = col + 1 |
| 244 | +GOSUB GetCell |
| 245 | +
|
| 246 | +if cellChar = \"@\" Then |
| 247 | + liveNeighbors = liveNeighbors + 1 |
| 248 | +EndIf |
| 249 | +
|
| 250 | +newChar = \".\" |
| 251 | +if curCell = \"@\" Then |
| 252 | + if liveNeighbors = 2 Then |
| 253 | + newChar = \"@\" |
| 254 | + EndIf |
| 255 | + |
| 256 | + if liveNeighbors = 3 Then |
| 257 | + newChar = \"@\" |
| 258 | + EndIf |
| 259 | + Else |
| 260 | + if liveNeighbors = 3 Then |
| 261 | + newChar = \"@\" |
| 262 | + EndIf |
| 263 | +EndIf |
| 264 | +
|
| 265 | +NextText = NextText + newChar |
| 266 | +Next col |
| 267 | +Next row |
| 268 | +
|
| 269 | +SREWIND GridStream |
| 270 | +STOS GridStream FROM NextText |
| 271 | +RESULT 0 |
| 272 | +END |
| 273 | +
|
| 274 | +GetCell: |
| 275 | +if qRow < 1 Then |
| 276 | + cellChar = \".\" |
| 277 | + RETURN |
| 278 | +EndIf |
| 279 | +if qRow > N Then |
| 280 | + cellChar = \".\" |
| 281 | + RETURN |
| 282 | +EndIf |
| 283 | +
|
| 284 | +if qCol < 1 Then |
| 285 | + cellChar = \".\" |
| 286 | + RETURN |
| 287 | +EndIf |
| 288 | +
|
| 289 | +if qCol > N Then |
| 290 | + cellChar = \".\" |
| 291 | + RETURN |
| 292 | +EndIf |
| 293 | +
|
| 294 | +idx = (qRow - 1) * N + qCol |
| 295 | +cellChar = mid(GridText, idx, 1) |
| 296 | +RETURN |
| 297 | +" |
| 298 | +return |
0 commit comments