first commit
[rofl0r-oopoker.git] / tools_terminal.cpp
blob895e9761204fc39ce59935c1843b33ab1ecd0aea
1 /*
2 OOPoker
4 Copyright (c) 2010 Lode Vandevenne
5 All rights reserved.
7 This file is part of OOPoker.
9 OOPoker is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 OOPoker is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with OOPoker. If not, see <http://www.gnu.org/licenses/>.
23 #include "tools_terminal.h"
25 #include "card.h"
26 #include "io_terminal.h"
27 #include "pokermath.h"
28 #include "util.h"
30 #include <string>
31 #include <vector>
33 Card charsToCard(char c0, char c1)
35 if(c0 >= 'a' && c0 <= 'z') c0 += ('A' - 'a');
36 if(c1 >= 'A' && c1 <= 'Z') c1 += ('a' - 'A');
37 std::string c; c += c0; c += c1;
38 return Card(c);
42 Give it a string like AsTh and it returns ace of spades, ten of hearts in a vector
43 Case doesn't matter.
44 Cards are appended to the std::vector (it isn't cleared)
46 static void stringToCards(std::vector<Card>& cards, const std::string& s)
48 for(size_t i = 0; i < s.size() / 2; i++)
50 cards.push_back(charsToCard(s[i * 2 + 0], s[i * 2 + 1]));
55 String must have 4 chars.
56 Case doesn't matter.
58 static bool stringToHoleCards(Card& card1, Card& card2, const std::string& s)
60 if(s.size() != 4) return false;
62 card1 = charsToCard(s[0], s[1]);
63 card2 = charsToCard(s[2], s[3]);
65 return true;
68 void runConsolePotEquityCalculator()
70 std::string s;
72 std::cout << std::endl << "Pot Equity Calculator" << std::endl << std::endl;
73 std::cout << "values: 23456789TJQKA" << std::endl;
74 std::cout << "suits: cdhs" << std::endl;
76 std::cout << "enter num players (2-23): ";
77 s = getLine();
78 int numPlayers = strtoval<int>(s);
79 std::cout << std::endl;
81 std::cout << "enter board cards (e.g. AsTh5h, or 'x' for none): ";
82 s = getLine();
83 std::vector<Card> boardCards;
84 stringToCards(boardCards, s);
85 std::cout << std::endl;
87 std::cout << "enter hole cards (e.g. 5dTh): ";
88 s = getLine();
89 std::vector<Card> holeCards;
90 stringToCards(holeCards, s);
91 std::cout << std::endl;
93 if(holeCards.size() != 2)
95 std::cout << "please enter 4 characters, e.g. 5dTh for 5 of diamonds, ten of hearts." << std::endl;
96 return;
98 else
100 std::cout << "Pot Equity: " << getPotEquity(holeCards, boardCards, numPlayers - 1) << std::endl;
104 void runConsoleShowdownCalculator()
106 std::string s;
108 std::cout << std::endl << "Showdown Calculator" << std::endl << std::endl;
109 std::cout << "values: 23456789TJQKA" << std::endl;
110 std::cout << "suits: cdhs" << std::endl;
112 std::cout << "enter num players (2-23): ";
113 s = getLine();
114 int numPlayers = strtoval<int>(s);
115 std::cout << std::endl;
117 std::cout << "enter board cards (e.g. AsTh5h, or 'x' for none): ";
118 s = getLine();
119 std::vector<Card> boardCards;
120 stringToCards(boardCards, s);
121 std::cout << std::endl;
123 std::vector<Card> holeCards1, holeCards2;
125 for(int i = 0; i < numPlayers; i++)
127 std::cout << "enter hole cards of player " << i + 1 << " (e.g. 5dTh): ";
128 s = getLine();
129 std::cout << std::endl;
130 Card c1, c2;
131 if(!stringToHoleCards(c1, c2, s))
133 std::cout << "please enter 4 characters, e.g. 5dTh for 5 of diamonds, ten of hearts." << std::endl;
134 i--;
136 else
138 holeCards1.push_back(c1);
139 holeCards2.push_back(c2);
143 std::vector<double> win, tie, lose;
144 if(!getWinChanceWithKnownHands(win, tie, lose, holeCards1, holeCards2, boardCards))
146 std::cout << "invalid board cards were entered, no calculation could be done" << std::endl;
148 else
150 std::cout << "Results: (chance to win, tie, lose)" << std::endl;
151 for(int i = 0; i < numPlayers; i++)
153 std::cout << "Player " << i + 1 << " (" << holeCards1[i].getShortName() << " " << holeCards2[i].getShortName()
154 << "): " << win[i] << ", " << tie[i] << ", " << lose[i] << std::endl;