C++ (Qt)namespace CardGame { // ...}
C++ (Qt)#ifndef CARD_H#define CARD_H #include "string" namespace CardGame { std::string suits[4] = {"Diamonds ", "Hearts ", "Spades ", "Clubs "}; std::string cds[9] = {"6 ", "7 ", "8 ", "9 ", "10", "Jack ", "Queen ", "King ", "Ace "}; enum suit { diamonds, hearts, spades, clubs }; enum cname { six, seven, eight, nine, ten, jack, queen, king, ace }; class Card { public: void set_suit( suit s ) { m_ms = s; } void set_cn( cname c ) { m_cn = c; } std::string get_suit( ) { return suits[m_ms]; } std::string get_cn( ) { return cds[m_cn]; } private: suit m_ms; cname m_cn; }; } #endif // CARD_H
C++ (Qt)#ifndef DECK_H#define DECK_H #include "Card.h"#include <deque>#include <algorithm> namespace CardGame { class Deck { public: void ini( ) { Card tmp; for ( cname i = six; i <= ace; i = static_cast<cname> (i + 1) ) for ( suit j = diamonds; j <= clubs; j = static_cast<suit> (j + 1) ) { tmp.set_suit( j ); tmp.set_cn( i ); m_tdeck.push_back( tmp ); } shuffle( ); } void shuffle( ) { std::random_shuffle( m_tdeck.begin( ), m_tdeck.end( ) ); } Card get( ) { Card tmp = m_tdeck.front( ); m_tdeck.pop_front( ); return tmp; } private: std::deque<Card> m_tdeck; }; } #endif // DECK_H
C++ (Qt)#ifndef PLAYER_H#define PLAYER_H #include <deque>#include <iostream>#include "Card.h" namespace CardGame { class Player { public: Player(std::string name) : m_name(name) { } void show( ) { for ( unsigned i = 0; i < m_hand.size( ); i++ ) std::cout << m_hand[i].get_cn( ) << " "; std::cout << std::endl; for ( unsigned i = 0; i < m_hand.size( ); i++ ) std::cout << m_hand[i].get_suit( ) << " "; std::cout << std::endl << std::endl; } void ini( ) { m_hand.clear( ); } void add( Card c ) { m_hand.push_back( c ); } private: std::deque<Card> m_hand; std::string m_name; }; } #endif // PLAYER_H
C++ (Qt)#include <QCoreApplication>#include <vector>#include "Deck.h"#include "Player.h"#include <QDebug> int main( int argc, char *argv[] ) { QCoreApplication app( argc, argv ); // Колода CardGame::Deck deck; // Перемешиваем колоду deck.ini( ); // Игроки std::vector<CardGame::Player> players; players.push_back( CardGame::Player( "Ivan" ) ); players.push_back( CardGame::Player( "David" ) ); players.push_back( CardGame::Player( "Joan" ) ); // Раздаём игрокам по шесть карт for ( std::size_t i = 0; i < 6; ++i ) { try { players[i].add( deck.get( ) ); } catch ( ... ) { qDebug( ) << "Uncaught exception."; } } // Показываем карты игроков for ( std::size_t i = 0; i < players.size( ); ++i ) { players[i].show( ); } return app.exec( );}
C++ (Qt)int main( int argc, char *argv[] ) { QCoreApplication app( argc, argv ); // ... return app.exec( );}
C++ (Qt) // Раздаём игрокам по шесть карт for ( std::size_t i = 0; i < 6; ++i ) { try { players.at(i).add( deck.get( ) ); } catch (const std::out_of_range &e) { std::cerr << e.what() << std::endl; return 1; } catch ( ... ) { std::cerr << "Uncaught exception." << std::endl; return 1; } }