Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c. With part 2, only one is processed and the board redisplayed, showing the mov

ID: 3569676 • Letter: C

Question

c. With part 2, only one is processed and the board redisplayed, showing the move?s results. Need it in java code and needs to be able to be compiled in jGrasp without errors. I will award points for just Part 1 and more for both. Thanks and Merry Christmas! The goal of Peg Solitaire is to have the game end with just one peg left on the board. If there is one peg left on the board at game?s end, the player wins, otherwise a loss occurs. The game is begun with one open hole. The game proceeds with the player jumping a peg over a neighbor peg - the jumped peg is removed from the board. Only immediate neighbor pegs can be jumped. The player keeps jumping until no more jumps are possible. The project consists of three parts. Parts 1 and 2 are required. Part 3 is optional. Extra points will be awarded to submissions that fully implement the game. The game is due at midnight of the last class. Game parts: 1. Project Part 1: a. Store the game board in an array. b. Initialize the game board with 14 pegs, as shown in the diagram below. c. Display the game board as a triangle, something like this: a: * b: ** C: *** d: **** e: Where ''*'' is a peg and ''-'' is an empty position 2. Project Part 2: a. Accept a move input from the user. For part 2, assume the move is correct. b. Move the peg and remove the jumped peg. i. The format of the move request is from to, from is the starting location, express as letter number, to is the new location, expressed as letter number. ii. Here is an example move command: ''e 1 e 3''.

Explanation / Answer

import java.util.*;

public class PegSolitaireSolver {

    private static final HashSet<Long> seenBoards = new HashSet<Long>();

    private static final ArrayList<Long> solution = new ArrayList<Long>();

    private static final long GOAL_BOARD = 16777216L;

    private static final long INITIAL_BOARD = 124141717933596L;

    private static final long VALID_BOARD_CELLS = 124141734710812L;

    private static final long[][] moves = new long[76][];

    private static void printBoard(long board) {

        for (int i = 0; i < 49; i++) {

            boolean validCell = ((1L << i) & VALID_BOARD_CELLS) != 0L;

            System.out.print(validCell ? (((1L << i) & board) != 0L ? "X " : "O ") : " ");

            if (i % 7 == 6) System.out.println();

        }

        System.out.println("-------------");

    }

    private static void createMoves(int bit1, int bit2, int bit3, ArrayList<long[]> moves) {

        moves.add(new long[]{(1L << bit1), (1L << bit2) | (1L << bit3), (1L << bit1) | (1L << bit2) | (1L << bit3)});

        moves.add(new long[]{(1L << bit3), (1L << bit2) | (1L << bit1), (1L << bit1) | (1L << bit2) | (1L << bit3)});

    }

    private static boolean search(long board) {

        for (long[] move : moves) {

            if ((move[1] & board) == 0L && (move[0] & board) != 0L) {

                long newBoard = board ^ move[2];

                if (!seenBoards.contains(newBoard)) {

                    seenBoards.add(newBoard);

                    if (newBoard == INITIAL_BOARD || search(newBoard)) {

                        solution.add(board);

                        return true;

                    }

                }

            }

        }

        return false;

    }

    public static void main(String[] args) {

        long time = System.currentTimeMillis();

        solution.add(INITIAL_BOARD);

        ArrayList<long[]> moves = new ArrayList<long[]>();

        int[] starts = new int[] {2,9,14,15,16,17,18,21,22,23,24,25,28,29,30,31,32,37,44};

        for (int start : starts) {

            createMoves(start, start + 1, start + 2, moves);

            createMoves((start%7) * 7 + start/7, (start%7 + 1) * 7 + start/7, (start%7 + 2) * 7 + start/7, moves);

        }

        Collections.shuffle(moves);

        moves.toArray(PegSolitaireSolver.moves);

        search(GOAL_BOARD);

        for (long step : solution)

            printBoard(step);

        System.out.println("Completed in " + (System.currentTimeMillis() - time) + " ms.");

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote