반응형
중간고사 시험때 짠 소스입니다.
야구 경기 시뮬레이션 소스 입니다.각팀의 전력은 default값 기준으로 랜덤입니다. 10개팀이 있으며 한팀당 각팀이랑 16번씩 경기를 합니다.
그러면 한팀당 하는 경기수는 9×16=144경기겠지요? 3루에 선수가 있을때 볼넷이면 전진안한다는데 저는 그냥 전진해서 점수를 따는 식으로 짰습니다.(야못알의 흔한 폐혜)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class Main { public static void main(String[] args) { // TODO Auto-generated method stub KBO SEASON2017 = new KBO("마린갓"); SEASON2017.KBOMenu(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | import java.util.Scanner; public class KBO { Team[] team = new Team[10]; public KBO() { for (int i = 0; i < 10; i++) team[i] = new Team(); team[0].teamName = "KIA"; team[1].teamName = "KT"; team[2].teamName = "SK"; team[3].teamName = "LOTTE"; team[4].teamName = "LG"; team[5].teamName = "DOOSAN"; team[6].teamName = "HANWHA"; team[7].teamName = "NEXEN"; team[8].teamName = "SAMSUNG"; team[9].teamName = "MYTEAM"; } public KBO(String myTeam) { for (int i = 0; i < 10; i++) team[i] = new Team(); team[0].teamName = "KIA"; team[1].teamName = "KT"; team[2].teamName = "SK"; team[3].teamName = "LOTTE"; team[4].teamName = "LG"; team[5].teamName = "DOOSAN"; team[6].teamName = "HANWHA"; team[7].teamName = "NEXEN"; team[8].teamName = "SAMSUNG"; team[9].teamName = myTeam; } void KBOMenu() { int num; vs oneGame; Scanner s=new Scanner(System.in); while (true) { System.out.println("====== KBO 2017 ======"); System.out.println("<1>각팀 전력보기 <2>KBO2017 경기시작 <3>팀성적 "); System.out.println("<4>팀별 베스트히터 <5>팀별 베스트홈런왕 <6>팀별 홈런개수 <7>종료"); num=s.nextInt(); if (num == 7) { System.out.println("프로그램을 종료합니다.\n"); break; } switch (num) { case 1: viewTeamAbility(); break; case 2: for (int i = 0; i < 8; i++) { for (int j = 0; j < 10; j++) { for (int k = 0; k < 10; k++) { if (j == k) continue; oneGame = new vs(); oneGame.teamFight(team[j], team[k], oneGame); } } } break; case 3: for(int i=0;i<10;i++) System.out.printf("%s팀의 성적 -> 승:%d 무:%d 패:%d\n",team[i].teamName,team[i].winCount,team[i].drawCount,team[i].loseCount); break; case 4: int n; System.out.println("==== KBO2017 팀별 best Hit player 입니다... ===="); for(int i=0;i<10;i++) { n=searchBestHitter(team[i]); System.out.printf("%s best Hit player(%d번 타자) 타율: %.3f (%d타수 %d 안타)\n",team[i].teamName,n+1,(double)team[i].safetyBall[n]/team[i].timeAtBat[n],team[i].timeAtBat[n],team[i].safetyBall[n]); } break; case 5: int k=0; System.out.println("==== KBO2017 팀별 best HR player 입니다.... ===="); for(int i=0;i<10;i++) { k=searchBestHomeRun(team[i]); System.out.printf("%s best HR player(%d번 타자) 홈런: %d개 타율 %.3f\n",team[i].teamName,k+1,team[i].hitHomeRun[k],(double)team[i].safetyBall[k]/team[i].timeAtBat[k]); } break; case 6: System.out.println("KBO 2017 팀별 홈런 개수입니다..."); int []result=new int[10]; int []value1=new int[10]; int []value2=new int[10]; for(int i=0;i<10;i++){ result[i]=0; value1[i]=0; value2[i]=0; } for(int i=0;i<10;i++) { for(int j=0;j<9;j++){ result[i]+=team[i].hitHomeRun[j]; value1[i]+=team[i].timeAtBat[j]; value2[i]+=team[i].safetyBall[j]; } } for(int i=0;i<10;i++) System.out.printf("%s 총 홈런개수 %d 타율 %.3f\n",team[i].teamName,result[i],(double)value2[i]/value1[i]); } } s.close(); } void viewTeamAbility() { for (int i = 0; i < 10; i++) { System.out.printf("%s 팀 전력 :", team[i].teamName); System.out.printf(" %.3f(1루타: %.3f%% 2루타: %.3f%% 3루타: %.3f%% 홈런: %.3f%%) - 땅볼/아웃: %.3f%%\n", team[i].oneHitRatio + team[i].twoHitRatio + team[i].threeHitRatio + team[i].hrHitRatio, team[i].oneHitRatio, team[i].twoHitRatio, team[i].threeHitRatio, team[i].hrHitRatio, team[i].hitOutRatio); } } int searchBestHitter(Team a) { double []ratio=new double[9]; double best=0; int num=0; for(int i=0;i<9;i++) ratio[i]=(double)a.safetyBall[i]/a.timeAtBat[i]; for(int i=0;i<9;i++) { if(ratio[i]>best) { best=ratio[i]; num=i; } } return num; } int searchBestHomeRun(Team a) { int best=0,num=0; for(int i=0;i<9;i++) { if(a.hitHomeRun[i]>best) { best=a.hitHomeRun[i]; num=i; } } return num; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | public class Team { String teamName; double strikeRatio=20; double ballRatio=40; double hitOutRatio=70; double oneHitRatio=10; double twoHitRatio=10; double threeHitRatio=5; double hrHitRatio=5; int winCount=0; int drawCount=0; int loseCount=0; int []timeAtBat=new int[9]; int []safetyBall=new int[9]; int []hitHomeRun=new int[9]; public Team() { for(int i=0;i<9;i++) { timeAtBat[i]=0; safetyBall[i]=0; hitHomeRun[i]=0; } double rand=Math.random()*10; rand+=-5; hitOutRatio+=rand; oneHitRatio-=rand/3; twoHitRatio-=rand/4; threeHitRatio-=rand/4; hrHitRatio-=rand/6; } public Team(String teamName) { this.teamName=teamName; double rand=Math.random()*10; rand+=-5; hitOutRatio+=rand; oneHitRatio-=rand/3; twoHitRatio-=rand/3; threeHitRatio-=rand/6; hrHitRatio-=rand/6; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | //KBO에서 두팀에 대해 입력받을꺼니까 매번 초기화<<KBO에서 매번 생성 //전반(끝나고base리셋, 후반) //두팀 입력받으면 두팀이 싸우는 함수 public class vs { int team1Point; int team2Point; int []base=new int[3]; int []team1TakeResult=new int[9]; int []team1HitterResult=new int[9]; int []team2TakeResult=new int[9]; int []team2HitterResult=new int[9]; public vs() { team1Point=0; team2Point=0; for(int i=0;i<3;i++) base[i]=0; for(int i=0;i<9;i++) { team1TakeResult[i]=0; team1HitterResult[i]=0; team2TakeResult[i]=0; team2HitterResult[i]=0; } } void teamFight(Team a,Team b, vs c) { baseballplay game=new baseballplay(); int num; System.out.printf("==== 경기시작 %s팀 VS %s팀 ====\n",a.teamName,b.teamName); for(int i=0;i<9;i++) { c.team1TakeResult[i]=a.timeAtBat[i]; c.team1HitterResult[i]=a.safetyBall[i]; c.team2TakeResult[i]=b.timeAtBat[i]; c.team2HitterResult[i]=b.safetyBall[i]; } for(int i=1;i<10;i++) { System.out.printf("**%s팀 %d회초 공격시작**\n",a.teamName,i); num=game.hit(c, a, 1); System.out.printf("->%d 회초 %s팀 : %d점, %s팀 : %d점\n",i,a.teamName,team1Point,b.teamName,team2Point); System.out.printf("===%s팀 %d점 증가===\n",a.teamName,num); for(int j=0;j<3;j++) base[j]=0; System.out.printf("**%s팀 %d회말 공격시작**\n",b.teamName,i); num=game.hit(c, b, 2); System.out.printf("->%d 회말 %s팀 : %d점, %s팀 : %d점\n",i,a.teamName,team1Point,b.teamName,team2Point); System.out.printf("===%s팀 %d점 증가===\n",b.teamName,num); for(int j=0;j<3;j++) base[j]=0; } System.out.printf("====경기종료 %s팀:%d점, %s팀:%d점====\n",a.teamName,team1Point,b.teamName,team2Point); if(team1Point>team2Point) { System.out.println("경기결과>> "+a.teamName+"팀 승리!\n"); a.winCount++; b.loseCount++; } else if(team1Point==team2Point) { System.out.println("경기결과>> 무승부!\n"); a.drawCount++; b.drawCount++; } else { System.out.println("경기결과>> "+b.teamName+"팀 승리!\n"); a.loseCount++; b.winCount++; } System.out.printf("****%s팀 개인성적\n",a.teamName); for(int i=0;i<9;i++) { System.out.printf("[%d번타자] %d타수 %d안타\n",i+1,a.timeAtBat[i]-c.team1TakeResult[i],a.safetyBall[i]-c.team1HitterResult[i]); } System.out.printf("****%s팀 개인성적\n",b.teamName); for(int i=0;i<9;i++) { System.out.printf("[%d번타자] %d타수 %d안타\n",i+1,b.timeAtBat[i]-c.team2TakeResult[i],b.safetyBall[i]-c.team2HitterResult[i]); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | public class baseballplay { int hit(vs a, Team b, int teamNum) { int hitterNum = 0; int ball = 0,count=0; int strike = 0; int out = 0; double hit; if(teamNum==1) count=a.team1Point; else if(teamNum==2) count=a.team2Point; while (out != 3) { hitterNum++; b.timeAtBat[(hitterNum - 1) % 9]++; System.out.printf("[%d번 타자] ", hitterNum % 9); while (true) { System.out.printf("(%dS,%dB)", strike, ball); hit = Math.random() * 100; if (hit < b.ballRatio)// 볼 ball++; else if (hit < b.ballRatio + b.strikeRatio)// 스트라이크 strike++; else {// 쳣어 hit = Math.random() * 100; if (hit < b.hitOutRatio) {// 땅볼/뜬공 out++; System.out.printf(", 결과 -> 땅볼/뜬공(%d아웃)(주자: %d, %d, %d)\n", out, a.base[0], a.base[1], a.base[2]); break; } else if (hit < b.hitOutRatio + b.oneHitRatio)// 1힛 { b.safetyBall[(hitterNum - 1) % 9]++; movePlayer(a, 1, teamNum); System.out.printf(", 결과-> 1루타(%d아웃)(주자: %d, %d, %d)\n", out, a.base[0], a.base[1], a.base[2]); break; } else if (hit < b.hitOutRatio + b.oneHitRatio + b.twoHitRatio)// 2힛 { b.safetyBall[(hitterNum - 1) % 9]++; movePlayer(a, 2, teamNum); System.out.printf(", 결과-> 2루타(%d아웃)(주자: %d, %d, %d)\n", out, a.base[0], a.base[1], a.base[2]); break; } else if (hit < b.hitOutRatio + b.oneHitRatio + b.twoHitRatio + b.threeHitRatio)// 3힛 { b.safetyBall[(hitterNum - 1) % 9]++; movePlayer(a, 3, teamNum); System.out.printf(", 결과-> 3루타(%d아웃)(주자: %d, %d, %d)\n", out, a.base[0], a.base[1], a.base[2]); break; } else// 홈런! { b.safetyBall[(hitterNum - 1) % 9]++; b.hitHomeRun[(hitterNum - 1) % 9]++; movePlayer(a, 4, teamNum); System.out.printf(", 결과-> 홈런!!(%d아웃)(주자: %d, %d, %d)\n", out, a.base[0], a.base[1], a.base[2]); break; } } if (ball == 4) { movePlayer(a, 1, teamNum); System.out.printf(", 결과-> 볼넷(%d아웃)(주자: %d, %d, %d)\n", out, a.base[0], a.base[1], a.base[2]); break; } if (strike == 3) { out++; System.out.printf(", 결과-> 삼진(%d아웃)(주자: %d, %d, %d)\n", out, a.base[0], a.base[1], a.base[2]); break; } } ball = 0; strike = 0; } if(teamNum==1) return (a.team1Point-count); else return a.team2Point-count; } void movePlayer(vs a, int plusNum, int teamNum) {// 진보할 갯수, 팀 1, 2 if(plusNum==1) { for(int i=2;i>=0;i--) { if(a.base[i]==1) { if(i==2) { a.base[i]=0; if(teamNum==1) a.team1Point++; else if(teamNum==2) a.team2Point++; else System.out.println("오류"); } else{ a.base[i+1]=1; a.base[i]=0; } } } a.base[0]=1; } else if(plusNum==2) { for(int i=2;i>=0;i--) { if(a.base[i]==1) { if(i==1||i==2) { a.base[i]=0; if(teamNum==1) a.team1Point++; else if(teamNum==2) a.team2Point++; else System.out.println("오류"); } else { a.base[i+2]=1; a.base[i]=0; } } } a.base[1]=1; } else if(plusNum==3) { for(int i=2;i>=0;i--) { if(a.base[i]==1) { a.base[i]=0; if(teamNum==1) a.team1Point++; else if(teamNum==2) a.team2Point++; } } a.base[2]=1; } else if(plusNum==4) { for(int i=2;i>=0;i--) { if(a.base[i]==1) { if(teamNum==1) a.team1Point++; else if(teamNum==2) a.team2Point++; a.base[i]=0; } } if(teamNum==1) a.team1Point++; else if(teamNum==2) a.team2Point++; } } } |
※과제로 복붙해서 내시는 분들 있기 때문에 복붙 방지가 되어있습니다. 스스로 쳐보고 소스를 이해해주시길 바랍니다. 본 블로그는 실력을 향상하기 위해서 있는거지 과제를 복붙하기 위해 있는게 아님을 알려드립니다.
※이번 소스는 너무 길어서 소스 파일을 공유 합니다... 소스가 주어졌다 하더라도 자기가 스스로 짜보시면 좋겠습니다.
by여러분의 실력이 늘기 바라는 마린<<
반응형
'Coding Challenge' 카테고리의 다른 글
[소스공유] C++로 엘리베이터 구현하기 (0) | 2017.05.27 |
---|---|
[소스공유] C언어로 만든 지뢰찾기 (2) | 2017.05.24 |
[소스공유/c언어] 후위연산자 계산기 (2) | 2017.04.29 |
[소스공유/C언어] 다항식 연산 프로그램 (1) | 2017.04.29 |
프로그래밍 공부법 (코딩 실력 늘리기) (9) | 2017.04.29 |