Código
#include <iostream> #include <string> #include <Windows.h> #include <iomanip> #include <cstdlib> #include <ctime> using namespace std; // Constantes const int COLUMNAS = 8; const int FILAS = 8; const int maxIntentos = 15; const char tresBloques[4] = {char(219), char(219), char(219), char(0)}; // Vector tresBloques const char tresLineas[4] = {char(196), char(196), char(196), char(0)}; // Vector tresLineas // Tipos enumerados typedef enum {neutro, magenta, amarillo, azul, verde, none} tFicha; typedef enum {arriba, abajo, derecha, izquierda} tDireccion; typedef enum { black, // 0 dark_blue, // 1 dark_green, // 2 dark_cyan, // 3 dark_red, // 4 dark_magenta, // 5 dark_yellow, // 6 light_gray, // 7 dark_gray, // 8 light_blue, // 9 light_green, // 10 light_cyan, // 11 light_red, // 12 light_magenta, // 13 light_yellow, // 14 white // 15 } tColor; // Arrays typedef tFicha tTablero[FILAS][COLUMNAS]; typedef bool tMatrizBooleana[FILAS][COLUMNAS]; // Estructuras typedef struct { tTablero tablero; int intentos; int puntos; } tJuego; typedef struct { int fila; int columna; tDireccion direccion; } tMovimiento; // Prototipos void colorCrush(tJuego &juego); void generarTablero(tJuego &juego); void procesarTablero(tJuego &juego); void mostrarTablero(tJuego &juego); void lineaArriba(); void lineaMedio(); void lineaAbajo(); void comprobarGrupo(); void movimientoUsuario(); void rellenarHuecos(); void setColor(tColor color); void columnas(tTablero tablero, int i); void pintarEspacios(tFicha cuadrado); void iniciarArrayBool(tMatrizBooleana arrayBool); void comprobarFilas(tJuego &juego, tMatrizBooleana arrayBool); void comprobarColumnas(tJuego &juego, tMatrizBooleana arrayBool); // Next void gestionCaida(tJuego &juego); int main() { tJuego juego; srand(time(NULL)); colorCrush(juego); return 0; } void generarTablero(tTablero tablero) { for (int i = 0; i < FILAS; i++) { for (int j = 0; j < COLUMNAS; j++) { tablero[i][j] = tFicha(1 + rand() % 4); } } } void setColor(tColor color) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(handle, color); } void mostrarTablero(tJuego &juego) { setColor(tColor(15)); cout << setw(15) << "Attempts: " << juego.intentos << setw(15) << "Points: " << juego.puntos << endl; lineaArriba(); for (int i = FILAS - 1; i > (FILAS - (FILAS)); i--) { cout << setw(2); cout << i + 1; cout << setw(2); columnas(juego.tablero, i); lineaMedio(); } int i = 0; cout << setw(2); cout << FILAS - (FILAS - 1); cout << setw(2); columnas(juego.tablero, i); lineaAbajo(); cout << setw(6); for (int j = 1; j < COLUMNAS; j++) { cout << j << setw(4); } cout << COLUMNAS << endl; } void columnas(tTablero tablero, int i) { for (int j = 0; j < COLUMNAS; j++) { cout << char(179); pintarEspacios(tablero[i][j]); setColor(tColor(15)); } cout << char(179) << endl; } void lineaArriba() { cout << setw(4) << char(218) << tresLineas; for (int i = 0; i < COLUMNAS - 2; i++) { cout << char(194) << tresLineas; } cout << char(194) << tresLineas << char(191) << endl; } void lineaMedio() { cout << setw(4) << char(195) << tresLineas; for (int i = 0; i < COLUMNAS - 2; i++) { cout << char(197) << tresLineas; } cout << char(197) << tresLineas << char(180) << endl; } void lineaAbajo() { cout << setw(4) << char(192) << tresLineas; for (int i = 0; i < COLUMNAS - 2; i++) { cout << char(193) << tresLineas; } cout << char(193) << tresLineas << char(217) << endl; } void pintarEspacios(tFicha cuadrado) { if (cuadrado == magenta) { setColor(tColor(13)); cout << tresBloques; } else if (cuadrado == amarillo) { setColor(tColor(14)); cout << tresBloques; } else if (cuadrado == azul) { setColor(tColor(3)); cout << tresBloques; } else if (cuadrado == verde) { setColor(tColor(2)); cout << tresBloques; } else if (cuadrado == neutro) { setColor(tColor(0)); cout << tresBloques; } } void colorCrush(tJuego &juego) { juego.intentos = maxIntentos; juego.puntos = 0; generarTablero(juego.tablero); mostrarTablero(juego); procesarTablero(juego); int j = 0; while (j < 10) { cout << endl << endl; mostrarTablero(juego); procesarTablero(juego); cout << endl; system("pause"); j++; } } void procesarTablero (tJuego &juego) { tFicha fichaAnterior = neutro; tMatrizBooleana arrayBool; iniciarArrayBool(arrayBool); comprobarFilas(juego, arrayBool); comprobarColumnas(juego, arrayBool); } void iniciarArrayBool(tMatrizBooleana arrayBool) { for (int i = 0; i < FILAS; i++) { for (int j = 0; j < COLUMNAS; j++) { arrayBool[i][j] = false; } } } void comprobarFilas(tJuego &juego, tMatrizBooleana arrayBool) { int contador = 0; int j = 0; tFicha fichaAnterior = neutro; for (int i = 0; i < FILAS; i++) { while (j < COLUMNAS) { while (juego.tablero[i][j] == fichaAnterior && j < COLUMNAS) { contador++; fichaAnterior = juego.tablero[i][j]; j++; } if (contador == 2) { juego.puntos = juego.puntos + 3; for (int h = contador + 1; h > 0; h--) { arrayBool[i][j-h] = true; juego.tablero[i][j-h] = neutro; } } else if (contador == 3) { juego.puntos = juego.puntos + 8; for (int h = contador + 1; h > 0; h--) { arrayBool[i][j-h] = true; juego.tablero[i][j-h] = neutro; } } else if (contador >= 4) { juego.puntos = juego.puntos + 15; for (int h = contador + 1; h > 0; h--) { arrayBool[i][j-h] = true; juego.tablero[i][j-h] = neutro; } } contador = 0; fichaAnterior = juego.tablero[i][j]; j++; } j = 0; fichaAnterior = none; } } void comprobarColumnas(tJuego &juego, tMatrizBooleana arrayBool) { int contador = 0; int j = 0; tFicha fichaAnterior = neutro; for (int i = 0; i < COLUMNAS; i++) { while (j < FILAS) { while (juego.tablero[j][i] == fichaAnterior && j < COLUMNAS) { contador++; fichaAnterior = juego.tablero[j][i]; j++; } if (contador == 2) { juego.puntos = juego.puntos + 3; for (int h = contador + 1; h > 0; h--) { arrayBool[j-h][i] = true; juego.tablero[j-h][i] = neutro; } } else if (contador == 3) { juego.puntos = juego.puntos + 8; for (int h = contador + 1; h > 0; h--) { arrayBool[j-h][i] = true; juego.tablero[j-h][i] = neutro; } } else if (contador >= 4) { juego.puntos = juego.puntos + 15; for (int h = contador + 1; h > 0; h--) { arrayBool[j-h][i] = true; juego.tablero[j-h][i] = neutro; } } contador = 0; fichaAnterior = juego.tablero[j][i]; j++; } fichaAnterior = neutro; j = 0; } } // void gestionCaida(tJuego &juego){ // bool hanCaido= true; // while (hanCaido) { // Volvemos a hacer caer todo el tablero // caidaFichas(juego.tablero, hanCaido); // if (hanCaido) { // Muestra el tablero // mostrarTablero(juego); // } // } // }
Tan solo me faltaría que las casillas de colores fueran desplazándose hacia abajo (para rellenar los huecos) y que, una vez que no hay más combinaciones de 3, 4 o 5 casillas adyacentes del mismo color, se llegara a un estado "estable" del tablero en el que se le pide al usuario qué ficha quiere mover y en qué dirección (arriba, abajo, izquierda o derecha) para que pueda seguir sumando el mayor número de puntos. El juego termina cuando no hay posibles combinaciones para el usuario.
Como ya he dicho, estoy en ello. Una vez que tenga este "Color Crush" terminado, subiré el código por si alguien quiere echarse una partidilla

Un saludo y gracias!


