Last active
August 29, 2015 14:04
-
-
Save Evsenev/86de362170db8229fc72 to your computer and use it in GitHub Desktop.
Code
This file contains 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 <vector> | |
#include <stdio.h> | |
using namespace std; | |
static int PlayerBalance = 1000; | |
static int ComputerBalance = 1000; | |
class Game | |
{ | |
public: | |
int Bank, Bet; | |
int ComputerScore, PlayerScore; | |
Opponent WinResult () | |
{ | |
if(ComputerScore > 21 && PlayerScore > 21) | |
return Nobody; | |
if(ComputerScore > 21) | |
return Player; | |
if(PlayerScore > 21) | |
return Computer; | |
if(ComputerScore == PlayerScore) | |
return Nobody; | |
if(ComputerScore > PlayerScore) | |
return Computer; | |
else | |
return Player; | |
} | |
void PrintWinner(Opponent Winner){ | |
cout<<("\n\nОчки компьютера: {0}\t\tВаши очки: {1}",ComputerScore, PlayerScore); | |
if (Winner == Computer) | |
cout << "\nВы ПРОИГРАЛИ!"; | |
else if (Winner == Player) | |
cout << "\nВы ВЫИГРАЛИ!"; | |
else | |
cout << "\nНИЧЬЯ!"; | |
} | |
void AskPlayerCard () | |
{ | |
cout << "\nХотите взять еще карту?\nНажмите ДА (Y) или НЕТ (N)"; | |
char PressKey; | |
cin >> PressKey; | |
if (PressKey == 'Y' || PressKey == 'y') { | |
GiveCard (Player); | |
ShowCards (Player); | |
CalculateResult (); | |
if (PlayerScore > 21) { | |
return; | |
} else { | |
AskPlayerCard (); | |
} | |
} else if (PressKey == 'N' || PressKey == 'n') { | |
return; | |
} else { | |
AskPlayerCard (); | |
} | |
} | |
void StartGame () | |
{ | |
PrintBalance (); | |
BetToBank (); | |
PrintBalance (); | |
AddCards (); | |
GiveStartCards (); | |
ComputerLogic (); | |
ShowCards (Player); | |
AskPlayerCard (); | |
CalculateResult (); | |
system("cls"); | |
ShowCards (Player); | |
ShowCards (Computer); | |
Opponent isWinner = WinResult (); | |
MoneyToWinner (isWinner); | |
PrintWinner (isWinner); | |
} | |
void PrintBalance () | |
{ | |
cout << ("\nВаш баланс: ${0}.\tБаланс комьютера: ${1}", PlayerBalance, ComputerBalance); | |
} | |
void BetToBank () | |
{ | |
for (;;) { | |
cout << "Введите вашу ставку цифрами до $100.\nМоя ставка: "; | |
Bet =(int) scanf; | |
if (Bet <= 0 && Bet > 100) { | |
cout << "Попробуйте еще раз. Введите ставку от 1 до 100 цифрами."; | |
Bet = 0; | |
} | |
if (0 < Bet && Bet <= 100) { | |
if (Bet > PlayerBalance) { | |
cout <<"Попробуйте еще раз. Введите ставку меньше или равную балансу."; | |
Bet = 0; | |
} else { | |
break; | |
} | |
} | |
} | |
PlayerBalance -= Bet; | |
ComputerBalance -= Bet; | |
Bank = Bet * 2; | |
cout << ("\nВаша ставка ${0}.\t\tБанк: ${1}", Bet, Bank); | |
} | |
std::vector <Card> PlayerPack; | |
std::vector <Card> ComputerPack; | |
std::vector <Card> CardsPack; | |
std::string IntToString(int val) | |
{ | |
char buf[16]; | |
sprintf(buf, "%d", val); | |
return buf; | |
} | |
void AddCards () | |
{ | |
CardsPack.clear(); | |
ComputerScore = 0; | |
PlayerScore = 0; | |
int value = 2; | |
for (int i=0; i < 13; i++) { | |
for (int j = 0; j < 4; j++) { | |
if(i <= 8)CardsPack.push_back(Card(value, (Color)j,IntToString(value))); | |
if(i == 9) CardsPack.push_back (Card (value, (Color)j,"J")); | |
if(i == 10) CardsPack.push_back (Card (value, (Color)j,"Q")); | |
if(i == 11) CardsPack.push_back (Card (value, (Color)j,"K")); | |
if(i == 12) CardsPack.push_back (Card (value, (Color)j,"A")); | |
} | |
if (value < 10) | |
value++; | |
if (i > 10) | |
value = 11; | |
} | |
} | |
void ShowCards (Opponent WhosePack) | |
{ | |
if (WhosePack == Player) { | |
cout << "\nВаши карты:"; | |
for (int i = 0; i < PlayerPack.size; i++) { | |
printf ("\t{0} ", PlayerPack[i].view); | |
if (PlayerPack [i].CardColor == Clubs) | |
cout << "♣"; | |
if (PlayerPack [i].CardColor == Spades) | |
cout << "♠"; | |
if (PlayerPack [i].CardColor == Hearts) | |
cout << "♥"; | |
if (PlayerPack [i].CardColor == Diamonds) | |
cout << "♦"; | |
} | |
} | |
if (WhosePack == Computer) { | |
cout << "\nКарты компьютера:"; | |
for (int i = 0; i < ComputerPack.size; i++) { | |
printf ("\t{0} ", ComputerPack [i].view); | |
if (ComputerPack [i].CardColor == Clubs) | |
cout << "♣"; | |
if (ComputerPack [i].CardColor == Spades) | |
cout << "♠"; | |
if (ComputerPack [i].CardColor == Hearts) | |
cout << "♥"; | |
if (ComputerPack [i].CardColor == Diamonds) | |
cout << "♦"; | |
} | |
} | |
} | |
void GiveCard (Opponent WhoTakeCard) | |
{ | |
int randomCard = rand() % CardsPack.size; | |
if (WhoTakeCard == Player) { | |
PlayerPack.push_back (CardsPack [randomCard]); | |
CardsPack.erase(CardsPack.begin()+randomCard); | |
} else { | |
ComputerPack.push_back(CardsPack[randomCard]); | |
CardsPack.erase(CardsPack.begin()+randomCard); | |
} | |
} | |
void GiveStartCards () | |
{ | |
PlayerPack.clear(); | |
ComputerPack.clear(); | |
GiveCard (Player); | |
GiveCard (Computer); | |
GiveCard (Player); | |
GiveCard (Computer); | |
} | |
void ComputerLogic () //Пока что простая и тупая логика | |
{ | |
CalculateResult (); | |
if (ComputerScore <= 15) { | |
GiveCard (Computer); | |
ComputerLogic (); | |
} else { | |
return; | |
} | |
} | |
void CalculateResult () | |
{ | |
PlayerScore = 0; | |
ComputerScore = 0; | |
for (int i = 0; i < PlayerPack.size; i++) { | |
PlayerScore += PlayerPack [i].card_score; | |
} | |
for (int i = 0; i < ComputerPack.size; i++) { | |
ComputerScore += ComputerPack [i].card_score; | |
} | |
} | |
void MoneyToWinner (Opponent Winner) | |
{ | |
if (Winner == Player) { | |
PlayerBalance += Bank; | |
} else if (Winner == Computer) { | |
ComputerBalance += Bank; | |
} else if (Winner == Nobody) { | |
ComputerBalance += Bet; | |
PlayerBalance += Bet; | |
} | |
} | |
}; | |
enum Color | |
{ Spades, | |
Hearts, | |
Clubs, | |
Diamonds | |
}; | |
enum Opponent {Player, Computer, Nobody} ; | |
class Card | |
{ | |
public: | |
int card_score; | |
Color CardColor; | |
string view; | |
Card (int theCardScore, Color theCardColor, string theView) | |
{ | |
card_score = theCardScore; | |
CardColor = theCardColor; | |
view = theView; | |
} | |
}; | |
void main() | |
{ | |
Game GamePlay; | |
cout << "♠ ♣ ♥ ♦ Добро пожаловать в Black Jack ♦ ♥ ♣ ♠\n"; | |
for(;;) | |
{ | |
GamePlay.StartGame (); | |
if (ComputerBalance <= 0) { | |
cout << "У Компьютера кончились деньги. "; | |
break; | |
} | |
if (PlayerBalance <= 0) { | |
cout << "У Вас кончились деньги."; | |
break; | |
} | |
printf("\nНажмите любую клавишу, чтобы продолжить игру..."); | |
cin.ignore(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment