71
KODING PROGRAM
1. Wordladder.php
if (preg_match("/[^a-z]/", $kataawal) || preg_match("/[^a-z]/", $katatarget)) { die("Kata harus menggunakan karakter alfabet\n"); } if (strlen($kataawal) < 4) { die("Kata awal sedikitnya terdiri dari 4 karakter"); } if (strlen($kataawal) > 6) { die("Kamus tidak tersedia untuk panjang karakter lebih dari 6"); } if (strlen($kataawal) != strlen($katatarget)) { print "Panjang Kata Awal Harus Sama Dengan Panjang Kata Target\n"; } $thelength = strlen($kataawal); @$q = mysql_query("SELECT kata FROM kata WHERE panjang_kata = '$thelength'") or die(mysql_error()); while($kata= mysql_fetch_array($q)){ $words[] = $kata['kata']; } foreach ($words as $key => $word) { $words[$key] = trim(strtolower($word));//mengubah karakter menjadi huruf kecil } if (!count($words)) { die("Kata tidak ada dalam database"); } try { $steps = array(); //array untuk menyimpan langkah yang menuju ke target $ladder = new wordladder($kataawal); $ladder::$kataawal = $kataawal; //inisialisasi kata awal $ladder::$katatarget = $katatarget; //inisialisasi kata target $ladder->solve(); //jika langkah tidak kosong, maka berhasil if (count($steps)) {
Universitas Sumatera Utara
72
print join("->", array_reverse($steps)); } else { print 'Tidak Ada Solusi.'; } } catch (Exception $e) { print 'Error ' . $e->getMessage(); } print "\n"; class wordladder { public public public public public
$word; $matches = array(); $myparent = null; static $kataawal; static $katatarget;
public function __construct($word, $mp=null) { $this->word = trim(strtolower($word)); $this->myparent = $mp; } public function solve() { global $words; global $steps; foreach ($words as $key => $word) { if ($this->isAmatch($word)) { $this->matches[] = $word; unset($words[$key]); } } if (in_array(wordladder::$katatarget, $this->matches)) { $steps[] = wordladder::$katatarget; $this->addStep(); } else { if (count($this->matches)) { foreach ($this->matches as $match) { $wordmatch = new wordladder($match, $this); $wordmatch->solve(); } } } } protected function isAmatch($aword) { $matchcount = 0; for ($i = 0; $i < strlen($this->word); $i++) { if ($this->word[$i] == $aword[$i]) { $matchcount++; } } if ($matchcount == strlen($this->word) - 1) { return true; } return false; } public function addStep() {
Universitas Sumatera Utara
73
global $steps; $steps[] = $this->word; if ($this->myparent instanceof wordladder) { $this->myparent->addStep(); } } }
Universitas Sumatera Utara
74
2. Game.java package word.ladder; import java.util.ArrayList; import import import import import import import import import import import import import import import import import import import import import import import import import import import import
android.app.Activity; android.app.AlertDialog; android.content.Context; android.content.DialogInterface; android.content.Intent; android.graphics.Color; android.os.Bundle; android.os.CountDownTimer; android.view.Gravity; android.view.LayoutInflater; android.view.Menu; android.view.MenuInflater; android.view.MenuItem; android.view.View; android.view.View.OnClickListener; android.view.ViewGroup; android.view.ViewGroup.LayoutParams; android.widget.AdapterView; android.widget.AdapterView.OnItemClickListener; android.widget.BaseAdapter; android.widget.Button; android.widget.GridView; android.widget.LinearLayout; android.widget.ScrollView; android.widget.TableLayout; android.widget.TableRow; android.widget.TextView; android.widget.Toast;
public class Game extends Activity { private ArrayList<String> items; private GridViewAdapter adapter; ScrollView scrollView; GridView gridView; String hasil = null; int columnPadaGridview; String soal, target; boolean mulai; int waktu = 0; TextView viewWaktu; /* untuk alert builder solusi */ String soalAwal; String textYangDiSelcect = ""; int posisiTextYangDiSelect; Koneksi koneksi = new Koneksi();
Universitas Sumatera Utara
75
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game); Bundle extras = getIntent().getExtras(); String soalMentah = extras.getString("soal"); /* soal pertama kali dijalankan */ String[] soalArray = new String[2]; soalArray = soalMentah.split(" - "); soal = soalArray[0]; target = soalArray[1].trim(); soalAwal = soal; /* jumlah column dipanggil dari activity level */ columnPadaGridview = soal.length(); /* Setting waktu */ if (columnPadaGridview == 4) { waktu = 120000; } else if (columnPadaGridview == 5) { waktu = 180000; } else if (columnPadaGridview == 6) { waktu = 240000; } viewWaktu = (TextView) findViewById(R.id.viewWaktu); viewWaktu.setText(String.valueOf(waktu)); final MyCounter timer = new MyCounter(waktu, 1000); timer.start(); String[] targetYangDiTampilkan = target.split(""); /* table untuk menampilkan target */ TableLayout tableTarget = (TableLayout) findViewById(R.id.tableTarget); TableRow rowTarget = new TableRow(this); rowTarget.setGravity(Gravity.CENTER); for (int i = 0; i <= columnPadaGridview; i++) { TextView viewTarget = new TextView(this); viewTarget.setText(String.valueOf(targetYangDiTampilkan[i])); viewTarget.setBackgroundColor(Color.BLUE); viewTarget.setPadding(10, 10, 10, 10); rowTarget.addView(viewTarget); } tableTarget.addView(rowTarget, new TableLayout.LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); Toast.makeText(Game.this, String.valueOf(soalMentah), Toast.LENGTH_LONG).show(); /* adapater */ items = new ArrayList<String>();
Universitas Sumatera Utara
76
adapter = new GridViewAdapter(this); gridView = (GridView) findViewById(R.id.gridView); // set jumlah kolom grid gridView.setNumColumns(columnPadaGridview); gridView.setAdapter(adapter); gridView.setDrawSelectorOnTop(true); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView> av, View v, int position,long id) { textYangDiSelcect = String.valueOf(adapter.getItem(position)); posisiTextYangDiSelect = position; if (position > ((adapter.getCount() columnPadaGridview) - 1)) { v.setPressed(true); } else { v.setBackgroundColor(Color.BLACK); } } });
/* inisialisasi saat mulai main */ int counterMulaiMain = 0; if (counterMulaiMain == 0) { String[] textSoal = soal.trim().toUpperCase().split(""); /* add jumlah item pada gridview sekali klik */ for (int i = 1; i < textSoal.length; i++) { items.add(textSoal[i]); adapter.notifyDataSetChanged(); counterMulaiMain++; posisiTextYangDiSelect = -1; } } scrollView = (ScrollView) findViewById(R.id.scrollView1); /* multiple button */ LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1); int count = 0; for (int i = 0; i < 3; i++) { LinearLayout row = new LinearLayout(this); row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); for (int j = 0; j < 9; j++) { if (count < 26) { final Button btnTag = new Button(this);
Universitas Sumatera Utara
77
android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f); btnTag.setLayoutParams(params); char abjad = (char) (count + 'A'); btnTag.setText(String.valueOf(abjad)); btnTag.setId(count); row.addView(btnTag); btnTag.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (posisiTextYangDiSelect > ((adapter.getCount() columnPadaGridview) - 1)) { String textYangDiKlik = btnTag.getText().toString(); String[] soalSebelemunya = soal.split(""); String soalBaru = ""; if (items.size() >= columnPadaGridview) { for (int i = 0; i < soalSebelemunya.length; i++) { if ((posisiTextYangDiSelect % columnPadaGridview) == (i - 1)) soalBaru = soalBaru + textYangDiKlik; else soalBaru = soalBaru + soalSebelemunya[i]; } } /* cek apakah kata yang baru ada dalam database */ String hasilCekDatabase = koneksi.cekKata(soalBaru); if (!hasilCekDatabase.trim().equals("1")) { Toast.makeText(Game.this,soalBaru.toUpperCase() + " Tidak Ada Dalam Daftar Kata",Toast.LENGTH_LONG).show(); return; } /* memastikkan kata yang baru tidak kosong */ if (!soalBaru.equals("")) { soal = soalBaru; } else if(!soalBaru.equals(soal)){ soalAwal = soalBaru; } String[] textSoal = soal.trim().toUpperCase().split(""); /* add jumlah item pada gridview sekali klik */ for (int i = 1; i < textSoal.length; i++) { items.add(textSoal[i]); adapter.notifyDataSetChanged(); } scrollMyListViewToBottom();
Universitas Sumatera Utara
78
/* Sukses */ if (target.equals(soalBaru)) { timer.cancel(); viewWaktu.setFreezesText(true); Score.setScore((adapter.getCount()/columnPadaGridview1)*50+Integer.parseInt(viewWaktu.getText().toString())); AlertDialog.Builder adb = new AlertDialog.Builder(Game.this); adb.setTitle("Kamu Berhasil, Selamat!"); adb.setMessage("Score " + Score.getNama()+ " : " + Score.getScore()); adb.setNegativeButton("Tutup",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0,int arg1) { Intent intent = new Intent(Game.this, Level.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } }); adb.show(); } } else { Toast.makeText(Game.this,"Klik Salah Satu Huruf Pada Kotak Merah", Toast.LENGTH_LONG).show(); } } }); } count++; } layout.addView(row); } } /* scroll berdasarkan jumlah item yang diinginkan */ private void scrollMyListViewToBottom() { gridView.post(new Runnable() { public void run() { gridView.setSelection(adapter.getCount() - 4); } }); } public class GridViewAdapter extends BaseAdapter { public GridViewAdapter(Context c) { } public int getCount() { return items.size(); } public Object getItem(int position) { return items.get(position); } public long getItemId(int position) { return position; }
Universitas Sumatera Utara
79
public View getView(int position, View convertView, ViewGroup parent) { View v; if (convertView == null) { LayoutInflater li = getLayoutInflater(); v = li.inflate(R.layout.grid_item, null); } else { v = convertView; } //memastikan warna background hitam saat ada penambahan view di gridview if (position > ((adapter.getCount() - columnPadaGridview) - 1)) { v.setBackgroundColor(Color.RED); } else { v.setBackgroundColor(Color.BLACK); } TextView tv1 = (TextView) v.findViewById(R.id.view_item); tv1.setText(items.get(position)); return v; } } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_utama: Intent intent = new Intent(this, MainMenu.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); return true; case R.id.menu_level: Intent intent2 = new Intent(this, Level.class); intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent2); return true; case R.id.hapus: items.clear(); String[] textSoal = soalAwal.trim().toUpperCase().split(""); for (int i = 1; i < textSoal.length; i++) { items.add(textSoal[i]); adapter.notifyDataSetChanged(); } soal=soalAwal; return true; case R.id.menu_solusi:
Universitas Sumatera Utara
80
String hasil = koneksi.solusi(soalAwal, target); String[] h = hasil.split("->"); hasil = ""; for (int i = 0; i < h.length; i++) { hasil = hasil + h[i] + "\n"; } System.out.println("hasil : " + hasil); AlertDialog.Builder adb = new AlertDialog.Builder(Game.this); adb.setTitle("Solusi " + soalAwal + " - " + target); adb.setIcon(R.drawable.ic_launcher); adb.setMessage(hasil.toUpperCase()); adb.setNegativeButton("Tutup", null); adb.show(); return true; default: return super.onOptionsItemSelected(item); } } public class MyCounter extends CountDownTimer { public MyCounter(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onFinish() { System.out.println("0"); viewWaktu.setText("0"); Score.setScore((adapter.getCount()/columnPadaGridview1)*50+Integer.parseInt(viewWaktu.getText().toString())); AlertDialog.Builder adb = new AlertDialog.Builder(Game.this); adb.setTitle("Waktu Habis!"); adb.setMessage("Score " + Score.getNama() + " : " + Score.getScore()); adb.setNegativeButton("Tutup", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { startActivity(new Intent(Game.this, Level.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); } }); adb.show(); } public void onTick(long millisUntilFinished) { viewWaktu.setText((millisUntilFinished / 1000) + ""); System.out.println("Timer : " + (millisUntilFinished /1000)); } } }
Universitas Sumatera Utara
81
KUESIONER PENILAIAN PENGGUNA APLIKASI PERMAINAN WORD LADDER
Aspek Antarmuka 1. Desain aplikasi secara umum 2. Desain tombol menu 3. Ukuran dan warna tulisan
Aspek Penggunaan Aplikasi 4. Kemudahan menggunakan aplikasi 5. Kemudahan memahami fungsi menu-menu yang ada 6. Kesesuaian tingkat kesulitan pada setiap level 7. Pemahaman aturan dan cara bermain
No
Nama
Ttd
Aspek
Aspek Penggunaan
Antarmuka
Aplikasi
1
2
3
4
5
6
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. Keterangan : A=sangat baik, B=baik, C=cukup, D=buruk, E=sangat buruk
Universitas Sumatera Utara
7