+1 (315) 557-6473 

Create A Program to Create a Treasure Hunting Game in Java Assignment Solution.


Instructions

Objective
Write a program to create a treasure hunting game in java language.

Requirements and Specifications

Program to treasure hunting game in java

Source Code

import java.io.IOException;

import java.util.Arrays;

public class FindPath {

    private Map pyramidMap;

    public FindPath(String filename) {

        try {

            pyramidMap = new Map(filename);

        }

        catch (IOException e) {

            throw new RuntimeException(e);

        }

    }

    public DLStack<Chamber> path() {

        DLStack<Chamber> stack = new DLStack<>();

        Chamber start = pyramidMap.getEntrance();

        int n = pyramidMap.getNumTreasures();

        stack.push(start);

        start.markPushed();

        int found = 0;

        while(!stack.isEmpty()) {

            Chamber top = stack.peek();

            if (top.isTreasure()) {

                found++;

                if (found == n) {

                    break;

                }

            }

            Chamber c = bestChamber(top);

            if (c == null) {

                top.markPopped();

                stack.pop();

            }

            else {

                stack.push(c);

                c.markPushed();

            }

        }

        return stack;

    }

    public Map getMap() {

        return pyramidMap;

    }

    public boolean isDim(Chamber currentChamber) {

        if (currentChamber != null && !currentChamber.isSealed() && !currentChamber.isLighted()) {

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

                Chamber neighbor = currentChamber.getNeighbour(i);

                if (neighbor == null) {

                    continue;

                }

                if (neighbor.isLighted()) {

                    return true;

                }

            }

        }

        return false;

    }

    public Chamber bestChamber(Chamber currentChamber) {

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

            Chamber neighbor = currentChamber.getNeighbour(i);

            if (neighbor == null) {

                continue;

            }

            if (!neighbor.isMarked() && neighbor.isTreasure()) {

                return neighbor;

            }

        }

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

            Chamber neighbor = currentChamber.getNeighbour(i);

            if (neighbor == null) {

                continue;

            }

            if (!neighbor.isMarked() && neighbor.isLighted()) {

                return neighbor;

            }

        }

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

            Chamber neighbor = currentChamber.getNeighbour(i);

            if (neighbor == null) {

                continue;

            }

            if (!neighbor.isMarked() && isDim(neighbor)) {

                return neighbor;

            }

        }

        return null;