diff --git a/lab2/Arithmetic/Arithmetic.java b/lab2/Arithmetic/Arithmetic.java
index 5edc3bb8b..7c779eb99 100644
--- a/lab2/Arithmetic/Arithmetic.java
+++ b/lab2/Arithmetic/Arithmetic.java
@@ -20,6 +20,6 @@ public static int product(int a, int b) {
* @return Sum of a and b
* */
public static int sum(int a, int b) {
- return a * b;
+ return a + b;
}
}
diff --git a/lab2/DebugExercise/DebugExercise2.java b/lab2/DebugExercise/DebugExercise2.java
index daae4817c..e8a0aa127 100644
--- a/lab2/DebugExercise/DebugExercise2.java
+++ b/lab2/DebugExercise/DebugExercise2.java
@@ -12,7 +12,7 @@ public static int max(int a, int b) {
step out button because you're not going to learn anything. */
int z = ~(b - a) >> 31;
- int max = b & w | a & z;
+ int max = a & w | b & z;
return max;
}
@@ -58,7 +58,7 @@ public static int arraySum(int[] x) {
int i = 0;
int sum = 0;
while (i < x.length) {
- sum = sum + add(sum, x[i]);
+ sum = add(sum, x[i]);
i = i + 1;
}
return sum;
diff --git a/lab2/IntList/IntListExercises.java b/lab2/IntList/IntListExercises.java
index f7f995e6a..9c2923c67 100644
--- a/lab2/IntList/IntListExercises.java
+++ b/lab2/IntList/IntListExercises.java
@@ -9,9 +9,9 @@ public class IntListExercises {
* @param lst IntList from Lecture
*/
public static void addConstant(IntList lst, int c) {
- IntList head = lst;
+ IntList head=new IntList(0,lst);
while (head.rest != null) {
- head.first += c;
+ head.rest.first += c;
head = head.rest;
}
}
@@ -51,7 +51,7 @@ public static int max(IntList L) {
*/
public static boolean firstDigitEqualsLastDigit(int x) {
int lastDigit = x % 10;
- while (x > 10) {
+ while (x >= 10) {
x = x / 10;
}
int firstDigit = x % 10;
@@ -76,7 +76,7 @@ public static boolean squarePrimes(IntList lst) {
if (currElemIsPrime) {
lst.first *= lst.first;
}
-
- return currElemIsPrime || squarePrimes(lst.rest);
+ boolean restResult = squarePrimes(lst.rest);
+ return currElemIsPrime || restResult;
}
}
diff --git a/lab2/IntList/SquarePrimesTest.java b/lab2/IntList/SquarePrimesTest.java
index 9182a29f7..160cbd565 100644
--- a/lab2/IntList/SquarePrimesTest.java
+++ b/lab2/IntList/SquarePrimesTest.java
@@ -17,4 +17,12 @@ public void testSquarePrimesSimple() {
assertEquals("14 -> 15 -> 16 -> 289 -> 18", lst.toString());
assertTrue(changed);
}
+
+ @Test
+ public void testSquarePrimesSimpe() {
+ IntList lst = IntList.of(2, 15, 16, 17, 18);
+ boolean changed = IntListExercises.squarePrimes(lst);
+ assertEquals("4 -> 15 -> 16 -> 289 -> 18", lst.toString());
+ assertTrue(changed);
+ }
}
diff --git a/lab2setup/pom.xml b/lab2setup/pom.xml
index 658ca0072..c42757857 100644
--- a/lab2setup/pom.xml
+++ b/lab2setup/pom.xml
@@ -4,6 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
+
CS61B
61BMasterPom
@@ -15,24 +16,48 @@
lab2setup
1.0-SNAPSHOT
+
+
+
+
+ junit
+ junit
+ 4.12
+ system
+ ${basedir}/../library-sp21/javalib/junit-4.12.jar
+
+
+
+
+ org.hamcrest
+ hamcrest-core
+ 1.3
+ system
+ ${basedir}/../library-sp21/javalib/hamcrest-core-1.3.jar
+
+
+
+
+ CS61B
+ jh61b
+ 1.0
+ system
+ ${basedir}/../library-sp21/javalib/jh61b.jar
+
+
+
${project.basedir}
${project.basedir}
-
- org.apache.maven.plugins
- maven-compiler-plugin
- 3.1
-
- 1.9
- 1.9
-
-
+
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
+ 9
+ 9
-J-XX:+ShowCodeDetailsInExceptionMessages
@@ -40,5 +65,12 @@
-
-
+
+
+
+
+ local-only
+ file://${basedir}/../library-sp21/javalib
+
+
+
\ No newline at end of file
diff --git a/lab3/randomizedtest/BuggyAList.java b/lab3/randomizedtest/BuggyAList.java
index b2b70c2b8..8b3d4fbc7 100644
--- a/lab3/randomizedtest/BuggyAList.java
+++ b/lab3/randomizedtest/BuggyAList.java
@@ -60,7 +60,7 @@ public int size() {
* returns deleted item. */
public Item removeLast() {
if ((size < items.length / 4) && (size > 4)) {
- resize(size / 4);
+ resize(size );
}
Item x = getLast();
items[size - 1] = null;
diff --git a/lab3/randomizedtest/TestBuggyAList.java b/lab3/randomizedtest/TestBuggyAList.java
index 8afd045db..e8091f359 100644
--- a/lab3/randomizedtest/TestBuggyAList.java
+++ b/lab3/randomizedtest/TestBuggyAList.java
@@ -1,6 +1,8 @@
package randomizedtest;
+import afu.org.checkerframework.checker.igj.qual.I;
import edu.princeton.cs.algs4.StdRandom;
+import net.sf.saxon.om.Item;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -9,4 +11,60 @@
*/
public class TestBuggyAList {
// YOUR TESTS HERE
+ @Test
+ public void testThreeAddThreeRemove() {
+ AListNoResizing correct=new AListNoResizing<>();
+ BuggyAList broken =new BuggyAList<>();
+ correct.addLast(5);
+ correct.addLast(20);
+ correct.addLast(10);
+
+ broken.addLast(5);
+ broken.addLast(20);
+ broken.addLast(10);
+
+ assertEquals(correct.size(),broken.size());
+
+ assertEquals(correct.removeLast(), broken.removeLast());
+ assertEquals(correct.removeLast(), broken.removeLast());
+ assertEquals(correct.removeLast(), broken.removeLast());
+ }
+ @Test
+ public void JUnit() {
+ AListNoResizing correct = new AListNoResizing<>();
+ BuggyAList broken = new BuggyAList<>();
+ int N = 5000;
+ for (int i = 0; i < N; i += 1) {
+ int operationNumber = StdRandom.uniform(0, 4);
+
+ if (operationNumber == 0) {
+ // addLast
+ int randVal = StdRandom.uniform(0, 100);
+ correct.addLast(randVal);
+ broken.addLast(randVal);
+ System.out.println("addLast(" + randVal + ")");
+
+ } else if (operationNumber == 1) {
+ // size
+ int correct_size = correct.size();
+ int broken_size=broken.size();
+ assertEquals("Size mismatch after operation " + i,correct_size,broken_size);
+ System.out.println("size: " + correct_size);
+
+ }else if (operationNumber == 2 && correct.size() > 0) {
+ // getLast - 仅当列表非空时执行
+ Integer correctLast = correct.getLast();
+ Integer brokenLast = broken.getLast();
+ assertEquals("getLast mismatch after operation " + i, correctLast, brokenLast);
+ System.out.println("getLast: " + correctLast);
+
+ }else if (operationNumber == 3 && correct.size() > 0) {
+ Integer correctRemoved = correct.removeLast();
+ Integer brokenRemoved = broken.removeLast();
+ assertEquals("removeLast mismatch after operation " + i, correctRemoved, brokenRemoved);
+ System.out.println("removeLast: " + correctRemoved);
+ }
+ assertEquals("Size mismatch after operation " + i, correct.size(), broken.size());
+ }
+ }
}
diff --git a/lab3/timingtest/AList.java b/lab3/timingtest/AList.java
index 866478d9f..66b066897 100644
--- a/lab3/timingtest/AList.java
+++ b/lab3/timingtest/AList.java
@@ -1,5 +1,4 @@
package timingtest;
-
/** Array based list.
* @author Josh Hug
*/
@@ -63,4 +62,7 @@ public Item removeLast() {
size = size - 1;
return x;
}
+
+
}
+
diff --git a/lab3/timingtest/TimeAList.java b/lab3/timingtest/TimeAList.java
index 907028317..e727ef992 100644
--- a/lab3/timingtest/TimeAList.java
+++ b/lab3/timingtest/TimeAList.java
@@ -23,5 +23,24 @@ public static void main(String[] args) {
public static void timeAListConstruction() {
// TODO: YOUR CODE HERE
+ AList Ns = new AList<>();
+ AList times = new AList<>();
+ AList opCounts = new AList<>();
+
+ int currentN = 1000;
+ while (currentN <= 128000) {
+ Stopwatch sw = new Stopwatch();
+ AList list = new AList<>();
+ for (int i = 0; i < currentN; i++) {
+ list.addLast(0);
+ }
+ double time = sw.elapsedTime();
+ Ns.addLast(currentN);
+ times.addLast(time);
+ opCounts.addLast(currentN);
+ currentN *= 2;
+ }
+
+ printTimingTable(Ns, times, opCounts);
}
}
diff --git a/lab3/timingtest/TimeSLList.java b/lab3/timingtest/TimeSLList.java
index 025e0fcd7..c5ea5dbad 100644
--- a/lab3/timingtest/TimeSLList.java
+++ b/lab3/timingtest/TimeSLList.java
@@ -23,6 +23,29 @@ public static void main(String[] args) {
public static void timeGetLast() {
// TODO: YOUR CODE HERE
- }
+ AList Ns = new AList<>();
+ AList times = new AList<>();
+ AList opCounts = new AList<>();
+
+ int currentN = 1000;
+ int M = 10000; // 固定每次调用 M 次 getLast
+
+ while (currentN<=128000) {
+ SLList list=new SLList<>();
+ for (int i=0;i= 0; row--) {
+ Tile tile = board.tile(col, row);
+ if (tile == null) continue;
+
+ int targetRow = row;
+ // 向上寻找可移动的位置
+ while (targetRow < nextRow && board.tile(col, targetRow + 1) == null) {
+ targetRow++;
+ }
+
+ // 检查是否可以合并
+ if (targetRow < size - 1 &&
+ board.tile(col, targetRow + 1) != null &&
+ board.tile(col, targetRow + 1).value() == tile.value() &&
+ board.tile(col, targetRow + 1).value() != prevValue) {
+
+ // 执行合并
+ board.move(col, targetRow + 1, tile);
+ score += tile.value() * 2; // 更新分数
+ changed = true;
+ prevValue = tile.value() * 2; // 标记已合并的值
+ nextRow = targetRow + 1; // 更新下一个可用位置
+
+ } else if (targetRow != row) {
+ // 只移动不合并
+ board.move(col, targetRow, tile);
+ changed = true;
+ prevValue = -1; // 重置合并标记
+ nextRow = targetRow;
+ } else {
+ // 无法移动
+ prevValue = -1; // 重置合并标记
+ nextRow = row;
+ }
+ }
+ }
+
+ board.setViewingPerspective(Side.NORTH);
checkGameOver();
if (changed) {
setChanged();
@@ -138,6 +179,13 @@ private static boolean checkGameOver(Board b) {
* */
public static boolean emptySpaceExists(Board b) {
// TODO: Fill in this function.
+ for (int i=0;i<=3;i++) {
+ for (int j=0;j<=3;j++) {
+ if (b.tile(i,j)== null){
+ return true;
+ }
+ }
+ }
return false;
}
@@ -148,6 +196,15 @@ public static boolean emptySpaceExists(Board b) {
*/
public static boolean maxTileExists(Board b) {
// TODO: Fill in this function.
+ for (int i=0;i<=3;i++) {
+ for (int j=0;j<=3;j++) {
+ if (b.tile(i,j)==null){
+ continue;
+ }else if(b.tile(i,j).value()==MAX_PIECE){
+ return true;
+ }
+ }
+ }
return false;
}
@@ -159,6 +216,16 @@ public static boolean maxTileExists(Board b) {
*/
public static boolean atLeastOneMoveExists(Board b) {
// TODO: Fill in this function.
+ if (emptySpaceExists(b)){
+ return true;
+ }
+ for (int i=0;i<=2;i++) {
+ for (int j=0;j<=2;j++) {
+ if (b.tile(i,j).value()==b.tile(i,j+1).value() || b.tile(i,j).value()==b.tile(i+1,j).value() || b.tile(i+1,j+1).value()==b.tile(i,j+1).value()){
+ return true;
+ }
+ }
+ }
return false;
}