Yasin DEMİR

#herseypaylasmakicin

Puzzle Game

Project Definition: Use JFrame or JApplet and implement a Puzzle game such that, it allows user to select an image from the file system and gets input about the size of tiles.The game should inform the user when he or she completes the puzzle.
Deliverables: Full source code, Documentation.

 

Download here : Source code and documentation of the Puzzle Game 

 

———————————————————————————————————

PRESENTATION OF PUZZLE GAME (WITH GUI)

 

Step1: Welcome message to Puzzle Game.

 

Step 2: Main Menu to Puzzle Game. This here have a choose picture, puzzle types (3, 5, 6, 7 e.g.) and mix&start button from Main.

Step3: Press to browse button at Main Menu provide a choose picture with file system.

 

 

 

 

 

 

Step4: After selecting picture, choose puzzle types (3, 4, 5, 6, e.g.) from Combo box.


 

Step5: After that press the original picture button will be show the Original picture.


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Step6: After that press the Mixed Start button will be mixing picture and starting Puzzle Game.


 

 

 

 

 

 

 

 

 

 

 

 

 

 

Step 7: Puzzle Game finished or its exit, again come back the Main Menu. We will again choose picture and puzzle types, after we will start the new Puzzle Game.


 

ENJOY OF THE PUZZLE GAME   🙂


PUZZLE GAME

PUZZLE GAME SOURCE CODE AND CODE DEFINITIONS

 

Project Definitions: Use JFrame or JApplet and implement a Puzzle game such that, it allows user to select an image from the file system and gets input about the size of tiles. The game should inform the user when he or she completes the puzzle. Deliverables: Full source code, Documentation.

Request to us;

1-      JFrame or JApplet make use Puzzle Game

2-      Puzzle Game picture’ choose with file system

3-      Determine the Puzzle Game Types’ (3,5,6 e.g. tiles)

4-      Information of Puzzle Game Users

Description the written codes and Puzzle Game definitions;

 

 

String values[] = {“3”, “4”, “5”,”6″,”7″,”8″,”9″,”10″};                    TABLE 1String path = “”;JLabel selectpicture = new JLabel(“Select Puzzle Picture”);JLabel selecttype = new JLabel(“Select Puzzle Types”);JComboBox combo1 = new JComboBox(values);JButton startbtn = new JButton(“Start”);JLabel l1 = new JLabel();JLabel l2 = new JLabel();JButton button[][];private Image source;private Image image;

int width, height;

int n =5;

 

TABLE 1: Firstly, I make combo box and label buttons definitions for Main Menu. At the same time take picture will be change the icon definitions of variables. I give a puzzle types for Puzzle Game.

PuzzleGame(){        TABLE 2JOptionPane.showMessageDialog(null, “WELCOME TO THE PUZZLE GAME”+”\n”);JButton Browse = new JButton(“Browse”);Browse.addActionListener( new ActionListener() {public void actionPerformed(ActionEvent arg0) {JFileChooser fc = new JFileChooser();fc.showOpenDialog(null);path=fc.getSelectedFile().toString();}});

 

TABLE 2: Write a message starting at the Puzzle Game. This is a meeting for Puzzle Game. After that, we see some an example PropTest in the lesson, make a Browse button and use to this way (JFileChooser) choose picture and added with file system. So; I take picture and its variable equal “path”.

TABLE 3this.setVisible(true);this.setSize( new Dimension(300, 100) );this.setTitle(“PUZZLE GAME by ySndmr”);this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);this.setLocationRelativeTo(null);this.setLayout( new GridLayout(3,3,1,1) );this.add( selectpicture);this.add(l1);add(Browse);this.add( selecttype);

this.add(l2);

this.add(combo1);

this.add(startbtn);

Browse.addActionListener( this );

startbtn.addActionListener( this );

combo1.addActionListener(new CbLsnr());

pack();

}

 

TABLE 3: Here, I make buttons added on Grid layout to in the Main Menu. (Added Main Menu size, Main Menu is in the center at Pc screen, added JDialog.DISPOSE_ON_CLOSE for not lost from screen)Browse, start buttons and combo box assign for Action Listener. I use Pack() for good GUI.

public class CbLsnr implements ActionListener{                          TABLE 4public void actionPerformed(ActionEvent e) {JComboBox combo = (JComboBox)e.getSource();String selected;selected = (String) combo.getSelectedItem();n=Integer.parseInt(selected)+2;}}

 

TABLE 4: I take choose variable from Combo box action listener’. This value added 2 and equal n variable. This reason Puzzle Game window buttons’ to be enable (false) ( button[2][2].setEnabled(false);). I will explained later in this subject

