<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://www.lensovet.net/~sysadmin/w/index.php?action=history&amp;feed=atom&amp;title=Computer_Science%2F61b%2FProjects%2FOcean%2FRunLengthEncoding.java</id>
	<title>Computer Science/61b/Projects/Ocean/RunLengthEncoding.java - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://www.lensovet.net/~sysadmin/w/index.php?action=history&amp;feed=atom&amp;title=Computer_Science%2F61b%2FProjects%2FOcean%2FRunLengthEncoding.java"/>
	<link rel="alternate" type="text/html" href="http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Projects/Ocean/RunLengthEncoding.java&amp;action=history"/>
	<updated>2026-05-02T21:33:07Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.16</generator>
	<entry>
		<id>http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Projects/Ocean/RunLengthEncoding.java&amp;diff=24425&amp;oldid=prev</id>
		<title>Lensovet: Lensovet moved page CS/61b/Projects/Ocean/RunLengthEncoding.java to Computer Science/61b/Projects/Ocean/RunLengthEncoding.java</title>
		<link rel="alternate" type="text/html" href="http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Projects/Ocean/RunLengthEncoding.java&amp;diff=24425&amp;oldid=prev"/>
		<updated>2023-02-20T03:51:36Z</updated>

		<summary type="html">&lt;p&gt;Lensovet moved page &lt;a href=&quot;/~sysadmin/w/CS/61b/Projects/Ocean/RunLengthEncoding.java&quot; class=&quot;mw-redirect&quot; title=&quot;CS/61b/Projects/Ocean/RunLengthEncoding.java&quot;&gt;CS/61b/Projects/Ocean/RunLengthEncoding.java&lt;/a&gt; to &lt;a href=&quot;/~sysadmin/w/Computer_Science/61b/Projects/Ocean/RunLengthEncoding.java&quot; title=&quot;Computer Science/61b/Projects/Ocean/RunLengthEncoding.java&quot;&gt;Computer Science/61b/Projects/Ocean/RunLengthEncoding.java&lt;/a&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 03:51, 20 February 2023&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Lensovet</name></author>
		
	</entry>
	<entry>
		<id>http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Projects/Ocean/RunLengthEncoding.java&amp;diff=3963&amp;oldid=prev</id>
		<title>Lensovet: moved CS 61b/Projects/Ocean/RunLengthEncoding.java to CS/61b/Projects/Ocean/RunLengthEncoding.java:&amp;#32;fix CS 61b hierarchy</title>
		<link rel="alternate" type="text/html" href="http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Projects/Ocean/RunLengthEncoding.java&amp;diff=3963&amp;oldid=prev"/>
		<updated>2010-11-14T05:59:46Z</updated>

		<summary type="html">&lt;p&gt;moved &lt;a href=&quot;/~sysadmin/w/CS_61b/Projects/Ocean/RunLengthEncoding.java&quot; class=&quot;mw-redirect&quot; title=&quot;CS 61b/Projects/Ocean/RunLengthEncoding.java&quot;&gt;CS 61b/Projects/Ocean/RunLengthEncoding.java&lt;/a&gt; to &lt;a href=&quot;/~sysadmin/w/CS/61b/Projects/Ocean/RunLengthEncoding.java&quot; class=&quot;mw-redirect&quot; title=&quot;CS/61b/Projects/Ocean/RunLengthEncoding.java&quot;&gt;CS/61b/Projects/Ocean/RunLengthEncoding.java&lt;/a&gt;: fix CS 61b hierarchy&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 05:59, 14 November 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Lensovet</name></author>
		
	</entry>
	<entry>
		<id>http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Projects/Ocean/RunLengthEncoding.java&amp;diff=2242&amp;oldid=prev</id>
		<title>Lensovet at 03:19, 14 November 2006</title>
		<link rel="alternate" type="text/html" href="http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Projects/Ocean/RunLengthEncoding.java&amp;diff=2242&amp;oldid=prev"/>
		<updated>2006-11-14T03:19:10Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{code}}&lt;br /&gt;
 /* RunLengthEncoding.java */&lt;br /&gt;
 &lt;br /&gt;
 /**&lt;br /&gt;
  *  The RunLengthEncoding class defines an object that run-length encodes an&lt;br /&gt;
  *  Ocean object.  Descriptions of the methods you must implement appear below.&lt;br /&gt;
  *  They include constructors of the form&lt;br /&gt;
  *&lt;br /&gt;
  *      public RunLengthEncoding(int i, int j, int starveTime);&lt;br /&gt;
  *      public RunLengthEncoding(int i, int j, int starveTime,&lt;br /&gt;
  *                               int[] runTypes, int[] runLengths) {&lt;br /&gt;
  *      public RunLengthEncoding(Ocean ocean) {&lt;br /&gt;
  *&lt;br /&gt;
  *  that create a run-length encoding of an Ocean having width i and height j,&lt;br /&gt;
  *  in which sharks starve after starveTime timesteps.&lt;br /&gt;
  *&lt;br /&gt;
  *  The first constructor creates a run-length encoding of an Ocean in which&lt;br /&gt;
  *  every cell is empty.  The second constructor creates a run-length encoding&lt;br /&gt;
  *  for which the runs are provided as parameters.  The third constructor&lt;br /&gt;
  *  converts an Ocean object into a run-length encoding of that object.&lt;br /&gt;
  *&lt;br /&gt;
  *  See the README file accompanying this project for additional details.&lt;br /&gt;
  **/&lt;br /&gt;
 &lt;br /&gt;
 public class RunLengthEncoding {&lt;br /&gt;
 	&lt;br /&gt;
 	/**&lt;br /&gt;
 	 *  Define any variables associated with a RunLengthEncoding object here.&lt;br /&gt;
 	 *  These variables MUST be private.&lt;br /&gt;
 	 **/&lt;br /&gt;
 	&lt;br /&gt;
 	// since this uses a separate doubly-linked list class, most fields are already defined there&lt;br /&gt;
 	private int runsleft;&lt;br /&gt;
 	private DList rle;&lt;br /&gt;
 	&lt;br /&gt;
 	/**&lt;br /&gt;
 	 *  The following methods are required for Part II.&lt;br /&gt;
 	 **/&lt;br /&gt;
 	&lt;br /&gt;
 	/**&lt;br /&gt;
 	 *  RunLengthEncoding() (with three parameters) is a constructor that creates&lt;br /&gt;
 	 *  a run-length encoding of an empty ocean having width i and height j,&lt;br /&gt;
 	 *  in which sharks starve after starveTime timesteps.&lt;br /&gt;
 	 *  @param i is the width of the ocean.&lt;br /&gt;
 	 *  @param j is the height of the ocean.&lt;br /&gt;
 	 *  @param starveTime is the number of timesteps sharks survive without food.&lt;br /&gt;
 	 **/&lt;br /&gt;
 	&lt;br /&gt;
 	public RunLengthEncoding(int i, int j, int starveTime) {&lt;br /&gt;
 		rle = new DList(i, j, starveTime);&lt;br /&gt;
 		rle.insertFront(Ocean.EMPTY, i*j);&lt;br /&gt;
 		runsleft = 1;&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	/**&lt;br /&gt;
 	 *  RunLengthEncoding() (with five parameters) is a constructor that creates&lt;br /&gt;
 	 *  a run-length encoding of an ocean having width i and height j, in which&lt;br /&gt;
 	 *  sharks starve after starveTime timesteps.  The runs of the run-length&lt;br /&gt;
 	 *  encoding are taken from two input arrays.  Run i has length runLengths[i]&lt;br /&gt;
 	 *  and species runTypes[i].&lt;br /&gt;
 	 *  @param i is the width of the ocean.&lt;br /&gt;
 	 *  @param j is the height of the ocean.&lt;br /&gt;
 	 *  @param starveTime is the number of timesteps sharks survive without food.&lt;br /&gt;
 	 *  @param runTypes is an array that represents the species represented by&lt;br /&gt;
 	 *         each run.  Each element of runTypes is Ocean.EMPTY, Ocean.FISH,&lt;br /&gt;
 	 *         or Ocean.SHARK.  Any run of sharks is treated as a run of newborn&lt;br /&gt;
 	 *         sharks (which are equivalent to sharks that have just eaten).&lt;br /&gt;
 	 *  @param runLengths is an array that represents the length of each run.&lt;br /&gt;
 	 *         The sum of all elements of the runLengths array should be i * j.&lt;br /&gt;
 	 */&lt;br /&gt;
 	&lt;br /&gt;
 	public RunLengthEncoding(int i, int j, int starveTime,&lt;br /&gt;
 							 int[] runTypes, int[] runLengths) {&lt;br /&gt;
 		rle = new DList(i, j, starveTime);&lt;br /&gt;
 		if (runTypes.length != runLengths.length) { System.out.println(&amp;quot;INCOMPATIBLE ARRAYS&amp;quot;);&lt;br /&gt;
 		} else {&lt;br /&gt;
 			for (int ii = runTypes.length-1; ii&amp;gt;=0; ii--) {&lt;br /&gt;
 				if (runTypes[ii] == Ocean.SHARK) {&lt;br /&gt;
 					rle.insertFront(runTypes[ii], runLengths[ii], starveTime);&lt;br /&gt;
 				} else {&lt;br /&gt;
 					rle.insertFront(runTypes[ii], runLengths[ii]);&lt;br /&gt;
 				}&lt;br /&gt;
 			}&lt;br /&gt;
 			runsleft = runTypes.length;&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	/**&lt;br /&gt;
 	 *  restartRuns() and nextRun() are two methods that work together to return&lt;br /&gt;
 	 *  all the runs in the run-length encoding, one by one.  Each time&lt;br /&gt;
 	 *  nextRun() is invoked, it returns a different run (represented as a&lt;br /&gt;
 	 *  TypeAndSize object), until every run has been returned.  The first time&lt;br /&gt;
 	 *  nextRun() is invoked, it returns the first run in the encoding, which&lt;br /&gt;
 	 *  contains cell (0, 0).  After every run has been returned, nextRun()&lt;br /&gt;
 	 *  returns null, which lets the calling program know that there are no more&lt;br /&gt;
 	 *  runs in the encoding.&lt;br /&gt;
 	 *&lt;br /&gt;
 	 *  The restartRuns() method resets the enumeration, so that nextRun() will&lt;br /&gt;
 	 *  once again enumerate all the runs as if nextRun() were being invoked for&lt;br /&gt;
 	 *  the first time.&lt;br /&gt;
 	 *&lt;br /&gt;
 	 *  (Note:  Don't worry about what might happen if nextRun() is interleaved&lt;br /&gt;
 	 *  with addFish() or addShark(); it won't happen.)&lt;br /&gt;
 	 */&lt;br /&gt;
 	&lt;br /&gt;
 	/**&lt;br /&gt;
 	 *  restartRuns() resets the enumeration as described above, so that&lt;br /&gt;
 	 *  nextRun() will enumerate all the runs from the beginning.&lt;br /&gt;
 	 */&lt;br /&gt;
 	&lt;br /&gt;
 	public void restartRuns() {&lt;br /&gt;
 		runsleft = rle.length();&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	/**&lt;br /&gt;
 	 *  nextRun() returns the next run in the enumeration, as described above.&lt;br /&gt;
 	 *  If the runs have been exhausted, it returns null.  The return value is&lt;br /&gt;
 	 *  a TypeAndSize object, which is nothing more than a way to return two&lt;br /&gt;
 	 *  integers at once.&lt;br /&gt;
 	 *  @return the next run in the enumeration, represented by a TypeAndSize object.&lt;br /&gt;
 	 */&lt;br /&gt;
 	&lt;br /&gt;
 	public TypeAndSize nextRun() {&lt;br /&gt;
 		if (runsleft == 0) { return null; }&lt;br /&gt;
 		else {&lt;br /&gt;
 			int curindex = rle.length()-runsleft+1;&lt;br /&gt;
 			DListNode curnode = rle.nth(curindex);&lt;br /&gt;
 			int tip = curnode.type;&lt;br /&gt;
 			int freq = curnode.consecs;&lt;br /&gt;
 			runsleft--;&lt;br /&gt;
 			return new TypeAndSize(tip, freq); }&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	/**&lt;br /&gt;
 	 *  toOcean() converts a run-length encoding of an ocean into an Ocean&lt;br /&gt;
 	 *  object.  You will need to implement the three-parameter addShark method&lt;br /&gt;
 	 *  in the Ocean class for this method's use.&lt;br /&gt;
 	 *  @return the Ocean represented by a run-length encoding.&lt;br /&gt;
 	 */&lt;br /&gt;
 	&lt;br /&gt;
 	public Ocean toOcean() {&lt;br /&gt;
 		int index = 0;&lt;br /&gt;
 		Ocean newocean = new Ocean(rle.dimx, rle.dimy, rle.starvetime);&lt;br /&gt;
 		for (int il = 1; il&amp;lt;=rle.length(); il++) { // walks through each node in the rle list&lt;br /&gt;
 			DListNode curnode = rle.nth(il);&lt;br /&gt;
 			int tip = curnode.type;&lt;br /&gt;
 			int freq = curnode.consecs;&lt;br /&gt;
 			int hunger = curnode.hunger;&lt;br /&gt;
 			int ia = index;&lt;br /&gt;
 			for (; ia &amp;lt; index+freq; ia++) {&lt;br /&gt;
 				if (tip == Ocean.SHARK) {&lt;br /&gt;
 					newocean.addShark(newocean.coordx(ia), newocean.coordy(ia), hunger);&lt;br /&gt;
 				} else if (tip == Ocean.FISH) {&lt;br /&gt;
 					newocean.addFish(newocean.coordx(ia), newocean.coordy(ia));&lt;br /&gt;
 				} else { } // do nothing, the cell is already EMPTY&lt;br /&gt;
 			}&lt;br /&gt;
 			index = ia;&lt;br /&gt;
 		}&lt;br /&gt;
 		return newocean;&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	/**&lt;br /&gt;
 	 *  The following method is required for Part III.&lt;br /&gt;
 	 */&lt;br /&gt;
 	&lt;br /&gt;
 	/**&lt;br /&gt;
 	 *  RunLengthEncoding() (with one parameter) is a constructor that creates&lt;br /&gt;
 	 *  a run-length encoding of an input Ocean.  You will need to implement&lt;br /&gt;
 	 *  the sharkFeeding method in the Ocean class for this constructor's use.&lt;br /&gt;
 	 *  @param sea is the ocean to encode.&lt;br /&gt;
 	 */&lt;br /&gt;
 	&lt;br /&gt;
 	public RunLengthEncoding(Ocean sea) {&lt;br /&gt;
 		int is=0;&lt;br /&gt;
 		int ir=1;&lt;br /&gt;
 		int seasize = sea.width()*sea.height();&lt;br /&gt;
 		rle = new DList(sea.width(), sea.height(), sea.starveTime());&lt;br /&gt;
 		while (is&amp;lt;seasize) {&lt;br /&gt;
 			int tip = sea.cellContents(sea.coordx(is), sea.coordy(is));&lt;br /&gt;
 			int hunger = sea.sharkFeeding(is);&lt;br /&gt;
 			rle.insertEnd(tip, 1, hunger);&lt;br /&gt;
 			is++;&lt;br /&gt;
 			for (; sea.cellContents(sea.coordx(is), sea.coordy(is)) == tip &amp;amp;&amp;amp; sea.sharkFeeding(is) == hunger &amp;amp;&amp;amp; is&amp;lt;seasize; is++) {&lt;br /&gt;
 				rle.nth(ir).type = tip;&lt;br /&gt;
 				rle.nth(ir).consecs++;&lt;br /&gt;
 				rle.nth(ir).hunger = hunger;&lt;br /&gt;
 			}&lt;br /&gt;
 			ir++;&lt;br /&gt;
 		}&lt;br /&gt;
 		runsleft = ir-1;&lt;br /&gt;
 		check();&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	/**&lt;br /&gt;
 	 *  The following methods are required for Part IV.&lt;br /&gt;
 	 */&lt;br /&gt;
 	&lt;br /&gt;
 	/**&lt;br /&gt;
 	 *  addFish() places a fish in cell (x, y) if the cell is empty.  If the&lt;br /&gt;
 	 *  cell is already occupied, leave the cell as it is.  The final run-length&lt;br /&gt;
 	 *  encoding should be compressed as much as possible; there should not be&lt;br /&gt;
 	 *  two consecutive runs of sharks with the same degree of hunger.&lt;br /&gt;
 	 *  @param x is the x-coordinate of the cell to place a fish in.&lt;br /&gt;
 	 *  @param y is the y-coordinate of the cell to place a fish in.&lt;br /&gt;
 	 */&lt;br /&gt;
 	&lt;br /&gt;
 	// This helper helps us convert (x,y) into a single-digit position but is slightly modified from the version in Ocean.java to work with RLEs&lt;br /&gt;
 	public int itempos(int x, int y) {&lt;br /&gt;
 		x = x%rle.dimx;&lt;br /&gt;
 		y = y%rle.dimy;&lt;br /&gt;
 		if (x &amp;lt; 0) { x = x+rle.dimx; }&lt;br /&gt;
 		if (y &amp;lt; 0) { y = y+rle.dimy; }&lt;br /&gt;
 		int ind = (rle.dimx*y+x) + 1;&lt;br /&gt;
 		return ind;&lt;br /&gt;
 	}&lt;br /&gt;
 &lt;br /&gt;
 	public void addFish(int x, int y) {&lt;br /&gt;
 		int index = itempos(x, y);&lt;br /&gt;
 		int rlepos = 1;&lt;br /&gt;
 		int i = 1;&lt;br /&gt;
 		for (; i&amp;lt;rle.length(); i++) {&lt;br /&gt;
 			rlepos = rlepos + rle.nth(i).consecs;&lt;br /&gt;
 			if (rlepos &amp;gt; index) { &lt;br /&gt;
 				rlepos = rlepos - rle.nth(i).consecs - 1;&lt;br /&gt;
 				break; }&lt;br /&gt;
 		}&lt;br /&gt;
 		if (i&amp;gt;=rle.length()) { System.out.println(&amp;quot;Something terribly wrong happened, sorry...&amp;quot;); }&lt;br /&gt;
 		else if (rle.nth(i).type != Ocean.EMPTY) { System.out.println(&amp;quot;Destination cell is not empty&amp;quot;); }&lt;br /&gt;
 		else actualinsertion: { &lt;br /&gt;
 			DListNode removednode = rle.nth(i);&lt;br /&gt;
 			int after = removednode.consecs - (index - rlepos);&lt;br /&gt;
 			int before = removednode.consecs - after - 1;&lt;br /&gt;
 			DListNode insertionnode = new DListNode(Ocean.FISH);&lt;br /&gt;
 			insertionnode.consecs = 1;&lt;br /&gt;
 			DListNode beforenode;&lt;br /&gt;
 			DListNode afternode;&lt;br /&gt;
 			boolean nodeisgone = false;&lt;br /&gt;
 			if (after==0 &amp;amp;&amp;amp; removednode.next.type==Ocean.FISH &amp;amp;&amp;amp; removednode.prev.type!=Ocean.FISH) { // there are no empty spaces, following block is a fish, and previous block is NOT a fish&lt;br /&gt;
 				removednode.next.consecs++;&lt;br /&gt;
 				nodeisgone = true;&lt;br /&gt;
 			}  if (before==0 &amp;amp;&amp;amp; removednode.prev.type==Ocean.FISH &amp;amp;&amp;amp; removednode.next.type!=Ocean.FISH) { // there are no empty spaces, previous block is fish, and following block is NOT fish&lt;br /&gt;
 				removednode.prev.consecs++;&lt;br /&gt;
 				nodeisgone = true;&lt;br /&gt;
 			} if (after==0 &amp;amp;&amp;amp; before==0 &amp;amp;&amp;amp; removednode.prev.type!=Ocean.FISH &amp;amp;&amp;amp; removednode.next.type!=Ocean.FISH) { // this cell is nestled between two blocks neither of which is a fish&lt;br /&gt;
 				insertionnode.next = removednode.next;&lt;br /&gt;
 				insertionnode.next.prev = insertionnode;&lt;br /&gt;
 				insertionnode.prev = removednode.prev;&lt;br /&gt;
 				insertionnode.prev.next = insertionnode;&lt;br /&gt;
 				// we're done here&lt;br /&gt;
 				break actualinsertion;&lt;br /&gt;
 			} if (removednode.next.type==Ocean.FISH &amp;amp;&amp;amp; removednode.prev.type==Ocean.FISH) { // both previous and next blocks contain fish, so we need to combine three blocks into one&lt;br /&gt;
 				int combination = removednode.next.consecs + removednode.prev.consecs + 1;&lt;br /&gt;
 				DListNode overhaul = new DListNode(Ocean.FISH);&lt;br /&gt;
 				overhaul.consecs = combination;&lt;br /&gt;
 				overhaul.next = removednode.next.next;&lt;br /&gt;
 				overhaul.next.prev = overhaul;&lt;br /&gt;
 				overhaul.prev = removednode.prev.prev;&lt;br /&gt;
 				overhaul.prev.next = overhaul;&lt;br /&gt;
 				rle.size = rle.size -2;&lt;br /&gt;
 				runsleft = rle.length();&lt;br /&gt;
 				// we're done here&lt;br /&gt;
 				break actualinsertion;&lt;br /&gt;
 			} if (nodeisgone) { // if as a result of the previous testcases, our initially created node is no longer needed (nodeisgone == true), we need to combine the before and after frequencies, make a single new node, and link it to the list appropriately&lt;br /&gt;
 				int combined = after + before;&lt;br /&gt;
 				if (combined &amp;gt; 0) {&lt;br /&gt;
 					DListNode superduper = new DListNode(Ocean.EMPTY);&lt;br /&gt;
 					superduper.consecs = combined;&lt;br /&gt;
 					superduper.next = removednode.next;&lt;br /&gt;
 					superduper.prev = removednode.prev;&lt;br /&gt;
 					superduper.prev.next = superduper;&lt;br /&gt;
 					superduper.next.prev = superduper;&lt;br /&gt;
 				} else {&lt;br /&gt;
 					removednode.prev.next = removednode.next;&lt;br /&gt;
 					removednode.next.prev = removednode.prev;&lt;br /&gt;
 					rle.size--;&lt;br /&gt;
 				}&lt;br /&gt;
 			}  else if (before&amp;gt;0 || after&amp;gt;0) {&lt;br /&gt;
 				if (before &amp;gt; 0) { // there are still some empty spaces before this fish&lt;br /&gt;
 					beforenode = new DListNode(Ocean.EMPTY);&lt;br /&gt;
 					beforenode.consecs = before;&lt;br /&gt;
 					beforenode.prev = removednode.prev;&lt;br /&gt;
 					removednode.prev.next = beforenode;&lt;br /&gt;
 					beforenode.next = insertionnode;&lt;br /&gt;
 					insertionnode.prev = beforenode;&lt;br /&gt;
 					if (after == 0) {&lt;br /&gt;
 						insertionnode.next = removednode.next;&lt;br /&gt;
 						insertionnode.next.prev = insertionnode; }&lt;br /&gt;
 					rle.size++;&lt;br /&gt;
 					runsleft = rle.length();&lt;br /&gt;
 				} if (after &amp;gt; 0) { // there are still some empty spaces after this fish&lt;br /&gt;
 					afternode = new DListNode(Ocean.EMPTY);&lt;br /&gt;
 					afternode.consecs = after;&lt;br /&gt;
 					afternode.next = removednode.next;&lt;br /&gt;
 					afternode.next.prev = afternode;&lt;br /&gt;
 					insertionnode.next = afternode;&lt;br /&gt;
 					if (before == 0) {&lt;br /&gt;
 						insertionnode.prev = removednode.prev;&lt;br /&gt;
 						insertionnode.prev.next = insertionnode; }&lt;br /&gt;
 					afternode.prev = insertionnode;&lt;br /&gt;
 					rle.size++;&lt;br /&gt;
 					runsleft = rle.length();&lt;br /&gt;
 				}&lt;br /&gt;
 			}&lt;br /&gt;
 		}&lt;br /&gt;
 		check();&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	/**&lt;br /&gt;
 	 *  addShark() (with two parameters) places a newborn shark in cell (x, y) if&lt;br /&gt;
 	 *  the cell is empty.  A &amp;quot;newborn&amp;quot; shark is equivalent to a shark that has&lt;br /&gt;
 	 *  just eaten.  If the cell is already occupied, leave the cell as it is.&lt;br /&gt;
 	 *  The final run-length encoding should be compressed as much as possible;&lt;br /&gt;
 	 *  there should not be two consecutive runs of sharks with the same degree&lt;br /&gt;
 	 *  of hunger.&lt;br /&gt;
 	 *  @param x is the x-coordinate of the cell to place a shark in.&lt;br /&gt;
 	 *  @param y is the y-coordinate of the cell to place a shark in.&lt;br /&gt;
 	 */&lt;br /&gt;
 	&lt;br /&gt;
 	public void addShark(int x, int y) {&lt;br /&gt;
 		int index = itempos(x, y);&lt;br /&gt;
 		int rlepos = 1;&lt;br /&gt;
 		int i = 1;&lt;br /&gt;
 		for (; i&amp;lt;rle.length(); i++) {&lt;br /&gt;
 			rlepos = rlepos + rle.nth(i).consecs;&lt;br /&gt;
 			if (rlepos &amp;gt; index) { &lt;br /&gt;
 				rlepos = rlepos - rle.nth(i).consecs - 1;&lt;br /&gt;
 				break; }&lt;br /&gt;
 		}&lt;br /&gt;
 		if (i&amp;gt;=rle.length()) { System.out.println(&amp;quot;Something terribly wrong happened, sorry...&amp;quot;); }&lt;br /&gt;
 		else if (rle.nth(i).type != Ocean.EMPTY) { System.out.println(&amp;quot;Destination cell is not empty&amp;quot;); }&lt;br /&gt;
 		else actualinsertion: { &lt;br /&gt;
 			DListNode removednode = rle.nth(i);&lt;br /&gt;
 			int destinationhunger = rle.starvetime;&lt;br /&gt;
 			int after = removednode.consecs - (index - rlepos);&lt;br /&gt;
 			int before = removednode.consecs - after - 1;&lt;br /&gt;
 			DListNode insertionnode = new DListNode(Ocean.SHARK, destinationhunger);&lt;br /&gt;
 			insertionnode.consecs = 1;&lt;br /&gt;
 			DListNode beforenode;&lt;br /&gt;
 			DListNode afternode;&lt;br /&gt;
 			boolean nodeisgone = false;&lt;br /&gt;
 			if (after==0 &amp;amp;&amp;amp; removednode.next.type==Ocean.SHARK &amp;amp;&amp;amp; removednode.next.hunger==destinationhunger &amp;amp;&amp;amp; removednode.prev.hunger!=destinationhunger) { // there are no empty spaces, following block is a shark with the same hunger, and previous block is NOT a shark with the same hunger&lt;br /&gt;
 				removednode.next.consecs++;&lt;br /&gt;
 				nodeisgone = true;&lt;br /&gt;
 			}  if (before==0 &amp;amp;&amp;amp; removednode.prev.type==Ocean.SHARK &amp;amp;&amp;amp; removednode.prev.hunger==destinationhunger &amp;amp;&amp;amp; removednode.next.hunger!=destinationhunger) { // there are no empty spaces, previous block is a shark with the same hunger, and following block is NOT a shark with the same hunger&lt;br /&gt;
 				removednode.prev.consecs++;&lt;br /&gt;
 				nodeisgone = true;&lt;br /&gt;
 			} if (after==0 &amp;amp;&amp;amp; before==0 &amp;amp;&amp;amp; removednode.prev.hunger!=destinationhunger &amp;amp;&amp;amp; removednode.next.hunger!=destinationhunger) { // this cell is nestled between two blocks that differ from the shark we're inserting.&lt;br /&gt;
 				insertionnode.next = removednode.next;&lt;br /&gt;
 				insertionnode.next.prev = insertionnode;&lt;br /&gt;
 				insertionnode.prev = removednode.prev;&lt;br /&gt;
 				insertionnode.prev.next = insertionnode;&lt;br /&gt;
 				// we're done here&lt;br /&gt;
 				break actualinsertion;&lt;br /&gt;
 			} if (after==0 &amp;amp;&amp;amp; before==0 &amp;amp;&amp;amp; removednode.next.type==Ocean.SHARK &amp;amp;&amp;amp; removednode.prev.type==Ocean.SHARK &amp;amp;&amp;amp; removednode.next.hunger==destinationhunger &amp;amp;&amp;amp; removednode.prev.hunger==destinationhunger) { // we're inserting into a cell surrounded by blocks of sharks both of which have the same hunger levels, so we need to combine three blocks into one&lt;br /&gt;
 				int combination = removednode.next.consecs + removednode.prev.consecs + 1;&lt;br /&gt;
 				DListNode overhaul = new DListNode(Ocean.SHARK, destinationhunger);&lt;br /&gt;
 				overhaul.consecs = combination;&lt;br /&gt;
 				overhaul.next = removednode.next.next;&lt;br /&gt;
 				overhaul.next.prev = overhaul;&lt;br /&gt;
 				overhaul.prev = removednode.prev.prev;&lt;br /&gt;
 				overhaul.prev.next = overhaul;&lt;br /&gt;
 				rle.size = rle.size -2;&lt;br /&gt;
 				runsleft = rle.length();&lt;br /&gt;
 				// we're done here&lt;br /&gt;
 				break actualinsertion;&lt;br /&gt;
 			} if (nodeisgone) { // if as a result of the previous testcases, our initially created node is no longer needed (nodeisgone == true), we need to combine the before and after frequencies, make a single new node, and link it to the list appropriately&lt;br /&gt;
 				int combined = after + before;&lt;br /&gt;
 				if (combined &amp;gt; 0) {&lt;br /&gt;
 					DListNode superduper = new DListNode(Ocean.EMPTY);&lt;br /&gt;
 					superduper.consecs = combined;&lt;br /&gt;
 					superduper.next = removednode.next;&lt;br /&gt;
 					superduper.prev = removednode.prev;&lt;br /&gt;
 					superduper.prev.next = superduper;&lt;br /&gt;
 					superduper.next.prev = superduper;&lt;br /&gt;
 				} else {&lt;br /&gt;
 					removednode.prev.next = removednode.next;&lt;br /&gt;
 					removednode.next.prev = removednode.prev;&lt;br /&gt;
 					rle.size--;&lt;br /&gt;
 				}&lt;br /&gt;
 			} else if (before&amp;gt;0 || after&amp;gt;0) {&lt;br /&gt;
 				if (before &amp;gt; 0) { // there are still some empty spaces before this shark or there are sharks but with different hunger levels&lt;br /&gt;
 					beforenode = new DListNode(Ocean.EMPTY);&lt;br /&gt;
 					beforenode.consecs = before;&lt;br /&gt;
 					beforenode.prev = removednode.prev;&lt;br /&gt;
 					removednode.prev.next = beforenode;&lt;br /&gt;
 					beforenode.next = insertionnode;&lt;br /&gt;
 					insertionnode.prev = beforenode;&lt;br /&gt;
 					if (after == 0) {&lt;br /&gt;
 						insertionnode.next = removednode.next;&lt;br /&gt;
 						insertionnode.next.prev = insertionnode; }&lt;br /&gt;
 					rle.size++;&lt;br /&gt;
 					runsleft = rle.length();&lt;br /&gt;
 				} if (after &amp;gt; 0) { // there are still some empty spaces after this shark or there are sharks but with different hunger levels&lt;br /&gt;
 					afternode = new DListNode(Ocean.EMPTY);&lt;br /&gt;
 					afternode.consecs = after;&lt;br /&gt;
 					afternode.next = removednode.next;&lt;br /&gt;
 					afternode.next.prev = afternode;&lt;br /&gt;
 					insertionnode.next = afternode;&lt;br /&gt;
 					if (before == 0) {&lt;br /&gt;
 						insertionnode.prev = removednode.prev;&lt;br /&gt;
 						insertionnode.prev.next = insertionnode; }&lt;br /&gt;
 					afternode.prev = insertionnode;&lt;br /&gt;
 					rle.size++;&lt;br /&gt;
 					runsleft = rle.length();&lt;br /&gt;
 				}&lt;br /&gt;
 			} &lt;br /&gt;
 		}&lt;br /&gt;
 		check();&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	/**&lt;br /&gt;
 	 *  check() walks through the run-length encoding and prints an error message&lt;br /&gt;
 	 *  if two consecutive runs have the same contents, or if the sum of all run&lt;br /&gt;
 	 *  lengths does not equal the number of cells in the ocean.&lt;br /&gt;
 	 */&lt;br /&gt;
 	&lt;br /&gt;
 	public void check() {&lt;br /&gt;
 		int ti = 0;&lt;br /&gt;
 		for (int i=1; i&amp;lt;rle.length(); i++) {&lt;br /&gt;
 			if (rle.nth(i).type == rle.nth(i+1).type &amp;amp;&amp;amp; rle.nth(i).hunger == rle.nth(i+1).hunger) {&lt;br /&gt;
 				System.out.print(&amp;quot;I've found a bug! You have the same type of cells in two consecutive RLE nodes! Problem is at nodes &amp;quot; + i + &amp;quot; and &amp;quot; + (i+1) + &amp;quot;\n&amp;quot;);&lt;br /&gt;
 			}&lt;br /&gt;
 		}&lt;br /&gt;
 		for (int ii=1; ii&amp;lt;=rle.length(); ii++) {&lt;br /&gt;
 			ti = ti + rle.nth(ii).consecs;&lt;br /&gt;
 		}	&lt;br /&gt;
 		if (ti != rle.dimx*rle.dimy) {&lt;br /&gt;
 			System.out.print(&amp;quot;I've found a bug! The sum of run lengths is NOT equal to the Ocean's size - runlengths is &amp;quot; + ti + &amp;quot; vs Ocean size of &amp;quot; + (rle.dimx*rle.dimy) + &amp;quot;\n&amp;quot;);&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Lensovet</name></author>
		
	</entry>
</feed>