Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class Main {
- static final int TILE = 70;
- static final int EXPLANATION = 40;
- static final int NEWGAMEHEIGHT = 40;
- static final int NEWGAMEWIDTH = 130;
- static final int MARGIN = 5;
- static final int WIDTH = 8*TILE + 2 * MARGIN + 6 + 8;
- static final int HEIGHT = 8*TILE + 30 + 4*MARGIN + NEWGAMEHEIGHT + 8 + EXPLANATION;
- static int ypos, xpos, counter;
- static JFrame frame;
- static JButton[][] tiles = new JButton[8][8];
- static JLabel points;
- static void reset(){
- for(int i = 0; i < 8; i++){
- for(int j = 0; j < 8; j++){
- tiles[i][j].setText("");
- if((i + j) % 2 == 0){
- tiles[i][j].setBackground(new Color(235, 221, 156));
- }
- else{
- tiles[i][j].setBackground(new Color(131, 81, 54));
- }
- }
- }
- tiles[0][0].setText("K");
- tiles[0][0].setBackground(new Color(223, 73, 66));
- ypos = 0;
- xpos = 0;
- counter = 1;
- points.setText("1");
- }
- static boolean checkLoser(){
- if (xpos - 2 >= 0 && ypos - 1 >= 0) {
- if (!tiles[xpos - 2][ypos - 1].getText().equals(" ")) {
- return false;
- }
- }
- if (xpos - 2 >= 0 && ypos + 1 < 8) {
- if (!tiles[xpos - 2][ypos + 1].getText().equals(" ")) {
- return false;
- }
- }
- if (xpos - 1 >= 0 && ypos - 2 >= 0) {
- if (!tiles[xpos - 1][ypos - 2].getText().equals(" ")) {
- return false;
- }
- }
- if (xpos - 1 >= 0 && ypos + 2 < 8) {
- if (!tiles[xpos - 1][ypos + 2].getText().equals(" ")) {
- return false;
- }
- }
- if (xpos + 1 < 8 && ypos - 2 >= 0) {
- if (!tiles[xpos + 1][ypos - 2].getText().equals(" ")) {
- return false;
- }
- }
- if (xpos + 1 < 8 && ypos + 2 < 8) {
- if (!tiles[xpos + 1][ypos + 2].getText().equals(" ")) {
- return false;
- }
- }
- if (xpos + 2 < 8 && ypos - 1 >= 0) {
- if (!tiles[xpos + 2][ypos - 1].getText().equals(" ")) {
- return false;
- }
- }
- if (xpos + 2 < 8 && ypos + 1 < 8) {
- if (!tiles[xpos + 2][ypos + 1].getText().equals(" ")) {
- return false;
- }
- }
- return true;
- }
- static void loser(){
- JOptionPane.showMessageDialog(frame, "You have no more legal moves! Score: " + counter, "GAME OVER!", JOptionPane.INFORMATION_MESSAGE);
- reset();
- }
- static void winner(){
- JOptionPane.showMessageDialog(frame, "Congratulations, you win! Score: 64", "YOU WIN!", JOptionPane.INFORMATION_MESSAGE);
- reset();
- }
- static boolean legalMove(int x, int y, int nx, int ny){
- 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)){
- return true;
- }
- return false;
- }
- public static void main(String[] args){
- frame = new JFrame("Knight's tour");
- frame.setSize(WIDTH, HEIGHT);
- frame.setResizable(false);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- JPanel panel = new JPanel();
- panel.setLayout(null);
- panel.setBackground(new Color(69, 43, 29));
- frame.add(panel);
- //EXPLANATION
- JLabel exp = new JLabel("Guide the knight (K) to every square only once.", SwingConstants.CENTER);
- exp.setFont(new Font("Arial", Font.BOLD, 20));
- exp.setForeground(new Color(231, 215, 137));
- exp.setSize(8*TILE + 7, EXPLANATION);
- exp.setLocation(MARGIN, MARGIN);
- panel.add(exp);
- //POINTS LABEL
- points = new JLabel("1", SwingConstants.RIGHT);
- points.setFont(new Font("Arial", Font.BOLD, 30));
- points.setForeground(new Color(231, 215, 137));
- points.setSize(50, NEWGAMEHEIGHT);
- points.setLocation(WIDTH - MARGIN - 56, EXPLANATION + 3 * MARGIN + 8 * (TILE + 1));
- panel.add(points);
- //SETTING UP THE BOARD
- for(int i = 0; i < 8; i++){
- for(int j = 0; j < 8; j++){
- tiles[i][j] = new JButton();
- tiles[i][j].setLocation(MARGIN + i * (TILE + 1), EXPLANATION + 2 * MARGIN + j * (TILE + 1));
- tiles[i][j].setSize(TILE, TILE);
- tiles[i][j].setFont(new Font("Arial", Font.BOLD, 25));
- tiles[i][j].setFocusPainted(false);
- tiles[i][j].setBorder(null);
- panel.add(tiles[i][j]);
- }
- }
- reset();
- //NEW GAME BUTTON
- JButton newGame = new JButton("New game");
- newGame.setSize(NEWGAMEWIDTH, NEWGAMEHEIGHT);
- newGame.setLocation(WIDTH / 2 - NEWGAMEWIDTH / 2 - 3, EXPLANATION + 3 * MARGIN + 8 * (TILE + 1));
- newGame.setFocusPainted(false);
- newGame.setBackground(new Color(231, 215, 137));
- newGame.setBorder(null);
- panel.add(newGame);
- newGame.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- reset();
- }
- });
- //GAMEPLAY
- for(int i = 0; i < 8; i++){
- for(int j = 0; j < 8; j++){
- tiles[i][j].addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JButton button = (JButton) e.getSource();
- if(!button.getText().equals("K") && !button.getText().equals(" ")){
- button.setText(" ");
- int tmpx = -10, tmpy = -10;
- if (xpos - 2 >= 0 && ypos - 1 >= 0) {
- if (tiles[xpos - 2][ypos - 1].getText().equals(" ")) {
- tmpy = ypos - 1;
- tmpx = xpos - 2;
- }
- }
- if (xpos - 2 >= 0 && ypos + 1 < 8) {
- if (tiles[xpos - 2][ypos + 1].getText().equals(" ")) {
- tmpy = ypos + 1;
- tmpx = xpos - 2;
- }
- }
- if (xpos - 1 >= 0 && ypos - 2 >= 0) {
- if (tiles[xpos - 1][ypos - 2].getText().equals(" ")) {
- tmpy = ypos - 2;
- tmpx = xpos - 1;
- }
- }
- if (xpos - 1 >= 0 && ypos + 2 < 8) {
- if (tiles[xpos - 1][ypos + 2].getText().equals(" ")) {
- tmpy = ypos + 2;
- tmpx = xpos - 1;
- }
- }
- if (xpos + 1 < 8 && ypos - 2 >= 0) {
- if (tiles[xpos + 1][ypos - 2].getText().equals(" ")) {
- tmpy = ypos - 2;
- tmpx = xpos + 1;
- }
- }
- if (xpos + 1 < 8 && ypos + 2 < 8) {
- if (tiles[xpos + 1][ypos + 2].getText().equals(" ")) {
- tmpy = ypos + 2;
- tmpx = xpos + 1;
- }
- }
- if (xpos + 2 < 8 && ypos - 1 >= 0) {
- if (tiles[xpos + 2][ypos - 1].getText().equals(" ")) {
- tmpy = ypos - 1;
- tmpx = xpos + 2;
- }
- }
- if (xpos + 2 < 8 && ypos + 1 < 8) {
- if (tiles[xpos + 2][ypos + 1].getText().equals(" ")) {
- tmpy = ypos + 1;
- tmpx = xpos + 2;
- }
- }
- if (legalMove(xpos, ypos, tmpx, tmpy)) {
- button.setBackground(new Color(223, 73, 66));
- button.setText("K");
- counter++;
- tiles[xpos][ypos].setText(" ");
- points.setText(Integer.toString(counter));
- ypos = tmpy;
- xpos = tmpx;
- if(counter == 64){
- winner();
- }
- if(checkLoser()){
- loser();
- }
- } else {
- button.setText("");
- }
- }
- }
- });
- }
- }
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement