どうも、Pythonを主軸に開発を行っている学生エンジニアのirohasです。
今回はAndroidstudioで簡易的なじゃんけんアプリを作成してみたので共有しようと思います。
~使用物~
・AndroidStudio
・Java
・デバイス(AndroidStudio内のエミュレーターでもおk)
まずはじゃんけんをするために必要な画像たちをresフォルダのdrawableフォルダに格納していきます。
必要な画像を格納したら、画面のデザインに入っていきます。
画面のデザインはActivity_main.xmlで行います。
コードでデザインしたい方はコードで、目視でデザインをしたい方はデザインタブをクリックして自由にデザインしましょう。
僕はコードがやりやすかったのでコードでデザインしました。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/Chand"
android:layout_width="146dp"
android:layout_height="164dp"
android:layout_gravity="center_horizontal"
app:srcCompat="@drawable/crock" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/result"
android:layout_width="416dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="じゃーんけーん"
android:textSize="36sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/Uhand"
android:layout_width="146dp"
android:layout_height="164dp"
android:layout_weight="1"
app:srcCompat="@drawable/prock" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageButton
android:id="@+id/Rock"
android:layout_width="269dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/comand" />
<ImageButton
android:id="@+id/Scissors"
android:layout_width="wrap_content"
android:layout_height="171dp"
android:layout_weight="1"
app:srcCompat="@drawable/comand2" />
<ImageButton
android:id="@+id/Paper"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/comand3" />
</LinearLayout>
デザインが完成したらいよいよプログラミングに入っていきます。
じゃんけんのシステムはそこまで複雑ではないので解説なしでコードだけおいておきます。
分からなかったらコメントください。
package com.websarva.wings.android."YOUR ACTIVITY NAME";
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ImageButton Rock, Scissors, Paper;
int hands[] = new int[3];
ImageView me, enemy;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hands[0] = R.drawable.comand;
hands[1] = R.drawable.comand2;
hands[2] = R.drawable.comand3;
me = (ImageView) findViewById(R.id.Uhand);
enemy = (ImageView) findViewById(R.id.Chand);
Rock = (ImageButton) findViewById(R.id.Rock);
Scissors = (ImageButton) findViewById(R.id.Scissors);
Paper = (ImageButton) findViewById(R.id.Paper);
result = (TextView) findViewById(R.id.result);
Rock.setOnClickListener(this);
Scissors.setOnClickListener(this);
Paper.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Random random = new Random();
int n = random.nextInt(3);
int hand=0;
//R:Rock S:Scissors P:Paper
if(view == Rock){
hand = 0;
}else if (view == Scissors){
hand = 1;
}else if (view == Paper){
hand = 2;
}
me.setImageResource(hands[hand]);
enemy.setImageResource(hands[n]);
int wl = (hand - n) + 3;
if (wl%3 == 0){
result.setText("Draw");
}else if (wl%3 == 1){
result.setText("You Lose!!!");
}else if (wl%3 == 2){
result.setText("You Win!!!");
}
}
}
ここまで出来たらあとはエミュレーターを起動させて確認するだけです。
~苦労した点~
APIがアプリ起動時の条件を満たしていなくgradleを開いて設定しなければいけなかった。
~解決策~
build.gradle
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
build.gradleにこれ追加したら治った。
・感想
アプリのデザインから考えるのが楽しかった。
Javaの延長線上みたいな感じだったので、Javaをしっかり学んでいればスラスラ書けるなと感じました。
VScodeみたいに予測してコマンドを表示してくれるのでスピーディにコードが書けるのも素晴らしいと思いました。