Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New changements #3

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
"xlocmes": "cpp",
"complex": "cpp",
"codecvt": "cpp",
"unordered_set": "cpp"
}
"unordered_set": "cpp",
"ranges": "cpp",
"cfenv": "cpp",
"typeindex": "cpp",
"__nullptr": "cpp"
},
"cmake.sourceDirectory": "/home/thierno/Documents/m1_ubuntu/cpp/CPP_Exercises/tp03/3-hrsoft"
}
28 changes: 28 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
11 changes: 5 additions & 6 deletions tp01/ex1/main.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include <iomanip>
#include <iostream>
#include <string>

int main()
{
int main() {
std::cout << "Entre ton nom: ";

char name[20] = "";
std::cin >> std::setw(20) >> name;
std::cout << "Bonjour Palluche La Falluche !" << std::endl;
std::string name;
std::cin >> name;
std::cout << name << std::endl;

return 0;
}
48 changes: 38 additions & 10 deletions tp01/ex2/main.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
#include <iostream>
#include <vector>

void ajoute_double(std::vector<int> v)
{
for (auto n : v)
void ajoute_double(std::vector<int>& v) {
auto size = v.size();
for (auto i = 0; i < size; i++)
{
v.emplace_back(n * 2);
v.push_back(v[i] * 2);
}
}

int main()
{
auto entiers = std::vector<int>{1, 3, 3, 7};

void affiche(const std::vector<int>& v) {
std::cout << "Le tableau contient les valeurs :";
for (int i = 0; i < entiers.size(); i++)
for (auto entier : v)
{
std::cout << " " << entiers[i];
std::cout << " " << entier;
}
std::cout << std::endl;
}

int main() {
auto entiers = std::vector<int> {};
auto value = 0;
while (1)
{
std::cin >> value;
if (std::cin.fail())
{
std::cout << "Ceci n'est pas un entier !" << std::endl;
return 1;
}
if (value == 0)
{
break;
}
if (value < 0)
{
if (!entiers.empty())
entiers.pop_back();
}
else
{
entiers.emplace_back(value);
}
}
affiche(entiers);
if (!entiers.empty())
std::cout << "first: " << entiers.front() << " last: " << entiers.back() << std::endl;
ajoute_double(entiers);

affiche(entiers);
return 0;
}
5 changes: 2 additions & 3 deletions tp01/ex3/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "utils.hpp"

int main()
{
int main() {
print_hello();
// print_bye();
print_bye();
return 0;
}
8 changes: 4 additions & 4 deletions tp01/ex3/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <iostream>
#pragma once

void print_hello();

// void print_bye()
// {
// std::cout << "Bye" << std::endl;
// }
inline void print_bye() {
std::cout << "Bye" << std::endl;
}
61 changes: 61 additions & 0 deletions tp02/Card.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "Card.hpp"

#include <iostream>

Card::Card(CardValeur value, CardColor color)
: _value { value }
, _color { color } {
}

Card::Card(unsigned int value, unsigned int color)
: Card{static_cast<CardValeur>(value), static_cast<CardColor>(color)} {

}

void Card::print() const {
std::cout << Card::string_from_value(_value) << " de " << Card::string_from_color(_color) << std::endl;
}

std::string Card::string_from_color(CardColor color) {
switch (color)
{
case CardColor::Coeur:
return "Coeur";
case CardColor::Carreau:
return "Carreau";
case CardColor::Trefle:
return "Trefle";
case CardColor::Pique:
return "Pique";
default:
return "?";
}
}

std::string Card::string_from_value(CardValeur value) {
switch (value)
{
case CardValeur::As:
return "AS";
case CardValeur::Roi:
return "Roi";
case CardValeur::Dame:
return "Dame";
case CardValeur::Valet:
return "Valet";
default:
return std::to_string(static_cast<int>(value));
}
}

std::ostream& operator<<(std::ostream& stream, Card card) {
return stream << Card::string_from_value(card._value) << " de " << Card::string_from_color(card._color);
}

bool Card::operator==(Card other) const {
return _value == other._value;
}

bool Card::operator<(Card other) const {
return _value < other._value;
}
47 changes: 47 additions & 0 deletions tp02/Card.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <ostream>
#include <string>
#pragma once

enum class CardColor
{
Coeur = 1,
Pique = 2,
Carreau = 3,
Trefle = 4
};

enum class CardValeur
{
Sept = 7,
Huit = 8,
Neuf = 9,
Dix = 10,
Valet = 11,
Dame = 12,
Roi = 13,
As = 14
};

class Card
{
public:

Card(CardValeur value, CardColor color);

Card(unsigned int value, unsigned int color);

void print() const;

friend std::ostream& operator<<(std::ostream& stream, Card card);

bool operator==(Card other) const;

bool operator<(Card other) const;

private:
CardValeur _value;
CardColor _color;
static std::string string_from_color(CardColor color);

static std::string string_from_value(CardValeur value);
};
95 changes: 95 additions & 0 deletions tp02/Player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "Player.hpp"

#include "Card.hpp"

#include <algorithm>
#include <random>
#include <vector>

Player::Player(std::string name)
: _name { name }
, _score { 0u } {};

unsigned int Player::turn_number = 0u;

void Player::deal_all_cards(Player& p1, Player& p2) {
std::vector<Card> all_cards;
for (unsigned int i = 7; i < 15; i++)
{
for (unsigned int j = 1; j < 5; j++)
{
all_cards.emplace_back(i, j);
}
}

std::random_device rd;
std::shuffle(all_cards.begin(), all_cards.end(), std::default_random_engine(rd()));
auto milieu = all_cards.size() / 2;
p1._cards.assign(all_cards.begin(), all_cards.begin() + milieu);
p2._cards.assign(all_cards.begin() + milieu, all_cards.end());
}

Card Player::operator[](unsigned int index) const {
if (index < _cards.size())
return _cards[index];
std::cerr << "Problème d'index !!!" << std::endl;
exit(EXIT_FAILURE);
}

unsigned int Player::get_score() const {
return _score;
}
std::string Player::get_name() const {
return _name;
}

bool Player::play(Player& p1, Player& p2) {
std::cout << "Turn Number: " << turn_number + 1 << std::endl;
std::cout << "Carte de " << p1._name << ": " << p1._cards[turn_number] << std::endl;
std::cout << "Carte de " << p2._name << ": " << p2._cards[turn_number] << std::endl;
if (!(p1._cards[turn_number] == p2._cards[turn_number]))
{
if (p1._cards[turn_number] < p2._cards[turn_number])
{
std::cout << p2._name << " a gagné 1 point." << std::endl;
p2._score++;
}
else
{
std::cout << p1._name << " a gagné 1 point." << std::endl;
p1._score++;
}
}
else {
turn_number++;
equality(p1, p2, 1);
}
turn_number++;
return turn_number >= 15;
}

void Player::equality(Player& p1, Player& p2, int point_to_win) {
if (turn_number >= 15) {
return;
}
std::cout << "C'est une égalité, on rejoue !!" << std::endl;
std::cout << "Carte de " << p1._name << ": " << p1._cards[turn_number] << std::endl;
std::cout << "Carte de " << p2._name << ": " << p2._cards[turn_number] << std::endl;
if (!(p1._cards[turn_number] == p2._cards[turn_number]))
{
if (p1._cards[turn_number] < p2._cards[turn_number])
{
std::cout << p2._name << " a gagné " << point_to_win + 1 << " points." << std::endl;
p2._score += point_to_win + 1;
}
else
{
std::cout << p1._name << " a gagné " << point_to_win + 1 << " points." << std::endl;
p1._score += point_to_win + 1;
}
}
else {
turn_number++;
equality(p1, p2, ++point_to_win);
}
}
24 changes: 24 additions & 0 deletions tp02/Player.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "Card.hpp"

#include <iostream>
#include <string>
#include <vector>
#pragma once

class Player
{
public:
static unsigned int turn_number;
Player(std::string name);
static void deal_all_cards(Player& p1, Player& p2);
Card operator[](unsigned int index) const;
static bool play(Player& p1, Player& p2);
unsigned int get_score() const;
std::string get_name() const;

private:
std::string _name;
std::vector<Card> _cards;
unsigned int _score;
static void equality(Player& p1, Player& p2, int point_to_win);
};
Binary file added tp02/main
Binary file not shown.
Loading