▶ shuffle() 메소드
컬렉션의 shuffle(메소드)는 리스트와 같은 컬렉션에서 배열안에 있는 데이터를 랜덤으로 섞어주는 기능을 한다.
▶ shuffle() 예제(1)
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Suffle {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for(int i = 0; i <= 10; i++)
list.add(i);
//Collections.shuffle 메소드를 활용해 랜덤으로 데이터를 출력
Collections.shuffle(list);
System.out.println(list);
}
}
shuffle() 메소드로 list 안에 내용들을 랜덤으로 섞어주고 출력하였다.
shuffle() 메소드를 사용하려면 java.util.Collections를 import 해주어야 한다.
▶ shuffle() 예제(2)
Card : 카드 저장 및 출력
Player : 카드를 획득하는 플레이어
Deck : 카드의 경우의 수를 저장하고 shuffle()을 이용해 랜덤으로 섞어줌
CardGame : main문 실행
//카드 저장 클래스
public class Card {
String suit;
String number;
public Card(String suit, String number) {
this.suit = suit;
this.number = number;
}
//Card객체에 저장된 데이터를 표현하기 위해 toString 메소드를 활용
@Override
public String toString() {
return "(" + suit + " " + number + ")";
}
}
import java.util.ArrayList;
//카드를 획득하는 플레이어 클래스
public class Player {
ArrayList<Card> card = new ArrayList<Card>();
//Card 타입 ArrayList 객체 생성
public void getCard(Card card) {
this.card.add(card);
}
public void showCards() {
System.out.println(card);
}
}
import java.util.ArrayList;
import java.util.Collections;
//카드를 랜덤으로 섞어주는 클래스
public class Deck {
//Card 타입의 ArrayList 객체를 생성하고 배열에 카드모양와 숫자생성
ArrayList<Card> deck = new ArrayList<Card>();
String[] suit = {"CLUB", "DIAMOND", "HEART", "SPACE"};
String[] number = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
public Deck() { //전체카드(경우의 수) 전체 deck ArrayList에 저장
for(int i = 0; i < suit.length; i++){
for(int j = 0; j < number.length; j++)
deck.add(new Card(suit[i],number[j]));
}
}
public void shuffle() {
//Collections.shuffler : 저장된 데이터들을 랜덤으로 출력한다.
//deck ArrayList에 저장된 데이터들 중에서 랜덤으로 출력
Collections.shuffle(deck);
}
public Card deal() {
return deck.remove(0);
} //카드게임에서 카드 한 장 나눠줄 때를 구현
//remove() : 리스트에서 해당 인덱스의 값을 제거하고, 그 값을 반환
//즉, deal() 메소드는 deck()메소드에서 52개의 Card객체를
//만들어 준 것중에 첫번째 요소를 제거하고 그 제거한 요소를 반환한다
}
public class CardGame {
public static void main(String[] args) {
//Deck 객체를 생성하고
//Deck 기본 생성자를 실행하면, for문을 활용해 카드 정보들이 저장됨
Deck deck = new Deck();
//객체를 활용해 shuffle 메소드를 실행함, 랜덤으로 저장된 값 중 하나를 획득함
deck.shuffle();
//카드를 획득하는 Player 객체 생성
Player p1 = new Player();
Player p2 = new Player();
//getCard 메소드를 실행해 덱에서 카드 정보를 ArrayList 객체에 저장함
//deal 메소드로 52개의 Card객체 중 하나를 가져와서 Player 클래스의 getCard 메소드로 보낸다.
//그럼 Player 클래스는 이 받아온 Card 객체를 멤버 변수에 담아놨다가
//ShowCards 메소드 호출로 출력
p1.getCard(deck.deal());
p2.getCard(deck.deal());
//저장된 데이터를 출력함
p1.showCards();
p2.showCards();
}
}
랜덤 카드를 뽑아 대결하는 간단한 코드를 만들어봤다.
이 코드를 베이스로 몇 명의 플레이어의 게임인지, 승/패 출력과 같은 코드를 덧붙여 게임을 만들어 볼 수 있다.
'JAVA' 카테고리의 다른 글
java 내부클래스를 이용하여 ActionListner 이벤트 만들기 (0) | 2022.04.10 |
---|---|
JAVA 파일 입출력과 스트림(FileInputStream, InputStreamReader, FileReader) (0) | 2022.04.06 |
[java] Set이란? (+HashSet, TreeSet 예제) (0) | 2022.04.03 |
[java] Map예제 - map을 이용해 이름으로 전화번호 검색 하기 (0) | 2022.04.02 |
java 컬렉션(Collection)이란? (+ 예제) (0) | 2022.03.29 |
댓글