浏览代码

readline completion

master
gtheler 5 年前
父节点
当前提交
fd16c14dd5
共有 4 个文件被更改,包括 101 次插入28 次删除
  1. +4
    -3
      src/base.h
  2. +6
    -7
      src/blackjack.cpp
  3. +87
    -14
      src/tty.cpp
  4. +4
    -4
      src/tty.h

+ 4
- 3
src/base.h 查看文件

@@ -77,7 +77,7 @@ enum class Info {
Shuffle,
CardPlayer,
CardDealer,
CardDealerHoleRevealed,
CardDealerRevealsHole,
DealerBlackjack,
PlayerWinsInsurance,
PlayerBlackjackAlso,
@@ -86,8 +86,9 @@ enum class Info {
PlayerBlackjack,
PlayerWins,
NoBlackjacks,
PlayerBustedAllHands,
DealerBusted,
PlayerBustsAllHands,
DealerBusts,
Help,
Bye,
};


+ 6
- 7
src/blackjack.cpp 查看文件

@@ -222,7 +222,7 @@ void Blackjack::deal(Player *player) {
// step 8. check if there are any blackjack
playerBlackack = player->currentHand->blackjack();
if (hand.blackjack()) {
player->info(Info::CardDealerHoleRevealed, holeCard);
player->info(Info::CardDealerRevealsHole, holeCard);
player->info(Info::DealerBlackjack);
player->blackjacksDealer++;

@@ -311,8 +311,8 @@ void Blackjack::deal(Player *player) {

if (player->bustedAllHands) {

player->info(Info::PlayerBustedAllHands);
player->info(Info::CardDealerHoleRevealed, holeCard);
player->info(Info::PlayerBustsAllHands);
player->info(Info::CardDealerRevealsHole, holeCard);
// TODO: no tengo que sacarle todo el dinero?
player->actionRequired = PlayerActionRequired::None;
@@ -328,7 +328,7 @@ void Blackjack::deal(Player *player) {
case DealerAction::HitDealerHand:
player->info(Info::CardDealerHoleRevealed, holeCard);
player->info(Info::CardDealerRevealsHole, holeCard);

// hit if count is less than 17 (or equalt to soft 17 if hit_soft_17 is true)
dealerTotal = hand.total();
@@ -339,7 +339,7 @@ void Blackjack::deal(Player *player) {
dealerTotal = std::abs(hand.total());

if (hand.busted()) {
player->info(Info::DealerBusted);
player->info(Info::DealerBusts);
player->bustsDealer++;
for (auto playerHand : player->hands) {
if (playerHand.busted() == false) {
@@ -433,9 +433,8 @@ int Blackjack::process(Player *player) {
///ig+help+desc Ask for help
///ig+help+detail A succinct help message is written on the standard output.
///ig+help+detail This command makes sense only when issued by a human player.
// TODO
case PlayerActionTaken::Help:
std::cout << "help yourself" << std::endl;
player->info(Info::Help);
return 0;
break;

+ 87
- 14
src/tty.cpp 查看文件

@@ -34,6 +34,60 @@
#include "blackjack.h"
#include "tty.h"





// extern "C" {

char *blackjack_commands[] = {
"help",
"hit",
"stand",
"yes",
"no",
NULL
};


char *blackjack_command_generator(const char *text, int state) {
static int list_index, len;
char *name;

if (!state) {
list_index = 0;
len = strlen(text);
}

while ((name = blackjack_commands[list_index++])) {
name = strdup(name);

if (strncmp(name, text, len) == 0) {
return name;
} else {
free(name);
}
}

return NULL;
}

char **blackjack_rl_completion(const char *text, int start, int end) {
char **matches = NULL;

#ifdef HAVE_LIBREADLINE
matches = rl_completion_matches(text, blackjack_command_generator);
#endif

return matches;
}



// }



Tty::Tty(Configuration &conf) {
conf.set(&flat_bet, {"flat_bet", "flatbet"});
@@ -49,7 +103,7 @@ void Tty::info(Info msg, int intData) {
// TODO: choose utf8 or other representation
switch (msg) {
case Info::InvalidBet:
if (intData < 0) {
// s = "bet_negative";
@@ -65,6 +119,7 @@ void Tty::info(Info msg, int intData) {

case Info::NewHand:
// s = "new_hand";
std::cout << std::endl;
s = "Starting new hand #" + std::to_string(intData);
dealerHand.cards.clear();
break;
@@ -108,7 +163,7 @@ void Tty::info(Info msg, int intData) {
dealerHand.cards.push_back(intData);
break;
case Info::CardDealerHoleRevealed:
case Info::CardDealerRevealsHole:
// s = "card_dealer_hole";
s = "Dealer's hole card was " + card[intData].utf8();
*(++(dealerHand.cards.begin())) = intData;
@@ -157,7 +212,7 @@ void Tty::info(Info msg, int intData) {
s = "No blackjacks";
break;
case Info::PlayerBustedAllHands:
case Info::PlayerBustsAllHands:
// s = "player_busted_all_hands";
if (hands.size() == 1) {
s = "Player busted";
@@ -166,6 +221,18 @@ void Tty::info(Info msg, int intData) {
}
break;
case Info::DealerBusts:
// s = "no_blackjacks";
s = "Dealer busts!";
break;
case Info::Help:
std::cout << "help yourself" << std::endl;
break;

case Info::Bye:
// s = "bye";
s = "Bye bye! We'll play Blackjack again next time.";
@@ -189,19 +256,12 @@ int Tty::play() {
break;

case PlayerActionRequired::Insurance:
renderTable();
s = "Insurance?";
break;
case PlayerActionRequired::Play:
std::cout << " -- Dealer's hand: --------" << std::endl;
render(&dealerHand);
std::cout << " Total: " << dealerHand.total() << std::endl;

std::cout << " -- Player's hand --------" << std::endl;
for (auto hand : hands) {
render(&hand);
std::cout << " Total: " << hand.total() << std::endl;
}
renderTable();
s = "Play?";
break;
}
@@ -214,7 +274,7 @@ int Tty::play() {
}
#ifdef HAVE_LIBREADLINE
rl_attempted_completion_function = blackjack_rl_completion;
if ((input_buffer = readline(prompt.c_str())) == nullptr) {
// EOF means "quit"
@@ -302,9 +362,22 @@ int Tty::play() {
return 0;
}

void Tty::renderTable(void) {

std::cout << " -- Dealer's hand: --------" << std::endl;
renderHand(&dealerHand);
std::cout << " Total: " << dealerHand.total() << std::endl;

std::cout << " -- Player's hand --------" << std::endl;
for (auto hand : hands) {
renderHand(&hand);
std::cout << " Total: " << hand.total() << std::endl;
}

return;
}

void Tty::render(Hand *hand) {
void Tty::renderHand(Hand *hand) {

for (auto it : hand->cards) {
std::cout << " _____ ";

+ 4
- 4
src/tty.h 查看文件

@@ -32,11 +32,11 @@ class Tty : public Player {
int play() override;
void info(Info = Info::None, int = 0) override;

void render(Hand *);

private:

void renderHand(Hand *);
void renderTable(void);
#ifdef HAVE_LIBREADLINE
char *input_buffer;
#else

正在加载...
取消
保存