치춘짱베리굿나이스

191008 가위바위보 게임 본문

기타공부/Android

191008 가위바위보 게임

치춘 2021. 2. 8. 20:32
package com.example.a1008;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int cpu, me;
Random random = new Random();
int wincount, losecount, trycount;
String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button rock = findViewById(R.id.rock);
Button scissor = findViewById(R.id.scissor);
Button paper = findViewById(R.id.paper);
Button reset = findViewById(R.id.reset);
final ImageView cpu_view = findViewById(R.id.cpu_view);
final ImageView me_view = findViewById(R.id.me_view);
final TextView N_WIN = findViewById(R.id.nwin);
final TextView N_LOSE = findViewById(R.id.nlose);
final TextView N_TRY = findViewById(R.id.ntry);
final TextView WIN_LOSE = findViewById(R.id.winlose);
final RSP rsp = new RSP();
rock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
me = 0; //me = rock
cpu = random.nextInt(3);
rsp.fight(me, cpu);
N_LOSE.setText(String.valueOf(losecount));
N_WIN.setText(String.valueOf(wincount));
N_TRY.setText(String.valueOf(trycount));
WIN_LOSE.setText(str);
me_view.setImageResource(R.drawable.rock);
if(cpu == 0){
cpu_view.setImageResource(R.drawable.rock);
}
else if(cpu == 1){
cpu_view.setImageResource(R.drawable.scissor);
}
else{
cpu_view.setImageResource(R.drawable.paper);
}
}
});
scissor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
me = 1; //me = scissor
cpu = random.nextInt(3);
rsp.fight(me, cpu);
N_LOSE.setText(String.valueOf(losecount));
N_WIN.setText(String.valueOf(wincount));
N_TRY.setText(String.valueOf(trycount));
WIN_LOSE.setText(str);
me_view.setImageResource(R.drawable.scissor);
if(cpu == 0){
cpu_view.setImageResource(R.drawable.rock);
}
else if(cpu == 1){
cpu_view.setImageResource(R.drawable.scissor);
}
else{
cpu_view.setImageResource(R.drawable.paper);
}
}
});
paper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
me = 2; //me = paper
cpu = random.nextInt(3);
rsp.fight(me, cpu);
N_LOSE.setText(String.valueOf(losecount));
N_WIN.setText(String.valueOf(wincount));
N_TRY.setText(String.valueOf(trycount));
WIN_LOSE.setText(str);
me_view.setImageResource(R.drawable.paper);
if(cpu == 0){
cpu_view.setImageResource(R.drawable.rock);
}
else if(cpu == 1){
cpu_view.setImageResource(R.drawable.scissor);
}
else{
cpu_view.setImageResource(R.drawable.paper);
}
}
});
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
trycount = 0;
wincount = 0;
losecount = 0;
N_LOSE.setText(String.valueOf(losecount));
N_WIN.setText(String.valueOf(wincount));
N_TRY.setText(String.valueOf(trycount));
Toast.makeText(MainActivity.this,"값을 리셋합니다", Toast.LENGTH_SHORT).show();
}
});
}
class RSP{
public void fight(int me, int cpu){
if(me == 0){
switch(cpu){
case 0:
trycount++;
str = "무승부!";
break;
case 1:
wincount++;
trycount++;
str = "승리!";
break;
case 2:
losecount++;
trycount++;
str = "패배!";
break;
}
}
else if (me == 1){
switch(cpu){
case 0:
losecount++;
trycount++;
str = "패배!";
break;
case 1:
trycount++;
str = "무승부!";
break;
case 2:
wincount++;
trycount++;
str = "승리!";
break;
}
}
else{
switch(cpu){
case 0:
wincount++;
trycount++;
str = "승리!";
break;
case 1:
losecount++;
trycount++;
str = "패배!";
break;
case 2:
trycount++;
str = "무승부!";
break;
}
}
}
}
}
view raw main.java hosted with ❤ by GitHub
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="77dp"
tools:layout_editor_absoluteY="214dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5"
android:orientation="horizontal">
<TextView
android:id="@+id/Try"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:gravity="center"
android:text="시도 횟수"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="bold"
android:visibility="visible" />
<TextView
android:id="@+id/ntry"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:gravity="center"
android:text="TextView"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5"
android:orientation="horizontal">
<TextView
android:id="@+id/win"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="승리"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/nwin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/lose"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="패배"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/nlose"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/cpu_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@android:color/background_light" />
<ImageView
android:id="@+id/me_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@android:color/background_light" />
</LinearLayout>
<LinearLayout
android:id="@+id/notification"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5"
android:orientation="horizontal"
android:weightSum="5">
<TextView
android:id="@+id/enemy"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:gravity="center"
android:text="상대편"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:id="@+id/winlose"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:gravity="center"
android:text="내편"
android:textColor="#FFFFFF"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/scissor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#F44336"
android:text="가위"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold" />
<Button
android:id="@+id/rock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF9800"
android:text="바위"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold" />
<Button
android:id="@+id/paper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFC107"
android:text="보"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold" />
<Button
android:id="@+id/reset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000"
android:text="초기화"
android:textColor="#FF0000"
android:textSize="36sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
view raw main.xml hosted with ❤ by GitHub

xml 연습 겸 수업 외적으로 개인적으로 만들어봄

상대편 수는 랜덤으로 나오고 이길 때마다 위에 승패가 쌓임

Comments