2020.10.29 고급자바 과제 경마게임 코드

*정렬하는법 


방법 1) 

Arrays.sort(horses) 

//horses 는 배열 




방법 2 ) 배열의 데이터를 List 에 담고 List 를 정렬하여 출력하기 


ArrayList<Horse> horseList = new ArrayList<>(); 

for ( Horse h : horses ) { 

    horseList.add(h);

}


Collection.sort(horseList) 


for(Horse h : horseList) {

    System.out.println(h) 

}



*

 * 문제 ) 10마리의 말들이 경주하는 경마 프로그램 작성하기 

 * 

 * 경주마는 Horse 라는 이름의 클래스로 구성하고 

 * 이 클래스는 말이름(String ), 등수 ( int ) , 현재위치 (int ) 를 멤버변수로 갖는다. // 

 * 그리고 이 클래스에는0 등수를 오름차순으로 처리하는 내부 정렬 기준이 있다. 

 * (Comparable 인터페이스 구현하기 ) 

 * - 이 Horse 클래스는 쓰레드로 작성한다. //

 * - 경기구간은 1 ~ 50 구간으로 되어 있다. //

 * - 경기 중 중간 중간에 각 말들의 위치를 나타내시오. 

 *

 * 예 ) 01번 말 :  ------>-----------------------------------

 * 예 ) 02번 말 :  ---->-------------------------------------

 * 예 ) 03번 말 :  ---------------->-------------------------

 * 예 ) 04번 말 :  ---------->-------------------------------

 * 예 ) 05번 말 :  ----------------->------------------------

 *  ........

 *  예 ) 10번 말 : ------------------------------>------------

 *  

 *  -경기가 끝나면 등수 순으로 경기 결과를 출력한다. 

 * 

 * 

 */

 


package kr.or.ddit.basic;


import java.util.ArrayList;


public class Thread14 {


public static void main(String[] args) {

ArrayList<Horse> horseList = new ArrayList<>();

//Horse horses = new Horse ("1번말");

horseList.add(new Horse("1번말", 0 , 0 ));

horseList.add(new Horse("2번말", 0 , 0 ));

horseList.add(new Horse("3번말", 0 , 0 ));

horseList.add(new Horse("4번말", 0 , 0 ));

horseList.add(new Horse("5번말", 0 , 0 ));

horseList.add(new Horse("6번말", 0 , 0 ));

horseList.add(new Horse("7번말", 0 , 0 ));

horseList.add(new Horse("8번말", 0 , 0 ));

horseList.add(new Horse("9번말", 0 , 0 ));

horseList.add(new Horse("10번말", 0 , 0 ));

for( Horse horse  : horseList) { 

horse.start();

}

System.out.println();

//horseList 배열에 thread 여러개를 넣은것이다. 

} //main 닫기


} //Thread14 닫기 







class Horse extends Thread implements Comparable<Horse> {

private String horseName ; //말의 이름

public static int rank = 1 ;  // 말의 등수 

int nowNumber ; //말의 현재 위치 

Horse (String name , int rank, int nowNumber) {

this.horseName = name ;

this.rank = rank; 

this.nowNumber = nowNumber; 

}

@Override

public void run() {

for(int i = 1 ; i <=10 ; i++) { 

nowNumber = i ; 

System.out.println(horseName + "말의 " + nowNumber + "번째 완주중");

try {

Thread.sleep((int)(Math.random() * 400 + 101 )); 

} catch (InterruptedException e) {

// 

}

} //for 문 끝 

System.out.println(horseName + "말의 완주 끝 " + "등수 : " + rank);

rank++;

} //run 메소드 









public String getHorseName() {

return horseName;

}






public void setHorseName(String horseName) {

this.horseName = horseName;

}






public static  int getRank() {

return rank;

}






public static  void setRank(int rank) {

Horse.rank = rank;

}






public  int getNowNumber() {

return nowNumber;

}






public void setNowNumber(int nowNumber) {

this.nowNumber = nowNumber;

}






@Override

public int compareTo(Horse o) {

// TODO Auto-generated method stub

return new Integer(rank).compareTo(o.getRank()); 

}

} // horse 클래스끝



-----------------------------------------------------------------------------------------------------

import java.util.ArrayList;

import java.util.HashMap;


public class Test {

public static void main(String[] args) {

Horse horse = new Horse("1번째말", 0 , 0);

horse.start();

new Horse("2번째말", 0 , 0);

new Horse("3번째말", 0 , 0);

new Horse("4번째말", 0 , 0);

new Horse("5번째말", 0 , 0);

new Horse("6번째말", 0 , 0);

new Horse("7번째말", 0 , 0);

new Horse("8번째말", 0 , 0);

new Horse("9번째말", 0 , 0);

new Horse("10번째말", 0 , 0);

} //main 클래스

} // horse 클래스끝



class Horse extends Thread {

public static ArrayList<HashMap<String, Integer>> list = new ArrayList <> (); 

public static HashMap<String , Integer> map = new HashMap(); 

int rank ; 

String name ; 

int whereNow; 


Horse (String name, int rank, int whereNow){

this.name = name;

this.rank = rank;

this.whereNow = whereNow; 

}


@Override

public void run() {

for ( int i = 1 ; i <= 15 ; i++){

System.out.println(name + "말은 지금" + i + "회를 돌고 있습니다." );

if ( i == 15  ) { 

}

try {

Thread.sleep((int)(Math.random() * 400 + 100) ); 

} catch (Exception e) {

}

} //for 문 끝

rank++; 

System.out.println(name + "말이 완주했습니다." + rank );

String name_ = name ; 

int rank_ = rank ; 

map.put(name_, rank_); 

list.add(map); 

}


}



