Guest User

Main

a guest
Dec 14th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.53 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #include <time.h>
  4. #include "thegame.h"
  5. #include <Windows.h>
  6. #include <sstream>
  7. #include <random>
  8. #include <string>
  9.  
  10. void swapInt(int* a, int* b) {
  11.     //N.B. You must make sure *a and *b are not pointing to the same address
  12.     //N.B. You must make sure *a and *b are not pointing to the same address
  13.     *a = *a + *b;
  14.     *b = *a - *b;
  15.     *a = *a - *b;
  16. }
  17.  
  18. void sortIntArray(int* array, int size) {
  19.  
  20. }
  21.  
  22. int player_process_turn(TheGame* game, Player* player) {
  23.     int num_cards_played = 0;
  24.     bool turn = true;
  25.     while (turn == true){
  26.         int myCard;
  27.         int myPile;
  28.         PlayPile* playPile;
  29.         printf("Which card do you want to play? Press '9' to finish your turn if you've played at least 2 cards.\n");
  30.         scanf("%d", &myCard);
  31.         if (myCard == 9 && num_cards_played >= 2) {
  32.             turn = false;
  33.         }
  34.         //game->play_piles[myPile] =
  35.         printf("Which pile would you like to place your card on? Press '9' to finish your turn if you've played at least 2 cards.\n");
  36.         scanf("%d", &myPile);
  37.         if (myPile == 9 && num_cards_played >= 2){
  38.             turn = false;
  39.         }
  40.         myPile = myPile - 1;
  41.         playPile = &(game->play_piles[myPile]);
  42.         if (is_play_valid(myCard, playPile)) {
  43.             player->cards[myCard - 1] = 0;
  44.             num_cards_played = num_cards_played + 1;
  45.             player_play_card_to_pile(player, myCard, &(game->play_piles[myPile - 1]));
  46.  
  47.         }
  48.  
  49. }
  50.  
  51.     int whichcard;
  52.     int whichpile;
  53.     return num_cards_played;
  54. }
  55.  
  56. void initialise_player(Player* player, int hand_size, Deck* deck) {
  57.     player->max_hand_size = hand_size;
  58.     player->hand_size = 8;
  59.  
  60.     int ncards = player_draw_cards_from_deck(player, deck, player->max_hand_size);
  61.  
  62.     std::cout << "Player has drawn " << ncards << " from the deck.\n";
  63. }
  64.  
  65. void display_player_hand(const Player* player) {
  66.     int num_cards = player->hand_size;
  67.     const int* cards = player->cards;
  68.  
  69.     for (int i = 0; i < num_cards; i++) {
  70.         std::cout << i + 1 << ":[" << *(cards + i) << "] ";
  71.     }
  72.     std::cout << std::endl;
  73. }
  74.  
  75. void display_deck(const Deck* deck) {
  76.     std::cout << "Current deck size: " << deck->deck_size - *(deck->cnt_top);
  77.         std::cout<< " Current Top Card Index: " << *(deck->cnt_top) << std::endl;
  78. }
  79.  
  80. void display_game(const TheGame* game) {
  81.     display_deck(&(game->draw_deck));
  82.  
  83.     printf("Play Piles:\n1:A[%d], 2:A[%d], 3:D[%d], 4:D[%d]\n",
  84.         game->play_piles[0].top_card,
  85.         game->play_piles[1].top_card,
  86.         game->play_piles[2].top_card,
  87.         game->play_piles[3].top_card);
  88. }
  89.  
  90. bool is_play_valid(int card, PlayPile* pile) {
  91.     bool valid = false;
  92.  
  93.     if (pile->is_ascending) {
  94.         if (card > pile->top_card) {
  95.             valid = true;
  96.         }
  97.         if (card = (pile->top_card - 10)) {
  98.             valid = true;
  99.         }
  100.         else {
  101.             valid = false;
  102.         }
  103.     }
  104.  
  105.     if (!(pile->is_ascending)) {
  106.         if (card < pile->top_card) {
  107.             valid = true;
  108.         }
  109.             if (card = (pile->top_card + 10)) {
  110.                 valid = true;
  111.             }
  112.             else {
  113.                 valid = false;
  114.             }
  115.         }
  116.     return valid;
  117. }
  118.  
  119. bool player_play_card_to_pile(Player* player, int whichcard, PlayPile* pile) {
  120.     pile->top_card = whichcard;
  121.     pile->pile_size += 1;
  122.     return true;
  123. }
  124.  
  125. void initialise_deck(Deck* deck, int decksize) {
  126.     deck->cards = new int[decksize];
  127.     deck->deck_size = decksize;
  128.     deck->cnt_top = &(deck->cards[0]);
  129.  
  130.     for (int i = 0; i < decksize; i++) {
  131.         deck->cards[i] = i + 1;
  132.     }
  133.  
  134.     int shuf = (sizeof(deck->cards) / sizeof(deck->cards[0]));
  135.  
  136.     shuffle_deck(deck, shuf);
  137. }
  138.  
  139. int player_draw_cards_from_deck(Player* player, Deck* deck, int count) {
  140.     int num_card_drawn = 0;
  141.             for (count = 0; count < player->hand_size; count++) {
  142.                 if (player->cards[count] == 0) {
  143.                     player->cards[count] = *(deck->cnt_top);
  144.                     deck->cnt_top ++;
  145.                     num_card_drawn += 1;
  146.                 }
  147.     }
  148.         return num_card_drawn;
  149. }
  150.  
  151. void cleanup_deck(Deck* deck) {
  152.     delete[] deck->cards;
  153. }
  154.  
  155. void shuffle_deck(Deck* deck, int times) {
  156.         for (int i = 0; i < 98; i++) {
  157.             int r = rand() % 98;
  158.                 for (int c = 0; c < times; c++) {
  159.                     swapInt(&deck->cards[i], &deck->cards[r]);
  160.                     }
  161.             }
  162.     }
  163.  
  164.  
  165. void initialise_play_pile(PlayPile* pile, bool ascending) {
  166.     pile->pile_size = 0;
  167.     pile->top_card = ascending ? 1 : 100;
  168.     pile->is_ascending = ascending;
  169. }
  170.  
  171. TheGame* create_new_game(int num_players) {
  172.     TheGame* game = new TheGame();
  173.  
  174.     initialise_game(game, num_players);
  175.  
  176.     return game;
  177. }
  178.  
  179. void initialise_game(TheGame* game, int num_players) {
  180.     srand(time(NULL));
  181.  
  182.     initialise_deck(&(game->draw_deck), 98);
  183.     game->num_players = num_players;
  184.     game->turns = 0;
  185.  
  186.     initialise_play_pile(&(game->play_piles[0]), true);
  187.     initialise_play_pile(&(game->play_piles[1]), true);
  188.     initialise_play_pile(&(game->play_piles[2]), false);
  189.     initialise_play_pile(&(game->play_piles[3]), false);
  190.  
  191.     for (int i = 0; i < num_players; i++) {
  192.         initialise_player(&(game->players[i]), 8, &(game->draw_deck));
  193.     }
  194.  
  195. }
  196.  
  197. bool is_game_ended(const TheGame* game) {
  198.     bool ended = false;
  199.  
  200.     return ended;
  201. }
  202.  
  203. void cleanup_game(TheGame* game) {
  204.     cleanup_deck(&(game->draw_deck));
  205.  
  206.     delete game;
  207. }
  208.  
  209. int main(int argc, char** argv) {
  210.     //initialise the game with 1 player
  211.     TheGame* game = create_new_game(1);
  212.  
  213.     //
  214.     initialise_game(game, 1);
  215.  
  216.     //Start the game loop
  217.     //Feel free to modify game loop
  218.     while (!is_game_ended(game)) {
  219.         int cnt_player = game->turns % game->num_players;
  220.         Player* activeplayer = &(game->players[cnt_player]);
  221.  
  222.         display_game(game);
  223.         display_player_hand(activeplayer);
  224.         player_process_turn(game, activeplayer);
  225.         player_draw_cards_from_deck(activeplayer, &(game->draw_deck), activeplayer->max_hand_size);
  226.  
  227.         game->turns += 1;
  228.     }
  229.  
  230.     //Clean up the game
  231.     cleanup_game(game);
  232.  
  233.     return 0;
  234. }
Add Comment
Please, Sign In to add comment