テンプレート方式を採用しています。ゲームを作成し、AlwaysSwitch または NeverSwitch の 2 つのスイッチング クラスのいずれかを通過するドライバーが提供されました。次に、ドライバーは、Always Switch の試用版と、NeverSwitch の別の試用版を作成します。各試行は 100 回実行され、各ケースがゲームに勝った回数が集計されます。
これらのクラスはどちらも Game を拡張します。ゲームには、switching() という抽象メソッドがあります。どちらのスイッチング クラスもこのメソッドを継承します。両方のクラスに単純な print ステートメントを配置しました。AlwaysSwitch
「乗り換えたい」とNeverSwitch
「とどまります」
私の問題は、 を作成する Always switch トライアルでもnew Game g = new AlwaysSwitch();
、出力は常に「私は留まります」です。ではnew Game g = new NeverSwitch();
、出力は再び「I will stay.」です。
ドライバーで NeverSwitch 試用版をコメント アウトしようとしましたが、そうしないと AlwaysSwitch 試用版は機能しません。
neverSwitch クラスが alwaysSwitch クラスをオーバーライドしている理由がわかりません。
public class Host {
private Door prizeDoor;
/**
* Choose a random prize door and keep it secret.
*/
public void choosePrizeDoor() {
prizeDoor = Door.randomDoor();
}
/**
* Reveal a door that does not contain the prize and does not
* match the one the contestant chose.
*/
public Door revealADoor(Door contestantChoice) {
Door result = contestantChoice;
// Randomly pick a door. Might iterate few times.
while (result == contestantChoice || result == prizeDoor) {
result = Door.randomDoor();
}
return result;
}
/**
* Determine if the contestant's door wins or loses.
*/
public boolean isAWinner(Door contestantChoice) {
return prizeDoor == contestantChoice;
}
}
/**
* An enum representing a door. Left = 1, center = 2, right = 3.
* Can also choose random doors here.
*
* @author Todd Whittaker
* @version 20180110
*/
public enum Door {
LEFT(1),
CENTER(2),
RIGHT(3);
private int value;
private static final Random r = new Random();
Door(int value) {
this.value = value;
}
/**
* Find the door that matches the number.
*/
public static Door valueOf(int num) {
switch (num) {
case 1: return LEFT;
case 2: return CENTER;
default: return RIGHT;
}
}
/**
* return the number matching this door.
*/
public int valueOf() {
return value;
}
/**
* Pick a random door (LEFT, CENTER, RIGHT).
*/
public static Door randomDoor() {
return Door.valueOf(r.nextInt(3)+1);
}
}
public abstract class Game {
private Host host;
public Door contestantChoice;
public Door revealedDoor;
/**
* Creates the game and its host.
*/
public Game () {
this.host = new Host();
}
/**
* Implements the algorithm for the game:
* 1. The host chooses which door has a prize at random.
* 2. The contestant picks a door (left, center, or right) at random.
* 3. The host reveals one of the two other doors that does not contain
the prize.
* 4. The contestant can then switch to the other door or keep their
current door.
* 5. The prize is revealed and the contestant wins or loses.
*/
final boolean runGame() {
host.choosePrizeDoor(); //1
System.out.println("Let's Play");
contestantChoice = contestantChoice.randomDoor();//2
System.out.println("Contestant chooses " + contestantChoice);
revealedDoor = host.revealADoor(contestantChoice); //3
System.out.println("reveal door " + revealedDoor);
System.out.println("would you like to switch?");
switching();
if(host.isAWinner(contestantChoice) == true)
{
System.out.println("Winner!! \n");
return true;
}
System.out.println("Sorry you lose \n");
return false;
}
abstract void switching();
}
public class AlwaysSwitch extends Game {
void switching()
{
System.out.println("I would like to switch" );
}
}
public class NeverSwitch extends Game {
void switching()
{
System.out.println("I will stay." );
}
}
public class Driver {
private static final int TRIALS = 100;
public static void main(String [] args) {
int switchWins = 0;
int stayWins = 0;
for (int i = 0; i < TRIALS; ++i) {
Game g = new AlwaysSwitch();
if (g.runGame()) {
++switchWins;
}
}
for (int i = 0; i < TRIALS; ++i) {
Game g = new NeverSwitch();
if (g.runGame()) {
++stayWins;
}
}
System.out.println("Out of " + TRIALS + " trials:");
System.out.println(" Switch won " + switchWins + " times.");
System.out.println(" Stay won " + stayWins + " times.");
}
}
すべての結果は「私は残る」と言っています。