Share Pastebin
Guest
Public paste!

soko

By: a guest | Mar 22nd, 2010 | Syntax: Java | Size: 8.40 KB | Hits: 92 | Expires: Never
Copy text to clipboard
  1. package main;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.util.Scanner;
  7. import java.io.*;
  8.  
  9. public class Sokoban extends JFrame implements KeyListener, ActionListener{
  10.  
  11.         private static final long serialVersionUID = 1L;
  12.         private Level game;
  13.         private JPanel playground;
  14.         private JPanel container;
  15.         private JPanel info;
  16.         private JPanel buttons;
  17.         private JLabel top;
  18.         private JPanel header;
  19.         private JPanel moves;
  20.         private JButton undo;
  21.         private JButton newGame;
  22.         private JButton viewMoves;
  23.         private JButton restart;
  24.         private String levelMap;
  25.        
  26.        
  27.         private ImageIcon wall = new ImageIcon("resources/wall16x16.png");
  28.         private ImageIcon blank = new ImageIcon("resources/blank16x16.png");
  29.         private ImageIcon target = new ImageIcon("resources/target16x16.png");
  30.         private ImageIcon mover = new ImageIcon("resources/mover16x16.png");
  31.         private ImageIcon mover_on_target = new ImageIcon("resources/mover_on_target16x16.png");
  32.         private ImageIcon movable = new ImageIcon("resources/movable16x16.png");
  33.         private ImageIcon movable_on_target = new ImageIcon("resources/movable_on_target16x16.png");
  34.        
  35.        
  36.         /*
  37.         private ImageIcon wall = new ImageIcon(getClass().getResource("wall16x16.png"));
  38.         private ImageIcon blank = new ImageIcon(getClass().getResource("blank16x16.png"));
  39.         private ImageIcon target = new ImageIcon(getClass().getResource("target16x16.png"));
  40.         private ImageIcon mover = new ImageIcon(getClass().getResource("mover16x16.png"));
  41.         private ImageIcon mover_on_target = new ImageIcon(getClass().getResource("mover_on_target16x16.png"));
  42.         private ImageIcon movable = new ImageIcon(getClass().getResource("movable16x16.png"));
  43.         private ImageIcon movable_on_target = new ImageIcon(getClass().getResource("movable_on_target16x16.png"));
  44.         */
  45.        
  46.        
  47.         public Sokoban(boolean chooseMap, String map) throws Exception {
  48.                
  49.                 this.remove(this);
  50.                 this.setTitle("Sokoban");
  51.         this.setSize(600, 500);
  52.         this.setLocationRelativeTo(null);
  53.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54.         this.setVisible(true);
  55.                 this.setLayout(new GridLayout(3,1));
  56.                 this.setResizable(false);
  57.                
  58.                 this.setFocusable(true);
  59.                 this.addKeyListener(this);
  60.  
  61.                 this.header = new JPanel();
  62.                 this.header.setLayout(new GridLayout(2,1));
  63.                
  64.                 this.info = new JPanel();
  65.                 this.header.add(info);
  66.                
  67.                 this.moves = new JPanel();
  68.                 this.header.add(moves);
  69.                
  70.                 this.add(this.header);
  71.                
  72.                 Font headerFont = new Font("Verdana", Font.BOLD, 24);
  73.                 this.top = new JLabel("Sokoban", SwingConstants.CENTER);
  74.                 this.top.setFont(headerFont);
  75.                 this.info.add(top);
  76.                 this.info.setAlignmentY(CENTER_ALIGNMENT);
  77.  
  78.         this.container = new JPanel();
  79.         this.add(container);
  80.        
  81.         this.buttons = new JPanel();
  82.         this.undo = new JButton("Angre flytt (Z)");
  83.         this.viewMoves = new JButton("Vis alle flytt (V)");
  84.         this.restart = new JButton("Restart (R)");
  85.         this.newGame = new JButton("Nytt spill (N)");
  86.        
  87.         this.buttons.add(undo);
  88.         this.buttons.add(viewMoves);
  89.         this.buttons.add(restart);
  90.         this.buttons.add(newGame);
  91.        
  92.         this.undo.addActionListener(this);
  93.         this.viewMoves.addActionListener(this);
  94.         this.restart.addActionListener(this);
  95.         this.newGame.addActionListener(this);
  96.        
  97.        
  98.         this.add(buttons);
  99.        
  100.         this.pickLevel(chooseMap, map);
  101.                        
  102.         }
  103.        
  104.         public void pickLevel(boolean chooseMap, String map) {
  105.                 if (chooseMap) {
  106.                 JFileChooser fileChooser = new JFileChooser();
  107.                 if(fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
  108.                         File file = fileChooser.getSelectedFile();
  109.                         this.levelMap = file.getPath();
  110.                     Scanner input = null;
  111.                                 try {
  112.                                         input = new Scanner(file);
  113.                                 } catch (FileNotFoundException e) {
  114.                                         e.printStackTrace();
  115.                                 }
  116.                     game = new Level(input.nextLine());
  117.                 }
  118.             }
  119.             else {
  120.                         if(map == null) {
  121.                                 Reader reader = null;
  122.                                 try {
  123.                                         reader = new FileReader("resources/level1.txt");
  124.                                 } catch (FileNotFoundException e) {
  125.                                         e.printStackTrace();
  126.                                 }
  127.                                 this.levelMap = "resources/level1.txt";
  128.                                 BufferedReader b = new BufferedReader(reader);
  129.                                 String level = null;
  130.                                 try {
  131.                                         level = b.readLine();
  132.                                 } catch (IOException e) {
  133.                                         e.printStackTrace();
  134.                                 }
  135.                                 game = new Level(level);
  136.                         }
  137.                         else {
  138.                                 Reader reader = null;
  139.                                 try {
  140.                                         reader = new FileReader(map);
  141.                                 } catch (FileNotFoundException e) {
  142.                                         e.printStackTrace();
  143.                                 }
  144.                                 this.levelMap = map;
  145.                                 BufferedReader b = new BufferedReader(reader);
  146.                                 String level = null;
  147.                                 try {
  148.                                         level = b.readLine();
  149.                                 } catch (IOException e) {
  150.                                         e.printStackTrace();
  151.                                 }
  152.                                 game = new Level(level);
  153.                                
  154.                         }
  155.             }
  156.         this.container.setSize(game.getWidth()*16, game.getHeight()*16);
  157.                 this.draw();
  158.  
  159.  
  160.         }
  161.        
  162.         public void keyPressed(KeyEvent e) {
  163.         switch(e.getKeyCode()){
  164.         case KeyEvent.VK_DOWN: if (game.checkMove(0, 1)) {game.doMove(0, 1); this.draw();} break;
  165.         case KeyEvent.VK_UP: if (game.checkMove(0, -1)) {game.doMove(0, -1); this.draw();} break;
  166.         case KeyEvent.VK_LEFT: if (game.checkMove(-1, 0)) {game.doMove(-1, 0); this.draw();} break;
  167.         case KeyEvent.VK_RIGHT: if (game.checkMove(1, 0)) {game.doMove(1, 0); this.draw();} break;
  168.         case KeyEvent.VK_Z: game.undoMove(); this.draw(); this.requestFocus(); break;
  169.         case KeyEvent.VK_V: this.displayMoves(); this.requestFocus(); break;
  170.         case KeyEvent.VK_R: this.pickLevel(false, this.levelMap); this.requestFocus(); break;
  171.         case KeyEvent.VK_N: this.pickLevel(true, null); this.requestFocus(); break;
  172.         }
  173.         }
  174.         public void keyReleased(KeyEvent e)     {}
  175.         public void keyTyped(KeyEvent e) {}
  176.        
  177.         public void actionPerformed(ActionEvent e) {
  178.                 Object cause = e.getSource();
  179.                
  180.                 if (cause == restart) {
  181.                         this.pickLevel(false, this.levelMap);
  182.                         this.requestFocus();
  183.                 }
  184.                 if (cause == newGame) {
  185.                         this.pickLevel(true, null);
  186.                         this.requestFocus();
  187.                 }
  188.                 if (cause == undo) {
  189.                         game.undoMove();
  190.                         this.draw();
  191.                         this.requestFocus();
  192.                 }
  193.                 if (cause == viewMoves) {
  194.                         this.displayMoves();
  195.                 this.requestFocus();
  196.                 }
  197.                
  198.         }
  199.        
  200.        
  201.         private void draw() {
  202.  
  203.                 this.container.removeAll();
  204.                 this.moves.removeAll();
  205.                
  206.  
  207.                
  208.                
  209.                 playground = new JPanel();
  210.                 playground.setLayout(new GridLayout(game.getHeight(), game.getWidth(), 0, 0));
  211.  
  212.  
  213.  
  214.                 if (game.hasWon()) {
  215.                         this.info.removeAll();
  216.                         Font headerFont = new Font("Verdana", Font.BOLD, 24);
  217.                         this.top = new JLabel("Du har vunnet!", SwingConstants.CENTER);
  218.                         this.top.setFont(headerFont);
  219.                         this.info.add(top);
  220.                         this.info.setAlignmentY(CENTER_ALIGNMENT);
  221.                        
  222.                         this.removeKeyListener(this);
  223.                 }
  224.  
  225.                 this.container.add(playground);
  226.                        
  227.                
  228.                 String s = game.toString();
  229.        
  230.                
  231.                 for (int i = 0; i < s.length(); i++) {
  232.                                 char c = s.charAt(i);
  233.                                 switch(c) {
  234.                                 case '#':
  235.                                         this.playground.add(new JLabel(wall));
  236.                                         break;
  237.                                 case ' ':
  238.                                         this.playground.add(new JLabel(blank));
  239.                                         break;
  240.                                 case '.':
  241.                                         this.playground.add(new JLabel(target));
  242.                                         break;
  243.                                 case '@':
  244.                                         this.playground.add(new JLabel(mover));
  245.                                         break;
  246.                                 case '+':
  247.                                         this.playground.add(new JLabel(mover_on_target));
  248.                                         break;
  249.                                 case '$':
  250.                                         this.playground.add(new JLabel(movable));
  251.                                         break;
  252.                                 case '*':
  253.                                         this.playground.add(new JLabel(movable_on_target));
  254.                                         break;
  255.                                 default:
  256.                                         this.playground.add(new JLabel(blank));
  257.                                         break;
  258.                                 }
  259.                 }
  260.                        
  261.                 this.playground.revalidate();
  262.                
  263.                 this.moves.add(new JLabel("Antall flytt: "+game.getMoves()+". Antall dytt: "+game.getPushes()));
  264.                 this.moves.revalidate();
  265.         }
  266.  
  267.  
  268.         public void displayMoves() {
  269.                 History oldHistory = game.getHistory();
  270.                 History newHistory = new History();
  271.                 int j = oldHistory.getMoves();
  272.                 for (int i = 0; i < j; i++) {
  273.                         newHistory.add(game.undoMove());
  274.                 }
  275.                 this.draw();
  276.                 int ant = newHistory.getMoves();
  277.                 for (int i = 0; i < ant; i++) {
  278.                         Log move = newHistory.remove();
  279.                         game.doMove(move.getDx(), move.getDy());
  280.                         try {
  281.                                 Thread.sleep(700);
  282.                         } catch (InterruptedException e) {
  283.                                 e.printStackTrace();
  284.                         }
  285.                         System.out.println(move.getDx()+", "+move.getDy());
  286.                         this.draw();
  287.                 }
  288.         }
  289.        
  290.        
  291.        
  292.         public static void main(String[] args)  throws Exception{
  293.                 new Sokoban(false, null);
  294.     }
  295.  
  296.  
  297.  
  298. }