PageRenderTime 18ms CodeModel.GetById 9ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 0ms

/core-src/PieceCollection.cc

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