AndroidGameOfLife: El Juego de la Vida

[xtrasgu.org]Siguiendo con las aplicaciones programadas para Android, esta vez tengo el gusto de presentar una versión simplificada de El Juego de la Vida. La simplificación viene dada por las reglas que se aplican en la frontera del tablero, que no son las correctas.

A continuación, una captura de pantalla:

AndroidGameOfLife: El Juego de la Vida

El programa lo puedes descargar en AndroidGameOfLife, y el código fuente lo tienes aquí debajo:

/*
 * AndroidGameOfLife - Game of Life for Android. 
 * 
 * Copyright (c) 2011, Pablo López Cienfuegos <xtrasgu@gmail.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 */

package org.xtrasgu.gameoflife;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class AndroidGameOfLife extends Activity {
	ViewGameOfLife viewgameoflife;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Set full screen view:
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// Set screen always on:
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		// Set full brightness:
		WindowManager.LayoutParams lp = getWindow().getAttributes();
		lp.screenBrightness = 1.0f;
		getWindow().setAttributes(lp);

		// Set portrait orientation:
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		viewgameoflife = new ViewGameOfLife(this);
		setContentView(viewgameoflife);
	}

	private class ViewGameOfLife extends View {
		int HDIM = 160;
		int VDIM = 266;
		int SUM = 0;
		int MATRIXA[][] = new int[HDIM][VDIM];
		int MATRIXB[][] = new int[HDIM][VDIM];
		Paint paint = new Paint();

		public ViewGameOfLife(Context context) {
			super(context);
			generateGameOfLifeSeed();
		}

		public void generateGameOfLifeSeed() {
			for (int i = 0; i < HDIM; i++)
				for (int j = 0; j < VDIM; j++)
					MATRIXA[i][j] = (int) (Math.floor(Math.random() * 2.0));
			invalidate();
		}

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			generateGameOfLifeSeed();
			return true;
		}

		@Override
		protected void onDraw(Canvas canvas) {

			// Set background:
			paint.setColor(Color.BLACK);
			paint.setStyle(Paint.Style.FILL);
			canvas.drawPaint(paint);

			// Game of Life:
			for (int i = 1; i < HDIM - 1; i++)
				for (int j = 1; j < VDIM - 1; j++) {
					SUM = MATRIXA[i - 1][j - 1] + MATRIXA[i - 1][j]
							+ MATRIXA[i - 1][j + 1] + MATRIXA[i][j - 1]
							+ MATRIXA[i][j + 1] + MATRIXA[i + 1][j - 1]
							+ MATRIXA[i + 1][j] + MATRIXA[i + 1][j + 1];
					if (SUM == 2)
						MATRIXB[i][j] = MATRIXA[i][j];
					else if (SUM == 3)
						MATRIXB[i][j] = 1;
					else
						MATRIXB[i][j] = 0;
				}

			for (int i = 0; i < HDIM; i++)
				for (int j = 0; j < VDIM; j++)
					MATRIXA[i][j] = MATRIXB[i][j];

			paint.setColor(Color.BLUE);
			for (int i = 0; i < HDIM; i += 2) {
				canvas.drawCircle(i * 2 + 1, 1, 1, paint);
				canvas.drawCircle(i * 2 - 1, VDIM * 2 - 1, 1, paint);
			}
			for (int j = 0; j < VDIM; j += 2) {
				canvas.drawCircle(1, j * 2 + 1, 1, paint);
				canvas.drawCircle(HDIM * 2 - 1, j * 2 + 3, 1, paint);
			}

			paint.setColor(Color.YELLOW);
			for (int i = 1; i < HDIM; i += 2) {
				canvas.drawCircle(i * 2 + 1, 1, 1, paint);
				canvas.drawCircle(i * 2 - 1, VDIM * 2 - 1, 1, paint);
			}
			for (int j = 1; j < VDIM; j += 2) {
				canvas.drawCircle(1, j * 2 + 1, 1, paint);
				canvas.drawCircle(HDIM * 2 - 1, j * 2 - 1, 1, paint);
			}

			paint.setColor(Color.GREEN);
			for (int i = 0; i < HDIM; i++)
				for (int j = 0; j < VDIM; j++)
					if (MATRIXA[i][j] == 1)
						canvas.drawCircle(i * 2 + 1, j * 2 + 1, 1, paint);
			invalidate();
		}
	}
}

Deja un Comentario