-----------------------------------------------------------------------------------------------------



package kr.or.ddit.basic;


import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;


/*

문제) 10마리의 말들이 경주하는 경마 프로그램 작성하기

경주마는 Horse라는 이름의 클래스로 구성하고

이 클래스는 말이름(String), 등수(int), 현재위치(int)를 멤버변수로 갖는다.

그리고, 이 클래스에는 등수를 오름차순으로 처리하는 내부 정렬 기준이 있다.

       (Comparable인터페이스 구현하기)

- 이 Horse클래스는 쓰레드로 작성한다.

- 경기 구간은 1 ~ 50구간으로 되어 있다.

- 경기 중 중간 중간에 각 말들의 위치를 나타내시오.

예)

01번말 : ----->---------------------------------

02번말 : --->-----------------------------------

...

10번말 : ------>--------------------------------

- 경기가 끝나면 등수 순으로 경기 결과를 출력한다.


*/

public class ThreadTest13 {


public static void main(String[] args) {

Horse[] horses = new Horse[] {

new Horse("01번말"), new Horse("02번말"), new Horse("03번말"),

new Horse("04번말"), new Horse("05번말"), new Horse("06번말"),

new Horse("07번말"), new Horse("08번말"), new Horse("09번말"),

new Horse("10번말")

};


GameSate gs = new GameSate(horses);

System.out.println("경기 시작...");

for(Horse h : horses) {

h.start();

}

gs.start();

for(Horse h : horses) {

try {

h.join();

} catch (InterruptedException e) {

}

}

try {

gs.join();

} catch (InterruptedException e) {

}

System.out.println("경기 끝...");

/*

// 방법1) 배열을 직접 정렬하고 출력하기

// 정렬

Arrays.sort(horses);

// 출력

for(Horse h : horses) {

System.out.println(h);

}

*/

// 방법2) 배열의 데이터를 List에 담고 List를 정렬하여 출력하기

ArrayList<Horse> horseList = new ArrayList<>();

for(Horse h : horses) {

horseList.add(h);

}

Collections.sort(horseList);

for(Horse h : horseList) {

System.out.println(h);

}

}


}


/*

경주마는 Horse라는 이름의 클래스로 구성하고

이 클래스는 말이름(String), 등수(int), 현재위치(int)를 멤버변수로 갖는다.

그리고, 이 클래스에는 등수를 오름차순으로 처리하는 내부 정렬 기준이 있다.

       (Comparable인터페이스 구현하기)

- 이 Horse클래스는 쓰레드로 작성한다.

- 경기 구간은 1 ~ 50구간으로 되어 있다.

*/

class Horse extends Thread implements Comparable<Horse>{

public static int currentRank = 0; // 말들의 등수 계산에 사용되는 변수 선언

private String horseName; // 말이름

private int rank; // 등수

private int position; // 현재 위치

// 생성자

public Horse(String horseName) {

super();

this.horseName = horseName;

}


public String getHorseName() {

return horseName;

}


public void setHorseName(String horseName) {

this.horseName = horseName;

}


public int getRank() {

return rank;

}


public void setRank(int rank) {

this.rank = rank;

}


public int getPosition() {

return position;

}


public void setPosition(int position) {

this.position = position;

}


@Override

public String toString() {

return "경주마 " + horseName + "의 등수는 " + rank + "등 입니다.";

}

// 등수를 오름차순

@Override

public int compareTo(Horse horse) {

return Integer.compare(rank, horse.getRank());

}

// 말이 달리는 부분을 쓰레드로 처리한다.

@Override

public void run() {

for(int i=1; i<=50; i++) {

position = i; // 말의 현재 위치를 저장한다.

try {

Thread.sleep((int)(Math.random() * 500));

} catch (InterruptedException e) {

// TODO: handle exception

}

} // for문

// 한 마리의 말이 경주가 끝나면 등수를 구해서 rank에 설정한다.

currentRank++;

rank = currentRank;

}

}


/*

- 경기 중 중간 중간에 각 말들의 위치를 나타내시오.

예)

01번말 : ----->---------------------------------

02번말 : --->-----------------------------------

...

10번말 : ------>--------------------------------

*/

// 경기중 말의 현재 위치를 출력하는 쓰레드

class GameSate extends Thread{

private Horse[] ho;

public GameSate(Horse[] ho) {

this.ho = ho;

}

@Override

public void run() {

while(true) {

// 모든 말들의 경주가 끝났는지 여부를 검사한다.

if(Horse.currentRank==ho.length) {

break;

}

// 빈 줄 출력하기

for(int i=1; i<=15; i++) {

System.out.println();

}

for(int i=0; i<ho.length; i++) {

System.out.print(ho[i].getHorseName() + " : ");

for(int j=1; j<=50; j++) {

if(ho[i].getPosition()==j) {

System.out.print(">");

}else {

System.out.print("-");

}

}

System.out.println();

}

try {

Thread.sleep(100);

} catch (InterruptedException e) {

// TODO: handle exception

}

}

}

}









댓글