Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions exercises/risk-risiko.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,73 @@
M 3 vs 3 => blue win
O 2 vs 1 => red win
*/

#include <iostream>
#include <cstdlib>
#include <string>
#include <chrono>
#include <algorithm>

using namespace std;

const int NUM_DICES_FOR_USER = 3;
const int NUM_TOTAL_DICE = NUM_DICES_FOR_USER * 2;
const short int RED_WIN = 1;
const short int BLUE_WIN = -1;
const unsigned short int FACES_DICES = 6;

unsigned int * generateDicesNumber();
void compareValue(unsigned int * dices);
void showDices(unsigned int * dices);

int main() {
unsigned int seed = static_cast<unsigned int>(
chrono::steady_clock::now().time_since_epoch().count()
);
srand(seed);

unsigned int * dices = generateDicesNumber();

sort(dices, dices+NUM_DICES_FOR_USER);
sort(dices+NUM_DICES_FOR_USER, dices+(NUM_DICES_FOR_USER*2));

showDices(dices);
compareValue(dices);

free(dices);
return EXIT_SUCCESS;
}

unsigned int * generateDicesNumber(){
unsigned int* dices = new unsigned int[NUM_TOTAL_DICE];

for(int i = 0; i < NUM_TOTAL_DICE; i++)
dices[i] = rand() % FACES_DICES + 1;

return dices;
}

void compareValue(unsigned int * dices) {
cout << endl << " R B";

for(int i = NUM_DICES_FOR_USER-1; i >= 0; i--) {
cout << endl << "N " << dices[i] << " vs " << dices[i+NUM_DICES_FOR_USER];

if(dices[i] > dices[i+NUM_DICES_FOR_USER])
cout << " => RED WIN";
else
cout << " => BLUE WIN";
}
cout << endl;
}

void showDices(unsigned int * dices){
cout << "\nBlue dices: " << endl;

for(int i = NUM_TOTAL_DICE-1; i >= 0; i--) {
if(i == 2)
cout << "\n\nRed dices: " << endl;

cout << dices[i] << endl;
}
}