0

我正在使用模板方法。我有一个提供给我的驱动程序,它创建一个游戏并通过两个切换类之一,AlwaysSwitch 或 NeverSwitch。然后,驱动程序为 Always Switch 创建一个试用版和一个单独的 NeverSwitch 试用版。每个试验运行 100 次,并计算每个案例赢得比赛的次数。

这两个类都扩展了 Game。游戏有一个叫做switching()的抽象方法。两个交换类都继承了这个方法。我在两个类中都放了一个简单的打印语句。AlwaysSwitch有“我想换”,NeverSwitch有“我会留下”。

我的问题甚至是在创建 的 Always switch 试验中new Game g = new AlwaysSwitch();,输出始终是“我会留下来”。在 中new Game g = new NeverSwitch();,输出再次是“我会留下。”。

我已经尝试在驱动程序中注释掉 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.");
    }
}

所有结果都说“我会留下来”。

4

1 回答 1

0

要完成代码,您可以将签名更改switching()为:

abstract Door switching(Door contestantsChoice, Door revealedDoor);

然后你会这样调用它Game.runGame()

contestantsChoice = switching(contestantsChoice, revealedDoor);

您的AlwaysSwitch实施将是:

Door switching(Door contestantChoice, Door revealedDoor)
{
    System.out.println("I would like to switch" );
    return selectRemainingDoor(contestantChoice, revealedDoor);
}

private Door selectRemainingDoor(Door contestantChoice, Door revealedDoor) {
    List<Door> doors = new ArrayList<>(asList(LEFT, CENTER, RIGHT));
    doors.remove(contestantChoice);
    doors.remove(revealedDoor);
    return doors.get(0);
}

你的NeverSwitch实现会是这样的:

Door switching(Door contestantChoice, Door revealedDoor)
{
    System.out.println("I will stay." );
    return contestantChoice;
}
于 2019-02-18T02:57:38.320 回答