Added functionality to keep track of moves.

This commit is contained in:
Jonathan Turner 2024-02-03 15:25:22 -05:00
parent fe89cf76af
commit 489ee0a50c
4 changed files with 80 additions and 5 deletions

View File

@ -33,7 +33,7 @@ public class BoardGenerator {
} }
// Checks if the board is solveable. // Checks if the board is solvable.
Board generated = new Board(pieces); Board generated = new Board(pieces);
if (isSolvable(generated)) { if (isSolvable(generated)) {
return generated; return generated;

View File

@ -1,7 +1,6 @@
package Assignments.A1.models; package Assignments.A1.models;
import java.util.ArrayList; import java.util.*;
import java.util.List;
/** /**
* @author Jonathan Turner * @author Jonathan Turner
@ -9,8 +8,6 @@ import java.util.List;
*/ */
public class EightPuzzle { public class EightPuzzle {
private List<Board> boards = new ArrayList<>();
} }

View File

@ -0,0 +1,49 @@
package Assignments.A1.models;
/**
* Used to keep track of moves in either a stack or queues.
*
* @author Jonathan Turner
* @version Spring 2024
*/
public class Move {
private Board board;
private final Pair<Integer> points;
private boolean used;
/**
* Creates a new move with the given points.
*
* @precondition none
* @postcondition a new move is created.
* @param firstpoint the first point
* @param secondpoint the second point
*/
public Move(int firstpoint, int secondpoint) {
this.points = new Pair<>(firstpoint, secondpoint);
}
/**
* Checks if a move was used.
* @precondition none
* @postcondition none
* @return whether the move was used.
*/
public boolean used() {
return used;
}
/**
* Returns the points for the move and marks it as used.
*
* @precondition none
* @postcondition used == true
* @return the pair of points.
*/
public Pair<Integer> traverse() {
used = true;
return points;
}
}

View File

@ -0,0 +1,29 @@
package Assignments.A1.models;
/**
* Used to pair two points for moves.
* @author Jonathan Turner
* @version Spring 2024
* @param <E> the type of the pair.
*/
public class Pair<E> {
/* the first and second points */
public E first;
public E second;
/**
* Creates and sets the first and second point of the pair.
*
* @precondition none
* @postcondition a pair is created.
*
* @param first the first point of the pair
* @param second the second point of the pair
*/
public Pair(E first, E second) {
this.first = first;
this.second = second;
}
}