1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
enum ColourIndex { BLUE = 0, RED = 40, GREEN = 80, YELLOW = 120 };
enum CardIndex { C0 = 0, C1, C2, C3, C4, C5, C6, C7, C8, C9, CX1, CX2, CX3, CX4, CX5, CX6, CX7, CX8, CX9, CSKP, CXSKP, CP2, CXP2, CSWTCH, CXSWTCH, CARD_COUNT };
// Write a new card deck to file
void WriteDeck(std::string filename)
{
std::ofstream ofs(filename)
if(!ofs)
{
std::cout << "Could not open filestream.\n";
return;
}
for(int colour = 0; colour < ColourIndex::Yellow + 1; colour += 40)
{
for(int card = 0; card < CardIndex::CXSWTCH+1; card++)
{
ofs << colour+card << "\n";
}
}
}
std::ifstream deck_file;
bool init = false;
bool IfExists(std::string filename)
{
std::ifstream ifs(filename);
if(ifs) return true;
return false;
}
bool InitDeckFile(std::string filename)
{
deck_file.open(filename);
if(!deck_file)
{
std::cout << "Could not open " filename << ".\n";
return false;
}
init = true;
return true;
}
int GetNextCardFromDeck()
{
if(!init) return -1; // ifstream not open
if(ifs)
{
int card;
if(ifs >> card) return card;
return -2; // File error / end of stream perhaps
}
return -1; // Stream bad
}
// Check if player card and table card is of same colour
bool ValidColour(int pcard, int tcard)
{
if(pcard < ColourIndex::RED-5 && tcard < ColourIndex::RED-5) return 1; // Both player card and table card is BLUE(<35), valid move
if(pcard < ColourIndex::GREEN-5 && tcard < ColourIndex::GREEN-5)
if(pcard >= ColourIndex::RED && tcard >= ColourIndex::RED) return 1; // Both cards are RED(Between 40-74)
if(pcard < ColourIndex::YELLOW-5 && tcard < ColourIndex::YELLOW-5)
if(pcard >= ColourIndex::GREEN && tcard >= ColourIndex::GREEN) return 1; // Both cards are GREEN
if(pcard >= ColourIndex::YELLOW && tcard >= ColourIndex::YELLOW)
if(pcard < ColourIndex::YELLOW + CardIndex::Card_Count && tcard < ColourIndex::YELLOW + CardIndex::Card_Count ) return 1; // Both cards YELLOW
}
// Check if player card and table card is of same value
bool ValidCard(int pcard, int tcard)
{
// I'm still trying to figure out some witchery with division or modulus to get card offset
// I've gone completely blank
// Maybe something for you to do?
}
bool IsValid(int pcard, int tcard)
{
if(ValidColour(pcard, tcard) || ValidCard(pcard, tcard)) return true; // Card is of same colour OR same value
return false; // Card was not valid
}
int PickUp(int tcard)
{
if(tcard == ColourIndex::BLUE + CardIndex::CP2) return 2; // table card is a blue pick-up 2
if(tcard == ColourIndex::BLUE + CardIndex::CXP2) return 2; // table card is a blue pick-up 2
if(tcard == ColourIndex::RED + CardIndex::CP2) return 2; // table card is a red pick-up 2
if(tcard == ColourIndex::RED + CardIndex::CXP2) return 2; // table card is a red pick-up 2
if(tcard == ColourIndex::GREEN + CardIndex::CP2) return 2; // table card is a green pick-up 2
if(tcard == ColourIndex::GREEN + CardIndex::CXP2) return 2; // table card is a green pick-up 2
if(tcard == ColourIndex::YELLOW + CardIndex::CP2) return 2; // table card is a yellow pick-up 2
if(tcard == ColourIndex::YELLOW + CardIndex::CXP2) return 2; // table card is a yellow pick-up 2
return 0; // Not a pick up card;
}
int main()
{
if(!IfExists("Deck.txt")) WriteDeck("Deck.txt"); // If the deck file doesn't exist, make it
if(!InitDeckFile("Deck.txt")) return 1; // If we couldn't init the deck exit program with error code 1
int default_hand_number = 5; // Players start with a default number of cards in hand, feel free to change according to rules
int player1_held_cards = default_hand_number; // Give our player the default number of cards
int player1_current_card = GetNextCardFromDeck();
int ai_held_cards = default_hand_number;
int ai_current_card = GetNextCardFromDeck();
int table_card = GetNextCardFromDeck();
while(player1_held_cards > 0 || ai_held_cards > 0) // While player and Ai have more than 0 cards, play the game
{
// Player 1 turn
player1_held_cards += PickUp(table_card); // Have they gotta pick up?
if(IsValid(player1_current_card)) // Is their current card valid?
{
table_card = player1_current_card; // If so make that the new table card
player1_held_cards--; // Decrease number of cards;
player1_current_card == GetNextCardFromDeck(); // Get a new card
}
else player1_held_cards += 2; // Not a valid card, pick up 2
ai_held_cards += PickUp(table_card); // Has the AI gotta pick up?
if(IsValid(ai_current_card))
{
table_card = ai1_current_card;
ai_held_cards--; // Decrease number of cards;
ai_current_card == GetNextCardFromDeck(); // Get a new card
}
else ai_held_cards += 2; // AI card not valid, give them another 2 cards
}
if(player1_held_cards) std::cout << "AI Won!\n\n";
else std::cout << "P1 won!\n\n";
}
|