Share Pastebin
Guest
Public paste!

sokosoonfinish

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