Android Studio İle Kelime Tahmin Oyunu
Android Studio da java kullanarak kelime tahmin oyunu yapalım.
Kelimeleri dizi olarak tanımlayıp, rastgele seçilen bir kelimeyi tahmin etmeye çalışacağı.
Ekran Tasarımı Aşağıdaki Gibidir:
Kelime Tahmin Oyunu başlığı için: TexView
—– şeklinde gizli kelime için : TextView
Bir Harf Girin için: EditText
Tahmin Et Butonumuz ve Sonuç ve uyarıları göstermek için TextView kullandık.
Ekran Tasarım Kodları Aşağıdaki Gibidir:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<TextView
android:id=”@+id/textView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”24dp”
android:text=”KELİME TAHMİN OYUNU”
android:textSize=”24sp”
android:textStyle=”bold”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toTopOf=”parent” />
<TextView
android:id=”@+id/txtkelime”
android:layout_width=”302dp”
android:layout_height=”57dp”
android:layout_marginTop=”32dp”
android:text=”- – – – -”
android:textSize=”40dp”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toBottomOf=”@+id/textView” />
<EditText
android:id=”@+id/txtgirilen”
android:layout_width=”139dp”
android:layout_height=”56dp”
android:layout_marginTop=”32dp”
android:ems=”10″
android:hint=”Bir Harf Girin”
android:inputType=”text”
app:layout_constraintStart_toStartOf=”@+id/txtkelime”
app:layout_constraintTop_toBottomOf=”@+id/txtkelime” />
<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”44dp”
android:onClick=”tahminet”
android:text=”Tahmin Et”
app:layout_constraintStart_toEndOf=”@+id/txtgirilen”
app:layout_constraintTop_toTopOf=”@+id/txtgirilen” />
<TextView
android:id=”@+id/txtsonuc”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”48dp”
android:text=”Sonuç”
app:layout_constraintStart_toStartOf=”@+id/txtgirilen”
app:layout_constraintTop_toBottomOf=”@+id/txtgirilen” />
</androidx.constraintlayout.widget.ConstraintLayout>
Tasarımı bitirdikten sonra kodlamamızı yaptık. Kodlarımız aşağıdaki gibidir.
package com.example.subat172025kelimetahmin;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
String[] kelimeler={“java”,”html”,”kotlin”,”python”,”javascript”};
EditText txtgirilen;
TextView txtkelime,txtsonuc;
String secilenkelime;
char[] gizlikelime;
int hak=7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
txtkelime=findViewById(R.id.txtkelime);
txtgirilen=findViewById(R.id.txtgirilen);
txtsonuc=findViewById(R.id.txtsonuc);
Random rnd=new Random();
int randomsayi=rnd.nextInt(kelimeler.length);
secilenkelime=kelimeler[randomsayi];
gizlikelime=new char[secilenkelime.length()];
//secilen kelime uzunluğu kadar – işareti koy
for(int i=0;i<gizlikelime.length;i++)
{
gizlikelime[i]=’-‘;
}
//ekran güncelleme
txtkelime.setText(new String(gizlikelime).replace(“”,” “).trim());
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
public void tahminet(View view) {
String girilenharf = txtgirilen.getText().toString().toLowerCase();
if (girilenharf.length() != 1) {
txtsonuc.setText(“Lütfen bir harf girin”);
return;
}
char harf = girilenharf.charAt(0);
boolean dogrutahmin = false;
for (int i = 0; i < secilenkelime.length(); i++) {
if (secilenkelime.charAt(i) == harf) {
gizlikelime[i] = harf;
dogrutahmin = true;
}
}
if (!dogrutahmin) {
hak–;
txtsonuc.setText(“Yanlış Harf. Kalan Hakkınız=” + hak);
}
//ekran güncelleme
txtkelime.setText(new String(gizlikelime).replace(“”,” “).trim());
if (hak == 0) {
txtsonuc.setText(“Kaybettiniz. Doğru Kelime: ” + secilenkelime);
} else if (new String(gizlikelime).equals(secilenkelime)) {
txtsonuc.setText(“Tebrikler Kelimeyi buldunuz”);
}
txtgirilen.setText(“”);
}
}
Kodlarımızı bitirdikten sonra uygulamamızı çalıştırdığımızda aşağıdaki gibi bir ekran bizi karşılayacak
Yorum gönder
Yorum yapabilmek için oturum açmalısınız.