Super Life Source
_root.HEIGHT = 10;
_root.WIDTH = 10;
_root.XOFFSET = 50;
_root.YOFFSET = 50;
init();
//traceBoard();
makeBoardFromArray();
displayBoard();
setupButtons();
function init(){
//set up board array
_root.board = new Array(_root.HEIGHT + 2);
for(row = 0; row <= _root.HEIGHT + 1; row++){
_root.board[row] = new Array(_root.WIDTH + 2);
for (col = 0; col <= _root.WIDTH + 1; col++){
_root.board[row][col] = 0;
} // end col loop
} // end row loop
} // end init
function makeBoardFromArray(){
_root.board = new Array(
new Array(0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,1,1,0,0,0,0,0),
new Array(0,0,0,0,1,1,0,0,0,0,0,0),
new Array(0,0,0,0,0,1,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0)
);
} // end makeFromArray
function traceBoard(){
for (row = 0; row <= _root.HEIGHT + 1; row++){
theLine = "";
for (col = 0; col <= _root.WIDTH + 1; col++){
theLine += _root.board[row][col];
theLine += "\t";
} // end col loop
trace(theLine);
} // end row loop
} // end traceBoard
function displayBoard(){
//creates a display based on the board array
for (row = 1; row <= _root.HEIGHT; row++){
for (col = 1; col <= _root.WIDTH; col++){
cellInstance = "cell_" + row + "_" + col;
zorder = (row * 100) + col;
_root.attachMovie("cell", cellInstance, zorder);
theCell = eval(cellInstance);
theCell._x = _root.XOFFSET + ((theCell._width + 2) * col);
theCell._y = _root.YOFFSET + ((theCell._height + 2) * row);
if (_root.board[row][col] == 1){
theCell.gotoAndStop("alive");
} else {
theCell.gotoAndStop("dead");
} // end if
theCell.row = row;
theCell.col = col;
theCell.onMouseUp = function(){
//respond only if mouse is over me (aaaargh!)
if (this.hitTest(_root._xmouse, _root._ymouse, false)){
if (_root.board[this.row][this.col] == 0){
_root.board[this.row][this.col] = 1;
this.gotoAndStop("alive");
} else {
_root.board[this.row][this.col] = 0;
this.gotoAndStop("dead");
} // end 'alive' if
} // end 'over me' if
} // end function def
} // end col for loop
} // end row for loop
} // end displayBoard
function setupButtons(){
btnNext.onRelease = function(){
buildNextGen();
} // end function
btnSave.onRelease = function(){
result = boardToXML();
trace (result);
//following code packages data to send to a php script on a server
//sender = new LoadVars();
//sender.fileName = "temp.xml";
//sender.fileData = result;
//sender.send("http://shelob.cs.iupui.edu:18011/saveFile.php");
fileName = "temp.xml";
fileData = result;
getUrl ("http://shelob.cs.iupui.edu:18011/saveFile.php", "_blank", "POST");
} // end btnSave
btnFive.onRelease = function(){
theXML = new XML();
theXML.ignoreWhite = true;
theXML.onLoad = xmlToBoard;
theXML.load("fiveLine.xml");
} // end btnFive def
btnGlider.onRelease = function(){
theXML = new XML();
theXML.ignoreWhite = true;
theXML.onLoad = xmlToBoard;
theXML.load("glider.xml");
} // end btnFive def
} // end setupButtons
function boardToXML(){
//creates an XML variant of the board
xmlOut = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
xmlOut += "<board>\n";
for (row = 0; row <= _root.HEIGHT + 1; row++){
xmlOut += " <row>\n";
for (col = 0; col <= _root.WIDTH + 1; col++){
xmlOut += " <cell>";
xmlOut += _root.board[row][col];
xmlOut += "</cell>\n";
} // end col loop
xmlOut += " </row>\n";
} // end row loop
xmlOut += "</board>";
return xmlOut;
} // end boardToXML
function xmlToBoard(){
/reads data from file and copies to board array
theBoard = theXML.firstChild;
rows = theBoard.childNodes;
for (row = 0; row <= _root.HEIGHT + 1; row++){
currentRow = rows[row];
cols = currentRow.childNodes;
for (col = 0; col <= _root.WIDTH + 1; col++){
_root.board[row][col] = parseInt(cols[col].firstChild.toString());
cellInstance = "cell_" + row + "_" + col;
theCell = eval(cellInstance);
if (_root.board[row][col] == "1"){
theCell.gotoAndStop("alive");
} else {
theCell.gotoAndStop("dead");
} // end if
} // end col loop
} // end row loop
} // end xmlToBoard
function buildNextGen(){
//calculate and display next generation
//set up next generation array
nextGen = new Array(_root.HEIGHT + 2);
for(row = 0; row <= _root.HEIGHT + 1; row++){
nextGen[row] = new Array(_root.WIDTH + 2);
for (col = 0; col <= _root.WIDTH + 1; col++){
nextGen[row][col] = 0;
} // end col loop
} // end row loop
//look at neighbors -skip outer row
for (row = 1; row <= _root.HEIGHT; row++){
for (col = 1; col <= _root.WIDTH; col++){
//look how much trouble the following line would have caused...
//neigbors = 0;
neighbors = 0;
neighbors += _root.board[row-1][col-1];
neighbors += _root.board[row-1][col];
neighbors += _root.board[row-1][col+1];
neighbors += _root.board[row][col-1];
neighbors += _root.board[row][col+1];
neighbors += _root.board[row+1][col-1];
neighbors += _root.board[row+1][col];
neighbors += _root.board[row+1][col+1];
//trace("cell " + row + ", " + col + " has " + neighbors + " neighbors");
if (neighbors == 3){
nextGen[row][col] = 1;
} // end if
if ((neighbors == 2) &&
(_root.board[row][col] == 1)){
nextGen[row][col] = 1;
} // end if
} // end col for
} // end row for
//copy values of nextGen to board and update display
for (row = 1; row <= _root.HEIGHT; row++){
for (col = 1; col <= _root.WIDTH; col++){
_root.board[row][col] = nextGen[row][col];
theCell = eval("_root.cell_" + row + "_" + col);
if (_root.board[row][col] == 1){
theCell.gotoAndStop("alive");
} else {
theCell.gotoAndStop("dead");
} // end if
} // end col for loop
} // end row for loop
} // end nextGen function