Você está na página 1de 5

#include "miniwin.

h"
#include <stdlib.h>
#include <time.h>
#include <sstream>
#include <string>
using namespace std;
using namespace miniwin;

const int tan_general = 25;


const int tan_filas = 20;
const int tan_columnas = 10;

typedef int mapa[tan_columnas][tan_filas];

struct coordenada { int x, y; };

struct figura {
coordenada orig;
coordenada perif[3];
int color;

coordenada posicion(int n) const;


};

coordenada figura::posicion(int n) const {


coordenada ret = { orig.x, orig.y };
if (n != 0) {
ret.x += perif[n-1].x;
ret.y += perif[n-1].y;
}
return ret;
}

void cuadrado(int x,int y){


rectangulo_lleno(25+1+x*tan_general,
25+1+y*tan_general,
25+x*tan_general+tan_general,
25+y*tan_general+tan_general);
}
void pinta_pieza(const figura& P) {
color(P.color);
for (int i = 0; i < 4; i++) {
coordenada c = P.posicion(i);
cuadrado(c.x, c.y);
}
}

coordenada rota_derecha(const coordenada& c) {


coordenada ret = { -c.y, c.x };
return ret;
}
void rota_derecha(figura& P) {
for (int i = 0; i < 3; i++) {
P.perif[i] = rota_derecha(P.perif[i]);
}
}

void tablero_vacia(mapa& T) {
for (int i = 0; i < tan_columnas; i++) {
for (int j = 0; j < tan_filas; j++) {
T[i][j] = BLANCO;
}
}
}
void tablero_pinta(const mapa& T) {
for (int i = 0; i < tan_columnas; i++) {
for (int j = 0; j < tan_filas; j++) {
color(T[i][j]);
cuadrado(i, j);
}
}
}

void tablero_incrusta_pieza(mapa& T, const figura& P) {


for (int i = 0; i < 4; i++) {
coordenada c = P.posicion(i);
T[c.x][c.y] = P.color;
}
}

bool tablero_colision(const mapa& T, const figura& P) {


for (int i = 0; i < 4; i++) {
coordenada c = P.posicion(i);

if (c.x < 0 || c.x >= tan_columnas) {


return true;
}
if (c.y < 0 || c.y >= tan_filas) {
return true;
}

if (T[c.x][c.y] != BLANCO) {
return true;
}
}
return false;
}

const coordenada perifs[8][3] = {


{ { 1,0 }, { 0,1 }, { 1,1 } },
{ { 1,0 }, {-1,1 }, { 0,1 } },
{ { 0,1 }, { 1,1 }, {-1,0 } },
{ { 0,1 }, { 0,-1}, { 1,1 } },
{ { 0,1 }, { 0,-1}, {-1,1 } },
{ { 0,1 }, { 0,-1}, { 0,2 } },
{ {-1,0 }, { 1,0 }, { 0,1 } },
{ { 0,0 }, { 0,0 }, { 0,0 } },
};

void pieza_nueva (figura &p){


p.color=1+rand() %4;
int piecilla= rand()%8;
p.orig.x=12;
p.orig.y=18;
for(int i=0;i<3;i++){
p.perif[i]= perifs[piecilla][i];
}
}
bool tablero_fila_llena(const mapa &T, int fila){

for(int i=0;i<tan_columnas;i++){
if(T[i][fila]==BLANCO)return false;
}
return true;
}

void tablero_colapsa(mapa &T,int fila){


for (int j=fila;j>0;j--){
for(int i=0;i<tan_columnas;i++){
T[i][j]= T[i][j-1];
}

for(int i=0;i<tan_columnas;i++){
T[i][0]=BLANCO;
}
}
}

int tablero_cuenta_lineas(mapa &T){


int fila =tan_filas-1, cont=0;
while(fila>=0){
if(tablero_fila_llena(T,fila)) {
tablero_colapsa(T,fila);
cont++;
}
else{
fila--;
}

}
return cont;
}

string muestra_puntos(int puntos){


stringstream sout;
sout << puntos;
return sout.str();
}

void maquillaje(const mapa &T,const figura &P,const figura &S,int puntos){

color(BLANCO);
for (int i=18;i<25;i++){
rectangulo(i,i,tan_general*tan_columnas+8+i,tan_general*tan_filas+8+i);
}
for (int i=92;i<100;i++){
rectangulo(99-i,99-
i,tan_general*tan_columnas+100+i,tan_general*tan_columnas+220+i);
}

texto(tan_general*tan_columnas+50,20, "&&&&&&&&&&&&&&&&&&&&&&&");
texto(tan_general*tan_columnas+60,60, "& &");
texto(tan_general*tan_columnas+60,80, "& TETRIS &");
texto(tan_general*tan_columnas+60,100,"& &");
texto(tan_general*tan_columnas+60,140,"& INSTRUCCIONES: &");
texto(tan_general*tan_columnas+60,160,"& ROTAR: ESPACIO &");
texto(tan_general*tan_columnas+60,190,"& DESPLAZAMIENTO &");
texto(tan_general*tan_columnas+60,205,"& DERECHA:FLECHA D &");
texto(tan_general*tan_columnas+60,225,"& IZQUIERDA: FLECHA I &");
texto(tan_general*tan_columnas+60,240,"& ACELERAR: FLECHA AB &");
texto(tan_general*tan_columnas+60,280,"&&&&&&&&&&&&&&&&&&&&&&&");
texto(tan_general*tan_columnas+60,300," ");
texto(tan_general*tan_columnas+60,320,"$ $");
texto(tan_general*tan_columnas+60,335,"$ $");
texto(tan_general*tan_columnas+60,350,"$ $");
texto(tan_general*tan_columnas+58,380,"$ PUNTOS POR LINEA $");
texto(tan_general*tan_columnas+60,395,muestra_puntos(puntos));
texto(tan_general*tan_columnas+60,430,"Siguiente pieza");
pinta_pieza(P);
pinta_pieza(S);
refresca();
}

int main() {

srand(time(NULL));
vredimensiona(tan_general*tan_columnas + 200,tan_general*tan_filas + 70);

int freno;
int puntos=100;

mapa T;
tablero_vacia(T);
tablero_pinta(T);
figura copia;
figura cuadrada, siguiente;
pieza_nueva(cuadrada);
pieza_nueva(siguiente);
pinta_pieza(cuadrada);

refresca();

int t=tecla();
while (t!= ESCAPE){
copia=cuadrada;

if (freno>30){
freno=0;
t=ABAJO;
}

int x=cuadrada.orig.x;
int y=cuadrada.orig.y;

if (t==DERECHA) cuadrada.orig.x++;

else if (t==IZQUIERDA) cuadrada.orig.x--;

else if (t==ESPACIO) rota_derecha(cuadrada);

else if (t==ABAJO) cuadrada.orig.y++;


if (tablero_colision(T,cuadrada)){
cuadrada.orig.x= x;
cuadrada.orig.y= y;
cuadrada=copia;
if (t==ABAJO){
tablero_incrusta_pieza(T, cuadrada);
int cont=tablero_cuenta_lineas(T);

if (cont>=1) puntos=puntos+cont;

cuadrada=siguiente;
pieza_nueva(siguiente);
cuadrada.orig.x=4;
cuadrada.orig.y=1;
}
borra();

}
if (t != NINGUNA){
borra();
tablero_pinta(T);
maquillaje(T,cuadrada,siguiente,puntos);
}

espera(20);

freno++;
t=tecla();
}

vcierra();
return 0;
}

Você também pode gostar