Skip to content

Instantly share code, notes, and snippets.

@corlaez
Created June 16, 2025 04:04
Show Gist options
  • Save corlaez/8b28119b558f975894c1afd4c3fe4ca7 to your computer and use it in GitHub Desktop.
Save corlaez/8b28119b558f975894c1afd4c3fe4ca7 to your computer and use it in GitHub Desktop.
Carrera C++ con conteo de ganadas
#include "pch.h"
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <ctime>
#include <thread>
#include <vector>
#include <string>
using namespace System;
using namespace std;
const int ANCHO = 90; // columnas
const int ALTO = 34; // filas
const int numerocarros = 6;
const int carros_alto = 2;
const int carros_ancho = 14;
const ConsoleColor paleta[numerocarros] =
{
ConsoleColor::Red,
ConsoleColor::Blue,
ConsoleColor::Green,
ConsoleColor::Cyan,
ConsoleColor::Magenta,
ConsoleColor::Yellow
};
vector<string> paletaTexto =
{
"Rojo",
"Azul",
"Verde",
"Celeste",//Cian
"Magenta",
"Yellow"
};
vector<string> colorTexto =
{
"",
"",
"",
"",
"",
""
};
ConsoleColor color[numerocarros];
int posX[numerocarros];
int posY[numerocarros];
int vel[numerocarros];
int wins[numerocarros] = { 0 };
bool der[numerocarros];
int limIzq[numerocarros];
int limDer[numerocarros];
int etapa[numerocarros];
const int filaMeta[6] = { 20,22,24,26,28,30 };
const int metaFinalX = 0;
const int paso[6] = { 130000, 140000, 150000, 160000, 180000, 190000 };
int numeroDeCarreras = 0;
int carrerasGanadas[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
/* Bloquea la ejecucion del codigo hasta que detecta que la tecla ha sido presionada */
void esperarTecla(char tecla) {
while (true) {
if (kbhit)
{
if (getch() == tecla)
{
break;
}
}
}
}
/* Bloquea la ejecucion del codigo hasta que detecta que una de las teclas ha sido presionada y retorna su indice */
int esperarTeclas(std::initializer_list<char> teclas) {
while (true) {
if (kbhit)
{
char teclaPresionada = getch();
for (size_t i = 0; i < teclas.size(); ++i) {
if (teclaPresionada == teclas.begin()[i])
{
return i;
}
}
}
}
}
void moverCursor(int x, int y) {
if (x >= 0 && x < ANCHO && y >= 0 && y < ALTO)
Console::SetCursorPosition(x, y);
}
const char* carroDer[numerocarros][carros_alto] = {
{
"_/¯¯¯¯¯¯¯¯\\__",
"\\--O-----O--/"
},
{
"---------- ",
"-@-[O_O]-@-- "
},
{
" #-------->> ",
"--[]--------[]"
},
{
" >>═══════ ",
"¯¯(_)¯¯¯(_)¯¯ "
},
{
" /¯¯¯¯\\___ ",
"/\\(o¯¯o)__/ "
},
{
"_________ ",
"\\(_)---(_)-/ "
}
};
const char* carroIzq[numerocarros][carros_alto] = {
{
"__/¯¯¯¯¯¯¯¯\\_",
"\\--O-----O--/"
},
{
" ----------",
" --@-[O_O]-@-"
},
{
" <<--------# ",
"[]--------[]--"
},
{
" ═══════<< ",
" ¯¯(_)¯¯¯(_)¯¯"
},
{
" ___/¯¯¯¯¯\\ ",
" \\__(o¯¯o)/\\"
},
{
" _________",
" \\-(_)---(_)/"
}
};
void asignarVelocidades()
{
for (int i = 0; i < numerocarros; ++i)
{
int v;
bool repetido;
do {
v = rand() % 6; // ahora 0..5
repetido = false;
for (int j = 0; j < i; j++) {
if (vel[j] == v) { // ¿está tomado?
repetido = true;
break;
}
}
} while (repetido); // repite hasta ser único
vel[i] = v; // guarda velocidad del auto i
}
}
void configurarVentana() {
Console::SetWindowSize(ANCHO, ALTO);
Console::CursorVisible = false; // con esto cambias la ventana ddf
}
void opciones() {
moverCursor(24, 17); cout << "[1] Carrera";
moverCursor(38, 17); cout << "[2] Creditos";
moverCursor(53, 17); cout << "[3] Salir";
}
void nombreJuego() {
moverCursor(10, 5); cout << " ____ _ ____ ____ _____ ____ ___ ____ _ _ _____ ____";
moverCursor(10, 6); cout << "/ ___| / \\ | _ \\ | _ \\| ____/ ___|_ _/ ___| \\ | | ____| _ \\.";
moverCursor(10, 7); cout << "| | / _ \\ | |_) | | | | | _ |\\___\\ | | | _| \\| | _| | |_) |";
moverCursor(10, 8); cout << "| |___/ ___ \\| _ < | |_| | |___ ___) | | |_| | |\\ | |___| _ <";
moverCursor(10, 9); cout << "\\____/_/ \\_\\_| \\_\\ |____/|_____|____/___\\____|_| \\_|_____|_| \\_\\.";
}
void creditos() {
Console::SetCursorPosition(10, 10);
cout << "Hecho por Andres Espejo, Piero Burgos y Fabio Silva";
}
void asignarColores()
{
for (int i = 0; i < numerocarros; i++) {
int paletacarros;
bool repetido;
do {
paletacarros = rand() % 6; // elige un color de 0..5
repetido = false;
for (int j = 0; j < i; j++) { // ¿ya se usó?
if (color[j] == paleta[paletacarros]) {
repetido = true;
break;
}
}
} while (repetido); // repite hasta que no se repita
color[i] = paleta[paletacarros]; // guarda el color único
colorTexto[i] = paletaTexto[paletacarros];
}
}
void dibujarAuto(int id, int x, int y, bool derecha)
{
Console::ForegroundColor = color[id]; // color único
const char** linea = derecha ? carroDer[id] : carroIzq[id];
for (int r = 0; r < carros_alto; ++r) {
moverCursor(x, y + r);
cout << linea[r];
}
Console::ForegroundColor = ConsoleColor::Gray;
}
void colocarAutosInicio() {
/* límites personalizados – ajusta si lo deseas */
int izq[6] = { 1, 16, 30, 1, 16, 31 };
int der[6] = { 49, 60, 65, 51, 55, 61 };
for (int id = 0; id < numerocarros; ++id) {
limIzq[id] = izq[id];
limDer[id] = der[id];
posX[id] = limIzq[id]; // arranca pegado al límite izq.
posY[id] = 2 + (id / 3) * (carros_alto + 2);
der[id] = true;
etapa[id] = 0;
dibujarAuto(id, posX[id], posY[id], true);
}
}
void borrarCarro(int id) {
for (int r = 0; r < carros_alto; r++) {
moverCursor(posX[id], posY[id] + r);
cout << string(carros_ancho, ' ');
}
}
void dibujarmeta(int x, int y) {
char b = 219;
Console::SetCursorPosition(x, y + 20);
cout << " " << b << " " << b << " " << b << endl;
cout << b << " " << b << " " << b << " " << endl;
cout << " " << b << " " << b << " " << b << endl;
cout << b << " " << b << " " << b << " " << endl;
cout << " " << b << " " << b << " " << b << endl;
cout << b << " " << b << " " << b << " " << endl;
cout << " " << b << " " << b << " " << b << endl;
cout << b << " " << b << " " << b << " " << endl;
cout << " " << b << " " << b << " " << b << endl;
cout << b << " " << b << " " << b << " " << endl;
cout << " " << b << " " << b << " " << b << endl;
cout << b << " " << b << " " << b << " " << endl;
cout << " " << b << " " << b << " " << b << endl;
}
void iniciarcarrera() {};
void dibujarpista(int x, int y) {
char g = 238;
Console::SetCursorPosition(x, y + 10);
cout << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g;
Console::SetCursorPosition(x + 48, y + 10);
cout << "|";
Console::SetCursorPosition(x + 48, y + 11);
cout << "|";
Console::SetCursorPosition(x + 48, y + 12);
cout << "|";
Console::SetCursorPosition(x + 48, y + 13);
cout << "|";
Console::SetCursorPosition(x + 48, y + 14);
cout << "|";
Console::SetCursorPosition(x + 48, y + 15);
cout << "|";
Console::SetCursorPosition(x + 48, y + 16);
cout << "|";
Console::SetCursorPosition(x + 48, y + 17);
cout << "|";
Console::SetCursorPosition(x + 48, y + 18);
cout << "|";
Console::SetCursorPosition(x, y + 19);
cout << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g;
Console::SetCursorPosition(4, y + 11);
cout << "Carreras Ganadas:\n";
cout << colorTexto[0] + ": " + std::to_string(carrerasGanadas[0]) + "\n";
cout << colorTexto[1] + ": " + std::to_string(carrerasGanadas[1]) + "\n";
cout << colorTexto[2] + ": " + std::to_string(carrerasGanadas[2]) + "\n";
cout << colorTexto[3] + ": " + std::to_string(carrerasGanadas[3]) + "\n";
cout << colorTexto[4] + ": " + std::to_string(carrerasGanadas[4]) + "\n";
cout << colorTexto[5] + ": " + std::to_string(carrerasGanadas[5]) + "\n";
}
void bucleCarrera(int indiceDeCarrera) {
int frame = 0;
bool hayGanador = false;
while (!hayGanador) {
for (int id = 0; id < numerocarros; ++id) {
/* Avanza solo cada vel[id] frames */
if (frame % paso[vel[id]] == 0) {
borrarCarro(id);
if (etapa[id] == 0) {
if (posX[id] < limDer[id]) {
++posX[id];
der[id] = true;
}
else {
etapa[id] = 1;
}
}
else if (etapa[id] == 1) {
if (posY[id] < filaMeta[id]) ++posY[id];
else etapa[id] = 2;
der[id] = false;
}
else {
if (posX[id] > metaFinalX) {
--posX[id];
}
else {
wins[id]++; hayGanador = true;
//moverCursor(0, 12);
carrerasGanadas[id] += 1;
dibujarpista(0, 0);
//cout << "Ganador: Auto #" << id + 1;
//_getch();
}
}
dibujarAuto(id, posX[id], posY[id], der[id]);
}
}
Sleep(0);
++frame;
}
}
void ranking() {};
void simulacion_carrera() {
asignarColores();
dibujarpista(0, 0);
colocarAutosInicio(); // pinta autos y pone límites
_getch();
for (int i = 0; i < numeroDeCarreras; i++) {
Console::Clear();
dibujarpista(0, 0);
dibujarmeta(0, 0);
asignarVelocidades();
colocarAutosInicio(); // pinta autos y pone límites
bucleCarrera(i); // ← ¡ahora sí se mueven!
}
cout << "\nPulsa una tecla...";
_getch();
}
void inicio() {
Console::Clear();
int de0a4 = rand() % 5;
numeroDeCarreras = 4 + de0a4;
nombreJuego();
opciones();
int i = esperarTeclas({ '1', '2', '3' });
if (i == 0) {
Console::Clear();
simulacion_carrera();
}
if (i == 1) {
Console::Clear();
creditos();
esperarTecla(27);
inicio();
}
if (i == 2) {
std::exit(0);
}
}
/* ─── Programa principal ─── */
int main() {
srand(static_cast<unsigned int>(time(NULL)));
configurarVentana();
Console::Clear();
inicio();
//moverCursor(0, ALTO - 1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment