Share Pastebin
Guest
Public paste!

soko

By: a guest | Mar 21st, 2010 | Syntax: Java | Size: 3.71 KB | Hits: 89 | 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.        
  17.         private ImageIcon wall = new ImageIcon("icons/wall16x16.png");
  18.         private ImageIcon blank = new ImageIcon("icons/blank16x16.png");
  19.         private ImageIcon target = new ImageIcon("icons/target16x16.png");
  20.         private ImageIcon mover = new ImageIcon("icons/mover16x16.png");
  21.         private ImageIcon mover_on_target = new ImageIcon("icons/mover_on_target16x16.png");
  22.         private ImageIcon movable = new ImageIcon("icons/movable16x16.png");
  23.         private ImageIcon movable_on_target = new ImageIcon("icons/movable_on_target16x16.png");
  24.  
  25.        
  26.        
  27.        
  28.        
  29.         public Sokoban() throws Exception {
  30.                 this.addKeyListener(this);
  31.                
  32.                 this.setLayout(new GridLayout(3,1));
  33.                
  34.         JFileChooser fileChooser = new JFileChooser();
  35.         if(fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
  36.                 File file = fileChooser.getSelectedFile();
  37.                 Scanner input = new Scanner(file);
  38.                 game = new Level(input.nextLine());
  39.         }
  40.  
  41.                
  42.                
  43.                 this.info = new JPanel();
  44.                 this.add(info);
  45.                
  46.        
  47.         this.container = new JPanel();
  48.         this.container.setSize(game.getWidth()*16, game.getHeight()*16);
  49.         this.add(container);
  50.        
  51.         this.buttons = new JPanel();
  52.         this.add(buttons);
  53.  
  54.        
  55.        
  56.                 this.draw();
  57.  
  58.         }
  59.        
  60.        
  61.         public void keyPressed(KeyEvent e) {
  62.         switch(e.getKeyCode()){
  63.         case KeyEvent.VK_DOWN: if (game.checkMove(0, 1)) {game.doMove(0, 1); this.draw();} break;
  64.         case KeyEvent.VK_UP: if (game.checkMove(0, -1)) {game.doMove(0, -1); this.draw();} break;
  65.         case KeyEvent.VK_LEFT: if (game.checkMove(-1, 0)) {game.doMove(-1, 0); this.draw();} break;
  66.         case KeyEvent.VK_RIGHT: if (game.checkMove(1, 0)) {game.doMove(1, 0); this.draw();} break;
  67.         }
  68.         }
  69.         public void keyReleased(KeyEvent e)     {}
  70.         public void keyTyped(KeyEvent e) {}
  71.        
  72.        
  73.        
  74.         private void draw() {
  75.  
  76.                 container.removeAll();
  77.                
  78.                 playground = new JPanel();
  79.                 playground.setLayout(new GridLayout(game.getHeight(), game.getWidth(), 0, 0));
  80.  
  81.  
  82.  
  83.                 if (game.hasWon()) {
  84.                         Font headerFont = new Font("Verdana", Font.BOLD, 24);
  85.                         JLabel victory = new JLabel("Du har vunnet!", SwingConstants.CENTER);
  86.                         victory.setForeground(Color.DARK_GRAY);
  87.                         victory.setFont(headerFont);
  88.                         this.info.add(victory);
  89.                         this.removeKeyListener(this);
  90.                 }
  91.  
  92.                 this.container.add(playground);
  93.                        
  94.                
  95.                 String s = game.toString();
  96.        
  97.                
  98.                 for (int i = 0; i < s.length(); i++) {
  99.                                 char c = s.charAt(i);
  100.                                 switch(c) {
  101.                                 case '#':
  102.                                         playground.add(new JLabel(wall));
  103.                                         break;
  104.                                 case ' ':
  105.                                         playground.add(new JLabel(blank));
  106.                                         break;
  107.                                 case '.':
  108.                                         playground.add(new JLabel(target));
  109.                                         break;
  110.                                 case '@':
  111.                                         playground.add(new JLabel(mover));
  112.                                         break;
  113.                                 case '+':
  114.                                         playground.add(new JLabel(mover_on_target));
  115.                                         break;
  116.                                 case '$':
  117.                                         playground.add(new JLabel(movable));
  118.                                         break;
  119.                                 case '*':
  120.                                         playground.add(new JLabel(movable_on_target));
  121.                                         break;
  122.                                 default:
  123.                                         playground.add(new JLabel(blank));
  124.                                         break;
  125.                                 }
  126.                 }
  127.                
  128.                 this.playground.revalidate();
  129.         }
  130.  
  131.  
  132.        
  133.        
  134.        
  135.         public static void main(String[] args)  throws Exception{
  136.                 Sokoban frame = new Sokoban();
  137.         frame.setTitle("Sokoban");
  138.         frame.setSize(500, 500);
  139.         frame.setLocationRelativeTo(null);
  140.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  141.         frame.setVisible(true); }
  142. }