본문 바로가기

명품자바

[명품자바] 오픈 챌린지 4

import java.util.Scanner;

public class WorldGameApp {
	public void run() {
		Scanner scan = new Scanner(System.in);
		
		System.out.println("끝말잇기를 시작합니다...");
		System.out.print("게임에 참가하는 인원은 몇명입니까?>>");
		int count = scan.nextInt();
		scan.nextLine();
		
		Player p[] = new Player[count];
		
		for (int i = 0; i < count; i++) {
			System.out.print("참가자의 이름을 입력하세요>>");
			p[i] = new Player(scan.nextLine());
		}
		
		System.out.println("시작하는 단어는 아버지입니다.");
		p[(p.length-1)].word = "아버지";
		
		
		int i = 0;
		while(true) {
			System.out.print(p[i].name + ">>");
			p[i].word = scan.nextLine();
			
			char last;
			
			if (i == 0)
				last = p[(p.length-1)].getLastChar();
			else
				last = p[i-1].getLastChar();
			
			
			if (p[i].checkSucces(last))
			{
				if (i == (p.length-1))
					i = 0;
				else
					i++;
			}
			else
				break;
		}
		scan.close();
		System.out.println(p[i].name+"이 졌습니다.");
	}
	
	public static void main(String [] args) {
		new WorldGameApp().run();
	}
}

class Player {
	String name;
	String word;
	
	Player(String name) {
		this.name = name;
	}
	
	public void getWordFromUser(String word) {
		this.word = word;
	}
	
	public char getLastChar() {
		int lastIndex = (word.length()-1);
		return word.charAt(lastIndex);
	}
	
	public char getFirstChar() {
		return word.charAt(0);
	}
	
	public boolean checkSucces(char last) {
		if (last == getFirstChar())
			return true;
		else
			return false;
	}
}

'명품자바' 카테고리의 다른 글

[명품자바] 오픈 챌린지 6  (0) 2024.06.10
[명품자바] 오픈 챌린지 5  (0) 2024.06.10
[명품자바] 오픈 챌린지 3  (0) 2024.06.10
[명품자바] 오픈 챌린지 10  (0) 2024.06.09
[명품자바] 오픈 챌린지 9  (0) 2024.06.09