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 JLabel top;
private JButton undo;
private JButton newGame;
private JButton viewMoves;
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(boolean chooseMap) throws Exception {
this.remove(this);
this.setTitle("Sokoban");
this.setSize(500, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLayout(new GridLayout(3,1));
if (chooseMap) {
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());
}
}
else {
Reader reader = new FileReader("/Users/Ole/Desktop/level2.txt");
BufferedReader b = new BufferedReader(reader);
String level = b.readLine();
game = new Level(level);
}
this.info = new JPanel();
this.add(info);
Font headerFont = new Font("Verdana", Font.BOLD, 24);
this.top = new JLabel("Sokoban", SwingConstants.CENTER);
this.top.setFont(headerFont);
this.info.add(top);
this.info.setAlignmentY(CENTER_ALIGNMENT);
this.container = new JPanel();
this.container.setSize(game.getWidth()*16, game.getHeight()*16);
this.add(container);
this.container.addKeyListener(this);
this.container.setFocusable(true);
this.buttons = new JPanel();
this.undo = new JButton("Angre flytt (Z)");
this.viewMoves = new JButton("Vis alle flytt (V)");
this.newGame = new JButton("Nytt spill (N)");
this.newGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
new Sokoban(true);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
this.buttons.add(undo);
this.buttons.add(viewMoves);
this.buttons.add(newGame);
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()) {
this.info.removeAll();
Font headerFont = new Font("Verdana", Font.BOLD, 24);
this.top = new JLabel("Du har vunnet!", SwingConstants.CENTER);
this.top.setFont(headerFont);
this.info.add(top);
this.info.setAlignmentY(CENTER_ALIGNMENT);
this.container.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{
new Sokoban(false);
}
}