Skip to content

Commit

Permalink
guitar hero lite cleared
Browse files Browse the repository at this point in the history
  • Loading branch information
tttbw committed Apr 7, 2024
1 parent 8c9a6b2 commit 05f4374
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 18 deletions.
180 changes: 180 additions & 0 deletions proj1/deque/ArrayDeque.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package deque;
/** second part of project1A.
* deque implemented by array
* @author FlyingPig
*/
public class ArrayDeque<T> implements Deque<T> {

/** array to save data.*/
private T[] array;
/** size of the deque. */
private int size;

/** size of the array. */
private int length;

/** front index. */
private int front;

/** last index. */
private int last;

/** constructor for ArrayDeque. */
public ArrayDeque() {
array = (T[]) new Object[8];
size = 0;
length = 8;
front = 4;
last = 4;
}

/** decide if the deque is empty.
* @return true if the deque is empty, vice versa.
*/
@Override
public boolean isEmpty() {
return size == 0;
}

/** return the size of the deque. */
@Override
public int size() {
return size;
}

/** return the "index - 1".
* @param index index
*/
private int minusOne(int index) {
if (index == 0) {
return length - 1;
}
return index - 1;
}

/** return the "index + 1".
* @param index index
*/
private int plusOne(int index, int module) {
index %= module;
if (index == module - 1) {
return 0;
}
return index + 1;
}

private void grow() {
T[] newArray = (T[]) new Object[length * 2];
int ptr1 = front;
int ptr2 = length;
while (ptr1 != last) {
newArray[ptr2] = array[ptr1];
ptr1 = plusOne(ptr1, length);
ptr2 = plusOne(ptr2, length * 2);
}
front = length;
last = ptr2;
array = newArray;
length *= 2;
}

private void shrink() {
T[] newArray = (T[]) new Object[length / 2];
int ptr1 = front;
int ptr2 = length / 4;
while (ptr1 != last) {
newArray[ptr2] = array[ptr1];
ptr1 = plusOne(ptr1, length);
ptr2 = plusOne(ptr2, length / 2);
}
front = length / 4;
last = ptr2;
array = newArray;
length /= 2;
}

/** add one item at the front of the deque.
* @param item the item we want to add
*/
@Override
public void addFirst(T item) {
if (size == length - 1) {
grow();
}
front = minusOne(front);
array[front] = item;
size++;
}

/** add one item at the end of the deque.
* @param item item we want to add
*/
@Override
public void addLast(T item) {
if (size == length - 1) {
grow();
}
array[last] = item;
last = plusOne(last, length);
size++;
}

/** remove the first item.
* @return the removed first item
*/
@Override
public T removeFirst() {
if (length >= 16 && length / size >= 4) {
shrink();
}
if (size == 0) {
return null;
}
T ret = array[front];
front = plusOne(front, length);
size--;
return ret;
}

/** remove the last item.
* @return the removed last item
*/
@Override
public T removeLast() {
if (length >= 16 && length / size >= 4) {
shrink();
}
if (size == 0) {
return null;
}
last = minusOne(last);
size--;
return array[last];
}

/** return the item indexed at index.
* @param index index
*/
@Override
public T get(int index) {
if (index >= size) {
return null;
}
int ptr = front;
for (int i = 0; i < index; i++) {
ptr = plusOne(ptr, length);
}
return array[ptr];
}

/** print the entire deque from front to end. */
@Override
public void printDeque() {
int ptr = front;
while (ptr != last) {
System.out.print(array[ptr] + " ");
ptr = plusOne(ptr, length);
}
}

}
1 change: 1 addition & 0 deletions proj1/deque/Deque.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package deque;
public interface Deque<T> {
// Adds an item of type T to the front of the deque. */
public void addFirst(T item);
Expand Down
8 changes: 5 additions & 3 deletions proj1/deque/LinkedListDeque.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

package deque;
public class LinkedListDeque<T> implements Deque<T> {
public class Node {
private T value;
Expand Down Expand Up @@ -70,7 +70,8 @@ public T removeFirst() {
dropOne = sentinel.next;
sentinel.next.next.prev = sentinel;
sentinel.next = sentinel.next.next;
size --;
if(size != 0)
{size --;}
return dropOne.value;
}

Expand All @@ -82,7 +83,8 @@ public T removeLast() {
dropOne = sentinel.prev;
sentinel.prev.prev.next = sentinel;
sentinel.prev = sentinel.prev.prev;
size --;
if(size != 0)
{size --;}
return dropOne.value;
}

Expand Down
24 changes: 12 additions & 12 deletions proj1/deque/LinkedListDequeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class LinkedListDequeTest {
public void addIsEmptySizeTest() {

System.out.println("Make sure to uncomment the lines below (and delete this print statement).");
/*

LinkedListDeque<String> lld1 = new LinkedListDeque<String>();

assertTrue("A newly initialized LLDeque should be empty", lld1.isEmpty());
Expand All @@ -34,15 +34,15 @@ public void addIsEmptySizeTest() {

System.out.println("Printing out deque: ");
lld1.printDeque();
*/

}

@Test
/** Adds an item, then removes an item, and ensures that dll is empty afterwards. */
public void addRemoveTest() {

System.out.println("Make sure to uncomment the lines below (and delete this print statement).");
/*

LinkedListDeque<Integer> lld1 = new LinkedListDeque<Integer>();
// should be empty
assertTrue("lld1 should be empty upon initialization", lld1.isEmpty());
Expand All @@ -54,15 +54,15 @@ public void addRemoveTest() {
lld1.removeFirst();
// should be empty
assertTrue("lld1 should be empty after removal", lld1.isEmpty());
*/

}

@Test
/* Tests removing from an empty deque */
public void removeEmptyTest() {

System.out.println("Make sure to uncomment the lines below (and delete this print statement).");
/*

LinkedListDeque<Integer> lld1 = new LinkedListDeque<>();
lld1.addFirst(3);

Expand All @@ -77,14 +77,14 @@ public void removeEmptyTest() {
errorMsg += " actual size() returned 0\n";

assertEquals(errorMsg, 0, size);
*/

}

@Test
/* Check if you can create LinkedListDeques with different parameterized types*/
public void multipleParamTest() {

/*

LinkedListDeque<String> lld1 = new LinkedListDeque<String>();
LinkedListDeque<Double> lld2 = new LinkedListDeque<Double>();
LinkedListDeque<Boolean> lld3 = new LinkedListDeque<Boolean>();
Expand All @@ -96,31 +96,31 @@ public void multipleParamTest() {
String s = lld1.removeFirst();
double d = lld2.removeFirst();
boolean b = lld3.removeFirst();
*/

}

@Test
/* check if null is return when removing from an empty LinkedListDeque. */
public void emptyNullReturnTest() {

System.out.println("Make sure to uncomment the lines below (and delete this print statement).");
/*

LinkedListDeque<Integer> lld1 = new LinkedListDeque<Integer>();

boolean passed1 = false;
boolean passed2 = false;
assertEquals("Should return null when removeFirst is called on an empty Deque,", null, lld1.removeFirst());
assertEquals("Should return null when removeLast is called on an empty Deque,", null, lld1.removeLast());

*/

}

@Test
/* Add large number of elements to deque; check if order is correct. */
public void bigLLDequeTest() {

System.out.println("Make sure to uncomment the lines below (and delete this print statement).");
/*

LinkedListDeque<Integer> lld1 = new LinkedListDeque<Integer>();
for (int i = 0; i < 1000000; i++) {
lld1.addLast(i);
Expand All @@ -134,6 +134,6 @@ public void bigLLDequeTest() {
assertEquals("Should have the same value", i, (double) lld1.removeLast(), 0.0);
}

*/

}
}
47 changes: 47 additions & 0 deletions proj1/deque/MaxArrayDeque.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package deque;
import java.util.Comparator;

public class MaxArrayDeque<T> extends ArrayDeque<T>{
public Comparator<T> comparator;
public MaxArrayDeque(Comparator<T> c) {
super();
comparator = c;
}

public T max() {
T maxOne = null;
if(this.isEmpty()) {
return null;
}
if(this.size() == 1) {
maxOne = this.get(0);
}
for(int i = 0; i < this.size() - 1; i++) {
maxOne = this.get(i);
if(comparator.compare(maxOne,this.get(i +1)) < 0) {
maxOne = this.get(i + 1);
}
}
return maxOne;
}

public T max(Comparator<T> c) {
comparator = c;
T maxOne = null;
if(this.isEmpty()) {
return null;
}
if(this.size() == 1) {
maxOne = this.get(0);
}
for(int i = 0; i < this.size() - 1; i++) {
maxOne = this.get(i);
if(comparator.compare(maxOne,this.get(i +1)) < 0) {
maxOne = this.get(i + 1);
}
}
return maxOne;
}


}
Loading

0 comments on commit 05f4374

Please sign in to comment.