본문 바로가기
Programming/안드로이드스튜디오

안드로이드 앱 만들기 : 가위바위보게임

by 공부합시다홍아 2020. 3. 11.

기본 MAIN 화면 및 상대(Computer)가 '보'를 낸 화면
디자인 화면

[ 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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:weightSum="100">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="80"
android:orientation="vertical">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_replay_black_24dp"
android:scaleType="fitCenter"
android:id="@+id/set_hand_image_view"/>

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/hand_anim"
android:scaleType="centerInside"
android:id="@+id/hand_anim_image_view"/>



</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">

<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:src="@drawable/com_gawe"
android:scaleType="fitCenter"
android:backgroundTint="#FFEB3B"
android:id="@+id/gawe_button"
android:onClick="button_click"/>


<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:src="@drawable/com_bawe"
android:scaleType="fitCenter"
android:backgroundTint="#FFEB3B"
android:id="@+id/bawe_button"
android:onClick="button_click"/>


<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:src="@drawable/com_bo"
android:scaleType="fitCenter"
android:backgroundTint="#FFEB3B"
android:id="@+id/bo_button"
android:onClick="button_click"/>


<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:src="@drawable/ic_replay_black_24dp"
android:scaleType="fitCenter"
android:backgroundTint="#FFEB3B"
android:id="@+id/reply_button"
android:onClick="button_click"/>


</LinearLayout>


</LinearLayout>

--------------------------------------------------------------------------------------------------------------------

MainActivity.java

package com.example.gawebawebo;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

ImageView handAnimImageView;
ImageView setHandImageView;

ImageButton gaweButton;
ImageButton baweButton;
ImageButton boButton;
ImageButton replyButton;

AnimationDrawable animationDrawable;

TextToSpeech textToSpeech;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handAnimImageView = findViewById(R.id.hand_anim_image_view);
setHandImageView = findViewById(R.id.set_hand_image_view);

gaweButton = findViewById(R.id.gawe_button);
baweButton = findViewById(R.id.bawe_button);
boButton = findViewById(R.id.bo_button);
replyButton = findViewById(R.id.reply_button);

animationDrawable = (AnimationDrawable) handAnimImageView.getDrawable();

textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
if (i != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.KOREAN);
textToSpeech.setPitch(1.0f);
textToSpeech.setSpeechRate(1.0f);
}
}
});
}

public void button_click(View view) {
switch (view.getId()){
case R.id.reply_button:
setHandImageView.setVisibility(View.GONE);
handAnimImageView.setVisibility(View.VISIBLE);
animationDrawable.start();
voicePlay("가위바위보");
replyButton.setEnabled(false);
gaweButton.setEnabled(true);
baweButton.setEnabled(true);
boButton.setEnabled(true);
break;

case R.id.gawe_button:
case R.id.bawe_button:
case R.id.bo_button:
replyButton.setEnabled(true);
gaweButton.setEnabled(false);
baweButton.setEnabled(false);
boButton.setEnabled(false);
animationDrawable.stop();
handAnimImageView.setVisibility(View.GONE);
setHandImageView.setVisibility(View.VISIBLE);
int getComHand = new java.util.Random().nextInt(3) + 1;
switch (getComHand){
case 1:
setHandImageView.setImageResource(R.drawable.com_gawe);
if (view.getId() == R.id.gawe_button){
voicePlay("비겼습니다. 다시 시작하세요");
}else if(view.getId() == R.id.bawe_button){
voicePlay("승리하였습니다. 축하한다 쉐리야");
}else{
voicePlay("내가 이겼다 이시키야. 크크크하하하");
}
break;
case 2:
setHandImageView.setImageResource(R.drawable.com_bawe);
if (view.getId() == R.id.gawe_button){
voicePlay("내가 이겼다 이시키야. 크크크하하하");
}else if(view.getId() == R.id.bawe_button){
voicePlay("비겼습니다. 다시 시작하세요");
}else{
voicePlay("승리하였습니다. 축하한다 쉐리야");
}
break;
case 3:
setHandImageView.setImageResource(R.drawable.com_bo);
if (view.getId() == R.id.gawe_button){
voicePlay("승리하였습니다. 축하한다 쉐리야");
}else if(view.getId() == R.id.bawe_button){
voicePlay("내가 이겼다 이시키야. 크크크하하하");
}else{
voicePlay("비겼습니다. 다시 시작하세요");
}
break;
}
break;

default:
break;
}
}

@Override
protected void onStop() {
super.onStop();
textToSpeech.shutdown();
}
public void voicePlay(String voiceText){
textToSpeech.speak(voiceText, TextToSpeech.QUEUE_FLUSH, null,null);
}
}

 

-------------------------------------------------------------------------------------------------------------

Random.java

package com.example.gawebawebo;

public class Random {
public static void main(String[] args) {
double randomDouble = Math.random();
System.out.println("randomDoulbe = " + randomDouble);
int randomInt = (int) (randomDouble * 3) + 1;
System.out.println("randomInt = " + randomInt);
System.out.println("getRandom = " + ((int) (Math.random() * 3 ) + 1));

int randomClass = new java.util.Random().nextInt(3) + 1;
System.out.println("randomClass = " + randomClass);
}
}

------------------------------------------------------------------------------------------------------------------

이번 가위바위보게임에는 사운드 기능을 추가하여 본인이 낸 값에 대한 결과값에 따라 음성이 변하는 효과를 넣었습니다. 그리고 코드를 지속적으로 수정하여 최소화하는 작업을 진행하였습니다.

Switch , Case 문에 대한 지식을 조금 더 얻게되었고,, 아직은 따라하기에 앞서지만 모든 과정을 통달하고 스스로

앱을 짤 수 있기를 기대하겠습니다.

728x90