Add the ability to get a list of possible moves on the board.

This commit is contained in:
Jonathan Turner 2024-02-04 15:55:12 -05:00
parent 9bcf2ddb9d
commit 8ecead1b37
3 changed files with 31 additions and 15 deletions

View File

@ -1,6 +1,8 @@
package Assignments.A1.models; package Assignments.A1.models;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
/** /**
* This class keeps track of the current state (whether in permutation or not) of the board. * This class keeps track of the current state (whether in permutation or not) of the board.
@ -149,6 +151,34 @@ public class Board {
return ordered; return ordered;
} }
/**
* Adds all the possible moves the state can make based off the open space.
*
* @precondition none
* @postcondition none
* @return the list of possible moves.
*/
public List<Move> getMoves() {
List<Move> moves = new ArrayList<>();
int space = getOpenLocation();
int row = space / 3;
int column = space % 3;
if (row > 0) {
moves.add(new Move(space, space-1));
}
if (row < 2) {
moves.add(new Move(space, space+1));
}
if (column > 0) {
moves.add(new Move(space, space-3));
}
if (column < 2) {
moves.add(new Move(space, space+3));
}
return moves;
}
/** /**
* Hashes the board by adding the pieces to an array and hashes the array. * Hashes the board by adding the pieces to an array and hashes the array.
* *

View File

@ -1,13 +0,0 @@
package Assignments.A1.models;
import java.util.*;
/**
* @author Jonathan Turner
* @version Spring 2024
*/
public class EightPuzzle {
}

View File

@ -12,8 +12,7 @@ public class DFS {
private HashSet<Board> visited = new HashSet<>(); private HashSet<Board> visited = new HashSet<>();
private HashMap<Board, Stack<Move>> moves = new HashMap<>(); private HashMap<Board, Stack<Move>> moves = new HashMap<>();
public void dfs(Board currentState) { public void dfs(Board root) {
} }