pad.mattdiesel.co.uk

Snippet - 2048

2048 (C++)

2048 Implementation
Created 2014-07-15 22:32:46.727880 by Matt.
Modified 2014-07-16 12:21:49.569960 by Matt.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream>
#include <random>

enum Direction {
    UP = 0,
    RIGHT,
    DOWN,
    LEFT
};

template <int S>
class Grid
{
private:
    std::random_device rd;
    std::mt19937 gen;

    bool fillABlank() {
        if (numBlanks == 0) {
            return( false );
        }

        std::uniform_int_distribution<> dis(1, numBlanks);
        std::uniform_int_distribution<> disBlock(0, 4);

        int sq = dis(gen);

        for (int r = 0; r < S && sq; r++) {
            for (int c = 0; c < S; c++) {
                if (!squares[r][c] && !--sq) {
                    squares[r][c] = !disBlock(gen) ? 2 : 1;
                    numBlanks--;
                    break;
                }
            }
        }

        return( true );
    }

public:
    int squares[S][S] = {0};
    int numBlanks = S*S;

    Grid()
        : squares {0}
    {
        fillABlank();
    }

    bool print()
    {
        std::cout.width(2);
        std::cout.fill(' ');

        for (int r = 0; r < S; r++) {
            for (int c = 0; c < S; c++) {
                std::cout.width(2);
                std::cout.fill(' ');
                if (squares[r][c])
                    std::cout << squares[r][c] << " | ";
                else
                    std::cout << "   | ";
            }

            std::cout << "\n";
            std::cout.width(5*S);
            std::cout.fill('-');
            std::cout << ' ' << std::endl;
        }

        return( true );
    }

    int& dirAccess(int r, int c, Direction d)
    {
        switch (d) {
        case UP:
            return squares[r][c];
        case DOWN:
            return squares[S-r-1][S-c-1];
        case RIGHT:
            return squares[c][S-r-1];
        case LEFT:
            return squares[S-c-1][r];
        }
    }

    bool canMove() {
        if (numBlanks) {
            return( true );
        }

        // Can move if there are any adjacent blocks of the same value.

        for (int c = 0; c < S; c++) {
            for (int r = 0; r < S; r++) {
                if (!squares[r][c]) {
                    continue;
                }

                if ( (r && squares[r][c] == squares[r-1][c])
                        || (c && squares[r][c] == squares[r][c-1])
                        || (c < S-1 && squares[r][c] == squares[r][c+1])
                        || (r < S-1 && squares[r][c] == squares[r+1][c]) ) {
                    return( true );
                }
            }
        }

        return( false );
    }

    bool move(Direction d) {
        bool blocksMoved = false;

        int r, c;
        int r2;

        for (c = 0; c < S; c++) {
            // Move into blanks
            r2 = 0;
            for (r = 0; r < S; r++) {
                if (dirAccess(r, c, d)) {
                    if (r2 != r) {
                        dirAccess(r2, c, d) = dirAccess(r, c, d);
                        dirAccess(r, c, d) = 0;

                        blocksMoved = true;
                    }

                    r2++;
                }
            }

            // Combine
            for (r = 1; r < S; r++) {
                if (dirAccess(r, c, d) && dirAccess(r, c, d) == dirAccess(r-1, c, d)) {
                    dirAccess(r-1, c, d)++;
                    numBlanks++;

                    blocksMoved = true;

                    for (r2 = r+1; r2 < S; r2++) {
                        dirAccess(r2-1, c, d) = dirAccess(r2, c, d);
                        dirAccess(r2, c, d) = 0;
                    }
                }
            }
        }

        if (blocksMoved) {
            fillABlank();
        }

        return( blocksMoved );
    }
};


int main(int argc, char const *argv[])
{
    Grid<8> gr;
    Direction d = UP;

    gr.print();
    std::cout << std::endl;

    char c;
    bool running = true;
    for (;;) {
        std::cin >> c;

        switch (c) {
        case 'q':
            running = false;
            break;
        case 'w':
            gr.move(UP);
            break;
        case 'a':
            gr.move(LEFT);
            break;
        case 's':
            gr.move(DOWN);
            break;
        case 'd':
            gr.move(RIGHT);
            break;
        default:
            std::cout << "Dunno what you meant to do. Try WASD or Q." << std::endl;
            break;
        }

        if (!running) {
            break;
        }

        gr.print();
        std::cout << std::endl;
    }

    return 0;
}