-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPuzzleGame.java
More file actions
62 lines (52 loc) · 2.64 KB
/
PuzzleGame.java
File metadata and controls
62 lines (52 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import javax.swing.*;
public class PuzzleGame {
public static void main(String[] args) {
try {
// Creates a JOptionPane for the user to input the prompt to generate the image
String prompt = JOptionPane.showInputDialog("Enter a prompt for the image: ");
if (prompt == null || prompt.trim().isEmpty()) {
JOptionPane.showMessageDialog(null, "Prompt cannot be empty!");
return;
}
// Prompt the user to set up their desired dimensions for their puzzle
String rowsInput = JOptionPane.showInputDialog("Enter number of rows for the puzzle: ");
String colsInput = JOptionPane.showInputDialog("Enter number of columns for the puzzle: ");
int rows; // Stores the number of rows
int cols; // Stores the number of columns
try {
rows = Integer.parseInt(rowsInput); // Parses users input to an integer -> rows
cols = Integer.parseInt(colsInput); // Parses users input to an integer -> columns
} catch (NumberFormatException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Not a valid input, please try again!"); // If they are not integers
return;
}
// Checks if the inputted integers are greater than 0
if (rows <= 0 || cols <= 0) {
JOptionPane.showMessageDialog(null, "Rows and columns must be positive integers!");
return;
}
// Fetch the image URL from AzureAI class
String imageUrl = OpenAI.fetchImage(prompt);
// Invalid URL link
if (imageUrl == null) {
JOptionPane.showMessageDialog(null, "Not able to generate image, please try again!");
return;
}
// For the user to see the generate URL, be able to see how the puzzle should look like if needed
System.out.println("Generated Image URL: " + imageUrl);
// Sets up the GUI for the puzzle game
PuzzleBoard board = new PuzzleBoard(rows, cols, imageUrl);
PuzzleGUI gui = new PuzzleGUI(board, imageUrl);
JFrame frame = new JFrame("Puzzle Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(gui);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Error: " + e.getMessage()); // Prints the error if any error occurs
}
}
}