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, ActionListener{
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 JButton restart;
private String levelMap;
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, String map) throws Exception {
if (container != null) {
this.remove(container);
}
this.setTitle("Sokoban");
this.setSize(600, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLayout(new GridLayout(3,1));
this.setResizable(false);
if (chooseMap) {
JFileChooser fileChooser = new JFileChooser();
if(fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
this.levelMap = file.getPath();
Scanner input = new Scanner(file);
game = new Level(input.nextLine());
}
}
else {
if(map == null) {
Reader reader = new FileReader("/Users/Ole/Desktop/level2.txt");
this.levelMap = "/Users/Ole/Desktop/level2.txt";
BufferedReader b = new BufferedReader(reader);
String level = b.readLine();
game = new Level(level);
}
else {
Reader reader = new FileReader(map);
this.levelMap = map;
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.restart = new JButton("Restart (R)");
this.newGame = new JButton("Nytt spill (N)");
this.buttons.add(undo);
this.buttons.add(viewMoves);
this.buttons.add(restart);
this.buttons.add(newGame);
this.undo.addActionListener(this);
this.viewMoves.addActionListener(this);
this.restart.addActionListener(this);
this.newGame.addActionListener(this);
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) {}
public void actionPerformed(ActionEvent e) {
Object cause = e.getSource();
if (cause == restart) {
try {
new Sokoban(false, this.levelMap);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (cause == newGame) {
try {
new Sokoban(true, null);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
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, null);
}
}