/core-src/PieceCollection.cc

http://github.com/Quuxplusone/Homeworlds · C++ · 121 lines · 103 code · 12 blank · 6 comment · 22 complexity · a22958019db9694d8294bc6b58925cf2 MD5 · raw file

  1. #include <assert.h>
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "state.h"
  6. PieceCollection::PieceCollection()
  7. {
  8. this->clear();
  9. }
  10. void PieceCollection::clear()
  11. {
  12. memset(this->pieces, 0x00, sizeof this->pieces);
  13. }
  14. Size PieceCollection::smallestSizeOf(Color c) const
  15. {
  16. if (pieces[c][SMALL] > 0) return SMALL;
  17. if (pieces[c][MEDIUM] > 0) return MEDIUM;
  18. if (pieces[c][LARGE] > 0) return LARGE;
  19. assert(false);
  20. /*NOTREACHED*/
  21. }
  22. bool PieceCollection::contains(const PieceCollection& rhs) const
  23. {
  24. for (Color c = RED; c <= BLUE; ++c) {
  25. for (Size s = SMALL; s <= LARGE; ++s) {
  26. if (this->pieces[c][s] < rhs.pieces[c][s]) {
  27. return false;
  28. }
  29. }
  30. }
  31. return true;
  32. }
  33. bool PieceCollection::operator==(const PieceCollection& rhs) const
  34. {
  35. for (Color c = RED; c <= BLUE; ++c) {
  36. for (Size s = SMALL; s <= LARGE; ++s) {
  37. if (this->pieces[c][s] != rhs.pieces[c][s]) {
  38. return false;
  39. }
  40. }
  41. }
  42. return true;
  43. }
  44. void PieceCollection::operator-=(const PieceCollection& rhs)
  45. {
  46. for (Color c = RED; c <= BLUE; ++c) {
  47. for (Size s = SMALL; s <= LARGE; ++s) {
  48. assert(this->pieces[c][s] >= rhs.pieces[c][s]);
  49. this->pieces[c][s] -= rhs.pieces[c][s];
  50. }
  51. }
  52. }
  53. void PieceCollection::operator+=(const PieceCollection& rhs)
  54. {
  55. for (Color c = RED; c <= BLUE; ++c) {
  56. for (Size s = SMALL; s <= LARGE; ++s) {
  57. this->pieces[c][s] += rhs.pieces[c][s];
  58. }
  59. }
  60. }
  61. /* This function takes a char buffer, stores the representation of this
  62. * PieceCollection in it, and returns a pointer to the terminating '\0'.
  63. * This is useful in the common case that the caller wants to concatenate
  64. * something onto the end of the string. */
  65. char *PieceCollection::toString(char *buffer) const
  66. {
  67. assert(buffer != nullptr);
  68. for (Color c = RED; c <= BLUE; ++c) {
  69. for (Size s = SMALL; s <= LARGE; ++s) {
  70. for (int i=0; i < this->pieces[c][s]; ++i) {
  71. *buffer++ = "rygb"[c];
  72. *buffer++ = '1'+(int)s;
  73. }
  74. }
  75. }
  76. *buffer = '\0';
  77. return buffer;
  78. }
  79. std::string PieceCollection::toString() const
  80. {
  81. static char buffer[MAXSTRLEN+1];
  82. (void)this->toString(buffer);
  83. return buffer;
  84. }
  85. /* Return true on success; return false on failure. */
  86. bool PieceCollection::scan(const char *text)
  87. {
  88. this->clear();
  89. for (int i=0; text[i] != '\0'; ++i) {
  90. Color c;
  91. Size s;
  92. switch (text[i]) {
  93. case 'r': c = RED; break;
  94. case 'y': c = YELLOW; break;
  95. case 'g': c = GREEN; break;
  96. case 'b': c = BLUE; break;
  97. default: return false;
  98. }
  99. ++i;
  100. switch (text[i]) {
  101. case '1': s = SMALL; break;
  102. case '2': s = MEDIUM; break;
  103. case '3': s = LARGE; break;
  104. default: return false;
  105. }
  106. this->insert(c, s);
  107. }
  108. return true;
  109. }