mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Added MiniScript versions of 55_Life and 56_Life_for_Two.
This commit is contained in:
19
00_Alternate_Languages/55_Life/MiniScript/README.md
Normal file
19
00_Alternate_Languages/55_Life/MiniScript/README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
Original source downloaded from [Vintage Basic](http://www.vintage-basic.net/games.html).
|
||||
|
||||
Conversion to [MiniScript](https://miniscript.org).
|
||||
|
||||
Ways to play:
|
||||
|
||||
1. Command-Line MiniScript:
|
||||
Download for your system from https://miniscript.org/cmdline/, install, and then run the program with a command such as:
|
||||
|
||||
```
|
||||
miniscript life.ms
|
||||
```
|
||||
2. Mini Micro:
|
||||
Download Mini Micro from https://miniscript.org/MiniMicro/, launch, and then click the top disk slot and chose "Mount Folder..." Select the folder containing the MiniScript program and this README file. Then, at the Mini Micro command prompt, enter:
|
||||
|
||||
```
|
||||
load "life"
|
||||
run
|
||||
```
|
||||
132
00_Alternate_Languages/55_Life/MiniScript/life.ms
Normal file
132
00_Alternate_Languages/55_Life/MiniScript/life.ms
Normal file
@@ -0,0 +1,132 @@
|
||||
print " "*34 + "LIFE"
|
||||
print " "*15 + "Creative Computing Morristown, New Jersey"
|
||||
print; print; print
|
||||
import "listUtil" // (needed for list.init2d)
|
||||
|
||||
maxx = 66 // (size adjusted for Mini Micro display)
|
||||
maxy = 23
|
||||
A = list.init2d(maxx+1, maxy+1, 0)
|
||||
|
||||
// Stuff the given pattern into the center of the cell array.
|
||||
// Return the number of live cells.
|
||||
stuffIntoCenter = function(pattern)
|
||||
maxLen = 0
|
||||
for p in pattern
|
||||
if p.len > maxLen then maxLen = p.len
|
||||
end for
|
||||
|
||||
population = 0
|
||||
y = floor(maxy/2 - pattern.len/2)
|
||||
for row in pattern
|
||||
x = floor(maxx/2 - maxLen/2)
|
||||
for c in row
|
||||
if c != " " then
|
||||
A[x][y] = 1
|
||||
population += 1
|
||||
end if
|
||||
x += 1
|
||||
end for
|
||||
y += 1
|
||||
end for
|
||||
return population
|
||||
end function
|
||||
|
||||
// Get the initial pattern from the user
|
||||
initToUserPattern = function
|
||||
print "Enter your pattern (enter DONE when done):"
|
||||
userPattern = []
|
||||
while true
|
||||
p = input("?")
|
||||
if p.upper == "DONE" then break
|
||||
if p and p[0] == "." then p = " " + p[1:]
|
||||
userPattern.push p
|
||||
end while
|
||||
return stuffIntoCenter(userPattern)
|
||||
end function
|
||||
|
||||
// For testing purposes, skip asking the user and just use a hard-coded pattern.
|
||||
initToStandardPattern = function
|
||||
pattern = [
|
||||
" **",
|
||||
" * *",
|
||||
" *"]
|
||||
return stuffIntoCenter(pattern)
|
||||
end function
|
||||
|
||||
// Or, just for fun, initialize to a random pattern of junk in the center.
|
||||
initRandom = function
|
||||
for x in range(ceil(maxx*0.3), floor(maxx*0.7))
|
||||
for y in range(ceil(maxy*0.3), floor(maxy*0.7))
|
||||
A[x][y] = rnd > 0.5
|
||||
end for
|
||||
end for
|
||||
end function
|
||||
|
||||
// Define a function to draw the current game state.
|
||||
// This also changes 2 (dying) to 0 (dead), and 3 (being born) to 1 (alive).
|
||||
drawGameState = function(generation=0, population=0, invalid=false)
|
||||
if version.hostName == "Mini Micro" then text.row = 26 else print
|
||||
print "Generation: " + generation + " Population: " + population +
|
||||
" " + "INVALID!" * invalid
|
||||
for y in range(0, maxy)
|
||||
s = ""
|
||||
for x in range(0, maxx)
|
||||
if A[x][y] == 2 then
|
||||
A[x][y] = 0
|
||||
else if A[x][y] == 3 then
|
||||
A[x][y] = 1
|
||||
end if
|
||||
if A[x][y] then s += "*" else s += " "
|
||||
end for
|
||||
print s
|
||||
end for
|
||||
end function
|
||||
|
||||
// Update the game state, setting cells that should be born to 3 and
|
||||
// cells that should die to 2. Return the number of cells that will
|
||||
// be alive after this update. Also, set globals.invalid if any live
|
||||
// cells are found on the edge of the map.
|
||||
updateGameState = function
|
||||
population = 0
|
||||
for x in range(0, maxx)
|
||||
for y in range(0, maxy)
|
||||
c = A[x][y] == 1 or A[x][y] == 2 // center state
|
||||
n = -c // number of neighbors
|
||||
for nx in range(x-1, x+1)
|
||||
if nx < 0 or nx > maxx then continue
|
||||
for ny in range(y-1, y+1)
|
||||
if ny < 0 or ny > maxy then continue
|
||||
n += A[nx][ny] == 1 or A[nx][ny] == 2
|
||||
end for
|
||||
end for
|
||||
if c and n != 2 and n != 3 then // live cell with < 2 or > 3 neighbors...
|
||||
A[x][y] = 2 // dies
|
||||
else if not c and n == 3 then // dead cell with 3 neighbors...
|
||||
A[x][y] = 3 // comes to life
|
||||
if x == 0 or x == maxx or y == 0 or y == maxy then
|
||||
globals.invalid = true
|
||||
end if
|
||||
end if
|
||||
population += (A[x][y] == 1 or A[x][y] == 3)
|
||||
end for
|
||||
end for
|
||||
return population
|
||||
end function
|
||||
|
||||
|
||||
// Initialize the game state (uncomment one of the following three lines)
|
||||
population = initToUserPattern
|
||||
//population = initToStandardPattern
|
||||
//population = initRandom
|
||||
|
||||
// Main loop
|
||||
if version.hostName == "Mini Micro" then clear
|
||||
invalid = false
|
||||
generation = 0
|
||||
while population > 0
|
||||
drawGameState generation, population, invalid
|
||||
population = updateGameState
|
||||
generation += 1
|
||||
//key.get // <-- Uncomment this to single-step with each keypress!
|
||||
end while
|
||||
drawGameState generation, population, invalid
|
||||
Reference in New Issue
Block a user