public void actionPerformed(ActionEvent arg0) {                           TABLE 5if ( arg0.getSource().equals(startbtn)){JFrame slidePuzzle = new JFrame();ImageIcon imic = new ImageIcon(path);button=new JButton[n][n];slidePuzzle.setLayout(new GridLayout(n,n));slidePuzzle.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);slidePuzzle.setVisible(true);slidePuzzle.setSize(imic.getIconWidth(),imic.getIconHeight());slidePuzzle.setTitle(“PUZZLE GAME”);slidePuzzle.setLocationRelativeTo(null);

 

TABLE 5: Start button to Action listener, press to start button take a previously and assigned keep to path picture define as icon. I define an array buttons (at Puzzle Game Window) and throw in the Grid layout. I give a title Puzzle Game’ and it’s put the center in the page.

for(int i = 0 ; i<n;i++){                                              TABLE 6for(int j = 0;j<n;j++){button[i][j]=new JButton();button[i][j].addActionListener(new actionlistener());slidePuzzle.add(button[i][j]);}}button[2][2].setText(“5555”);button[2][2].setEnabled(false);for(int i = 0 ; i<n;i++){

for(int j = 0;j<n;j++){

if(i==0 || i==n-1){

button[i][j].setEnabled(false);

button[i][j].setBackground(Color.BLACK);

}

if(j==0 || j==n-1){

button[i][j].setEnabled(false);

button[i][j].setBackground(Color.BLACK);

}

}

}

 

TABLE 6: I define a previously make a buttons to button array. Puzzle Game make a buttons all of them and outmost buttons is enable (false).Because, my write such puzzle game algorithm. I assigned changeable button is define empty and its button is coordinate a 2.2. I assigned color background of buttons.

source = imic.getImage();                                               TABLE 7width = imic.getIconWidth();height = imic.getIconHeight();for ( int i = 0; i < n; i++) {for ( int j = 0; j < n; j++) {image = createImage(new FilteredImageSource(source.getSource(),new CropImageFilter(j*width/(n), i*height/n, (width/(n))+1, height/n)));button[i][j].setIcon(new ImageIcon(image));}      }}}

 

TABLE 7: This here source code is take a before picture and define as icon for translate, make e crop image of source code. This code I bought from Java Book.  Therefore, These are to be show a similarities.

class actionlistener implements ActionListener{                           TABLE 8public void actionPerformed(ActionEvent e) {JButton BtnConnect=(JButton)(e.getSource());Dimension size = BtnConnect.getSize();ImageIcon icon =new ImageIcon();int XAxis = BtnConnect.getX();int YAxis = BtnConnect.getY();int x = XAxis / size.width;int y = YAxis / size.height;int row = x;x = y;

y=row;

 

TABLE 8: I write it previously tell about descriptions of my algorithms. This here buttons keep the address of button array’ and keep icons and descriptions of necessary coordinate and define are variables. It’s were of an X and Y coordinate-axis. These are necessary for my Puzzle Game Algorithms.

if(button[x+1][y].getText()==”5555″){                                     TABLE 9button[x][y].setText(button[x+1][y].getText());button[x+1][y].setText(“”);button [x][y].setEnabled(false);icon= (ImageIcon) button [x][y].getIcon();button[x][y].setIcon(button [x+1][y].getIcon());button[x+1][y].setIcon(icon);button[x+1][y].setEnabled(true);}elseif(button[x-1][y].getText()==”5555″){button[x][y].setText(button[x-1][y].getText());

button[x-1][y].setText(“”);

button [x][y].setEnabled(false);

icon= (ImageIcon) button [x][y].getIcon();

button[x][y].setIcon(button [x-1][y].getIcon());

button[x-1][y].setIcon(icon);

button[x-1][y].setEnabled(true);

}else

if(button[x][y-1].getText()==”5555″){

button[x][y].setText(button[x][y-1].getText());

button[x][y-1].setText(“”);

button [x][y].setEnabled(false);

icon= (ImageIcon) button [x][y].getIcon();

button[x][y].setIcon(button [x][y-1].getIcon());

button[x][y-1].setIcon(icon);

button[x][y-1].setEnabled(true);

}

}

}

}

}

 

TABLE 9: My algorithm is a very simple and very useful for Puzzle Game. I created a complete buttons in the Puzzle Game Window. Changeable buttons are one by one check it moves the right, left, up, down aspects.  Clicking one of these, after swap an icons betweens are with clicked button and changeable button. This way provides moves are buttons. I create outmost buttons are enable (false), because not existing error at corner buttons and side buttons. I passed in front of the make error with this way. In my opinion, this way is practice resolution and a useful method. Good visualization, interesting design.

Bir cevap yazın

E-posta hesabınız yayımlanmayacak.