errorsandexceptions

Code, etc.

Tag Archives: code

Conway’s Game of Life in Java

This morning I was looking at Wikipedia pages about cellular automata and I got an urge to implement Conway’s Game of Life as a Java exercise. This is what I came up with. It generates a random matrix for the starting position that then iteratively evolves in accordance with the Rules of the Game.

package Conway;

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.util.Timer;

public class GameOfLife extends Canvas {
	
	private static final long serialVersionUID = 1L;
	
	int[][] arena = new int[200][200];
	int[][] newArena = new int[200][200];
	
	public GameOfLife() {
		Random rand = new Random();
		for (int i=0; i<200; i++) {
			for (int j=0; j<200; j++) {
				arena[i][j] = rand.nextInt(2);
			}
		}
	}
	
	public void paint(Graphics graphics) {
		for (int i=0; i<200; i++) {
			for (int j=0; j<200; j++) {
				if (arena[i][j] == 1) {
					int vPos = i * 4;
					int hPos = j * 4;
					
					graphics.fillRect(hPos, vPos, 4, 4);
				}
			}
		}
	}
	
	public void LifeLoop() {
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(new NextIter(),2000, 500);
	}
	
	class NextIter extends TimerTask {
		public void run() {
			int square1;
			int square2;
			int square3;
			int square4;
			int square5;
			int square6;
			int square7;
			int square8;
			int neighbours;
			
			for (int i=0; i<200; i++) {
				for (int j=0; j<200; j++) {
					
					//Get the eight adjacent squares
					try {
						square1 = arena[i-1][j-1];
					}
					catch (ArrayIndexOutOfBoundsException e) {
						square1 = 0;
					}
					
					try {
						square2 = arena[i-1][j];
					}
					catch (ArrayIndexOutOfBoundsException e) {
						square2 = 0;
					}
					
					try {
						square3 = arena[i-1][j+1];
					}
					catch (ArrayIndexOutOfBoundsException e) {
						square3 = 0;
					}
					
					try {
						square4 = arena[i][j+1];
					}
					catch (ArrayIndexOutOfBoundsException e) {
						square4 = 0;
					}
					
					try {
						square5 = arena[i+1][j+1];
					}
					catch (ArrayIndexOutOfBoundsException e) {
						square5 = 0;
					}
					
					try {
						square6 = arena[i+1][j];
					}
					catch (ArrayIndexOutOfBoundsException e) {
						square6 = 0;
					}
					
					try {
						square7 = arena[i+1][j-1];
					}
					catch (ArrayIndexOutOfBoundsException e) {
						square7 = 0;
					}
					
					try {
						square8 = arena[i][j-1];
					}
					catch (ArrayIndexOutOfBoundsException e) {
						square8 = 0;
					}
					
					neighbours = square1 + square2 + square3 + 
						square4 + square5 + square6 + square7 + square8;
					
					if (arena[i][j] == 1 & (neighbours == 2 | neighbours == 3)) {
						newArena[i][j] = 1;
					}
					
					else if (arena[i][j] == 0 & neighbours == 3) {
						newArena[i][j] = 1;
					}
					
					else {
						newArena[i][j] = 0;
					}
				}
			}
			
			for (int i=0; i<200; i++) {
				for (int j=0; j<200; j++) {
					arena[i][j] = newArena[i][j];
				}
			}
			
			repaint();
		}
	}
	
	public static void main(String[] args) {
		GameOfLife canvas = new GameOfLife();
		JFrame frame = new JFrame();
		frame.setTitle("Conway's Bestiary");
		frame.setSize(816, 838);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(canvas);
		frame.setVisible(true);
		
		canvas.LifeLoop();
	}
}

Quicksort and Mergesort in Python

I wrote these small implementations of the quicksort and mergesort algorithms in Python. They take a list of numbers and return it sorted from lowest to highest. The WordPress code tags behave very strangely.

Here is Quicksort:

# Sorting a List using Quicksort in Python
# 2011-05-16

def quickSort(toSort):
	if len(toSort) <= 1:
		return toSort

	end = len(toSort) - 1
	pivot = toSort[end]

	low = []
	high = []

	for num in toSort[:end]:
		if num <= pivot:
			low.append(num)
		else:
			high.append(num)

	sortedList = quickSort(low)
	sortedList.append(pivot)
	sortedList.extend(quickSort(high))

	return sortedList

def main():
	l = [1, 6, 7, 2, 76, 45, 23, 4, 8, 12, 11]
	sortedList = quickSort(l)
	print sortedList

if __name__ == '__main__':
	main()

And Mergesort:

# Sorting a List using Mergesort in Python
# 2011-05-16

def mergeSort(toSort):
	if len(toSort) <= 1:
		return toSort

	mIndex = len(toSort) / 2
	left = mergeSort(toSort[:mIndex])
	right = mergeSort(toSort[mIndex:])

	result = []
	while len(left) > 0 and len(right) > 0:
		if left[0] > right[0]:	
			result.append(right.pop(0))
		else:
			result.append(left.pop(0))

	if len(left) > 0:
		result.extend(mergeSort(left))
	else:
		result.extend(mergeSort(right))

	return result

def main():
	l = [1, 6, 7, 2, 76, 45, 23, 4, 8, 12, 11]
	sortedList = mergeSort(l)
	print sortedList

if __name__ == '__main__':
	main()