Game Puzzle

Tugas Besar Kelompok 2 PBO Java
Di ampuh oleh bpk Lilik Trihardanto,S.Si

Coding Puzzle, Class 1

/**
* Title : Class 1 Puzzle
* Deskripsi : Game Puzzle yang di tampilkan di Applet
*/
import java.awt.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Number.*;
import java.applet.*;

public class Puzzle extends Panel implements MouseListener {
private PuzzleItem item[];
private int num[];
private int elemen;private int numOfMove=0;
private int opened1Pos, opened2Pos, openedImageID;
private AudioClip clickClip, finishClip;
private Puzzle8 ep;

public Puzzle(Image image[], int seed, int row, int col,
AudioClip clickClip, AudioClip finishClip, Puzzle8 ep) {
elemen = row * col;
this.ep = ep;
this.clickClip = clickClip;
this.finishClip = finishClip;
scramble(seed, elemen);
item = new PuzzleItem[elemen];
setLayout(new GridLayout(row, col));
int ipos=0, jpos=0;
for (int i = 0; i < elemen; i++) {
if((i!=0) && (i % col == 0)) {
ipos++;
jpos=0;
}
item[i] = new PuzzleItem(image[num[i]], num[i], ipos, jpos);
item[i].addMouseListener(this);
add(item[i]);
jpos++;
}
}
private void scramble(int seed, int elemen) {
num = new int[elemen];

for (int i = 0; inum[i]=-1;
}
int slot=0;
while (slot < num.length) {
int rand = (int)(seed * Math.random());
boolean duplicate = false;
for(int i=0; iif(num[i] == rand) {
duplicate = true;
}
}
if(!duplicate) {
num[slot] = rand;
slot++;
}
}
}
public void mousePressed(MouseEvent e) {

int zeroPos = 0;
PuzzleItem it = (PuzzleItem) e.getSource();

for(int i=0; i < elemen; i++) {
if(it == item[i]) {

int clickedID = item[i].getImageID();
Image clickedImage = item[i].getImage();
if(clickedID != 0) {

for(int t=0; t < elemen; t++) {
if(item[t].getImageID() == 0) {
zeroPos = t;
break;
}
}

int ipos = item[i].getRow();
int jpos = item[i].getCol();

int zipos = item[zeroPos].getRow();
int zjpos = item[zeroPos].getCol();
Image zeroImage = item[zeroPos].getImage();
boolean validMove = false;
if((ipos == zipos) || (jpos == zjpos)) {
if((Math.abs(zipos - ipos) == 1) ||
(Math.abs(zjpos - jpos) == 1)) {
validMove = true;
}
}
if(validMove) {
numOfMove++;
item[i].setImageID(0);
item[i].setImage(zeroImage);
item[i].setRow(ipos);
item[i].setCol(jpos);
item[i].update();
item[zeroPos].setImageID(clickedID);
item[zeroPos].setImage(clickedImage);
item[zeroPos].setRow(zipos);
item[zeroPos].setCol(zjpos);
item[zeroPos].update();

boolean goal = true;

for(int t=0; t < (elemen-1); t++) {
if(item[t].getImageID() != (t+1)) {
goal = false;
break;
}
}
if(!goal) {
clickClip.play();
}
else {
finishClip.play();
ep.setScore("Total Move = " + numOfMove);
for(int t=0; t < elemen; t++) {
item[t].removeMouseListener(this);
}
}
}
}
break;
}
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}


Coding Puzzle8 , Class 2

/**
* Title : Class 2 Puzzle8
* Deskripsi : Game Puzzle yang di tampilkan di Applet
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Puzzle8 extends Applet {
private Image image[];
private String imageFile, imageType;
private String clickSound, finishSound;
private int row, col, imageNum;
private Label title, score;
private Panel puzzle, bottom;
private AudioClip clickClip, finishClip;
public String getAppletInfo() {
return "Java Assix";
}
public String[][] getParameterInfo() {
String[][] info = {
{"imageFile", "string", "Image file name"},
{"clickSound", "string", "Sound if clicked"},
{"finishSound", "string", "Sound if puzzle complete"},
{"imageNum", "int", "Total number of image to be randomed"},
{"row", "int", "Number of row of the puzzle"},
{"col", "int", "Number of col of the puzzle"},
{"imageType", "string", "Type of image file"}
};
return info;
}
public void init() {
imageFile = getParameter("imageFile");
clickSound = getParameter("clickSound");
finishSound = getParameter("finishSound");
imageType = getParameter("imageType");
imageNum = Integer.parseInt(getParameter("imageNum"));
row = Integer.parseInt(getParameter("row"));
col = Integer.parseInt(getParameter("col"));
image = new Image[imageNum];

for (int i = 0; i < imageNum; i++) {
image[i] = getImage(getDocumentBase(),
imageFile + i + "." + imageType);
}
showStatus(imageFile + " loaded...");
repaint();
clickClip = getAudioClip(getDocumentBase(), clickSound);
finishClip = getAudioClip(getDocumentBase(), finishSound);
setLayout(new BorderLayout());
setBackground(Color.white);
setForeground(Color.black);
puzzle = new Puzzle(image, imageNum, row, col,clickClip, finishClip,

this);
bottom = new Panel();
bottom.setLayout(new BorderLayout());
bottom.setBackground(Color.gray);
setFont(new Font("Helvetica", Font.PLAIN, 14));
score = new Label();
score.setForeground(Color.white);
title = new Label(" Java Assix K");
title.setForeground(Color.white);
bottom.add("North", score);
bottom.add("Center", title);
add("Center",puzzle);
add("South", bottom);
}
public void start()
{
repaint();
}
public void stop()
{
repaint();
}
public void setScore(String s)
{
score.setText(s);
repaint();
}
}

Coding PuzzleItem , Class 3

/**
* Title : Class 3 PuzzleItem
* Deskripsi : Game Puzzle yang di tampilkan di Applet
*/
import java.awt.*;
import java.awt.image.*;
public class PuzzleItem extends Panel
{
private Image image;
private int ipos, jpos;
private int imageID;
private String status;
//Constructor PuzzleItem
public PuzzleItem(Image image, int imageID, int ipos, int jpos)
{
this.image = image;
this.imageID = imageID;
this.ipos = ipos;
this.jpos = jpos;

}
public void paint(Graphics g)
{
Rectangle r = getBounds();
g.drawImage(image,0,0,this);
g.setColor(Color.cyan);
g.drawRect(0,0,r.width-1,r.height-1);
}
public int getRow()
{
return ipos;
}
public int getCol()
{
return jpos;
}
public int getImageID()
{
return imageID;
}
public Image getImage()
{
return image;
}
public void setRow(int ipos)
{
this.ipos = ipos;
}
public void setCol(int jpos)
{
this.jpos = jpos;
}
public void setImageID(int id)
{
this.imageID = id;
}
public void setImage(Image image)
{
this.image = image;
}
public void update()
{
repaint();
}
}

Tampilan pada Browser, HTML nya..



Puzzle Game


Puzzle Photo Assix















By Java Assix Kelompok 2






Enjoy the game





Keterangan Game..

Puzzle ini terdiri dari 9 kotak,dimana 8 kotak untuk gambar,1 kotak untuk pemindahan gambar tersebut.Puzzle ini menyusun sebuah potongan foto menjadi foto yang utuh.
Algoritma yang digunakan dalam pembuatan game ini adalah algotirma Transversal,yaitu untuk mencapai goal state (status akhir).
Java merupakan bahasa pemograman berorientasi objek yang mengizinkan pemrograman mengunakan class yang telah ada dan mewarisi sifatnya kepada class dibawahya.Sehingga sifat dan kemampuan yang telah ada pada class sebelumnya tidak perlu diimplementasikan ulang,namun dapat secara langsung. Penurunan class pada Java disebut dengan Inheritance.
Game Puzzle ini dijalankan melalui web browser (Aplikasi Applet pada Java) , maka mutlak program yang dibuat tersebut menurunkan sifat class Applet yang sudah secara build-in disedikan oleh Java.



Komentar

Anonim mengatakan…
nich memang bener programnya...
dah pernah di tes...
andien mengatakan…
makasih udah berkunjung ke blog ku=)
bisa... cobain aja. ne, tugas akhir matkul PBO ku ntar tinggal ganti nama gambarnya aja kok
selamat mencoba=)
Anonim mengatakan…
di save nya dengan nama ap nih?
misal nya.
class1.java
atau yg gmana nih?
kami jalan kan kok ad 6 eror sih di class 1.
trus class ke 2 ada 3 eror.
di class 3 ada 2 eror.
gimana tu mbak?
mohon bimbingan nya.
:)

Postingan populer dari blog ini

I WANNA BE RICH..

Kejutan di Aceh