Created
March 8, 2025 04:02
-
-
Save kishida/fd5907b798a96c23cdd040d7096a5db3 to your computer and use it in GitHub Desktop.
ChatGPTによるGUI版倉庫番
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
public class SokobanGUI extends JPanel implements KeyListener { | |
private static final int TILE_SIZE = 50; | |
private static char[][] level = { | |
{'#', '#', '#', '#', '#', '#', '#', '#', '#'}, | |
{'#', '.', ' ', ' ', '$', ' ', ' ', '.', '#'}, | |
{'#', ' ', '#', ' ', ' ', ' ', '#', ' ', '#'}, | |
{'#', ' ', '$', ' ', '@', ' ', '$', ' ', '#'}, | |
{'#', '#', ' ', '#', ' ', '#', ' ', '#', '#'}, | |
{'#', ' ', '.', ' ', '$', ' ', ' ', '.', '#'}, | |
{'#', '#', '#', '#', '#', '#', '#', '#', '#'} | |
}; | |
private static int playerX, playerY; | |
private static char[][] originalLevel; | |
public SokobanGUI() { | |
findPlayerPosition(); | |
backupOriginalLevel(); | |
setPreferredSize(new Dimension(level[0].length * TILE_SIZE, level.length * TILE_SIZE)); | |
addKeyListener(this); | |
setFocusable(true); | |
} | |
private void findPlayerPosition() { | |
for (int i = 0; i < level.length; i++) { | |
for (int j = 0; j < level[i].length; j++) { | |
if (level[i][j] == '@') { | |
playerX = i; | |
playerY = j; | |
return; | |
} | |
} | |
} | |
} | |
private void backupOriginalLevel() { | |
originalLevel = new char[level.length][level[0].length]; | |
for (int i = 0; i < level.length; i++) { | |
originalLevel[i] = level[i].clone(); | |
} | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
for (int i = 0; i < level.length; i++) { | |
for (int j = 0; j < level[i].length; j++) { | |
g.setColor(Color.WHITE); | |
g.fillRect(j * TILE_SIZE, i * TILE_SIZE, TILE_SIZE, TILE_SIZE); | |
g.setColor(Color.GRAY); | |
g.drawRect(j * TILE_SIZE, i * TILE_SIZE, TILE_SIZE, TILE_SIZE); | |
g.setColor(Color.BLACK); | |
switch (level[i][j]) { | |
case '#' -> g.fillRect(j * TILE_SIZE, i * TILE_SIZE, TILE_SIZE, TILE_SIZE); | |
case '.' -> { | |
g.setColor(Color.ORANGE); | |
g.fillOval(j * TILE_SIZE + 15, i * TILE_SIZE + 15, 20, 20); | |
} | |
case '$' -> { | |
g.setFont(new Font("Serif", Font.BOLD, 40)); | |
g.drawString("田", j * TILE_SIZE + 10, i * TILE_SIZE + 40); | |
} | |
case '@' -> { | |
g.setFont(new Font("Serif", Font.BOLD, 40)); | |
g.drawString("人", j * TILE_SIZE + 10, i * TILE_SIZE + 40); | |
} | |
} | |
} | |
} | |
} | |
@Override | |
public void keyPressed(KeyEvent e) { | |
int dx = switch (e.getKeyCode()) { | |
case KeyEvent.VK_W, KeyEvent.VK_UP -> -1; | |
case KeyEvent.VK_S, KeyEvent.VK_DOWN -> 1; | |
default -> 0; | |
}; | |
int dy = switch (e.getKeyCode()) { | |
case KeyEvent.VK_A, KeyEvent.VK_LEFT -> -1; | |
case KeyEvent.VK_D, KeyEvent.VK_RIGHT -> 1; | |
default -> 0; | |
}; | |
movePlayer(dx, dy); | |
} | |
private void movePlayer(int dx, int dy) { | |
if (dx == 0 && dy == 0) return; | |
int newX = playerX + dx; | |
int newY = playerY + dy; | |
if (level[newX][newY] == ' ' || level[newX][newY] == '.') { | |
level[playerX][playerY] = (originalLevel[playerX][playerY] == '.' ? '.' : ' '); | |
playerX = newX; | |
playerY = newY; | |
level[playerX][playerY] = '@'; | |
} else if (level[newX][newY] == '$') { | |
int boxX = newX + dx; | |
int boxY = newY + dy; | |
if (level[boxX][boxY] == ' ' || level[boxX][boxY] == '.') { | |
level[playerX][playerY] = (originalLevel[playerX][playerY] == '.' ? '.' : ' '); | |
playerX = newX; | |
playerY = newY; | |
level[newX][newY] = '@'; | |
level[boxX][boxY] = '$'; | |
} | |
} | |
repaint(); | |
if (isGameWon()) { | |
JOptionPane.showMessageDialog(this, "You win!"); | |
} | |
} | |
private boolean isGameWon() { | |
for (int i = 0; i < level.length; i++) { | |
for (int j = 0; j < level[i].length; j++) { | |
if (originalLevel[i][j] == '.' && level[i][j] != '$') { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
@Override | |
public void keyReleased(KeyEvent e) {} | |
@Override | |
public void keyTyped(KeyEvent e) {} | |
public static void main(String[] args) { | |
JFrame frame = new JFrame("Sokoban"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.add(new SokobanGUI()); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
temp.mp4