The Game of LIFE
no, not life the board game, but life the mathematical simulation.
presented here as an actual game.
The Theory:
The game of life is a mathematical concept dating from 1970.
Take a grid and randomly fill in some spaces.
Then, the grid changes based on certain "rules".
1. an empty (dead) cell surrounded by exactly 3 live cells will come to life.
2. if a cell is alive, and it is surrounded by 2 or 3 other cells, it stays alive.
3. if a cell is surrounded by 0 or 1 other cell, it dies of loneliness;
if it is surrounded by more than 3 other cells, it dies of overcrowding.
these rules cause changes to propagate throughout the board every turn.
My Version:
i have changed the game of life into an actual GAME.
the board starts in a random state.
each turn, the human player uses the mouse to place one cell of his/her type.
then, 3 computer players do the same.
New cells may be placed in any empty place on the board.
then, the rules of LIFE are applied, and the board changes.
REPEAT.
the game ends when one player reaches 125 Victory Points.
VPs are scored every turn according to who has the most cells on the board.
3 Distinct AIs:
each computer player obeys different selection rules.
Player 2, the "Breeder", will only place a new cell in a space
with 2 or 3 surrounding cells, to be sure that it will grow.
Player 3, The "Killer", intentionally places cells where they
will cause existing cells to die.
Player 4, The "Wanderer", Places cells randomly in any open space.
each player can beat you, and each one can lose...
the game takes some amazing turns.
Screenshot:
The 20-Line Code:
set display mode 800,600,32 : VICTORY=125 : set text font "courier" : set text to bold : set text size 28 : randomize timer() : dim l$(40) as string : dim grid(20,20) : dim p(4) : dim c(9) : dim s(4) : dim v(4) : for t=1 to 27 : read k : l$(t)=chr$(k) : next t
Game_Starts_Here: : repeat : for t=1 to 4 : p(t)=rnd(26)+1 : next t : until p(1)<>p(2) and p(1)<>p(3) and p(1)<>p(4) and p(2)<>p(3) and p(2)<>p(4) and p(3)<>p(4)
for x=0 to 19 : for y=0 to 19 : a=rnd(26)+1 : for t=1 to 4 : if p(t)=a : a=0 : exit : endif : next t
grid(x,y)=0 : if rnd(7)=1 : grid(x,y)=a : endif : next y : next x : turn=1 : do : cls : for x=0 to 19 : for y=0 to 19 : text x*30,y*30,l$(grid(x,y)) : next y : next x : set text size 20
repeat : box 600,0,800,600,RGB(0,0,128),RGB(0,0,128),RGB(0,0,128),RGB(0,0,128) : text 620,20,"Turn: "+str$(turn) : for t=1 to 4 : center text 700,t*120-25,chr$(34)+l$(p(t))+chr$(34) : center text 700,t*120,"Cells: "+str$(s(t)) : center text 700,t*120+25,"VPs: "+str$(v(t)) : next t : x=mousex()/30 : y=mousey()/30
if mouseclick() : if x>=0 and y>=0 : if x<20 and y<20 : if grid(x,y)=0 : grid(x,y)=p(1) : ink rgb(0,255,0),0 : set text size 28 : text x*30,y*30,l$(p(1)) : moved=1 : endif : endif : endif : endif : until Moved=1 : wait 175 : moved=0 : repeat : x=rnd(19) : y=rnd(19)
if grid(x,y)=0 OR grid(x,y)=p(2) : n=0 : for t=x-1 to x+1 : for u=y-1 to y+1 : if t>-1 and u>-1 : if t<20 and u<20 : if grid(t,u)<>0 : inc n : endif : endif : endif : next u : next t : if (grid(x,y)=0 and n=3) OR ( grid(x,y)=p(2) AND ( n=2 OR n=3 ) )
grid(x,y)=p(2) : moved=1 : text x*30,y*30,l$(p(2)) : endif : endif : until moved=1 : wait 175 : moved=0 : repeat : x=rnd(19) : y=rnd(19) : if grid(x,y)>0 AND grid(x,y)<>p(3) : n=0 : for t=x-1 to x+1 : for u=y-1 to y+1 : if t>-1 and u>-1
if t<20 and u<20 : if grid(t,u)<>0 : inc n : endif : endif : endif : next u : next t : if n=2 or n=3 : tx=0 : ty=0 : for t=x-1 to x+1 : for u=y-1 to y+1 : if t>-1 and u>-1 : if t<20 and u<20
if grid(t,u)=0 : tx=t : ty=u : EXIT : endif : endif : endif : next u : next t : if tx>0 and ty>0 : grid(tx,ty)=p(3) : moved=1 : text tx*30,ty*30,l$(p(3)) : endif : endif
endif : until moved=1 : wait 175 : repeat : x=rnd(19) : y=rnd(19) : if grid(x,y)=0 : grid(x,y)=p(4) : text x*30,y*30,l$(p(4)) : Exit : endif : until 1=0 : wait 175 : for x=0 to 19 : for y=0 to 19
s=grid(x,y) : c=0 : n=0 : for t=0 to 9 : c(t)=0 : next t : for t=x-1 to x+1 : for u=y-1 to y+1 : inc c : if t>-1 and u>-1 : if t<20 and u<20 : if grid(t,u)<>0 : c(c)=grid(t,u) : inc n : endif : endif
endif : next u : next t : if s>0 : if (n=2 or n=3) : else : ink rgb(255,0,0),0 : text x*30,y*30,l$(grid(x,y)) : ink rgb(255,255,255),0 : grid(x,y)=0 : endif : else : if n=3 : Parent=0 : for u=1 to 9 : for t=4 to 1 step -1 : if c(u)=p(t) : Parent=p(t) : endif
next t : next u : if Parent=0 : repeat : Parent=rnd(26)+1 : until parent<>p(1) and parent<>p(2) and parent<>p(3) and parent<>p(4) : endif : for c=1 to 9 : for d=1 to 9 : if c<>d : if c>0 and d>0 : if c=d : Parent=c : Exit : endif
endif : endif : next d : next c : grid(x,y)=Parent : ink rgb(0,0,255),0 : text x*30,y*30,l$(grid(x,y)) : endif : endif : Text x*30,y*30,l$(grid(x,y)) : ink rgb(255,255,255),0 : next y : next x : for t=1 to 4 : s(t)=0
next t : for x=0 to 19 : for y=0 to 19 : for t=1 to 4 : if grid(x,y)=p(t) : inc s(t) : exit : endif : next t : next y : next x : for t=1 to 4 : v=1 : for u=1 to 4 : if t<>u : if s(t)>s(u)
inc v : endif : endif : next u : v(t)=v(t)+v : next t : for t=1 to 4 : if v(t)>=VICTORY : cls : set text size 28 : for x=0 to 26 : for y=0 to 19 : if y<3 or y>16 or x<3 or x>23 : text x*30,y*30,l$(p(t)) : endif : next y : next x
center text 400,200,"PLAYER "+str$(t)+" WINS!" : center text 400,240,str$(v(t))+" Victory Points" : center text 400,280,str$(s(t))+" Cells" : center text 400,320,"in "+str$(turn)+" turns!" : center text 400,400,"Press any key to play again."
wait key : for t=1 to 4 : v(t)=0 : next t : Moved=0 : turn=0 : goto Game_Starts_Here : endif : next t : Moved=0 : inc turn : loop
data 33,35,36,37,38,41,42,43,56,61,63,64,67,71,72,73,77,79,81,83,85,86,87,88,89,90,91
The Spaced-Out and Commented Code:
set display mode 800,600,32
set text font "courier"
set text to bold
set text size 28
randomize timer()
dim l$(40) as string
dim grid(20,20)
dim p(4)
dim c(9)
dim s(4)
dim v(4)
VICTORY=125
for t=1 to 27
read k
l$(t)=chr$(k)
next t
Game_Starts_Here:
` this assigns each player a random CELL
repeat
for t=1 to 4
p(t)=rnd(26)+1
next t
until p(1)<>p(2) and p(1)<>p(3) and p(1)<>p(4) and p(2)<>p(3) and p(2)<>p(4) and p(3)<>p(4)
` this establishes the starting board.
for x=0 to 19
for y=0 to 19
a=rnd(26)+1
for t=1 to 4
if p(t)=a
a=0
exit
endif
next t
grid(x,y)=0
if rnd(7)=1
grid(x,y)=a
endif
next y
next x
turn=1
do
cls
for x=0 to 19
for y=0 to 19
text x*30,y*30,l$(grid(x,y))
next y
next x
` above displays the CELL area, below checks for your move.
` since we have SYNC OFF, we don't need to call sync, or redraw the playing field.
set text size 20
repeat
box 600,0,800,600,RGB(0,0,128),RGB(0,0,128),RGB(0,0,128),RGB(0,0,128)
text 620,20,"Turn: "+str$(turn)
for t=1 to 4
center text 700,t*120-25,chr$(34)+l$(p(t))+chr$(34)
center text 700,t*120,"Cells: "+str$(s(t))
center text 700,t*120+25,"VPs: "+str$(v(t))
next t
` this checks to see if you are making a valid move:
x=mousex()/30
y=mousey()/30
if mouseclick()
if x>=0 and y>=0
if x<20 and y<20
if grid(x,y)=0
grid(x,y)=p(1)
ink rgb(0,255,0),0
set text size 28
text x*30,y*30,l$(p(1))
moved=1
endif
endif
endif
endif
until Moved=1
wait 175
` after each player's turn, there is a short pause so you can see what they did.
` THIS is where the COMPUTER players take their turn.
` PLAYER 2, THE BREEDER
` this player checks to make sure that his newly-placed cell will LIVE!
moved=0
repeat
x=rnd(19)
y=rnd(19)
if grid(x,y)=0 OR grid(x,y)=p(2)
n=0
for t=x-1 to x+1
for u=y-1 to y+1
if t>-1 and u>-1
if t<20 and u<20
if grid(t,u)<>0
inc n
endif
endif
endif
next u
next t
if (grid(x,y)=0 and n=3) OR ( grid(x,y)=p(2) AND ( n=2 OR n=3 ) )
grid(x,y)=p(2)
moved=1
text x*30,y*30,l$(p(2))
endif
endif
until moved=1
wait 175
` PLAYER 3, THE KILLER
` this player intentionally tries to make other player's cells DIE
moved=0
repeat
x=rnd(19)
y=rnd(19)
if grid(x,y)>0 AND grid(x,y)<>p(3)
` he'll try to kill anything. (Except himself!)
n=0
for t=x-1 to x+1
for u=y-1 to y+1
if t>-1 and u>-1
if t<20 and u<20
if grid(t,u)<>0
inc n
endif
endif
endif
next u
next t
` we know that this square belongs to a player,
` now we need to see if it is going to LIVE.
` if it IS, we need to SABOTAGE IT!
if n=2 or n=3
tx=0
ty=0
for t=x-1 to x+1
for u=y-1 to y+1
if t>-1 and u>-1
if t<20 and u<20
if grid(t,u)=0
tx=t
ty=u
EXIT
endif
endif
endif
next u
next t
if tx>0 and ty>0
grid(tx,ty)=p(3)
moved=1
text tx*30,ty*30,l$(p(3))
endif
endif
endif
until moved=1
wait 175
` PLAYER 4, THE WANDERER
` simply places in any empty square.
repeat
x=rnd(19)
y=rnd(19)
if grid(x,y)=0
grid(x,y)=p(4)
text x*30,y*30,l$(p(4))
Exit
endif
until 1=0
wait 175
` these are the lines for the PROCESSING OF LIFE
for x=0 to 19
for y=0 to 19
` S is for SELF, and is the CODE# of this form of life.
s=grid(x,y)
c=0
n=0
for t=0 to 9
c(t)=0
next t
` this stores the CODEs of every cell neighboring this cell, and counts the neighbors (n)
for t=x-1 to x+1
for u=y-1 to y+1
inc c
if t>-1 and u>-1
if t<20 and u<20
if grid(t,u)<>0
c(c)=grid(t,u)
inc n
endif
endif
endif
next u
next t
if s>0
` we are looking at a live cell.
if (n=2 or n=3)
` this says that if we're looking at a live cell, and it has 2 or 3 neighbors.
` IT STAYS ALIVE. no change.
else
` it has the wrong number of neighbors...IT DIES.
ink rgb(255,0,0),0
text x*30,y*30,l$(grid(x,y))
ink rgb(255,255,255),0
grid(x,y)=0
endif
else
` we are looking at a DEAD / EMPTY CELL.
if n=3
` it's needs to come to life. exactly the right # of neighbors...
` but we need to find out WHAT KIND OF CELL THIS SHOULD BE...(which player)
Parent=0
for u=1 to 9
for t=4 to 1 step -1
if c(u)=p(t)
Parent=p(t)
endif
next t
next u
` IF any player surrounds this square, he automatically gets it, BUT
` a random parent is prepared, just in case.
if Parent=0
repeat
Parent=rnd(26)+1
until parent<>p(1) and parent<>p(2) and parent<>p(3) and parent<>p(4)
endif
for c=1 to 9
for d=1 to 9
if c<>d
if c>0 and d>0
if c=d
` we have found two of the same type of cell...they need to be the parents.
Parent=c
Exit
` this EXIT only gets us out of one loop, but it saves a little time, and we
` shouldn't be able to get here if any other condition would change the results,
` so we use it for what it's worth, and just move on.
endif
endif
endif
next d
next c
grid(x,y)=Parent
ink rgb(0,0,255),0
text x*30,y*30,l$(grid(x,y))
endif
endif
Text x*30,y*30,l$(grid(x,y))
ink rgb(255,255,255),0
next y
next x
` these figure VICTORY POINTS for this turn:
for t=1 to 4
s(t)=0
next t
for x=0 to 19
for y=0 to 19
for t=1 to 4
if grid(x,y)=p(t)
inc s(t)
exit
endif
next t
next y
next x
for t=1 to 4
v=1
for u=1 to 4
if t<>u
if s(t)>s(u)
inc v
endif
endif
next u
v(t)=v(t)+v
next t
for t=1 to 4
if v(t)>=VICTORY
cls
set text size 28
for x=0 to 26
for y=0 to 19
if y<3 or y>16 or x<3 or x>23
text x*30,y*30,l$(p(t))
endif
next y
next x
center text 400,200,"PLAYER "+str$(t)+" WINS!"
center text 400,240,str$(v(t))+" Victory Points"
center text 400,280,str$(s(t))+" Cells"
center text 400,320,"in "+str$(turn)+" turns!"
center text 400,400,"Press any key to play again."
wait key
for t=1 to 4
v(t)=0
next t
Moved=0
turn=0
goto Game_Starts_Here
endif
next t
Moved=0
inc turn
loop
data 33,35,36,37,38,41,42,43,56,61,63,64,67,71,72,73,77,79,81,83,85,86,87,88,89,90,91
download a compiled version right here, less that 500k
http://forum.thegamecreators.com/xt/xt_apollo_download.php?i=921343
i did the whole thing in about 10 hours,
and as single lines, it's only 270 lines...zzz
bob