과거⚰️
스네이크 1 || 스네이크 2
아무루
2020. 11. 29. 10:32
package day048;
import java.util.Scanner;
/*
* # 스네이크 게임[1단계]
* 1. 10x10 배열을 설정한다.
* 2. 스네이크는 1234의 숫자로 표시한다.
* 3. 스네이크는 상하좌우로 이동이 가능하며, 꼬리가 따라온다.
* 4. 본인 몸과 부딪히면, 사망하게 된다.
*/
public class Day04801스네이크게임1문제 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int size = 10;
int[][] map = new int[size][size];
int snakeSize = 4;
int[] y = new int[snakeSize];
int[] x = new int[snakeSize];
// 스네이크 설정하기
for (int i = 0; i < snakeSize; i++) {
x[i] = i;
y[i] = 0;
map[y[i]][x[i]] = i + 1;
}
while (true) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if(map[i][j]==1){
System.out.print("🐲");
}else if(map[i][j]==0){
System.out.print("⬜️");
}else{
System.out.print("🟢");
}
}
System.out.println();
}
System.out.println();
System.out.println(" ▲(w)");
System.out.println("◀(a) ▼(s) ▶(d)");
String choice = scan.next();
int copyY = y[0];
int copyX=x[0];
if (choice.equals("w")) {
copyY -= 1;
if (copyY < 0 || copyY >= size) {
continue;
}
} else if (choice.equals("s")) {
copyY += 1;
if (copyY < 0 || copyY >= size) {
continue;
}
} else if (choice.equals("a")) {
copyX -= 1;
if (copyX < 0 || copyX >= size) {
continue;
}
} else if (choice.equals("d")) {
copyX += 1;
if (copyX < 0 || copyX >= size) {
continue;
}
}
map[y[3]][x[3]] = 0;
for (int i = snakeSize-1; i > 0 ; i--) {
y[i] = y[i - 1];
x[i] = x[i - 1];
}
if (choice.equals("w")) {
y[0] -= 1;
} else if (choice.equals("s")) {
y[0] += 1;
} else if (choice.equals("a")) {
x[0] -= 1;
} else if (choice.equals("d")) {
x[0] += 1;
}
if (map[y[0]][x[0]] != 0) {
System.out.println("lose");
break;
}
for (int i = 0; i < snakeSize; i++) {
map[y[i]][x[i]] = i + 1;
}
}
}
}
package day048;
import java.util.Random;
import java.util.Scanner;
/*
* # 스네이크 게임[2단계]
* 1. 10x10 배열을 설정한다.
* 2. 스네이크는 1234의 숫자로 표시한다.
* 3. 스네이크는 상하좌우로 이동이 가능하며, 꼬리가 따라온다.
* 4. 본인 몸과 부딪히면, 사망하게 된다.
* ----------------------------------------
* 5. 랜덤으로 아이템을 생성한다.
* 6. 스네이크는 아이템을 먹으면 꼬리가 1개 자란다.
* 7. 꼬리는 최대 8개까지만 증가할 수 있다.
*/
public class Day04801스네이크게임2문제 {
public static void main(String[] args) {
Random ran = new Random();
Scanner scan = new Scanner(System.in);
int size = 10;
int[][] map = new int[size][size];
int snakeSize = 4;
int[] y = new int[snakeSize];
int[] x = new int[snakeSize];
// 스네이크 설정하기
for (int i = 0; i < snakeSize; i++) {
x[i] = i;
y[i] = 0;
map[y[i]][x[i]] = i + 1;
}
//-------------------------------
// 아이템 설정하기
int itemCount = 8;
int item = -1;
while (itemCount != 0) {
int rY = ran.nextInt(size);
int rX = ran.nextInt(size);
if (map[rY][rX] == 0) {
map[rY][rX] = item;
itemCount = itemCount - 1;
}
}
//-------------------------------
while (true) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (map[i][j] == item) {
System.out.print("🎁");
}else if(map[i][j]==1){
System.out.print("🐲");
}else if(map[i][j]==0){
System.out.print("⬜️");
}else{
System.out.print("🟢");
}
}
System.out.println();
}
System.out.println();
System.out.println(" ▲(w)");
System.out.println("◀(a) ▼(s) ▶(d)");
String choice = scan.next();
int copyY = y[0];
int copyX = x[0];
if (choice.equals("w")) {
copyY -= 1;
} else if (choice.equals("s")) {
copyY += 1;
} else if (choice.equals("a")) {
copyX -= 1;
} else if (choice.equals("d")) {
copyX += 1;
}
if (copyX < 0 || copyX >= size || copyY < 0 || copyY >= size) {
continue;
}
boolean itemspotlight = false;
if (map[copyY][copyX] == item) {
int[] tmpY = y;
int[] tmpX = x;
snakeSize++;
y = new int[snakeSize];
x = new int[snakeSize];
for (int i = 0; i < snakeSize - 1; i++) {
y[i] = tmpY[i];
x[i] = tmpX[i];
}
itemspotlight = true;
}
map[y[snakeSize-1]][x[snakeSize-1]] = 0;
for (int i = snakeSize - 1; i > 0; i--) {
y[i] = y[i - 1];
x[i] = x[i - 1];
}
if (choice.equals("w")) {
y[0] -= 1;
} else if (choice.equals("s")) {
y[0] += 1;
} else if (choice.equals("a")) {
x[0] -= 1;
} else if (choice.equals("d")) {
x[0] += 1;
}
if (!itemspotlight) {
if (map[y[0]][x[0]] != 0) {
System.out.println("lose");
break;
}
}
for (int i = 0; i < snakeSize; i++) {
map[y[i]][x[i]] = i + 1;
}
}
}
}