This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Minimax | |
def choose_move(board, current_player, opponent) | |
copy_board = board.copy_board | |
find_best_move(copy_board, current_player, opponent) | |
end | |
private | |
def find_best_move(board, depth=0, current_player, opponent) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def score_move(board, depth, current_player, opponent) | |
if board.winning_line?(current_player) | |
10 - depth | |
elsif board.winning_line?(opponent) | |
depth - 10 | |
elsif board.complete? | |
0 | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Minimax | |
def choose_move(board, current_player, opponent) | |
copy_board = board.copy_board | |
find_best_move(copy_board, current_player, opponent) | |
end | |
private | |
def find_best_move(board, depth=0, current_player, opponent) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def evaluate_move(depth, scores) | |
depth.zero? ? max_best_move(scores) : max_best_score(scores) | |
end | |
def max_best_move(scores) | |
scores.max_by { |_key, value| value }[0] | |
end | |
def max_best_score(scores) | |
scores.max_by { |_key, value| value }[1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
available_squares.each do |square| | |
board.player_make_move(current_player, square) | |
best_score[square] = | |
-1 * find_best_move(board, depth + 1, opponent, current_player) | |
board.reset_square(square) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface MediaPlayer { | |
void play(); | |
void stop(); | |
void currentlyPlaying(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MusicPlayer implements MediaPlayer{ | |
private String songName; | |
private boolean isPlaying = false; | |
public MusicPlayer(String songName) { | |
this.songName = songName | |
} | |
@Override | |
public void play() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VideoPlayer implements MediaPlayer{ | |
private String videoName; | |
private boolean isPlaying = false; | |
public VideoPlayer(String videoName) { | |
this.videoName = videoName | |
} | |
@Override | |
public void play() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class PurchaseProcessTemplate { | |
public final void processPurchase() { | |
greetCustomer(); | |
doSelectItems(); | |
doPurchaseItems(); | |
doDeliverItems(); | |
thankCustomer(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InStorePurchase extends PurchaseProcessTemplate { | |
@Override | |
public void doSelectItems() { | |
System.out.println("Customer selects items from the rack."); | |
} | |
@Override | |
public void doPurchaseItems() { | |
System.out.println("Customer pays for items at the counter."); |
OlderNewer