Commit 60c758ff authored by Maya Persaud's avatar Maya Persaud
Browse files

work

parent 8aa9f8ed
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<Objective-C-extensions>
<file>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function" />
</file>
<class>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod" />
</class>
<extensions>
<pair source="cpp" header="h" fileNamingConvention="NONE" />
<pair source="c" header="h" fileNamingConvention="NONE" />
</extensions>
</Objective-C-extensions>
</code_scheme>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>
\ No newline at end of file
......@@ -5,11 +5,25 @@ public class ArrayDemos {
// TODO: Initialize each value in the array to the square of its index,
// so it will contain {0, 1, 4, 9, 16, 25, ...}
private static void initializeWithSquares(int[] array) {
for(int i = 0; i < array.length; i++) {
System.out.println("position" + i);
array[i] = i*i;
}
}
// TODO: Add up all the integers in the array, and return the sum.
private static int sumOfArray(int[] array) {
return 0; // placeholder, change this!
private static int sumOfArray(int[] array)
{
int sum = 0;
for (int i = 0; i < array.length; i++)
{
sum += array[i];
}
return sum;
}
private static String yesOrNo(boolean okay) {
......
......@@ -8,23 +8,99 @@ import java.util.Random;
public class FloodGame {
// TODO
static void randomize(FloodGrid grid, Random rng, int numColors) {
static void randomize(FloodGrid grid, Random rng, int numColors)
{
for(int i = 0; i < grid.numCells(); i ++)
{
grid.setColorAt(i, rng.nextInt(numColors));
}
}
// TODO
static String toString(FloodGrid grid) {
return "(grid)\n"; // placeholder
StringBuilder buf = new StringBuilder();
for(int r =0; r <grid.edgeSize(); r++)
{
for(int c =0; c <grid.edgeSize(); c++)
{
buf.append(grid.getColorAt(r,c));
}
buf.append('\n');
}
return buf.toString(); // placeholder
}
// TODO
static boolean gameOver(FloodGrid grid) {
return false; // placeholder
int targetColor = grid.getColorAt(0);
for(int i = 1; i < grid.numCells(); i++)
{
if (grid.getColorAt(i) != targetColor){
return false;
}
}
return true;
}
// TODO
static void flood(FloodGrid grid, int newColor) {
static void flood(FloodGrid grid, int newColor) {
boolean[][] visited = new boolean[grid.edgeSize()][grid.edgeSize()];
int prevColor = grid.getColorAt(0,0);
flood(grid, visited, 0, 0, prevColor, newColor);
}
/* The first helper for the flood algorithm floods FROM a particular row and
* column, changing any contiguous cells of prevColor into newColor. It
* must consider all four directions, but only proceed if okayToVisit.
*/
private static void flood(
FloodGrid grid,
boolean[][] visited,
int row,
int column,
int prevColor,
int newColor)
{
visited[row][column] = true;
grid.setColorAt(row, column, newColor);
// Down
if(okayToVisit(grid, visited, row+1, column, prevColor)) {
flood(grid, visited, row+1, column, prevColor, newColor);
}
// Right
if(okayToVisit(grid, visited, row, column+1, prevColor)) {
flood(grid, visited, row, column+1, prevColor, newColor);
}
// Up
if(okayToVisit(grid, visited, row-1, column, prevColor)) {
flood(grid, visited, row-1, column, prevColor, newColor);
}
// Left
if(okayToVisit(grid, visited, row, column-1, prevColor)) {
flood(grid, visited, row, column-1, prevColor, newColor);
}
}
/* The last helper for the flood algorithm just determines if it's okay to
* visit a particular coordinate -- it's in-bounds, hasn't been visited yet,
* and matches the existing color.
*/
private static boolean okayToVisit(
FloodGrid grid,
boolean[][] visited,
int row,
int column,
int color)
{
return (row >= 0
&& row < grid.edgeSize()
&& column >= 0
&& column < grid.edgeSize()
&& !visited[row][column]
&& grid.getColorAt(row, column) == color);
}
public static void main(String[] args) throws IOException {
final int numColors = 4;
FloodGrid grid = new FloodGridArray2D(6);
......
package edu.liu.arrays;
public interface FloodGrid {
public interface FloodGrid {
int edgeSize();
int numCells(); // Always equals edgeSize() squared
int getColorAt(int row, int column);
......
......@@ -4,6 +4,7 @@ class FloodGridArray2D implements FloodGrid {
private int[][] grid;
public FloodGridArray2D(int edgeSize) {
if(edgeSize < 2) {
throw new IllegalArgumentException("edgeSize too small");
......@@ -21,19 +22,23 @@ class FloodGridArray2D implements FloodGrid {
// TODO
public int getColorAt(int row, int column) {
return 0; // placeholder
return grid[row][column]; // placeholder
}
// TODO
public int getColorAt(int index) {
return 0; // placeholder
return getColorAt(index/edgeSize(),index%edgeSize());
}
// TODO
public void setColorAt(int row, int column, int color) {
grid[row][column] = color;
}
// TODO
public void setColorAt(int index, int color) {
setColorAt(index/edgeSize(),index%edgeSize(), color);
}
}
......@@ -2,6 +2,8 @@ package edu.liu.arrays;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
......@@ -12,6 +14,8 @@ public class MainActivity extends AppCompatActivity {
"Bert",
"Ernie",
"Julia",
"Jeff",
"Cookie",
"Zoe"
};
......@@ -21,5 +25,15 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main);
// TODO: Fill TextViews with Strings from names array
LinearLayout textContainer = findViewById(R.id.textContainer);
for(int i = 0; i < textContainer.getChildCount(); i++) {
if (i < names.length) {
TextView tv = (TextView) textContainer.getChildAt(i);
// tv.setText("DOING index " + i);
tv.setText(names[i]);
}
}
}
}
......@@ -49,5 +49,10 @@
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment