I made a rock-paper-scissors app with android

I added the win / loss count and the winning percentage to the basic rock-paper-scissors. Obviously, the more you do, the closer the winning percentage will be to 33.3%.

スクリーンショット 2018-05-07 14.08.58.png

MainActivity.java


package to.msn.wings.janken;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Random;


public class MainActivity extends AppCompatActivity {

    /* 
     TextView:
     player(Player text),playerRes (player's hand)
     com(Com text), comRes(Com hand)
result (win / loss result)
winCnt (win rate),loseCnt,drawCnt (number of draws)
totalCnt,winPer (win rate)
     button:
     gu, tyoki, pa
     */

    private int totalCnt; //Total number of battles
    private int drawCnt; //Number of draws
    private int loseCnt; //Number of losses
    private int winCnt; //Number of wins

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void btn_onclick(View view){

        //Hand on the player side
        TextView txt = (TextView)findViewById(R.id.playerRes);
        int player_hand = 0;
        //view.Judge the button clicked with getId
        // 0:gu, 1:tyoki, 2:par
        switch (view.getId()){
            case R.id.gu:
                txt.setText("Goo");
                player_hand = 0;
                break;
            case R.id.tyoki:
                txt.setText("Choki");
                player_hand = 1;
                break;
            case R.id.pa:
                txt.setText("Par");
                player_hand = 2;
        }

        //hand on the com side
        TextView com = (TextView)findViewById(R.id.comRes);

        //Set hands of com with random
        Random random = new Random();
        int n = random.nextInt(3);
        int com_hand = 0;
        // 0:gu, 1:tyoki, 2:par
        if (n == 0){
            com.setText("Goo");
            com_hand = n;
        } else if (n == 1){
            com.setText("Choki");
            com_hand = n;
        } else if (n == 2){
            com.setText("Par");
            com_hand = n;
        }

        //Count up the total number of matches
        TextView total = (TextView) findViewById(R.id.totalCnt);
        totalCnt++;
        total.setText("Total number:" + String.valueOf(totalCnt));

        //Rock-paper-scissors win / loss judgment
        TextView result = (TextView) findViewById(R.id.result);
        int judge = (player_hand - com_hand + 3)%3;
        if (judge == 0) {
            //In this case
            TextView draw = (TextView) findViewById(R.id.drawCnt);
            drawCnt++;
            draw.setText("Number of draws:" + String.valueOf(drawCnt));
            result.setText("Aiko");
        } else if (judge == 1) {
            //If you lose
            TextView lose = (TextView) findViewById(R.id.loseCnt);
            loseCnt++;
            lose.setText("Number of losses:" + String.valueOf(loseCnt));
            result.setText("Your defeat");
        } else if (judge == 2){
            //If you win
            TextView win = (TextView) findViewById(R.id.winCnt);
            winCnt++;
            win.setText("Number of wins:" + String.valueOf(winCnt));
            result.setText("Your win");
        }

        //Win rate calculation
        TextView per = (TextView) findViewById(R.id.winPer);
        double winPer = (double) winCnt / (double) totalCnt * 100; //Explicitly convert to double and calculate
        per.setText("Win rate:" + String.format("%.2f", winPer) + "%"); //format("%.2f", winPer)Truncate after the second decimal point
    }
}

Recommended Posts

I made a rock-paper-scissors app with android
I made a rock-paper-scissors app with kotlin
I made a matching app (Android app)
[Android] I made a pedometer app.
I made a chat app.
I made a shopify app @java
I made a GUI with Swing
I made a risky die with Ruby
I made a lock pattern using the volume key with the Android app. Fragment edition
[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 an Android app for MiRm service
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
A simple CRUD app made with Nuxt / Laravel (Docker)
[Ruby] I made a crawler with anemone and nokogiri.
I made a library for displaying tutorials on Android.
I made an Android application that GETs with HTTP
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
Import device images with Android app
Creating a timer app with a muddy
I made a package.xml generation tool.
The basics of the process of making a call with an Android app
I made a command line interface with WinMerge Plugin using JD-Core
I tried to create a simple map app in Android Studio
I recently made a js app in the rumored Dart language
I made a simple search form with Spring Boot + GitHub Search API.
[Ruby] I made a simple Ping client
[Rails6] Create a new app with Rails [Beginner]
I made an eco server with scala
Shiritori map app made with Swift UI
USB serial communication with Android app (usb-serial-for-android)
I tried playing with BottomNavigationView a little ①
I made a plugin for IntelliJ IDEA
Face recognition app with OpenCV + Android Studio
[Rails 5] Create a new app with Rails [Beginner]
I made a bulletin board using Docker 1
I have a question about Android studio.
[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 Diff tool for Java files
I made a primality test program in Java
[Stajyun also admitted] I want a gourmet spy, but it costs 1103.3543 trillion yen, so I made an android app
I made StringUtils.isBlank
I tried to implement a function equivalent to Felica Lite with HCE-F of Android
I made an app for myself! (Reading management app)
Publish the app made with ruby on rails
I made blackjack with Ruby (I tried using minitest)
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