Declaring a variable inside an `if` statement in Java that is a different type depending on the conditional

CpuPlayer cpu;

if (difficulty == 0){
    cpu = new EasyPlayer(num_rounds);
}
else{
    cpu = new HardPlayer(num_rounds);
}

If your intention is to call only methods available to the CpuPlayer class, then perhaps a better design pattern to use is the Strategy Pattern. In your case, you would probably add a new class called CpuStrategy, and modify your CpuPlayer constructor to something like:

public CpuPlayer(CpuStrategy strategy, int num_rounds)

This makes the rest of your code easier to read and probably easier to maintain too. Here's what your original snippet of code would look like:

CpuPlayer cpu = new CpuPlayer(new CpuStrategy(difficulty), num_rounds);

We got rid of the if/else since the CpuStrategy class will handle the difference between difficulty levels. This also makes sense since you can abstract away the notion of "difficulty levels" from the meat of your program, which I assume is the game playing part.