package main;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;
public class Sokoban extends JFrame implements KeyListener{
private Level game;
private JPanel playground;
private JPanel container;
private JPanel info;
private JPanel buttons;
private ImageIcon wall = new ImageIcon("icons/wall16x16.png");
private ImageIcon blank = new ImageIcon("icons/blank16x16.png");
private ImageIcon target = new ImageIcon("icons/target16x16.png");
private ImageIcon mover = new ImageIcon("icons/mover16x16.png");
private ImageIcon mover_on_target = new ImageIcon("icons/mover_on_target16x16.png");
private ImageIcon movable = new ImageIcon("icons/movable16x16.png");
private ImageIcon movable_on_target = new ImageIcon("icons/movable_on_target16x16.png");
public Sokoban() throws Exception {
this.addKeyListener(this);
this.setLayout(new GridLayout(3,1));
JFileChooser fileChooser = new JFileChooser();
if(fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
Scanner input = new Scanner(file);
game = new Level(input.nextLine());
}
this.info = new JPanel();
this.add(info);
this.container = new JPanel();
this.container.setSize(game.getWidth()*16, game.getHeight()*16);
this.add(container);
this.buttons = new JPanel();
this.add(buttons);
this.draw();
}
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_DOWN: if (game.checkMove(0, 1)) {game.doMove(0, 1); this.draw();} break;
case KeyEvent.VK_UP: if (game.checkMove(0, -1)) {game.doMove(0, -1); this.draw();} break;
case KeyEvent.VK_LEFT: if (game.checkMove(-1, 0)) {game.doMove(-1, 0); this.draw();} break;
case KeyEvent.VK_RIGHT: if (game.checkMove(1, 0)) {game.doMove(1, 0); this.draw();} break;
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
private void draw() {
container.removeAll();
playground = new JPanel();
playground.setLayout(new GridLayout(game.getHeight(), game.getWidth(), 0, 0));
if (game.hasWon()) {
Font headerFont = new Font("Verdana", Font.BOLD, 24);
JLabel victory = new JLabel("Du har vunnet!", SwingConstants.CENTER);
victory.setForeground(Color.DARK_GRAY);
victory.setFont(headerFont);
this.info.add(victory);
this.removeKeyListener(this);
}
this.container.add(playground);
String s = game.toString();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch(c) {
case '#':
playground.add(new JLabel(wall));
break;
case ' ':
playground.add(new JLabel(blank));
break;
case '.':
playground.add(new JLabel(target));
break;
case '@':
playground.add(new JLabel(mover));
break;
case '+':
playground.add(new JLabel(mover_on_target));
break;
case '$':
playground.add(new JLabel(movable));
break;
case '*':
playground.add(new JLabel(movable_on_target));
break;
default:
playground.add(new JLabel(blank));
break;
}
}
this.playground.revalidate();
}
public static void main(String[] args) throws Exception{
Sokoban frame = new Sokoban();
frame.setTitle("Sokoban");
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); }
}