Android - Java Apache Config Windows Batch Linux Bash CSS Gradle HTML ini File iOS - Swift Java JavaScript JSON Format Kotlin LESS Markdown MySQL PHP Plain Text Properties File Rust Go Lang SCSS Swift XML
package com.example.vahid.hangman
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
var word: String = selectWord()
var wordDashed: String = ""
var faildCount: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
wordDashed = getDashWord(word)
Log.i("LOG", "Word is: " + word)
Log.i("LOG", "wordDashed is: " + wordDashed)
img_face.setImageResource(R.drawable.face_1)
txt_word.text = wordDashed
}
// ذخیره کردن نسخه نقطه چین شده
fun getDashWord(word: String): String {
var dashed: String = ""
for (item in 0..word.length - 1) {
if (word.toCharArray().elementAt(item) != ' ') {
dashed += "-"
} else {
dashed += " "
}
}
return dashed
}
// انتخاب کلمه از بین کلمات ذخیره شده در آرایه
fun selectWord(): String {
var wordArray = arrayOf("Object Oriented", "Polymorphism", "Encapsulation", "Interface", "Composition", "Android", "Development", "Programming", "Design", "Research", "Google", "Refactoring", "Renaming", "Documentation")
var randomIndex: Int = (Math.random() * wordArray.size).toInt()
return wordArray[randomIndex]
}
fun winGame() {
val intent = Intent(applicationContext, FinishActivity::class.java)
intent.putExtra("result", "won")
this.startActivity(intent)
Log.i("LOG", "You won")
finish()
}
fun lostGame() {
val intent = Intent(applicationContext, FinishActivity::class.java)
intent.putExtra("result", "lost")
this.startActivity(intent)
finish()
}
fun replaceChar(target: String, index: Int, newChar: Char): String {
val wordDashedCharArray = target.toCharArray()
wordDashedCharArray[index] = newChar
return String(wordDashedCharArray)
}
// تعریف کلیک لیسنر و تبدیل آن به حرف
fun listener(view: View) {
val char = view as TextView
// val id: String = view.resources.getResourceEntryName(char.id)
val letter: String = char.text.toString().replace("txt_", "").toLowerCase()
val letterChar: Char = letter.elementAt(0)
//Log.i("LOG", "char is : " + letter)
val wordLowerCase: String = word.toLowerCase()
if (word.contains(letter.toLowerCase()) || word.contains(letter.toUpperCase())) {
for (item in 0..wordLowerCase.length - 1) {
if (wordLowerCase.elementAt(item) == letterChar) {
Log.i("LOG", "Found at index : " + item)
wordDashed = replaceChar(wordDashed,item,word.elementAt(item))
txt_word.text = wordDashed
if (!wordDashed.contains("-")) {
winGame()
return
}
}
}
Log.i("LOG", "char Found in : " + letter)
char.visibility = View.INVISIBLE
} else {
faildCount++
if (faildCount > 7) {
Log.i("LOG", "Game Over")
img_face.setImageResource(R.drawable.face_9)
lostGame()
return
}
img_face.setImageResource(R.drawable.face_1)
var imageId: Int = 0
when (faildCount) {
1 -> {
imageId = R.drawable.face_2
}
2 -> {
imageId = R.drawable.face_3
}
3 -> {
imageId = R.drawable.face_4
}
4 -> {
imageId = R.drawable.face_5
}
5 -> {
imageId = R.drawable.face_6
}
6 -> {
imageId = R.drawable.face_7
}
7 -> {
imageId = R.drawable.face_8
}
8 -> {
imageId = R.drawable.face_9
}
}
img_face.setImageResource(imageId)
Log.i("LOG", "char not Found")
}
}
}