please use JUnit test for switching player class and explain SwitchPlayer.class public class SwitchingPlayer extends Player
Question:
please use JUnit test for switching player class and explain
SwitchPlayer.class
public class SwitchingPlayer extends Player {
public SwitchingPlayer(List
super(doors, monty);
}
@Override
public boolean makeSecondSelection() {
for (int i = 0; i < doors.size(); i++) {
Door d = doors.get(i);
if (!d.isOpen() && i != selection) {
selection = i;
return monty.hearSecondChoice(selection);
}
} return false;
}
}
Player.class
abstract public class Player {
protected List
protected GameshowHost monty;
protected int selection;
public Player(List
this.doors = doors;
this.monty = monty;
}
public void makeFirstSelection() {
Random r = new Random();
selection = r.nextInt(3);
monty.hearFirstChoice(selection);
}
// concrete method
// Template Method Pattern
public boolean play() {
makeFirstSelection();
boolean bDidIWin = makeSecondSelection();
return bDidIWin;
}
protected abstract boolean makeSecondSelection();
}
GameshowHost.class
public class GameshowHost {
List
public GameshowHost(List
this.doorList = doorList;
for (Door d: doorList)
d.close();
Collections.shuffle(doorList);
}
public void hearFirstChoice(int selection) {
Random r = new Random();
int reveal;
do {
reveal = r.nextInt(3);
} while (doorList.get(reveal).peek() != Prize.GOAT || reveal == selection);
doorList.get(reveal).open();
}
public boolean hearSecondChoice(int selection) {
if (selection >= 3)
throw new IllegalArgumentException("Player choice must be between 0 and 2");
Door theDoor = doorList.get(selection);
theDoor.open();
return theDoor.look() == Prize.CAR;
}
Introduction To Java Programming And Data Structures Comprehensive Version
ISBN: 9780136520238
12th Edition
Authors: Y. Daniel Liang