Share Pastebin
Guest
Public paste!

sokoban

By: a guest | Mar 21st, 2010 | Syntax: Java | Size: 4.95 KB | Hits: 162 | 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{
  10.  
  11.         private Level game;
  12.         private JPanel playground;
  13.         private JPanel container;
  14.         private JPanel info;
  15.         private JPanel buttons;
  16.         private JLabel top;
  17.         private JButton undo;
  18.         private JButton newGame;
  19.         private JButton viewMoves;
  20.        
  21.         private ImageIcon wall = new ImageIcon("icons/wall16x16.png");
  22.         private ImageIcon blank = new ImageIcon("icons/blank16x16.png");
  23.         private ImageIcon target = new ImageIcon("icons/target16x16.png");
  24.         private ImageIcon mover = new ImageIcon("icons/mover16x16.png");
  25.         private ImageIcon mover_on_target = new ImageIcon("icons/mover_on_target16x16.png");
  26.         private ImageIcon movable = new ImageIcon("icons/movable16x16.png");
  27.         private ImageIcon movable_on_target = new ImageIcon("icons/movable_on_target16x16.png");
  28.  
  29.        
  30.        
  31.        
  32.        
  33.         public Sokoban(boolean chooseMap) throws Exception {
  34.                
  35.                
  36.         this.remove(this);
  37.                 this.setTitle("Sokoban");
  38.         this.setSize(500, 500);
  39.         this.setLocationRelativeTo(null);
  40.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  41.         this.setVisible(true);
  42.                 this.setLayout(new GridLayout(3,1));
  43.                
  44.         if (chooseMap) {
  45.                 JFileChooser fileChooser = new JFileChooser();
  46.                 if(fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
  47.                         File file = fileChooser.getSelectedFile();
  48.                 Scanner input = new Scanner(file);
  49.                 game = new Level(input.nextLine());
  50.                 }
  51.         }
  52.         else {
  53.                         Reader reader = new FileReader("/Users/Ole/Desktop/level2.txt");
  54.                         BufferedReader b = new BufferedReader(reader);
  55.                         String level = b.readLine();
  56.             game = new Level(level);
  57.         }
  58.  
  59.                
  60.                
  61.                 this.info = new JPanel();
  62.                 this.add(info);
  63.                
  64.                 Font headerFont = new Font("Verdana", Font.BOLD, 24);
  65.                 this.top = new JLabel("Sokoban", SwingConstants.CENTER);
  66.                 this.top.setFont(headerFont);
  67.                 this.info.add(top);
  68.                 this.info.setAlignmentY(CENTER_ALIGNMENT);
  69.  
  70.         this.container = new JPanel();
  71.         this.container.setSize(game.getWidth()*16, game.getHeight()*16);
  72.         this.add(container);
  73.                 this.container.addKeyListener(this);
  74.                 this.container.setFocusable(true);
  75.        
  76.        
  77.         this.buttons = new JPanel();
  78.         this.undo = new JButton("Angre flytt (Z)");
  79.         this.viewMoves = new JButton("Vis alle flytt (V)");
  80.         this.newGame = new JButton("Nytt spill (N)");
  81.        
  82.         this.newGame.addActionListener(new ActionListener() {
  83.                 public void actionPerformed(ActionEvent e) {
  84.                         try {
  85.                                         new Sokoban(true);
  86.                                 } catch (Exception e1) {
  87.                                         e1.printStackTrace();
  88.                                 }
  89.                 }
  90.         });
  91.                
  92.        
  93.         this.buttons.add(undo);
  94.         this.buttons.add(viewMoves);
  95.         this.buttons.add(newGame);
  96.        
  97.         this.add(buttons);
  98.        
  99.  
  100.        
  101.        
  102.                 this.draw();
  103.  
  104.         }
  105.        
  106.        
  107.         public void keyPressed(KeyEvent e) {
  108.         switch(e.getKeyCode()){
  109.         case KeyEvent.VK_DOWN: if (game.checkMove(0, 1)) {game.doMove(0, 1); this.draw();} break;
  110.         case KeyEvent.VK_UP: if (game.checkMove(0, -1)) {game.doMove(0, -1); this.draw();} break;
  111.         case KeyEvent.VK_LEFT: if (game.checkMove(-1, 0)) {game.doMove(-1, 0); this.draw();} break;
  112.         case KeyEvent.VK_RIGHT: if (game.checkMove(1, 0)) {game.doMove(1, 0); this.draw();} break;
  113.         }
  114.         }
  115.         public void keyReleased(KeyEvent e)     {}
  116.         public void keyTyped(KeyEvent e) {}
  117.        
  118.        
  119.        
  120.         private void draw() {
  121.  
  122.                 container.removeAll();
  123.                
  124.                 playground = new JPanel();
  125.                 playground.setLayout(new GridLayout(game.getHeight(), game.getWidth(), 0, 0));
  126.  
  127.  
  128.  
  129.                 if (game.hasWon()) {
  130.                         this.info.removeAll();
  131.                         Font headerFont = new Font("Verdana", Font.BOLD, 24);
  132.                         this.top = new JLabel("Du har vunnet!", SwingConstants.CENTER);
  133.                         this.top.setFont(headerFont);
  134.                         this.info.add(top);
  135.                         this.info.setAlignmentY(CENTER_ALIGNMENT);
  136.                        
  137.                         this.container.removeKeyListener(this);
  138.                 }
  139.  
  140.                 this.container.add(playground);
  141.                        
  142.                
  143.                 String s = game.toString();
  144.        
  145.                
  146.                 for (int i = 0; i < s.length(); i++) {
  147.                                 char c = s.charAt(i);
  148.                                 switch(c) {
  149.                                 case '#':
  150.                                         playground.add(new JLabel(wall));
  151.                                         break;
  152.                                 case ' ':
  153.                                         playground.add(new JLabel(blank));
  154.                                         break;
  155.                                 case '.':
  156.                                         playground.add(new JLabel(target));
  157.                                         break;
  158.                                 case '@':
  159.                                         playground.add(new JLabel(mover));
  160.                                         break;
  161.                                 case '+':
  162.                                         playground.add(new JLabel(mover_on_target));
  163.                                         break;
  164.                                 case '$':
  165.                                         playground.add(new JLabel(movable));
  166.                                         break;
  167.                                 case '*':
  168.                                         playground.add(new JLabel(movable_on_target));
  169.                                         break;
  170.                                 default:
  171.                                         playground.add(new JLabel(blank));
  172.                                         break;
  173.                                 }
  174.                 }
  175.                 this.playground.revalidate();
  176.         }
  177.  
  178.  
  179.        
  180.        
  181.        
  182.         public static void main(String[] args)  throws Exception{
  183.                 new Sokoban(false);
  184.     }
  185. }