#GPT-4 Conversation #1095691005816213624

1 messages · Page 1 of 1 (latest)

spice shadowBOT
midnight elbow
#

write a java code of a game involving solving photo montage puzzle

spice shadowBOT
# midnight elbow write a java code of a game involving solving photo montage puzzle

Here's a simple Java code for a console-based photo montage puzzle game using a 3x3 grid. This code will use numbers to simulate the puzzle pieces. The objective of the game is to arrange the tiles in order, from 1 to 8, with an empty space (0) at the bottom right corner.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class PhotoMontagePuzzleGame {
    private static final int GRID_SIZE = 3;
    private static final Integer[] GOAL = {1, 2, 3, 4, 5, 6, 7, 8, 0};

    public static void main(String[] args) {
        Integer[] puzzle = generatePuzzle();
        while (!isSolved(puzzle)) {
            printPuzzle(puzzle);
            int move = getInput();
            makeMove(puzzle, move);
        }
        System.out.println("Congratulations! You've solved the puzzle!");
        printPuzzle(puzzle);
    }

    private static Integer[] generatePuzzle() {
        List<Integer> tiles = Arrays.asList(GOAL);
        Collections.shuffle(tiles);
        return tiles.toArray(new Integer[0]);
    }

    private static boolean isSolved(Integer[] puzzle) {
        return Arrays.equals(puzzle, GOAL);
    }

    private static void printPuzzle(Integer[] puzzle) {
        for (int i = 0; i < puzzle.length; i++) {
            System.out.print((puzzle[i] > 0 ? puzzle[i] : " ") + " ");
            if ((i + 1) % GRID_SIZE == 0) {
                System.out.println();
            }
        }
    }

    private static int getInput() {
        System.out.println("Enter the number you'd like to move:");
        Scanner scanner = new Scanner(System.in);
        return scanner.nextInt();
    }

    private static void makeMove(Integer[] puzzle, int move) {
        int moveIndex = -1, emptyIndex = -1;

        for (int i = 0; i < puzzle.length; i++) {
            if (puzzle[i] == move) moveIndex = i;
            if (puzzle[i] == 0) emptyIndex = i;
        }

        if (canMove(moveIndex, emptyIndex)) {
            swa
#

p(puzzle, moveIndex, emptyIndex);
} else {
System.out.println("Invalid move! Please try again.");
}
}

private static boolean canMove(int moveIndex, int emptyIndex) {
    int moveRow = moveIndex / GRID_SIZE, emptyRow = emptyIndex / GRID_SIZE;
    int moveCol = moveIndex % GRID_SIZE, emptyCol = emptyIndex % GRID_SIZE;
    int rowDiff = Math.abs(moveRow - emptyRow), colDiff = Math.abs(moveCol - emptyCol);

    return (rowDiff == 0 && colDiff == 1) || (rowDiff == 1 && colDiff == 0);
}

private static void swap(Integer[] puzzle, int moveIndex, int emptyIndex) {
    int tempValue = puzzle[moveIndex];
    puzzle[moveIndex] = puzzle[emptyIndex];
    puzzle[emptyIndex] = tempValue;
}

}


Copy and paste the code in your favorite Java IDE, and compile and run it to test the game.

Please note that you are approaching the credit limit of your trial version. Type /balance to check your balance. If you want to continue using this bot, subscribe to it in https://discord.gg/JCnxEaE3Mx to get more credits.
midnight elbow
#

hi