[JAVA] I made a rock-paper-scissors app with kotlin

Introduction

I am a beginner college student with 2 months of java experience and 1 month of kotlin experience. I hope this article is a hint for similar beginners trying to build apps using kotlin.

The development environment is android studio 3.0.1.

Reference site https://blog.xsota.com/2013/07/android.html xsota blog "Let's make an Android application-rock-paper-scissors edition"

Each id name is ・ Image of gu: gu ・ Choki image: cho ・ Par image: per ・ GU ImageButton: gu ・ Choki ImageButton: choki ・ Par ImageButton: per ・ My hand ImageView: playerHand ・ Opponent's hand ImageView: comHand -Result TextView: result I'm doing

Code: kt

MainActivity.kt


package com.example.yusaku.jankenbuttonapp

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.ImageButton
import android.widget.ImageView
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : Activity(), View.OnClickListener {
    override fun onClick(v: View?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val gu: ImageButton = findViewById(R.id.gu)
        val choki: ImageButton = findViewById(R.id.choki)
        val per: ImageButton = findViewById(R.id.per)

        gu.setOnClickListener(listener)
        choki.setOnClickListener(listener)
        per.setOnClickListener(listener)
    }


    val listener = object : View.OnClickListener {
        override fun onClick(v: View?) {

            var hands = IntArray(3)
            //Assign an image file to your hand
            hands[0] = R.drawable.gu
            hands[1] = R.drawable.cho
            hands[2] = R.drawable.per

            var playerHand: ImageView = findViewById(R.id.playerHand)
            var comHand: ImageView = findViewById(R.id.comHand)

            val random = Random()
            val n = random.nextInt(3)

            var hand = 0
            //0:gu 1:choki 2:per
            if (v == findViewById(R.id.gu)) {
                hand = 0
            } else if (v == findViewById(R.id.choki)) {
                hand = 1
            } else if (v == findViewById(R.id.per)) {
                hand = 2
            }
            playerHand.setImageResource(hands[hand])
            comHand.setImageResource(hands[n])
            val syouhai = hand - n + 3

            if (syouhai % 3 == 0) {
                result.setText("Aiko")
            } else if (syouhai % 3 == 1) {
                result.setText("You lost")
            } else if (syouhai % 3 == 2) {
                result.setText("You win")
            }

        }
    }
}

Code: xml

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.yusaku.jankenbuttonapp.MainActivity">

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="@string/result_text"
        android:textColor="@android:color/black"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/playerHand"
        app:layout_constraintEnd_toEndOf="@+id/playerHand"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@+id/playerHand"
        app:layout_constraintTop_toBottomOf="@+id/comHand" />

    <ImageButton
        android:id="@+id/gu"
        android:layout_width="95dp"
        android:layout_height="83dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:foreground="@drawable/gu"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/choki"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/playerHand"
        app:srcCompat="@drawable/ic_launcher_foreground" />

    <ImageButton
        android:id="@+id/choki"
        android:layout_width="98dp"
        android:layout_height="97dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:foreground="@drawable/cho"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/per"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/gu"
        app:layout_constraintTop_toBottomOf="@+id/playerHand"
        app:srcCompat="@drawable/ic_launcher_background" />

    <ImageButton
        android:id="@+id/per"
        android:layout_width="95dp"
        android:layout_height="100dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:foreground="@drawable/per"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/choki"
        app:layout_constraintTop_toBottomOf="@+id/playerHand"
        app:srcCompat="@drawable/ic_launcher_background" />

    <ImageView
        android:id="@+id/comHand"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/result"
        app:layout_constraintEnd_toEndOf="@+id/result"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@+id/result"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_background" />

    <ImageView
        android:id="@+id/playerHand"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/choki"
        app:layout_constraintEnd_toEndOf="@+id/choki"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@+id/choki"
        app:layout_constraintTop_toBottomOf="@+id/result"
        app:srcCompat="@drawable/ic_launcher_background" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/player_text"
        app:layout_constraintBottom_toTopOf="@+id/gu"
        app:layout_constraintEnd_toStartOf="@+id/playerHand"
        app:layout_constraintHorizontal_bias="0.555"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.79" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/com_text"
        app:layout_constraintBottom_toTopOf="@+id/per"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.48"
        app:layout_constraintStart_toEndOf="@+id/comHand"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.201" />

