You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 line
2.4KB

  1. /*------------ -------------- -------- --- ----- --- -- - -
  2. * Libre Blackjack - main function
  3. *
  4. * Copyright (C) 2020 jeremy theler
  5. *
  6. * This file is part of Libre Blackjack.
  7. *
  8. * Libre Blackjack is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Libre Blackjack is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Libre Blackjack. If not, see <http://www.gnu.org/licenses/>.
  20. *------------------- ------------ ---- -------- -- - - -
  21. */
  22. #include <iostream>
  23. #include "base.h"
  24. #include "conf.h"
  25. #include "blackjack.h"
  26. #include "tty.h"
  27. #include "stdinout.h"
  28. int main(int argc, char **argv) {
  29. Dealer *dealer = nullptr;
  30. Player *player = nullptr;
  31. Configuration conf(argc, argv);
  32. if (conf.show_version) {
  33. Libreblackjack::shortversion();
  34. Libreblackjack::copyright();
  35. }
  36. if (conf.show_help) {
  37. Libreblackjack::help(argv[0]);
  38. }
  39. if (conf.show_version || conf.show_help) {
  40. return 0;
  41. }
  42. if (conf.getDealerName() == "blackjack") {
  43. dealer = new Blackjack(conf);
  44. } else {
  45. std::cerr << "Unknown dealer for '" << conf.getDealerName() <<"' game." << std::endl;
  46. return -1;
  47. }
  48. if (conf.getPlayerName() == "tty") {
  49. player = new Tty(conf);
  50. } else if (conf.getPlayerName() == "stdinout") {
  51. player = new StdInOut();
  52. // TODO: player strategy from file
  53. } else {
  54. std::cerr << "Unknown player '" << conf.getPlayerName() <<".'" << std::endl;
  55. return -1;
  56. }
  57. // let the action begin!
  58. int unknownCommands = 0;
  59. dealer->nextAction = DealerAction::StartNewHand;
  60. while (!dealer->finished()) {
  61. dealer->deal(player);
  62. if (player->actionRequired != PlayerActionRequired::None) {
  63. do {
  64. if (unknownCommands++ > conf.max_incorrect_commands) {
  65. std::cerr << "Too many unknown commands." << std::endl;
  66. return -2;
  67. }
  68. player->play();
  69. } while (dealer->process(player) <= 0);
  70. }
  71. }
  72. // TODO: write report
  73. delete player;
  74. delete dealer;
  75. return 0;
  76. }