Advertisement
Dekacc

Knight's tour

Jul 7th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.67 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. public class Main {
  7.  
  8.     static final int TILE = 70;
  9.     static final int EXPLANATION = 40;
  10.     static final int NEWGAMEHEIGHT = 40;
  11.     static final int NEWGAMEWIDTH = 130;
  12.     static final int MARGIN = 5;
  13.     static final int WIDTH = 8*TILE + 2 * MARGIN + 6 + 8;
  14.     static final int HEIGHT = 8*TILE + 30 + 4*MARGIN + NEWGAMEHEIGHT + 8 + EXPLANATION;
  15.     static int ypos, xpos, counter;
  16.     static JFrame frame;
  17.     static JButton[][] tiles = new JButton[8][8];
  18.     static JLabel points;
  19.  
  20.     static void reset(){
  21.         for(int i = 0; i < 8; i++){
  22.             for(int j = 0; j < 8; j++){
  23.                 tiles[i][j].setText("");
  24.                 if((i + j) % 2 == 0){
  25.                     tiles[i][j].setBackground(new Color(235, 221, 156));
  26.                 }
  27.                 else{
  28.                     tiles[i][j].setBackground(new Color(131, 81, 54));
  29.                 }
  30.             }
  31.         }
  32.         tiles[0][0].setText("K");
  33.         tiles[0][0].setBackground(new Color(223, 73, 66));
  34.         ypos = 0;
  35.         xpos = 0;
  36.         counter = 1;
  37.         points.setText("1");
  38.     }
  39.  
  40.     static boolean checkLoser(){
  41.         if (xpos - 2 >= 0 && ypos - 1 >= 0) {
  42.             if (!tiles[xpos - 2][ypos - 1].getText().equals("  ")) {
  43.                 return false;
  44.             }
  45.         }
  46.         if (xpos - 2 >= 0 && ypos + 1 < 8) {
  47.             if (!tiles[xpos - 2][ypos + 1].getText().equals("  ")) {
  48.                 return false;
  49.             }
  50.         }
  51.         if (xpos - 1 >= 0 && ypos - 2 >= 0) {
  52.             if (!tiles[xpos - 1][ypos - 2].getText().equals("  ")) {
  53.                 return false;
  54.             }
  55.         }
  56.         if (xpos - 1 >= 0 && ypos + 2 < 8) {
  57.             if (!tiles[xpos - 1][ypos + 2].getText().equals("  ")) {
  58.                 return false;
  59.             }
  60.         }
  61.         if (xpos + 1 < 8 && ypos - 2 >= 0) {
  62.             if (!tiles[xpos + 1][ypos - 2].getText().equals("  ")) {
  63.                 return false;
  64.             }
  65.         }
  66.         if (xpos + 1 < 8 && ypos + 2 < 8) {
  67.             if (!tiles[xpos + 1][ypos + 2].getText().equals("  ")) {
  68.                 return false;
  69.             }
  70.         }
  71.         if (xpos + 2 < 8 && ypos - 1 >= 0) {
  72.             if (!tiles[xpos + 2][ypos - 1].getText().equals("  ")) {
  73.                 return false;
  74.             }
  75.         }
  76.         if (xpos + 2 < 8 && ypos + 1 < 8) {
  77.             if (!tiles[xpos + 2][ypos + 1].getText().equals("  ")) {
  78.                 return false;
  79.             }
  80.         }
  81.         return true;
  82.     }
  83.  
  84.     static void loser(){
  85.         JOptionPane.showMessageDialog(frame, "You have no more legal moves! Score: " + counter, "GAME OVER!", JOptionPane.INFORMATION_MESSAGE);
  86.         reset();
  87.     }
  88.  
  89.     static void winner(){
  90.         JOptionPane.showMessageDialog(frame, "Congratulations, you win! Score: 64", "YOU WIN!", JOptionPane.INFORMATION_MESSAGE);
  91.         reset();
  92.     }
  93.  
  94.     static boolean legalMove(int x, int y, int nx, int ny){
  95.         if((nx == x-1 && ny == y-2)||(nx == x+1 && ny == y-2)||(nx == x-2 && ny == y-1)||(nx == x+2 && ny == y-1)||(nx == x-2 && ny == y+1)||(nx == x+2 && ny == y+1)||(nx == x-1 && ny == y+2)||(nx == x+1 && ny == y+2)){
  96.             return true;
  97.         }
  98.         return false;
  99.     }
  100.  
  101.     public static void main(String[] args){
  102.         frame = new JFrame("Knight's tour");
  103.         frame.setSize(WIDTH, HEIGHT);
  104.         frame.setResizable(false);
  105.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  106.         frame.setLocationRelativeTo(null);
  107.  
  108.         JPanel panel = new JPanel();
  109.         panel.setLayout(null);
  110.         panel.setBackground(new Color(69, 43, 29));
  111.         frame.add(panel);
  112.  
  113.         //EXPLANATION
  114.         JLabel exp = new JLabel("Guide the knight (K) to every square only once.", SwingConstants.CENTER);
  115.         exp.setFont(new Font("Arial", Font.BOLD, 20));
  116.         exp.setForeground(new Color(231, 215, 137));
  117.         exp.setSize(8*TILE + 7, EXPLANATION);
  118.         exp.setLocation(MARGIN, MARGIN);
  119.         panel.add(exp);
  120.  
  121.         //POINTS LABEL
  122.         points = new JLabel("1", SwingConstants.RIGHT);
  123.         points.setFont(new Font("Arial", Font.BOLD, 30));
  124.         points.setForeground(new Color(231, 215, 137));
  125.         points.setSize(50, NEWGAMEHEIGHT);
  126.         points.setLocation(WIDTH - MARGIN - 56, EXPLANATION + 3 * MARGIN + 8 * (TILE + 1));
  127.         panel.add(points);
  128.  
  129.         //SETTING UP THE BOARD
  130.         for(int i = 0; i < 8; i++){
  131.             for(int j = 0; j < 8; j++){
  132.                 tiles[i][j] = new JButton();
  133.                 tiles[i][j].setLocation(MARGIN + i * (TILE + 1), EXPLANATION + 2 * MARGIN + j * (TILE + 1));
  134.                 tiles[i][j].setSize(TILE, TILE);
  135.                 tiles[i][j].setFont(new Font("Arial", Font.BOLD, 25));
  136.                 tiles[i][j].setFocusPainted(false);
  137.                 tiles[i][j].setBorder(null);
  138.                 panel.add(tiles[i][j]);
  139.             }
  140.         }
  141.         reset();
  142.  
  143.         //NEW GAME BUTTON
  144.         JButton newGame = new JButton("New game");
  145.         newGame.setSize(NEWGAMEWIDTH, NEWGAMEHEIGHT);
  146.         newGame.setLocation(WIDTH / 2 - NEWGAMEWIDTH / 2 - 3, EXPLANATION + 3 * MARGIN + 8 * (TILE + 1));
  147.         newGame.setFocusPainted(false);
  148.         newGame.setBackground(new Color(231, 215, 137));
  149.         newGame.setBorder(null);
  150.         panel.add(newGame);
  151.  
  152.         newGame.addActionListener(new ActionListener() {
  153.             @Override
  154.             public void actionPerformed(ActionEvent e) {
  155.                 reset();
  156.             }
  157.         });
  158.  
  159.         //GAMEPLAY
  160.         for(int i = 0; i < 8; i++){
  161.             for(int j = 0; j < 8; j++){
  162.                 tiles[i][j].addActionListener(new ActionListener() {
  163.                     @Override
  164.                     public void actionPerformed(ActionEvent e) {
  165.                         JButton button = (JButton) e.getSource();
  166.                         if(!button.getText().equals("K") && !button.getText().equals("  ")){
  167.                             button.setText(" ");
  168.                             int tmpx = -10, tmpy = -10;
  169.                             if (xpos - 2 >= 0 && ypos - 1 >= 0) {
  170.                                 if (tiles[xpos - 2][ypos - 1].getText().equals(" ")) {
  171.                                     tmpy = ypos - 1;
  172.                                     tmpx = xpos - 2;
  173.                                 }
  174.                             }
  175.                             if (xpos - 2 >= 0 && ypos + 1 < 8) {
  176.                                 if (tiles[xpos - 2][ypos + 1].getText().equals(" ")) {
  177.                                     tmpy = ypos + 1;
  178.                                     tmpx = xpos - 2;
  179.                                 }
  180.                             }
  181.                             if (xpos - 1 >= 0 && ypos - 2 >= 0) {
  182.                                 if (tiles[xpos - 1][ypos - 2].getText().equals(" ")) {
  183.                                     tmpy = ypos - 2;
  184.                                     tmpx = xpos - 1;
  185.                                 }
  186.                             }
  187.                             if (xpos - 1 >= 0 && ypos + 2 < 8) {
  188.                                 if (tiles[xpos - 1][ypos + 2].getText().equals(" ")) {
  189.                                     tmpy = ypos + 2;
  190.                                     tmpx = xpos - 1;
  191.                                 }
  192.                             }
  193.                             if (xpos + 1 < 8 && ypos - 2 >= 0) {
  194.                                 if (tiles[xpos + 1][ypos - 2].getText().equals(" ")) {
  195.                                     tmpy = ypos - 2;
  196.                                     tmpx = xpos + 1;
  197.                                 }
  198.                             }
  199.                             if (xpos + 1 < 8 && ypos + 2 < 8) {
  200.                                 if (tiles[xpos + 1][ypos + 2].getText().equals(" ")) {
  201.                                     tmpy = ypos + 2;
  202.                                     tmpx = xpos + 1;
  203.                                 }
  204.                             }
  205.                             if (xpos + 2 < 8 && ypos - 1 >= 0) {
  206.                                 if (tiles[xpos + 2][ypos - 1].getText().equals(" ")) {
  207.                                     tmpy = ypos - 1;
  208.                                     tmpx = xpos + 2;
  209.                                 }
  210.                             }
  211.                             if (xpos + 2 < 8 && ypos + 1 < 8) {
  212.                                 if (tiles[xpos + 2][ypos + 1].getText().equals(" ")) {
  213.                                     tmpy = ypos + 1;
  214.                                     tmpx = xpos + 2;
  215.                                 }
  216.                             }
  217.                             if (legalMove(xpos, ypos, tmpx, tmpy)) {
  218.                                 button.setBackground(new Color(223, 73, 66));
  219.                                 button.setText("K");
  220.                                 counter++;
  221.                                 tiles[xpos][ypos].setText("  ");
  222.                                 points.setText(Integer.toString(counter));
  223.                                 ypos = tmpy;
  224.                                 xpos = tmpx;
  225.                                 if(counter == 64){
  226.                                     winner();
  227.                                 }
  228.                                 if(checkLoser()){
  229.                                     loser();
  230.                                 }
  231.                             } else {
  232.                                 button.setText("");
  233.                             }
  234.                         }
  235.                     }
  236.                 });
  237.             }
  238.         }
  239.         frame.setVisible(true);
  240.     }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement