From 445ac9417c5f242e54a1feaaf0855b0724d78e2b Mon Sep 17 00:00:00 2001 From: Charles Kawczynski Date: Mon, 28 Aug 2023 22:09:28 -0700 Subject: [PATCH] Move terminal code to separate folder --- src/TexasHoldem.jl | 3 +- src/player_options.jl | 77 ---------------------------- src/{ => terminal}/config_game.jl | 0 src/terminal/human_player_options.jl | 76 +++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 78 deletions(-) rename src/{ => terminal}/config_game.jl (100%) create mode 100644 src/terminal/human_player_options.jl diff --git a/src/TexasHoldem.jl b/src/TexasHoldem.jl index 3c20aeae..35f06a70 100644 --- a/src/TexasHoldem.jl +++ b/src/TexasHoldem.jl @@ -38,7 +38,8 @@ include("table.jl") include("game.jl") include("player_actions.jl") include("player_options.jl") -include("config_game.jl") +include(joinpath("terminal", "human_player_options.jl")) +include(joinpath("terminal", "config_game.jl")) include("recreate.jl") end # module diff --git a/src/player_options.jl b/src/player_options.jl index f18072a2..4d3846e6 100644 --- a/src/player_options.jl +++ b/src/player_options.jl @@ -301,83 +301,6 @@ end player_option(game::Game, player::Player, option) = player_option(game, player, round(game.table), option) -##### -##### Human player options (ask via prompts) -##### - -function player_option(game::Game, player::Player{Human}, ::CheckRaiseFold, io::IO=stdin) - table = game.table - vrr = valid_raise_range(table, player) - options = ["Check", "Raise [$(first(vrr)), $(last(vrr))]", "Fold"] - menu = RadioMenu(options, pagesize=4) - choice = request("$(name(player))'s turn to act:", menu) - choice == -1 && error("Uncaught case") - choice == 1 && return Check() - choice == 2 && return Raise(input_raise_amt(table, player, io)) - choice == 3 && return Fold() -end -function player_option(game::Game, player::Player{Human}, ::CallRaiseFold, io::IO=stdin) - table = game.table - vrr = valid_raise_range(table, player) - call_amt = call_amount(table, player) - blind_str = is_blind_call(table, player) ? " (blind)" : "" - options = ["Call $(call_amt)$blind_str", "Raise [$(first(vrr)), $(last(vrr))]", "Fold"] - menu = RadioMenu(options, pagesize=4) - choice = request("$(name(player))'s turn to act:", menu) - choice == -1 && error("Uncaught case") - choice == 1 && return Call(table, player) - choice == 2 && return Raise(input_raise_amt(table, player, io)) - choice == 3 && return Fold() -end -function player_option(game::Game, player::Player{Human}, ::CallAllInFold) - table = game.table - call_amt = call_amount(table, player) - all_in_amt = round_bank_roll(player) - blind_str = is_blind_call(table, player) ? " (blind)" : "" - options = ["Call $(call_amt)$blind_str", "Raise all-in ($(all_in_amt))", "Fold"] - menu = RadioMenu(options, pagesize=4) - choice = request("$(name(player))'s turn to act:", menu) - choice == -1 && error("Uncaught case") - choice == 1 && return Call(table, player) - choice == 2 && return AllIn(table, player) - choice == 3 && return Fold() -end -function player_option(game::Game, player::Player{Human}, ::CallFold) - table = game.table - call_amt = call_amount(table, player) - blind_str = is_blind_call(table, player) ? " (blind)" : "" - options = ["Call $(call_amt)$blind_str", "Fold"] - menu = RadioMenu(options, pagesize=4) - choice = request("$(name(player))'s turn to act:", menu) - choice == -1 && error("Uncaught case") - choice == 1 && return Call(table, player) - choice == 2 && return Fold() -end - -# io only works for tests, but does not for user input -# so we have a switch for the test suite -use_input_io() = false -println_io(io::IO, msg) = use_input_io() ? println(io, msg) : println(msg) - -function input_raise_amt(table, player::Player{Human}, io::IO=stdin) - raise_amt = nothing - while true - println_io(io, "Enter raise amt:") - raise_amt = readline(io) - try - raise_amt = parse(Int, raise_amt) - is_valid, msg = is_valid_raise_amount(table, player, raise_amt) - is_valid && break - println_io(io, msg) - catch - println_io(io, "Raise must be a Int") - end - end - @assert raise_amt ≠ nothing - amt = valid_raise_amount(table, player, raise_amt) - return amt -end - ##### ##### AbstractStrategy ##### diff --git a/src/config_game.jl b/src/terminal/config_game.jl similarity index 100% rename from src/config_game.jl rename to src/terminal/config_game.jl diff --git a/src/terminal/human_player_options.jl b/src/terminal/human_player_options.jl new file mode 100644 index 00000000..60909979 --- /dev/null +++ b/src/terminal/human_player_options.jl @@ -0,0 +1,76 @@ +##### +##### Human player options (ask via prompts) +##### + +function player_option(game::Game, player::Player{Human}, ::CheckRaiseFold, io::IO=stdin) + table = game.table + vrr = valid_raise_range(table, player) + options = ["Check", "Raise [$(first(vrr)), $(last(vrr))]", "Fold"] + menu = RadioMenu(options, pagesize=4) + choice = request("$(name(player))'s turn to act:", menu) + choice == -1 && error("Uncaught case") + choice == 1 && return Check() + choice == 2 && return Raise(input_raise_amt(table, player, io)) + choice == 3 && return Fold() +end +function player_option(game::Game, player::Player{Human}, ::CallRaiseFold, io::IO=stdin) + table = game.table + vrr = valid_raise_range(table, player) + call_amt = call_amount(table, player) + blind_str = is_blind_call(table, player) ? " (blind)" : "" + options = ["Call $(call_amt)$blind_str", "Raise [$(first(vrr)), $(last(vrr))]", "Fold"] + menu = RadioMenu(options, pagesize=4) + choice = request("$(name(player))'s turn to act:", menu) + choice == -1 && error("Uncaught case") + choice == 1 && return Call(table, player) + choice == 2 && return Raise(input_raise_amt(table, player, io)) + choice == 3 && return Fold() +end +function player_option(game::Game, player::Player{Human}, ::CallAllInFold) + table = game.table + call_amt = call_amount(table, player) + all_in_amt = round_bank_roll(player) + blind_str = is_blind_call(table, player) ? " (blind)" : "" + options = ["Call $(call_amt)$blind_str", "Raise all-in ($(all_in_amt))", "Fold"] + menu = RadioMenu(options, pagesize=4) + choice = request("$(name(player))'s turn to act:", menu) + choice == -1 && error("Uncaught case") + choice == 1 && return Call(table, player) + choice == 2 && return AllIn(table, player) + choice == 3 && return Fold() +end +function player_option(game::Game, player::Player{Human}, ::CallFold) + table = game.table + call_amt = call_amount(table, player) + blind_str = is_blind_call(table, player) ? " (blind)" : "" + options = ["Call $(call_amt)$blind_str", "Fold"] + menu = RadioMenu(options, pagesize=4) + choice = request("$(name(player))'s turn to act:", menu) + choice == -1 && error("Uncaught case") + choice == 1 && return Call(table, player) + choice == 2 && return Fold() +end + +# io only works for tests, but does not for user input +# so we have a switch for the test suite +use_input_io() = false +println_io(io::IO, msg) = use_input_io() ? println(io, msg) : println(msg) + +function input_raise_amt(table, player::Player{Human}, io::IO=stdin) + raise_amt = nothing + while true + println_io(io, "Enter raise amt:") + raise_amt = readline(io) + try + raise_amt = parse(Int, raise_amt) + is_valid, msg = is_valid_raise_amount(table, player, raise_amt) + is_valid && break + println_io(io, msg) + catch + println_io(io, "Raise must be a Int") + end + end + @assert raise_amt ≠ nothing + amt = valid_raise_amount(table, player, raise_amt) + return amt +end