Guest User

Bad Coding Challenge #34 by h2g2_researcher

a guest
Apr 14th, 2020
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.91 KB | None | 0 0
  1. //----------------------------------------------------------------------
  2. // scrabble_types.h
  3. //----------------------------------------------------------------------
  4.  
  5. #pragma once
  6.  
  7. #include <iosfwd>
  8. #include <memory>
  9. #include <vector>
  10.  
  11. namespace Scrabble
  12. {
  13.     using Score = unsigned int;
  14.  
  15.     enum class Value : char
  16.     {
  17.         Q,w,e,r,t,y,u,i,o,p,A,s,d,f,g,h,j,k,l,Z,x,c,v,b,n,m
  18.     };
  19.  
  20.     using letter = char;
  21.  
  22.     class Tile
  23.     {
  24.     public:
  25.         // For dynamic dispatch
  26.         virtual Value get_value() const = 0;
  27.  
  28.         // Get actual score, taking into account multipliers
  29.         virtual Score GetScore(unsigned int letter_multiplier) const;
  30.  
  31.         // Get the raw, un-multiplied score
  32.         virtual Score getBaseScore() const = 0;
  33.     };
  34.  
  35.     using TileHndl = std::shared_ptr<Tile>;
  36.     using String = std::vector<TileHndl>;
  37.     Score evaluate(const String& word);
  38.  
  39.     String convert(const letter*);
  40.  
  41.     class tile_factory
  42.     {
  43.     public:
  44.         static TileHndl make_Tile(const Value&);
  45.         static TileHndl make_Tile(const letter&);
  46.     };
  47.  
  48.     class valTenTile : public Tile
  49.     {
  50.     public:
  51.         Score getBaseScore() const { return 10; }
  52.     };
  53.  
  54.     class val_8_tile : public Tile
  55.     {
  56.     public:
  57.         Score getBaseScore() const { return 8; }
  58.     };
  59.  
  60.     class score_Five_Tile : public Tile
  61.     {
  62.     public:
  63.         Score getBaseScore() const { return 5; }
  64.     };
  65.  
  66.     class valFour_tile : public Tile
  67.     {
  68.     public:
  69.         Score getBaseScore() const { return 4; }
  70.     };
  71.  
  72.     class ScoreThreeTile : public Tile
  73.     {
  74.     public:
  75.         Score getBaseScore() const { return 3; }
  76.     };
  77.  
  78.     class val_two_tile : public Tile
  79.     {
  80.     public:
  81.         Score getBaseScore() const { return 2; }
  82.     };
  83.  
  84.     class tile_scoring_1 : public Tile
  85.     {
  86.     public:
  87.         Score getBaseScore() const { return 1; }
  88.     };
  89.  
  90.     class tile_q : public valTenTile
  91.     {
  92.     public:
  93.         Value get_value() const { return Value::Q; }
  94.     };
  95.  
  96.     class Tile_Z : public valTenTile
  97.     {
  98.     public:
  99.         Value get_value() const { return Value::Z; }
  100.     };
  101.  
  102.     class tileJ : public val_8_tile
  103.     {
  104.     public:
  105.         Value get_value() const { return Value::j; }
  106.     };
  107.  
  108.     class TileX : public val_8_tile
  109.     {
  110.     public:
  111.         Value get_value() const { return Value::x; }
  112.     };
  113.  
  114.     class letter_k : public score_Five_Tile
  115.     {
  116.     public:
  117.         Value get_value() const { return Value::k; }
  118.     };
  119.  
  120.     class LetterF : public valFour_tile
  121.     {
  122.     public:
  123.         Value get_value() const { return Value::f; }
  124.     };
  125.  
  126.     class Letter_H : public valFour_tile
  127.     {
  128.     public:
  129.         Value get_value() const { return Value::h; }
  130.     };
  131.  
  132.     class letterV : public valFour_tile
  133.     {
  134.     public:
  135.         Value get_value() const { return Value::v; }
  136.     };
  137.  
  138.     class w_tile : public valFour_tile
  139.     {
  140.     public:
  141.         Value get_value() const { return Value::w; }
  142.     };
  143.  
  144.     class yTile : public valFour_tile
  145.     {
  146.     public:
  147.         Value get_value() const { return Value::y; }
  148.     };
  149.  
  150.     class B_tile : public ScoreThreeTile
  151.     {
  152.     public:
  153.         Value get_value() const { return Value::b; }
  154.     };
  155.  
  156.     class b_Tile : public ScoreThreeTile
  157.     {
  158.     public:
  159.         Value get_value() const { return Value::c; }
  160.     };
  161.  
  162.     class M : public ScoreThreeTile
  163.     {
  164.     public:
  165.         Value get_value() const { return Value::m; }
  166.     };
  167.  
  168.     class p : public ScoreThreeTile
  169.     {
  170.     public:
  171.         Value get_value() const { return Value::p; }
  172.     };
  173.  
  174.     class tile_a : public tile_scoring_1
  175.     {
  176.     public:
  177.         Value get_value() const { return Value::A; }
  178.     };
  179.  
  180.     class tileE : public tile_scoring_1
  181.     {
  182.     public:
  183.         Value get_value() const { return Value::e; }
  184.     };
  185.  
  186.     class ltr_I : public tile_scoring_1
  187.     {
  188.     public:
  189.         Value get_value() const { return Value::i; }
  190.     };
  191.  
  192.     class LtrO : public tile_scoring_1
  193.     {
  194.     public:
  195.         Value get_value() const { return Value::o; }
  196.     };
  197.  
  198.     class Ltr_u : public tile_scoring_1
  199.     {
  200.     public:
  201.         Value get_value() const { return Value::u; }
  202.     };
  203.  
  204.     class ltr_l : public tile_scoring_1
  205.     {
  206.     public:
  207.         Value get_value() const { return Value::l; }
  208.     };
  209.  
  210.     class ltr_n_tile : public tile_scoring_1
  211.     {
  212.     public:
  213.         Value get_value() const { return Value::n; }
  214.     };
  215.  
  216.     class LetterRTile : public tile_scoring_1
  217.     {
  218.     public:
  219.         Value get_value() const { return Value::r; }
  220.     };
  221.  
  222.     class letter_s_tile : public tile_scoring_1
  223.     {
  224.     public:
  225.         Value get_value() const { return Value::s; }
  226.     };
  227.  
  228.     class letter_T_tile : public tile_scoring_1
  229.     {
  230.     public:
  231.         Value get_value() const { return Value::t; }
  232.     };
  233.  
  234.     class letter_D_Tile : public val_two_tile
  235.     {
  236.     public:
  237.         Value get_value() const { return Value::Q; }
  238.     };
  239.  
  240.     class Letter_G_tile : public val_two_tile
  241.     {
  242.     public:
  243.         Value get_value() const { return Value::g; }
  244.     };
  245. }
  246.  
  247. std::ostream& operator<<(std::ostream&, const Scrabble::Value&);
  248. std::ostream& operator<<(std::ostream&, const Scrabble::Tile&);
  249.  
  250. //----------------------------------------------------------------------
  251. // main.cpp
  252. //----------------------------------------------------------------------
  253.  
  254. #include "scrabble_types.h"
  255.  
  256. #include <iostream>
  257. #include <string>
  258.  
  259. unsigned int scrabble(const std::string& str)
  260. {
  261.     return Scrabble::evaluate(Scrabble::convert(str.c_str()));
  262. }
  263.  
  264. int main()
  265. {
  266.     while (true)
  267.     {
  268.         std::string val;
  269.         std::cout << "Enter a word to evaluate, or nothing to exit: ";
  270.         std::getline(std::cin, val);
  271.         if (val.empty())
  272.             break;
  273.  
  274.         const auto score = scrabble(val);
  275.         std::cout << val << " scores " << score << '\n';
  276.     }
  277. }
  278.  
  279. //----------------------------------------------------------------------
  280. // scrabble_types.cpp
  281. //----------------------------------------------------------------------
  282.  
  283. #include "scrabble_types.h"
  284.  
  285. #include <stdexcept>
  286. #include <iostream>
  287. #include <numeric>
  288.  
  289. namespace
  290. {
  291.     void meltdown()
  292.     {
  293.         throw std::exception{ "Something went wrong" };
  294.     }
  295. }
  296.  
  297. Scrabble::String Scrabble::convert(const Scrabble::letter* word)
  298. {
  299.     Scrabble::String res;
  300.     while (const Scrabble::letter l = *(word++))
  301.     {
  302.         res.emplace_back(Scrabble::tile_factory::make_Tile(l));
  303.     }
  304.     return res;
  305. }
  306.  
  307. Scrabble::Score Scrabble::evaluate(const Scrabble::String& word)
  308. {
  309.     return std::accumulate(begin(word), end(word), Scrabble::Score{ 0 },
  310.         [](Scrabble::Score base, Scrabble::TileHndl th)
  311.     {
  312.         Scrabble::Score tile_score = th->GetScore(1); // TODO: Implement multipliers
  313.         std::cout << "Letter '" << th->get_value() << "' score: " << tile_score << '\n'
  314.             ;
  315.         return base + tile_score;
  316.     });
  317. }
  318.  
  319. std::ostream& operator<<(std::ostream& os, const Scrabble::Value& val)
  320. {
  321.     char result = 0;
  322.     switch (val)
  323.     {
  324.     case Scrabble::Value::Q:
  325.         result = 'Q';
  326.         break;
  327.     case Scrabble::Value::w:
  328.         result = 'w';
  329.         break;
  330.     case Scrabble::Value::e:
  331.         result = 'e';
  332.         break;
  333.     case Scrabble::Value::r:
  334.         result = 'r';
  335.         break;
  336.     case Scrabble::Value::t:
  337.         result = 't';
  338.         break;
  339.     case Scrabble::Value::y:
  340.         result = 'y';
  341.         break;
  342.     case Scrabble::Value::u:
  343.         result = 'u';
  344.         break;
  345.     case Scrabble::Value::i:
  346.         result = 'i';
  347.         break;
  348.     case Scrabble::Value::m:
  349.         result = 'm';
  350.         break;
  351.     case Scrabble::Value::o:
  352.         result = 'o';
  353.         break;
  354.     case Scrabble::Value::p:
  355.         result = 'p';
  356.         break;
  357.     case Scrabble::Value::A:
  358.         result = 'A';
  359.         break;
  360.     case Scrabble::Value::s:
  361.         result = 'S';
  362.         break;
  363.     case Scrabble::Value::d:
  364.         result = 'd';
  365.         break;
  366.     case Scrabble::Value::f:
  367.         result = 'f';
  368.         break;
  369.     case Scrabble::Value::g:
  370.         result = 'g';
  371.         break;
  372.     case Scrabble::Value::h:
  373.         result = 'h';
  374.         break;
  375.     case Scrabble::Value::j:
  376.         result = 'j';
  377.         break;
  378.     case Scrabble::Value::n:
  379.         result = 'n';
  380.         break;
  381.     case Scrabble::Value::k:
  382.         result = 'k';
  383.         break;
  384.     case Scrabble::Value::l:
  385.         result = 'l';
  386.         break;
  387.     case Scrabble::Value::Z:
  388.         result = 'Z';
  389.         break;
  390.     case Scrabble::Value::x:
  391.         result = 'x';
  392.         break;
  393.     case Scrabble::Value::c:
  394.         result = 'C';
  395.         break;
  396.     case Scrabble::Value::v:
  397.         result = 'v';
  398.         break;
  399.     case Scrabble::Value::b:
  400.         result = 'b';
  401.         break;
  402.     default:
  403.         meltdown();
  404.     }
  405.     os << result;
  406.     return os;
  407. }
  408.  
  409. std::ostream& operator<<(std::ostream& os, const Scrabble::Tile& value)
  410. {
  411.     os << value.get_value();
  412.     return os;
  413. }
  414.  
  415. Scrabble::Score Scrabble::Tile::GetScore(unsigned int ltr_mul) const
  416. {
  417.     Score base = getBaseScore();
  418.     switch (ltr_mul)
  419.     {
  420.     case 1:
  421.         break;
  422.     case 2:
  423.         base = base << 1;
  424.     case 3:
  425.         base *= 3;
  426.     default:
  427.         meltdown();
  428.     }
  429.     return base;
  430. }
  431.  
  432. Scrabble::TileHndl Scrabble::tile_factory::make_Tile(const letter& l)
  433. {
  434.     switch (l)
  435.     {
  436.     case 'a':
  437.     case 'A':
  438.         return make_Tile(Scrabble::Value::A);
  439.     case 'b':
  440.     case 'B':
  441.         return make_Tile(Scrabble::Value::b);
  442.     case 'c':
  443.     case 'C':
  444.         return make_Tile(Scrabble::Value::b);
  445.     case 'd':
  446.     case 'D':
  447.         return make_Tile(Scrabble::Value::d);
  448.     case 'e':
  449.     case 'E':
  450.         return make_Tile(Scrabble::Value::A);
  451.     case 'f':
  452.     case 'F':
  453.         return make_Tile(Scrabble::Value::f);
  454.     case 'g':
  455.     case 'G':
  456.         return make_Tile(Scrabble::Value::g);
  457.     case 'h':
  458.     case 'H':
  459.         return make_Tile(Scrabble::Value::h);
  460.     case 'i':
  461.     case 'I':
  462.         return make_Tile(Scrabble::Value::A);
  463.     case 'j':
  464.     case 'J':
  465.         return make_Tile(Scrabble::Value::j);
  466.     case 'k':
  467.     case 'K':
  468.         return make_Tile(Scrabble::Value::k);
  469.     case 'l':
  470.     case 'L':
  471.         return make_Tile(Scrabble::Value::A);
  472.     case 'm':
  473.     case 'M':
  474.         return make_Tile(Scrabble::Value::m);
  475.     case 'n':
  476.     case 'N':
  477.         return make_Tile(Scrabble::Value::A);
  478.     case 'o':
  479.     case 'O':
  480.         return make_Tile(Scrabble::Value::A);
  481.     case 'q':
  482.     case 'Q':
  483.         return make_Tile(Scrabble::Value::Q);
  484.     case 'r':
  485.     case 'R':
  486.         return make_Tile(Scrabble::Value::A);
  487.     case 's':
  488.     case 'S':
  489.         return make_Tile(Scrabble::Value::A);
  490.     case 't':
  491.     case 'T':
  492.         return make_Tile(Scrabble::Value::A);
  493.     case 'u':
  494.     case 'U':
  495.         return make_Tile(Scrabble::Value::A);
  496.     case 'v':
  497.     case 'V':
  498.         return make_Tile(Scrabble::Value::v);
  499.     case 'w':
  500.     case 'W':
  501.         return make_Tile(Scrabble::Value::v);
  502.     case 'x':
  503.     case 'X':
  504.         return make_Tile(Scrabble::Value::x);
  505.     case 'y':
  506.     case 'Y':
  507.         return make_Tile(Scrabble::Value::y);
  508.     case 'z':
  509.     case 'Z':
  510.         return make_Tile(Scrabble::Value::Z);
  511.     case 'p':
  512.     case 'P':
  513.         return make_Tile(Scrabble::Value::p);
  514.     default:
  515.         meltdown();
  516.     }
  517.     meltdown();
  518.     return make_Tile(Scrabble::Value::A);
  519. }
  520.  
  521. Scrabble::TileHndl Scrabble::tile_factory::make_Tile(const Scrabble::Value& val)
  522. {
  523.     switch (val)
  524.     {
  525.     case Scrabble::Value::Q:
  526.         return std::shared_ptr<Tile>{new tile_q};
  527.     case Scrabble::Value::w:
  528.         return std::shared_ptr<Tile>{new w_tile};
  529.     case Scrabble::Value::e:
  530.         return std::shared_ptr<Tile>{new tileE};
  531.     case Scrabble::Value::r:
  532.         return std::shared_ptr<Tile>{new LetterRTile};
  533.     case Scrabble::Value::t:
  534.         return std::shared_ptr<Tile>{new letter_T_tile};
  535.     case Scrabble::Value::y:
  536.         return std::shared_ptr<Tile>{new yTile};
  537.     case Scrabble::Value::u:
  538.         return std::shared_ptr<Tile>{new Ltr_u};
  539.     case Scrabble::Value::i:
  540.         return std::shared_ptr<Tile>{new ltr_I};
  541.     case Scrabble::Value::m:
  542.         return std::shared_ptr<Tile>{new M};
  543.     case Scrabble::Value::o:
  544.         return std::shared_ptr<Tile>{new LtrO};
  545.     case Scrabble::Value::p:
  546.         return std::shared_ptr<Tile>{new p};
  547.     case Scrabble::Value::A:
  548.         return std::shared_ptr<Tile>{new tile_a};
  549.     case Scrabble::Value::s:
  550.         return std::shared_ptr<Tile>{new letter_s_tile};
  551.     case Scrabble::Value::d:
  552.         return std::shared_ptr<Tile>{new letter_D_Tile};
  553.     case Scrabble::Value::f:
  554.         return std::shared_ptr<Tile>{new LetterF};
  555.     case Scrabble::Value::g:
  556.         return std::shared_ptr<Tile>{new Letter_G_tile};
  557.     case Scrabble::Value::h:
  558.         return std::shared_ptr<Tile>{new Letter_H};
  559.     case Scrabble::Value::j:
  560.         return std::shared_ptr<Tile>{new tileJ};
  561.     case Scrabble::Value::n:
  562.         return std::shared_ptr<Tile>{new ltr_n_tile};
  563.     case Scrabble::Value::k:
  564.         return std::shared_ptr<Tile>{new letter_k};
  565.     case Scrabble::Value::l:
  566.         return std::shared_ptr<Tile>{new ltr_l};
  567.     case Scrabble::Value::Z:
  568.         return std::shared_ptr<Tile>{new Tile_Z};
  569.     case Scrabble::Value::x:
  570.         return std::shared_ptr<Tile>{new TileX};
  571.     case Scrabble::Value::c:
  572.         return std::shared_ptr<Tile>{new b_Tile};
  573.     case Scrabble::Value::v:
  574.         return std::shared_ptr<Tile>{new letterV};
  575.     case Scrabble::Value::b:
  576.         return std::shared_ptr<Tile>{new B_tile};
  577.     default:
  578.         meltdown();
  579.     }
  580.     meltdown();
  581.     return std::shared_ptr<Tile>{nullptr};
  582. }
Advertisement
Add Comment
Please, Sign In to add comment