0% found this document useful (0 votes)
30 views5 pages

Main Cppas

The document defines classes and functions for a card game including: - An enum to represent player teams (Red, Green, Blue) - Functions to generate a deck of cards and deal cards to players - A Player class to represent players with a name, team, deck and other attributes - Functions to display the game board, player decks, and possible moves - A function to place a token on the board if a card is valid and not already placed - A main function to initialize the game and call the play function to run game turns

Uploaded by

AMEERO THE HERO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views5 pages

Main Cppas

The document defines classes and functions for a card game including: - An enum to represent player teams (Red, Green, Blue) - Functions to generate a deck of cards and deal cards to players - A Player class to represent players with a name, team, deck and other attributes - Functions to display the game board, player decks, and possible moves - A function to place a token on the board if a card is valid and not already placed - A main function to initialize the game and call the play function to run game turns

Uploaded by

AMEERO THE HERO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

#include <iomanip>
#include <vector>

enum Team {NONE, RED, GREEN, BLUE};

std::vector<std::string> generateDeck() {
std::vector<std::string> cards;
std::vector<std::string> suits = {"H", "C", "S", "D"};
std::vector<std::string> ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K", "A"};

for (const auto& suit : suits) {


for (const auto& rank : ranks) {
cards.push_back(suit+rank);
}
}
return cards;

static std::vector<std::string> cards = generateDeck();

class Player {
public:
Player(std::string name, Team team, int deckSize)
: name(name), team(team), deckSize(deckSize) {
std::vector<std::string> deck;
};

std::string getName() const {


return name;
}

Team getTeam() const {


return team;
}

std::vector<std::string> getDeck() {
return deck;
}
int getDeckSize() {
return deckSize;
}
void displayPlayerDeck() {
for (int i = 0; i< deckSize; i++) {
std::cout << "decksize:"<< i << std::endl;
std::cout << deck.at(i) << ", ";
}
std::cout << std::endl;
}

void dealCards() {
while (deck.size()< deckSize) {
int cardIndex = (rand() % 52);
std::string selectedCard = cards.at(cardIndex);
deck.push_back(selectedCard);
cards.erase(cards.begin() + cardIndex);
std::cout << "Delt " << selectedCard << "." << std::endl;
}
}
private:
const std::string name;
const Team team;
const int deckSize;
std::vector<std::string> deck;

};

void displayTextUI(std::string gameBoard[10][10], std::vector<std::pair<Team,


std::pair<int, int>>>& tileMap) {
std::cout << "\n===========================================\n";
for (int i = 0; i < 10; i++) {
std::cout << "|";
for (int j = 0; j < 10; j++) {
std::string card = gameBoard[i][j];
std::string color = "\033[0m"; // Default Color

for (const auto& map : tileMap) {


int x = map.second.first;
int y = map.second.second;
if (i == y && j == x) {
Team team = map.first;
if (team == Team::RED) {
color = "\033[31m"; // Red color
}
else if (team == Team::GREEN) {
color = "\033[32m"; // Green color
}
else if (team == Team::BLUE) {
color = "\033[34m"; // Blue color
}
break;
}
}

std::cout << color << std::setw(3) << card << "\033[0m|";


}
std::cout << "\n-----------------------------------------\n";
}
}

std::string teamToString(Team team) {


std::string stringTeam = team == Team::RED ? "RED" : team == Team::BLUE ?
"BLUE" : "GREEN";
return stringTeam;

bool isAdjacent(std::vector<std::pair<Team, std::pair<int, int>>> tileMap,


std::pair<int, int> pos) {
std::pair<int, int> directions[8] = { {1, 0}, {1, 1}, {0, 1}, {-1, 0}, {-1, -
1}, {0, -1}, {1, -1}, {-1, 1} };
for (const auto& offset : directions) {
for (const auto& tile : tileMap) {
std::pair<int, int> tileCoords = tile.second;
if (tileCoords.first == pos.first + offset.first && tileCoords.second
== pos.second + offset.second) {
return 1;
}
}
}
return 0;
}

std::vector<std::pair<int, int>> getPossibleMoves(std::string gameBoard[10][10],


std::vector<std::pair<Team, std::pair<int, int>>> tileMap) {
std::vector<std::pair<int, int>> possibleMoves;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
for (const auto& tile : tileMap) {
if (i == tile.second.first && j == tile.second.second) {
std::pair<int, int> pos = std::make_pair(i, j);
if (isAdjacent(tileMap, pos)) {
possibleMoves.insert(possibleMoves.begin(), pos);
}
else {
possibleMoves.push_back(std::make_pair(i, j));
}
}
}
}
}
return possibleMoves;
}

