Program with a class containing 2D vector throwing 'std::bad_alloc'.
Apr 18, 2010 at 10:17pm UTC
This is a program to create and play the classic 15 puzzle. This is a homework assignment, with a required puzzle class and a method to play the game. The program compiles fine, but attempting to move the puzzle pieces gives me this error:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted
Any hints are greatly appreciated.
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
// Program generates a solvable 4x4 slide puzzle for you to play
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
class puzzle {
public :
vector <vector <int > > squares;
vector <int > row;
puzzle() {
int k = 1;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (k == 16) k = 0;
row.push_back( k++ );
}
squares.push_back( row );
row.clear();
}
}
void print() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout << squares[i][j] << " " ;
if (squares[i][j] < 10) cout << " " ;
}
cout << endl;
}
cout << endl;
}
bool check() {
int k = 1;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (k == 16) k = 0;
if (squares[i][j] != k++)
return false ;
}
}
return true ;
}
puzzle moveUp() {
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if ((squares[i][j] == 0) && ((i-1) >= 0)) {
squares[i][j] = squares[i-1][j];
squares[i-1][j] = 0;
}
print();
}
puzzle moveRight() {
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (squares[i][j] == 0 && ((j+1) < 4)) {
squares[i][j] = squares[i][j+1];
squares[i][j+1] = 0;
}
}
puzzle moveDown() {
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (squares[i][j] == 0 && ((i+1) < 4)) {
squares[i][j] = squares[i+1][j];
squares[i+1][j] = 0;
}
}
puzzle moveLeft() {
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (squares[i][j] == 0 && ((j-1) >= 0)) {
squares[i][j] = squares[i][j-1];
squares[i][j-1] = 0;
}
}
puzzle checkMove(int x) {
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (squares[i][j] == 0) {
if (squares[i-1][j] == x)
moveUp();
else if (squares[i][j+1] == x)
moveRight();
else if (squares[i+1][j] == x)
moveDown();
else if (squares[i][j-1] == x)
moveLeft();
else cout << "Invalid selection, choose again." ;
}
}
puzzle shuffle() {
srand (time (NULL));
for (int i = 0; i < 100; i++) {
int j = rand() % 4;
cout << j << endl;
switch (j) {
case 0:
if ((i-1) >= 0)
moveUp();
break ;
case 1:
if ((j+1) <= 3)
moveRight();
break ;
case 2:
if ((i+1) <= 3)
moveDown();
break ;
case 3:
if ((j-1) >= 0)
moveLeft();
break ;
}
}
}
};
int main() {
puzzle gameboard;
int move;
cout << endl << "Welcome to the 15 puzzle!" << endl;
gameboard.print();
cout << "Shuffling game board..." << endl << endl;
while (gameboard.check()) {
gameboard.shuffle();
}
while (!gameboard.check()) {
cout << "Your move? " ;
cin >> move;
gameboard.checkMove(move);
gameboard.print();
}
cout << "Success!" << endl;
}
Apr 18, 2010 at 10:49pm UTC
In checkMove(), you are going outside of the bounds of your array. When i/j are 0, then very first check will cause bad things to happen.
Apr 18, 2010 at 10:59pm UTC
Ok all I needed was to change the puzzle type to void for my class functions. Thanks.
Last edited on Apr 19, 2010 at 1:33am UTC
Topic archived. No new replies allowed.