Страница 1 из 1

BlackJack

Добавлено: 22 янв 2013, 15:07
fuzzyduzzy
Здравствуйте! Задание написать игру Блэк Джек). Я реализовал создание колоды тусовку и чтобы карты выпадали по порядку. Проблема в выводе карт игрока и дилера (Может нужен еще какой то блок?).
void TwentyOneGame::FillDeck(){

gamer.GetCardBatch().setCard_Batch();
gamer.GetCardBatch().Shuffle();
gamer.GetCardBatch().display();

}
Мне кажется что данный блок не правильно реализован..Так как он должен относится к общей колоде.. Но у меня не получается к ней добраться(.
Подскажите плиз как решить данную проблему... Возможно кто то даст совет по оптимизации.

Код: Выделить всё

//CARD_H
#include <iostream>
#ifndef CARD_H
#define CARD_H

using namespace std;

enum Suit {clubs, diamonds, hearts, spades};

class Card{
private:
	Suit suit;
	int face;
	int points;
public:
	Card();
	void setCard(int, Suit);
	void PrintCard();
	void setPoints(int);
};

#endif CARD_H

Код: Выделить всё

//Card.cpp

#include <iostream>
#include "Card.h"

using namespace std;

Card::Card(){
	this->face = 0;
	this->suit = (Suit)0;
}

void Card::setCard(int f, Suit s){
	this->face = f;
	this->suit = s;
}

void Card::PrintCard(){
	if (face >= 2 && face <= 10)
		cout << face;
	else{
		switch (face){
			case 11: cout << 'J'; break;
			case 12: cout << 'Q'; break;
			case 13: cout << 'K'; break;
			case 14: cout << 'A'; break;
		}
	}
	switch (suit)
	{
	   case clubs: cout << static_cast<char>(5); break;
	   case diamonds: cout << static_cast<char>(4); break;
	   case hearts: cout << static_cast<char>(3); break;
	   case spades: cout << static_cast<char>(6); break;
	}
//cout << "\n\n";
}

Код: Выделить всё

//CARDBATCH_H
#include <iostream>
#include "Card.h"
#ifndef CARDBATCH_H
#define CARDBATCH_H

class Card_Batch{
	Card* cards;
	int count;
//	int nTopCard;
public:
	Card_Batch();
	int GetCount() const {return count;}
//	Card Transfer();
	void setCard_Batch();
	void Shuffle(); 
	void display(); // Для проверки
	void AddCard();
	void Shell();
//	Card GetByIndex(int index);
	~Card_Batch();
};
#endif CARDBATCH_H

Код: Выделить всё

//CARDBATCH.cpp
#include <iostream>
#include "CardBatch.h"
#include <ctime>

using namespace std;

Card_Batch::Card_Batch(){
	const int size = 52;
	count = 0;
	cards = new Card[size];
}

//Создание колоды карт

void Card_Batch::setCard_Batch(){
	for(int i = 0; i < 52; i++){
		int f = (i%13) + 2;
		Suit s = Suit(i/13);
		cards[i].setCard(f, s);
	}
}

//Тусуется колода карт
void Card_Batch::Shuffle(){
	srand (unsigned(time(NULL)));
	for(int i = 0; i < 52; i++){
		int k = rand()%52;		//выбираем случайную карту
		Card temp = cards[i];	//и меняем ее с текущей
		cards[i] = cards[k];
		cards[k] = temp;
	}

}

void Card_Batch::display(){
	for (int i = 0; i < 52; i++ ){
		cards[i].PrintCard();
		if(!((i+1)%13))			// начинаем новую строку после каждой 13-й карты
		  cout << endl;
    }
	cout << "\n\n\n";
}

void Card_Batch::AddCard(){
	for(int i = count; i < count + 1; i++)
		cards[i].PrintCard();
	count++;
}

void Card_Batch::Shell(){
//	nTopCard = 0;
	count = 0;
}

Card_Batch::~Card_Batch(){
	delete[] cards;
}

Код: Выделить всё

//PLAYER_H
#include <iostream>
#include "CardBatch.h"
#ifndef PLAYER_H
#define PLAYER_H

class Player{
	Card_Batch deck;
	int playerScore;
public:
	Player();
	Card_Batch& GetCardBatch(){return deck;}

//	void SetPlayerScore(int);
//	int GetPlayerScore() const {return playerScore;}
	void Drop();
};
#endif PLAYER_H

Код: Выделить всё

//Player.cpp
#include "Player.h"
Player::Player(){
	Drop();
}

void Player: :D rop(){
	playerScore = 0;
	deck.Shell();
};

Код: Выделить всё

//TWENTYONEGAME_H
#include "CardBatch.h"
#include "Player.h"
#ifndef TWENTYONEGAME_H
#define TWENTYONEGAME_H

class TwentyOneGame{
	Player dealer;
	Player gamer;

	Card_Batch cb;
public:
	void startGames();
	void NewGames();
	void FillDeck();
	void ShowPlayer(Player&);
	void Show();
	void addPlayerCard(Player&);
//	void CalculateScore(Player& gamer);
};
#endif TWENTYONEGAME_H

Код: Выделить всё

//TWENTYONEGAME.cpp
#include <iostream>
#include "TwentyOneGame.h"

#include <stdlib.h>
using namespace std;

void TwentyOneGame::NewGames(){
	system("cls");
	dealer.Drop();
	gamer.Drop();

	FillDeck();
	addPlayerCard(dealer);
	cout << "\n";
	addPlayerCard(gamer);
	addPlayerCard(gamer);

	Show();
}


void TwentyOneGame::addPlayerCard(Player& game){
	gamer.GetCardBatch().AddCard();
}

//Этот блок скорее всего не правильный...
void TwentyOneGame::FillDeck(){

	gamer.GetCardBatch().setCard_Batch();
	gamer.GetCardBatch().Shuffle();
	gamer.GetCardBatch().display();

}


void TwentyOneGame::ShowPlayer(Player& gamer){

}

void TwentyOneGame::Show( ){
	cout << "Карты Дилера: \n";
	ShowPlayer(dealer);
	cout << "Ваши карты: \n";
	ShowPlayer(gamer);
}