반응형
안녕하세요.
Java를 이용해서 콘솔 창에 야구의 경기결과가 찍히는 프로그램을 짜보았습니다.
1. 투수가 공을 던지면 타자가 결과 값을 반환 하게 되고, 결과는 스트라이크, 볼, 안타, 홈런이 랜덤의 확률로 결정됩니다.
홈런 1%
안타 8%
볼 30%
스트라이크 70%
확률은 아웃을 잡는게 삼진아웃밖에 없어서 확률을 조정하기 위해서 스트라아크의 비율이 압도적으로 높습니다.(이렇게 해야 한 자리 수의 점수대가 나옵니다..ㅠㅠ)
2. 주자가 출루하게 되면 배열을 통해서 현재 나가 있는 주자를 확인하고 득점을 하게 됩니다. 홈런을 하게 된다면 현재 나가 있는 주자와 자신을 포함해서 득점을 하게 됩니다.
실행결과
.....
코드
Program.java
1 2 3 4 5 6 7 8 9 10 11 | public class Program { public static void main(String[] args) { Game game = new Game("롯데", "SK"); game.Start(); System.out.println("경기가 종료되었습니다."); System.out.print("최종 스코어는 "); game.End(); } } | cs |
Team.java
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 | public class Team { private String Name; private int Score; //점수 private int BaseCount=0; Pitcher pitcher; Hitter hitter; public Team(String name) { this.Name = name; this.Score = 0; pitcher = new Pitcher(); hitter = new Hitter(); this.resetBase(); } public void showInfo() { System.out.print(Name + " : " + Score); } public void resetBase() { BaseCount=0; } public int getBaseCount() { return BaseCount; } public void setBaseCount(int baseCount) { BaseCount = baseCount; } public String getName() { return Name; } public void setName(String name) { Name = name; } public int getScore() { return Score; } public void setScore(int score) { Score = score; } } | cs |
IPitcher.java(interface)
Pitcher.Java
1 2 3 4 5 6 7 8 9 10 11 12 | public class Pitcher implements IPitcher{ @Override public int pitch(Hitter hitter) { int num = hitter.Hit(); return num; } } | cs |
IHitter.java
1 2 3 4 5 | public interface IHitter { int Hit(); } | cs |
Hitter.java
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 | import java.util.Random; public class Hitter implements IHitter{ @Override public int Hit() { // TODO Auto-generated method stub Random r = new Random(); int num = r.nextInt(99)+1; if(num<2){ return 4; //HomeRun }else if(2 < num && num < 10) { return 3; //Hit }else if(2 < num && num < 30) { return 2; //Ball }else if(30 < num && num < 101) { return 1; //Strike } return 1; } } | cs |
IGame.java (interface)
1 2 3 4 5 6 7 8 | public interface IGame { void OnBase(int n); void Out(); void StrikeBallReset(); void HomeRun(int n); void CluchHit(int n); } | cs |
Game.java
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 186 187 188 189 190 191 192 | public class Game implements IGame{ Team[] team; private int Strike = 0; private int Ball = 0; private int Out = 0; public Game(String name1, String name2) { team = new Team[2]; team[0] = new Team(name1); team[1] = new Team(name2); } public void Start() { System.out.println("게임을 시작합니다."); team[0].showInfo(); System.out.print(" VS "); team[1].showInfo(); System.out.println(""); for (int i = 0; i < 9; i++) { this.countReset(); System.out.println(""); System.out.println((i + 1) + "회가 시작되었습니다."); this.inning(0); this.inning(1); System.out.println((i + 1) + "회가 종료되었습니다."); this.scoreDisplay(); System.out.println(""); } } public void End() { this.scoreDisplay(); } public void countReset() { this.Strike = 0; this.Ball = 0; this.Out = 0; } public void scoreDisplay() { System.out.print(team[0].getName() + " " + team[0].getScore()); System.out.print(" VS "); System.out.println(team[1].getName() + " " + team[1].getScore()); } public void countDisplay() { System.out.println("\t\t\t\tStrike : " + Strike); System.out.println("\t\t\t\tBall : " + Ball); System.out.println("\t\t\t\tOut :" + Out); } public void Out() { Out++; Strike = 0; Ball = 0; } public void showBase(Team team) { int count = team.getBaseCount(); if(count == 0) { System.out.println("주자가 없습니다."); } else if(count == 1) { System.out.println("\t\t\t\t1루 주자"); } else if(count == 2) { System.out.println("\t\t\t\t1루, 2루 주자"); } else if(count == 3) { System.out.println("\t\t\t\t1루, 2루, 3루 주자"); } } public void inning(int n) { int result; team[n].setBaseCount(0); System.out.println(team[n].getName() + "의 공격입니다."); this.countReset(); while (true) { if (Out < 3) { if(n==0) // Home팀인지 Away인 지 확인.. HardCoding result = team[1].pitcher.pitch(team[n].hitter); else result = team[0].pitcher.pitch(team[n].hitter); switch (result) { case 1: // Strike System.out.println("Strike"); Strike++; if (Strike == 3) { System.out.println("3 Strike Out"); this.Out(); } this.countDisplay(); break; case 2: // Ball System.out.println("Ball"); Ball++; if (Ball == 4) { // 출루 int BaseCount = team[n].getBaseCount(); if(BaseCount == 3) //주자 만루 4ball { System.out.println("4Ball"); this.CluchHit(n); }else { //그냥 4Ball System.out.println("4Ball"); this.OnBase(n); } //this.StrikeBallReset(); //this.showBase(team[n]); } this.countDisplay(); break; case 3: // 안타 System.out.println("Hit"); int BaseCount = team[n].getBaseCount(); if(BaseCount >= 3) //주자가 3루 주자 이상 일 때, { this.CluchHit(n); //적시타 }else { this.OnBase(n); //출루 } break; case 4: // 홈런 this.HomeRun(n); //홈런 break; } }else { break; } } } @Override public void OnBase(int n) { int BaseCount = team[n].getBaseCount(); team[n].setBaseCount(++BaseCount); this.StrikeBallReset(); this.showBase(team[n]); } @Override public void StrikeBallReset() { Strike = 0; Ball = 0; } @Override public void HomeRun(int n) { System.out.println("Homerun"); int num = team[n].getScore(); int BaseCount2 = team[n].getBaseCount(); num++; num+=BaseCount2; team[n].setScore(num); this.showBase(team[n]); this.scoreDisplay(); this.StrikeBallReset(); team[n].setBaseCount(0); } @Override public void CluchHit(int n) { int num = team[n].getScore(); num++; team[n].setScore(num); this.StrikeBallReset(); this.scoreDisplay(); this.showBase(team[n]); } } | cs |
728x90
반응형