bool placeToken(std::string gameBoard[10][10], std::vector<std::pair<Team,


std::pair<int, int>>> &tileMap, std::pair<int, int> pos, Player player) {
std::string card = gameBoard[pos.first][pos.second];
bool inDeck = false;
std::vector<std::string > playerDeck = player.getDeck();
for (int i = 0; i < playerDeck.size(); i++) {
if (card == playerDeck[i]) {
inDeck = true;
break;
}
}
if (!inDeck) {
std::cout << "Card not Owned." << std::endl;
return 0;
}

for (const auto& tile : tileMap) {


std::pair<int, int> tileCoords = std::make_pair(tile.second.first,
tile.second.second);
if (tileCoords.first == pos.first && tileCoords.second == pos.second) { //
Check if tile is already place there
return 0;
}
}

tileMap.push_back(std::make_pair(player.getTeam(), pos));
return 1;
}
void play(std::string gameBoard[10][10], std::vector<Player> players,
std::vector<std::pair<Team, std::pair<int, int>>> &tileMap) {
int turnValue = 0;
int rounds = 8;

for (Player player : players) {


player.dealCards();
}
for (int i = 0; i < rounds; i++) {

//Player turn variables


Player player = players.at(turnValue);
std::string playerString = player.getName();
Team team = player.getTeam();

//Visual Display
std::cout << playerString << "'s turn | TEAM: " << teamToString(team) <<
std::endl;
std::cout << player.getDeck().size();
player.displayPlayerDeck();
displayTextUI(gameBoard, tileMap);

//Player turn
bool placed = false;
while (!placed) {
std::pair<int, int> pos;
std::cout << "POS:";
std::cin >> pos.first >> pos.second;
placed = placeToken(gameBoard, tileMap, pos, player);
}

turnValue = (turnValue + 1) % players.size();

}
}

int main() {
std::string gameBoard[10][10] = {
{"F1", "JS", "9S", "KH", "9D", "QC", "9H", "KC", "JD", "F2"},
{"3C", "2H", "4H", "6D", "8S", "KS", "2S", "6C", "10C", "2D"},
{"5C", "QD", "10D", "QS", "JC", "2C", "4D", "8C", "AC", "4S"},
{"3D", "AH", "3H", "7D", "9S", "AS", "6S", "10S", "KD", "6H"},
{"5D", "JH", "5H", "7S", "JS", "4C", "8D", "QH", "KC", "8H"},
{"3S", "AD", "7H", "10H", "QS", "6D", "10D", "KH", "AC", "10H"},
{"5S", "KS", "5C", "7C", "2H", "8H", "JH", "9C", "2S", "QH"},
{"7S", "AS", "3C", "9C", "4H", "10S", "KD", "JC", "4C", "KH"},
{"6S", "QD", "5D", "7D", "6C", "QH", "KC", "3H", "8S", "9C"},
{"F3", "10C", "AC", "2D", "4S", "6H", "8H", "JS", "AD", "F4"}
};
Player Player1 = Player("P1", BLUE, 6);
Player Player2 = Player("P2", RED, 6);
Player Player3 = Player("P3", GREEN, 6);

std::vector<Player> players = { Player1, Player2, Player3 };


std::vector<std::pair<Team, std::pair<int, int>>> tileMap;
play(gameBoard, players, tileMap);

You might also like