</android.support.constraint.ConstraintLayout>

Impressions

-The app did not start even if I converted what was written in java as it was and the error disappeared. Therefore, I removed the declaration before fun on Create, which was a reference site, and made a declaration in each method, and it worked. ・ Val listener = object: View.OnClickListener also struggled quite a bit. I found the following site as a result of searching without going well with setOnClickListener (this). By putting the declaration object :, it seems that the interface class can be described as an instance for each implementation. For more information https://dev.classmethod.jp/smartphone/android-kotlin-introduction-02/ ·personally override fun onClick(v: View?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } The place is also unknown. It became like this when I left it to the error handling of android studio. Lack of study.

・ The way to write the code is still not optimal, so I would like to find a better way. ・ Next is http://seesaawiki.jp/genesix_android/d/%A4%B8%A4%E3%A4%F3%A4%B1%A4%F3%A5%B2%A1%BC%A5%E0%A4% F2% BA% EE% A4% EB I would like to try something a little complicated, including retries, with reference to.

Recommended Posts

I made a rock-paper-scissors app with kotlin
I made a rock-paper-scissors app with android
I made a shopify app @java
I made a GUI with Swing
I made a matching app (Android app)
[Android] I made a pedometer app.
I made a calculator app on Android
[Rails] I made a simple calendar mini app with customized specifications.
04. I made a front end with SpringBoot + Thymeleaf
I made a mosaic art with Pokemon images
I made a gender selection column with enum
I made a rock-paper-scissors game in Java (CLI)
I made a viewer app that displays a PDF
I made a LINE bot with Rails + heroku
I made a portfolio with Ruby On Rails
I made an app to scribble with PencilKit on a PDF file
A simple CRUD app made with Nuxt / Laravel (Docker)
Create a Chat app with WebSocket (Tyrus) + libGDX + Kotlin
I made a lock pattern using the volume key with the Android app. Fragment edition
I made a development environment with rails6 + docker + postgreSQL + Materialize.
[Rails] I tried to create a mini app with FullCalendar
I want to push an app made with Rails 6 to GitHub
I made a plugin to execute jextract with Gradle task
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I made a mod that instantly calls a vehicle with Minecraft
Ruby: I made a FizzBuzz program!
Creating a timer app with a muddy
I made a simple recommendation function.
I made a package.xml generation tool.
I made a command line interface with WinMerge Plugin using JD-Core
I recently made a js app in the rumored Dart language
[Ruby] I made a simple Ping client
I made an eco server with scala
I tried playing with BottomNavigationView a little ①
I made a plugin for IntelliJ IDEA
[Rails 5] Create a new app with Rails [Beginner]
I made a new Java deployment tool
I made a bulletin board using Docker 1
[LINE BOT] I made a ramen BOT with Java (Maven) + Heroku + Spring Boot (1)
I made a site that summarizes information on carbohydrate restriction with Vue.js
I made a primality test program in Java
Create a simple search app with Spring Boot
I made StringUtils.isBlank
I made an app for myself! (Reading management app)
Publish the app made with ruby on rails
I want to play a GIF image on the Andorid app (Java, Kotlin)
Build a Kotlin app using OpenJDK's Docker container
I made blackjack with Ruby (I tried using minitest)
I made an Android app for MiRm service
I tried to create a LINE clone app
I made a Docker container to run Maven
I made a Ruby extension library in C
[Rails] I made a draft function using enum
I built a Code Pipeline with AWS CDK.
Learn Java with Progate → I will explain because I made a basic game myself
A simple rock-paper-scissors game with JavaFX and SceneBuilder
I tried to break a block with java (1)
I made a function to register images with API in Spring Framework. Part 1 (API edition)
I made a Docker image of SDAPS for Japanese
I made an iPhone Theremin with Vision framework + AudioKit