Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <iostream>
- #include <time.h>
- #include "thegame.h"
- #include <Windows.h>
- #include <sstream>
- #include <random>
- #include <string>
- void swapInt(int* a, int* b) {
- //N.B. You must make sure *a and *b are not pointing to the same address
- //N.B. You must make sure *a and *b are not pointing to the same address
- *a = *a + *b;
- *b = *a - *b;
- *a = *a - *b;
- }
- void sortIntArray(int* array, int size) {
- }
- int player_process_turn(TheGame* game, Player* player) {
- int num_cards_played = 0;
- bool turn = true;
- while (turn == true){
- int myCard;
- int myPile;
- PlayPile* playPile;
- printf("Which card do you want to play? Press '9' to finish your turn if you've played at least 2 cards.\n");
- scanf("%d", &myCard);
- if (myCard == 9 && num_cards_played >= 2) {
- turn = false;
- }
- //game->play_piles[myPile] =
- 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");
- scanf("%d", &myPile);
- if (myPile == 9 && num_cards_played >= 2){
- turn = false;
- }
- myPile = myPile - 1;
- playPile = &(game->play_piles[myPile]);
- if (is_play_valid(myCard, playPile)) {
- player->cards[myCard - 1] = 0;
- num_cards_played = num_cards_played + 1;
- player_play_card_to_pile(player, myCard, &(game->play_piles[myPile - 1]));
- }
- }
- int whichcard;
- int whichpile;
- return num_cards_played;
- }
- void initialise_player(Player* player, int hand_size, Deck* deck) {
- player->max_hand_size = hand_size;
- player->hand_size = 8;
- int ncards = player_draw_cards_from_deck(player, deck, player->max_hand_size);
- std::cout << "Player has drawn " << ncards << " from the deck.\n";
- }
- void display_player_hand(const Player* player) {
- int num_cards = player->hand_size;
- const int* cards = player->cards;
- for (int i = 0; i < num_cards; i++) {
- std::cout << i + 1 << ":[" << *(cards + i) << "] ";
- }
- std::cout << std::endl;
- }
- void display_deck(const Deck* deck) {
- std::cout << "Current deck size: " << deck->deck_size - *(deck->cnt_top);
- std::cout<< " Current Top Card Index: " << *(deck->cnt_top) << std::endl;
- }
- void display_game(const TheGame* game) {
- display_deck(&(game->draw_deck));
- printf("Play Piles:\n1:A[%d], 2:A[%d], 3:D[%d], 4:D[%d]\n",
- game->play_piles[0].top_card,
- game->play_piles[1].top_card,
- game->play_piles[2].top_card,
- game->play_piles[3].top_card);
- }
- bool is_play_valid(int card, PlayPile* pile) {
- bool valid = false;
- if (pile->is_ascending) {
- if (card > pile->top_card) {
- valid = true;
- }
- if (card = (pile->top_card - 10)) {
- valid = true;
- }
- else {
- valid = false;
- }
- }
- if (!(pile->is_ascending)) {
- if (card < pile->top_card) {
- valid = true;
- }
- if (card = (pile->top_card + 10)) {
- valid = true;
- }
- else {
- valid = false;
- }
- }
- return valid;
- }
- bool player_play_card_to_pile(Player* player, int whichcard, PlayPile* pile) {
- pile->top_card = whichcard;
- pile->pile_size += 1;
- return true;
- }
- void initialise_deck(Deck* deck, int decksize) {
- deck->cards = new int[decksize];
- deck->deck_size = decksize;
- deck->cnt_top = &(deck->cards[0]);
- for (int i = 0; i < decksize; i++) {
- deck->cards[i] = i + 1;
- }
- int shuf = (sizeof(deck->cards) / sizeof(deck->cards[0]));
- shuffle_deck(deck, shuf);
- }
- int player_draw_cards_from_deck(Player* player, Deck* deck, int count) {
- int num_card_drawn = 0;
- for (count = 0; count < player->hand_size; count++) {
- if (player->cards[count] == 0) {
- player->cards[count] = *(deck->cnt_top);
- deck->cnt_top ++;
- num_card_drawn += 1;
- }
- }
- return num_card_drawn;
- }
- void cleanup_deck(Deck* deck) {
- delete[] deck->cards;
- }
- void shuffle_deck(Deck* deck, int times) {
- for (int i = 0; i < 98; i++) {
- int r = rand() % 98;
- for (int c = 0; c < times; c++) {
- swapInt(&deck->cards[i], &deck->cards[r]);
- }
- }
- }
- void initialise_play_pile(PlayPile* pile, bool ascending) {
- pile->pile_size = 0;
- pile->top_card = ascending ? 1 : 100;
- pile->is_ascending = ascending;
- }
- TheGame* create_new_game(int num_players) {
- TheGame* game = new TheGame();
- initialise_game(game, num_players);
- return game;
- }
- void initialise_game(TheGame* game, int num_players) {
- srand(time(NULL));
- initialise_deck(&(game->draw_deck), 98);
- game->num_players = num_players;
- game->turns = 0;
- initialise_play_pile(&(game->play_piles[0]), true);
- initialise_play_pile(&(game->play_piles[1]), true);
- initialise_play_pile(&(game->play_piles[2]), false);
- initialise_play_pile(&(game->play_piles[3]), false);
- for (int i = 0; i < num_players; i++) {
- initialise_player(&(game->players[i]), 8, &(game->draw_deck));
- }
- }
- bool is_game_ended(const TheGame* game) {
- bool ended = false;
- return ended;
- }
- void cleanup_game(TheGame* game) {
- cleanup_deck(&(game->draw_deck));
- delete game;
- }
- int main(int argc, char** argv) {
- //initialise the game with 1 player
- TheGame* game = create_new_game(1);
- //
- initialise_game(game, 1);
- //Start the game loop
- //Feel free to modify game loop
- while (!is_game_ended(game)) {
- int cnt_player = game->turns % game->num_players;
- Player* activeplayer = &(game->players[cnt_player]);
- display_game(game);
- display_player_hand(activeplayer);
- player_process_turn(game, activeplayer);
- player_draw_cards_from_deck(activeplayer, &(game->draw_deck), activeplayer->max_hand_size);
- game->turns += 1;
- }
- //Clean up the game
- cleanup_game(game);
- return 0;
- }
Add Comment
Please, Sign In to add comment