Last active
May 16, 2025 02:50
-
-
Save josue1dario2/4b9d30ab1d4ce530f59d84e76d192dcb to your computer and use it in GitHub Desktop.
juego 1 c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <ctime> | |
// Evitamos using namespace std; según Google Style Guide | |
// Constantes | |
const int kNumDice = 5; | |
const int kWinningScore = 25; | |
const int kMaxLeaderboardSize = 10; | |
// Prototipos | |
void InitializeLeaderboard(std::string ranking_names[], int ranking_rounds[], | |
int ranking_rounds_won[], int ranking_scores[], int size); | |
void RollDice(int dice[], int size); | |
void DisplayDice(int dice[], int size); | |
int GetMaxRoundScore(int dice[], int size); | |
bool FlipCoin(); | |
void PlayGame(std::string player_name, std::string ranking_names[], int ranking_rounds[], | |
int ranking_rounds_won[], int ranking_scores[], int &leaderboard_size); | |
void PlayTurn(std::string name, int dice[], int &total_score, int &rounds, int &round_score); | |
void UpdateLeaderboard(std::string player_name, int player_rounds, int rounds_won, int player_score, | |
std::string ranking_names[], int ranking_rounds[], int ranking_rounds_won[], | |
int ranking_scores[], int &leaderboard_size); | |
void DisplayGameResult(std::string player_name, int player_score, int player_rounds, | |
int player_rounds_won, int cpu_score, int cpu_rounds, int cpu_rounds_won); | |
void DisplayLeaderboard(std::string ranking_names[], int ranking_rounds[], | |
int ranking_rounds_won[], int ranking_scores[], int leaderboard_size); | |
int main() | |
{ | |
srand(time(0)); | |
std::string ranking_names[kMaxLeaderboardSize]; | |
int ranking_rounds[kMaxLeaderboardSize]; | |
int ranking_rounds_won[kMaxLeaderboardSize]; | |
int ranking_scores[kMaxLeaderboardSize]; | |
int leaderboard_size = 0; | |
std::string player_name; | |
int option; | |
InitializeLeaderboard(ranking_names, ranking_rounds, ranking_rounds_won, | |
ranking_scores, kMaxLeaderboardSize); | |
std::cout << "Enter your name: "; | |
std::getline(std::cin, player_name); | |
if (player_name.empty()) | |
{ | |
player_name = "Player"; | |
} | |
do | |
{ | |
std::cout << "\n=== MENU ===\n"; | |
std::cout << "1- Play\n"; | |
std::cout << "2- Leaderboard\n"; | |
std::cout << "0- Exit\n"; | |
std::cout << "Option: "; | |
std::cin >> option; | |
std::cin.ignore(); // Limpiar buffer | |
switch (option) | |
{ | |
case 1: | |
PlayGame(player_name, ranking_names, ranking_rounds, | |
ranking_rounds_won, ranking_scores, leaderboard_size); | |
break; | |
case 2: | |
DisplayLeaderboard(ranking_names, ranking_rounds, | |
ranking_rounds_won, ranking_scores, leaderboard_size); | |
break; | |
case 0: | |
std::cout << "Thanks for playing!\n"; | |
break; | |
default: | |
std::cout << "Invalid option\n"; | |
} | |
} while (option != 0); | |
return 0; | |
} | |
void InitializeLeaderboard(std::string ranking_names[], int ranking_rounds[], | |
int ranking_rounds_won[], int ranking_scores[], int size) | |
{ | |
for (int i = 0; i < size; ++i) | |
{ | |
ranking_names[i] = ""; | |
ranking_rounds[i] = 9999; // Valor alto para nuevos registros | |
ranking_rounds_won[i] = 0; | |
ranking_scores[i] = 0; | |
} | |
} | |
void RollDice(int dice[], int size) | |
{ | |
for (int i = 0; i < size; ++i) | |
{ | |
dice[i] = rand() % 6 + 1; | |
} | |
} | |
void DisplayDice(int dice[], int size) | |
{ | |
for (int i = 0; i < size; ++i) | |
{ | |
std::cout << dice[i] << " "; | |
} | |
std::cout << "\n"; | |
} | |
int GetMaxRoundScore(int dice[], int size) | |
{ | |
int max_score = dice[0]; | |
for (int i = 1; i < size; ++i) | |
{ | |
if (dice[i] > max_score) | |
{ | |
max_score = dice[i]; | |
} | |
} | |
return max_score; | |
} | |
bool FlipCoin() | |
{ | |
return rand() % 2 == 0; // true: player, false: CPU | |
} | |
void PlayGame(std::string player_name, std::string ranking_names[], int ranking_rounds[], | |
int ranking_rounds_won[], int ranking_scores[], int &leaderboard_size) | |
{ | |
int dice[kNumDice]; | |
int round_number = 0; | |
int player_score = 0, cpu_score = 0; | |
int player_rounds = 0, cpu_rounds = 0; | |
int player_rounds_won = 0, cpu_rounds_won = 0; | |
int player_round_score = 0, cpu_round_score = 0; | |
// Decidir quién empieza | |
std::cout << "\nFlipping coin to decide who starts...\n"; | |
bool player_starts = FlipCoin(); | |
std::cout << (player_starts ? player_name : "CPU") << " starts!\n"; | |
// Juego | |
while (player_score < kWinningScore && cpu_score < kWinningScore) | |
{ | |
++round_number; | |
std::cout << "\n---------------- ROUND " << round_number << " ------------------\n"; | |
if (player_starts) | |
{ | |
PlayTurn(player_name, dice, player_score, player_rounds, player_round_score); | |
if (player_score >= kWinningScore) | |
break; | |
PlayTurn("CPU", dice, cpu_score, cpu_rounds, cpu_round_score); | |
} | |
else | |
{ | |
PlayTurn("CPU", dice, cpu_score, cpu_rounds, cpu_round_score); | |
if (cpu_score >= kWinningScore) | |
break; | |
PlayTurn(player_name, dice, player_score, player_rounds, player_round_score); | |
} | |
if (player_round_score > cpu_round_score) | |
{ | |
++player_rounds_won; | |
} | |
else if (cpu_round_score > player_round_score) | |
{ | |
++cpu_rounds_won; | |
} | |
} | |
DisplayGameResult(player_name, player_score, player_rounds, player_rounds_won, | |
cpu_score, cpu_rounds, cpu_rounds_won); | |
if (player_score >= kWinningScore) | |
{ | |
UpdateLeaderboard(player_name, player_rounds, player_rounds_won, player_score, | |
ranking_names, ranking_rounds, ranking_rounds_won, | |
ranking_scores, leaderboard_size); | |
} | |
} | |
void PlayTurn(std::string name, int dice[], int &total_score, int &rounds, int &round_score) | |
{ | |
std::cout << name << ":\n"; | |
RollDice(dice, kNumDice); | |
DisplayDice(dice, kNumDice); | |
round_score = GetMaxRoundScore(dice, kNumDice); | |
total_score += round_score; | |
++rounds; | |
std::cout << "Round score: " << round_score << "\n"; | |
std::cout << "Total score: " << total_score << "\n\n"; | |
} | |
void UpdateLeaderboard(std::string player_name, int player_rounds, int rounds_won, int player_score, | |
std::string ranking_names[], int ranking_rounds[], int ranking_rounds_won[], | |
int ranking_scores[], int &leaderboard_size) | |
{ | |
if (leaderboard_size >= kMaxLeaderboardSize && | |
player_rounds >= ranking_rounds[kMaxLeaderboardSize - 1]) | |
{ | |
return; // No actualizar si no mejora el ranking | |
} | |
if (leaderboard_size < kMaxLeaderboardSize) | |
{ | |
++leaderboard_size; | |
} | |
// Insertar en orden (menor cantidad de rondas) | |
int i = leaderboard_size - 1; | |
while (i > 0 && ranking_rounds[i - 1] > player_rounds) | |
{ | |
ranking_names[i] = ranking_names[i - 1]; | |
ranking_rounds[i] = ranking_rounds[i - 1]; | |
ranking_rounds_won[i] = ranking_rounds_won[i - 1]; | |
ranking_scores[i] = ranking_scores[i - 1]; | |
--i; | |
} | |
ranking_names[i] = player_name; | |
ranking_rounds[i] = player_rounds; | |
ranking_rounds_won[i] = rounds_won; | |
ranking_scores[i] = player_score; | |
} | |
void DisplayGameResult(std::string player_name, int player_score, int player_rounds, | |
int player_rounds_won, int cpu_score, int cpu_rounds, int cpu_rounds_won) | |
{ | |
if (player_score >= kWinningScore) | |
{ | |
std::cout << "\n" | |
<< player_name << " won the game!\n"; | |
} | |
else | |
{ | |
std::cout << "\nCPU won the game!\n"; | |
} | |
std::cout << player_name << ": " << player_score << " points, " | |
<< player_rounds << " rounds, " << player_rounds_won << " rounds won\n"; | |
std::cout << "CPU: " << cpu_score << " points, " | |
<< cpu_rounds << " rounds, " << cpu_rounds_won << " rounds won\n"; | |
} | |
void DisplayLeaderboard(std::string ranking_names[], int ranking_rounds[], | |
int ranking_rounds_won[], int ranking_scores[], int leaderboard_size) | |
{ | |
std::cout << "\n=== LEADERBOARD ===\n"; | |
if (leaderboard_size == 0) | |
{ | |
std::cout << "No entries available\n"; | |
return; | |
} | |
for (int i = 0; i < leaderboard_size && i < kMaxLeaderboardSize; ++i) | |
{ | |
if (!ranking_names[i].empty()) | |
{ | |
std::cout << i + 1 << ". " << ranking_names[i] | |
<< " - Rounds: " << ranking_rounds[i] | |
<< ", Score: " << ranking_scores[i] | |
<< ", Rounds won: " << ranking_rounds_won[i] << "\n"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment