-
안드로이드 방탈출!! 거의완성..program_language 및 궁금한것/Android Studio 2018. 4. 5. 12:34
자바 소스에서 클레스 복사 후 안드로이드스튜디오에서 src에 붙여 넣기 한다면 그대로 불러옴
오류날 경우엔 전체내용 복붙
메인
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175package com.example.administrator.maze;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import java.util.ArrayList;public class MainActivity extends AppCompatActivity implements View.OnClickListener {TextView txt1;TextView info;TextView qq;TextView item;EditText edit;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);txt1 = (TextView) findViewById(R.id.txt1);info = (TextView) findViewById(R.id.info);qq = (TextView) findViewById(R.id.qq);item = (TextView) findViewById(R.id.item);edit = (EditText) findViewById(R.id.edit);init();Button buttonUp = (Button) findViewById(R.id.buttonUp);buttonUp.setOnClickListener(this);Button buttonLeft = (Button) findViewById(R.id.buttonLeft);buttonLeft.setOnClickListener(this);Button buttonRight = (Button) findViewById(R.id.buttonRight);buttonRight.setOnClickListener(this);Button buttonDown = (Button) findViewById(R.id.buttonDown);buttonDown.setOnClickListener(this);Button ok = (Button) findViewById(R.id.ok);ok.setOnClickListener(this);}Player player;Room[][] map;ArrayList<Player> playerArr;public void init() {playerArr = new ArrayList();player = new Player(2, 2, "개똥이");playerArr.add(player);map = new Room[10][4];setMap(map);info.setText("현재 위치 " + "[" + player.getPosX() + "]" + "[" + player.getPosY() + "]");qq.setText("어디로 이동하시겠습니까?");}public void setMap(Room[][] map) {for (int i = 0; i < map.length; i++) {for (int j = 0; j < map[0].length; j++) {map[i][j] = new Room(0, 0, 0, 0, 0);}}/** 이벤트 세팅 **/map[0][2].setEventType(3);map[4][2].setEventType(1);map[4][0].setEventType(2);/** 일반문 및 잠긴문 세팅 **/map[0][0].setRightDoor(1);map[1][0].setLeftDoor(1);map[1][0].setRightDoor(1);map[2][0].setLeftDoor(1);map[2][0].setRightDoor(1);map[2][0].setDownDoor(1);map[3][0].setLeftDoor(1);map[3][0].setRightDoor(1);map[4][0].setLeftDoor(1);map[2][1].setUpDoor(1);map[2][1].setDownDoor(1);map[0][2].setDownDoor(1);map[2][2].setUpDoor(1);map[2][2].setRightDoor(1);map[2][2].setDownDoor(1);map[4][2].setDownDoor(1);map[0][3].setUpDoor(2);map[0][3].setRightDoor(1);map[1][3].setLeftDoor(1);map[1][3].setRightDoor(1);map[2][3].setUpDoor(1);map[2][3].setRightDoor(1);map[2][3].setLeftDoor(1);map[3][3].setRightDoor(1);map[3][3].setLeftDoor(1);map[4][3].setUpDoor(3);map[4][3].setLeftDoor(1);}@Overridepublic void onClick(View v) {info.setText("현재 위치 " + "[" + player.getPosX() + "]" + "[" + player.getPosY() + "]");if (v.getId() == R.id.buttonUp) {txt1.setText("위로가");player.up(map, qq);} else if (v.getId() == R.id.buttonLeft) {txt1.setText("왼쪽으로가.");player.left(map, qq);} else if (v.getId() == R.id.buttonRight) {txt1.setText("오른쪽으로 가.");player.right(map, qq);} else if (v.getId() == R.id.buttonDown) {txt1.setText("아래로가.");player.down(map, qq);}if (map[player.getPosX()][player.getPosY()].getEventType() == 2) {qq.setText("퀴즈! 사과를 영어로?");edit.setHint("영어로입력");if (v.getId() == R.id.ok && edit.getText().toString().equals("apple")) {item.setText("1번키를 획득");// player.down(map, qq);}edit.setText("");}// }int playerIdx;// public class Main {// public void main(String[] args) {//// playerIdx = 0;// while (true) {// System.out.println();// System.out.println("-------------------------");// System.out.println("[" + player.getName() + "] 님의 차례");// System.out.println("어디로 이동하시겠습니까?");// System.out.println("1. 위 2. 아래 3. 왼쪽 4. 오른쪽");// System.out.println("현재 위치 " + "[" + player.getPosX() + "]" + "[" + player.getPosY() + "]");////// //이동 후 이벤트 체크// if (map[player.getPosX()][player.getPosY()].getEventType() == 2) {// //영어 문제 후 1번키// if (map[player.getPosX()][player.getPosY()].quiz()) {// player.setHasOneKey(true);// } item.setText("1번키 획득");// } else if (map[player.getPosX()][player.getPosY()].getEventType() == 1) {// //게임 클리어// System.out.println("게임이 클리어 하였습니다");// break;// } else if (map[player.getPosX()][player.getPosY()].getEventType() == 3) {// //2번키// player.setHasTwoKey(true);// System.out.println("2번 키를 획득하였습니다");// }//// playerIdx++;// }// }// }}}cs 플레이어
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166package com.example.administrator.maze;import android.widget.TextView;public class Player {private String name;private boolean hasOneKey = false;private boolean hasTwoKey = false;private int posX;private int posY;public Player(int posX, int posY, String name) {this.posX = posX;this.posY = posY;this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public void right(Room[][] map, TextView qq) {if (posX == map.length - 1) {// 벽인 경우qq.setText("못간다~~");} else if (map[posX][posY].getRightDoor() == 0) {// 문이 없는 경우qq.setText("못간다~~");} else if (map[posX][posY].getRightDoor() == 2) {// 빨간 문 경우if (hasOneKey == true) {posX++;} else {qq.setText("1번 키가 필요합니다.");}} else if (map[posX][posY].getRightDoor() == 3) {// 파란 문 경우if (hasTwoKey == true) {posX++;} else {qq.setText("2번 키가 필요합니다.");}} else {posX++;}}public void left(Room[][] map, TextView qq) {if (posX == 0) {// 벽인 경우qq.setText("못간다~~");} else if (map[posX][posY].getLeftDoor() == 0) {// 문이 없는 경우qq.setText("못간다~~");} else if (map[posX][posY].getLeftDoor() == 2) {// 빨간 문 경우if (hasOneKey == true) {posX--;} else {qq.setText("1번 키가 필요합니다.");}} else if (map[posX][posY].getLeftDoor() == 3) {// 파란 문 경우if (hasTwoKey == true) {posX--;} else {qq.setText("2번 키가 필요합니다.");}} else {posX--;}}public void up(Room[][] map, TextView qq) {if (posY == 0) {// 벽인 경우qq.setText("못간다~~");} else if (map[posX][posY].getUpDoor() == 0) {// 문이 없는 경우qq.setText("못간다~~");} else if (map[posX][posY].getUpDoor() == 2) {// 빨간 문 경우if (hasOneKey == true) {posY--;} else {qq.setText("1번 키가 필요합니다.");}} else if (map[posX][posY].getUpDoor() == 3) {// 파란 문 경우if (hasTwoKey == true) {posY--;} else {qq.setText("2번 키가 필요합니다.");}} else {posY--;}}public void down(Room[][] map, TextView qq) {if (posY == map[0].length - 1) {// 벽인 경우qq.setText("못간다~~");} else if (map[posX][posY].getDownDoor() == 0) {// 문이 없는 경우qq.setText("못간다~~");} else if (map[posX][posY].getDownDoor() == 2) {// 빨간 문 경우if (hasOneKey == true) {posY++;} else {qq.setText("1번 키가 필요합니다.");}} else if (map[posX][posY].getDownDoor() == 3) {// 파란 문 경우if (hasTwoKey == true) {posY++;} else {qq.setText("2번 키가 필요합니다.");}} else {posY++;}}public boolean isHasTwoKey() {return hasTwoKey;}public void setHasTwoKey(boolean hasTwoKey) {this.hasTwoKey = hasTwoKey;}public boolean isHasOneKey() {return hasOneKey;}public void setHasOneKey(boolean hasOneKey) {this.hasOneKey = hasOneKey;}public int getPosX() {return posX;}public void setPosX(int posX) {this.posX = posX;}public int getPosY() {return posY;}public void setPosY(int posY) {this.posY = posY;}cs room
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109package com.example.administrator.maze;import android.widget.EditText;import android.widget.TextView;import java.util.Random;import java.util.Scanner;public class Room {private int upDoor; // 0 문이 없다 1 문이 있다 2 빨간문 3 파란문private int leftDoor;private int downDoor;private int rightDoor;private int eventType; // 0 없음, 1 게임클리어, 2 영어문제 풀고 1번키 획득, 3 2번키 획득public Room(int upDoor, int leftDoor, int downDoor, int rightDoor, int eventType) {this.upDoor = upDoor;this.leftDoor = leftDoor;this.downDoor = downDoor;this.rightDoor = rightDoor;this.eventType = eventType;}String[] questionArr;int[] answerArr;int ranIdx;public boolean quiz(TextView qq,TextView item) {questionArr = new String[5];questionArr[0] = "1+1";questionArr[1] = "2+1";questionArr[2] = "3+3";questionArr[3] = "4+5";questionArr[4] = "5+10";answerArr = new int[questionArr.length];answerArr[0] = 2;answerArr[1] = 3;answerArr[2] = 6;answerArr[3] = 9;answerArr[4] = 15;Random rd = new Random();Scanner scan = new Scanner(System.in);ranIdx = rd.nextInt(questionArr.length);qq.setText(questionArr[ranIdx]);int input = Integer.parseInt(scan.nextLine());if(answerArr[ranIdx] == input) {qq.setText("정답입니다");item.setText("1번 키를 획득하였습니다");return true;}else {qq.setText("오답입니다");return false;}}public int getEventType() {return eventType;}public void setEventType(int eventType) {this.eventType = eventType;}public int getUpDoor() {return upDoor;}public void setUpDoor(int upDoor) {this.upDoor = upDoor;}public int getLeftDoor() {return leftDoor;}public void setLeftDoor(int leftDoor) {this.leftDoor = leftDoor;}public int getDownDoor() {return downDoor;}public void setDownDoor(int downDoor) {this.downDoor = downDoor;}public int getRightDoor() {return rightDoor;}public void setRightDoor(int rightDoor) {this.rightDoor = rightDoor;}}cs xml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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"><TextViewandroid:id="@+id/txt1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="130dp"android:background="#00ff00"android:text="Hello World!"android:textSize="25dp" /><Buttonandroid:id="@+id/buttonUp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@id/buttonDown"android:layout_alignLeft="@id/buttonDown"android:text="up"android:textColor="#ff0000"android:textSize="25dp"/><Buttonandroid:id="@+id/buttonLeft"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_toLeftOf="@id/buttonDown"android:text="Le"android:textColor="#ff0000"android:textSize="25dp" /><Buttonandroid:id="@+id/buttonRight"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:text="Ri"android:textColor="#ff0000"android:textSize="25dp"tools:layout_editor_absoluteX="294dp"tools:layout_editor_absoluteY="447dp" /><Buttonandroid:id="@+id/buttonDown"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_toLeftOf="@id/buttonRight"android:text="Do"android:textColor="#ff0000"android:textSize="25dp" /><TextViewandroid:id="@+id/qq"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="77dp"android:text="TextView"android:textColor="#0000ff"android:textSize="25dp" /><TextViewandroid:id="@+id/info"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentStart="true"android:layout_alignParentTop="true"android:background="#0000ff"android:text="TextView"android:textColor="#ffffff"android:textSize="25dp" /><TextViewandroid:id="@+id/item"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="25dp"android:layout_alignParentEnd="true"android:layout_alignParentTop="true"android:text="TextView" /><Buttonandroid:id="@+id/ok"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="202dp"android:text="입력"android:textColor="#ff0000"android:textSize="25dp" /><EditTextandroid:id="@+id/edit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="179dp"android:textSize="25dp"android:ems="10"android:inputType="textPersonName"android:text="Name" /></RelativeLayout>cs 키획득 텍스트 사이즈 조절
반응형'program_language 및 궁금한것 > Android Studio' 카테고리의 다른 글
setVisibility //// visible, invisible (0) 2018.04.12 안드로이드 숙제 (0) 2018.04.10 이전수업에 했던것 (0) 2018.04.05 버튼에 설정넣기. (0) 2018.04.05 안드로이드 기본틀 (0) 2018.04.05