void Board::takeStep(){ checkTurn(); vec step = inputVec(); if(step == vec(-1, -1)){ fprintf(stderr, "no input vector!\n"); step = inputVec(); } while(!canFlip(step.x, step.y, turnToPieceType(turn))){ fprintf(stderr, "invalid input (%d, %d)!\n", step.x, step.y); step = inputVec(); } setPieces(step.x, step.y); if(checkEnd()){ if(countColor(black) > countColor(white)){ printf("black: %d\n", countColor(black)); printf("white: %d\n", countColor(white)); printf("black wins!\n"); } else if(countColor(black) < countColor(white)){ printf("black: %d\n", countColor(black)); printf("white: %d\n", countColor(white)); printf("white wins!\n"); } else{ printf("black: %d\n", countColor(black)); printf("white: %d\n", countColor(white)); printf("ties!\n"); } printBoard(); exit(0); } else{ printBoard(); } }
int main(int argc, char *argv[]) { int opt; struct sockaddr_can addr; struct canfd_frame frame; int running = 1; int enable_canfd = 1; int play_traffic = 1; struct stat st; SDL_Event event; while ((opt = getopt(argc, argv, "Xdl:s:t:h?")) != -1) { switch(opt) { case 'l': difficulty = atoi(optarg); break; case 's': seed = atoi(optarg); break; case 't': traffic_log = optarg; break; case 'd': debug = 1; break; case 'X': play_traffic = 0; break; case 'h': case '?': default: usage(NULL); break; } } if (optind >= argc) usage("You must specify at least one can device"); if(stat(traffic_log, &st) == -1) { char msg[256]; snprintf(msg, 255, "CAN Traffic file not found: %s\n", traffic_log); usage(msg); } /* open socket */ if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { perror("socket"); return 1; } addr.can_family = AF_CAN; strcpy(ifr.ifr_name, argv[optind]); if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) { perror("SIOCGIFINDEX"); return 1; } addr.can_ifindex = ifr.ifr_ifindex; if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &enable_canfd, sizeof(enable_canfd))){ printf("error when enabling CAN FD support\n"); return 1; } if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); return 1; } door_id = DEFAULT_DOOR_ID; signal_id = DEFAULT_SIGNAL_ID; speed_id = DEFAULT_SPEED_ID; if (seed) { srand(seed); door_id = (rand() % 2046) + 1; signal_id = (rand() % 2046) + 1; speed_id = (rand() % 2046) + 1; door_pos = rand() % 9; signal_pos = rand() % 9; speed_pos = rand() % 8; printf("Seed: %d\n", seed); door_len = door_pos + 1; signal_len = signal_pos + 1; speed_len = speed_len + 2; } if(difficulty > 0) { if (door_len < 8) { door_len += rand() % (8 - door_len); } else { door_len = 0; } if (signal_len < 8) { signal_len += rand() % (8 - signal_len); } else { signal_len = 0; } if (speed_len < 8) { speed_len += rand() % (8 - speed_len); } else { speed_len = 0; } } if(play_traffic) { signal(SIGALRM,(void (*)(int))kill_child); play_id = fork(); if((int)play_id == -1) { printf("Error: Couldn't fork bg player\n"); exit(-1); } else if (play_id != 0) { play_can_traffic(); } } // GUI Setup SDL_Window *window = NULL; SDL_Surface *screenSurface = NULL; if(SDL_Init ( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) < 0 ) { printf("SDL Could not initializes\n"); exit(40); } if( SDL_NumJoysticks() < 1) { printf(" Warning: No joysticks connected\n"); } else { if(SDL_IsGameController(0)) { gGameController = SDL_GameControllerOpen(0); if(gGameController == NULL) { printf(" Warning: Unable to open game controller. %s\n", SDL_GetError() ); } else { gJoystick = SDL_GameControllerGetJoystick(gGameController); gHaptic = SDL_HapticOpenFromJoystick(gJoystick); print_joy_info(); } } else { gJoystick = SDL_JoystickOpen(0); if(gJoystick == NULL) { printf(" Warning: Could not open joystick\n"); } else { gHaptic = SDL_HapticOpenFromJoystick(gJoystick); if (gHaptic == NULL) printf("No Haptic support\n"); print_joy_info(); } } } window = SDL_CreateWindow("CANBus Control Panel", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); if(window == NULL) { printf("Window could not be shown\n"); } renderer = SDL_CreateRenderer(window, -1, 0); SDL_Surface *image = IMG_Load(get_data("joypad.png")); base_texture = SDL_CreateTextureFromSurface(renderer, image); SDL_RenderCopy(renderer, base_texture, NULL, NULL); SDL_RenderPresent(renderer); int button, axis; // Used for checking dynamic joystick mappings while(running) { while( SDL_PollEvent(&event) != 0 ) { switch(event.type) { case SDL_QUIT: running = 0; break; case SDL_WINDOWEVENT: switch(event.window.event) { case SDL_WINDOWEVENT_ENTER: case SDL_WINDOWEVENT_RESIZED: redraw_screen(); break; } case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_UP: throttle = 1; break; case SDLK_LEFT: turning = -1; break; case SDLK_RIGHT: turning = 1; break; case SDLK_LSHIFT: lock_enabled = 1; if(unlock_enabled) send_lock(CAN_DOOR1_LOCK | CAN_DOOR2_LOCK | CAN_DOOR3_LOCK | CAN_DOOR4_LOCK); break; case SDLK_RSHIFT: unlock_enabled = 1; if(lock_enabled) send_unlock(CAN_DOOR1_LOCK | CAN_DOOR2_LOCK | CAN_DOOR3_LOCK | CAN_DOOR4_LOCK); break; case SDLK_a: if(lock_enabled) { send_lock(CAN_DOOR1_LOCK); } else if(unlock_enabled) { send_unlock(CAN_DOOR1_LOCK); } break; case SDLK_b: if(lock_enabled) { send_lock(CAN_DOOR2_LOCK); } else if(unlock_enabled) { send_unlock(CAN_DOOR2_LOCK); } break; case SDLK_x: if(lock_enabled) { send_lock(CAN_DOOR3_LOCK); } else if(unlock_enabled) { send_unlock(CAN_DOOR3_LOCK); } break; case SDLK_y: if(lock_enabled) { send_lock(CAN_DOOR4_LOCK); } else if(unlock_enabled) { send_unlock(CAN_DOOR4_LOCK); } break; } kk_check(event.key.keysym.sym); break; case SDL_KEYUP: switch(event.key.keysym.sym) { case SDLK_UP: throttle = -1; break; case SDLK_LEFT: case SDLK_RIGHT: turning = 0; break; case SDLK_LSHIFT: lock_enabled = 0; break; case SDLK_RSHIFT: unlock_enabled = 0; break; } break; case SDL_JOYAXISMOTION: axis = event.jaxis.axis; if(axis == gAxisLeftH) { ud(event.jaxis.value); } else if(axis == gAxisLeftV) { turn(event.jaxis.value); } else if(axis == gAxisR2) { accelerate(event.jaxis.value); } else if(axis == gAxisRightH || axis == gAxisRightV || axis == gAxisL2 || axis == gJoyX || axis == gJoyY || axis == gJoyZ) { // Do nothing, the axis is known just not connected } else { if (debug) printf("Unkown axis: %d\n", event.jaxis.axis); } break; case SDL_JOYBUTTONDOWN: button = event.jbutton.button; if(button == gButtonLock) { lock_enabled = 1; if(unlock_enabled) send_lock(CAN_DOOR1_LOCK | CAN_DOOR2_LOCK | CAN_DOOR3_LOCK | CAN_DOOR4_LOCK); } else if(button == gButtonUnlock) { unlock_enabled = 1; if(lock_enabled) send_unlock(CAN_DOOR1_LOCK | CAN_DOOR2_LOCK | CAN_DOOR3_LOCK | CAN_DOOR4_LOCK); } else if(button == gButtonA) { if(lock_enabled) { send_lock(CAN_DOOR1_LOCK); } else if(unlock_enabled) { send_unlock(CAN_DOOR1_LOCK); } kk_check(SDLK_a); } else if (button == gButtonB) { if(lock_enabled) { send_lock(CAN_DOOR2_LOCK); } else if(unlock_enabled) { send_unlock(CAN_DOOR2_LOCK); } kk_check(SDLK_b); } else if (button == gButtonX) { if(lock_enabled) { send_lock(CAN_DOOR3_LOCK); } else if(unlock_enabled) { send_unlock(CAN_DOOR3_LOCK); } kk_check(SDLK_x); } else if (button == gButtonY) { if(lock_enabled) { send_lock(CAN_DOOR4_LOCK); } else if(unlock_enabled) { send_unlock(CAN_DOOR4_LOCK); } kk_check(SDLK_y); } else if (button == gButtonStart) { kk_check(SDLK_RETURN); } else { if(debug) printf("Unassigned button: %d\n", event.jbutton.button); } break; case SDL_JOYBUTTONUP: button = event.jbutton.button; if(button == gButtonLock) { lock_enabled = 0; } else if(button == gButtonUnlock) { unlock_enabled = 0; } else { //if(debug) printf("Unassigned button: %d\n", event.jbutton.button); } break; case SDL_JOYDEVICEADDED: // Only use the first controller if(event.cdevice.which == 0) { gJoystick = SDL_JoystickOpen(0); if(gJoystick) { gHaptic = SDL_HapticOpenFromJoystick(gJoystick); print_joy_info(); } } break; case SDL_JOYDEVICEREMOVED: if(event.cdevice.which == 0) { SDL_JoystickClose(gJoystick); gJoystick = NULL; } break; case SDL_CONTROLLERDEVICEADDED: // Only use the first controller if(gGameController == NULL) { gGameController = SDL_GameControllerOpen(0); gJoystick = SDL_GameControllerGetJoystick(gGameController); gHaptic = SDL_HapticOpenFromJoystick(gJoystick); print_joy_info(); } break; case SDL_CONTROLLERDEVICEREMOVED: if(event.cdevice.which == 0) { SDL_GameControllerClose(gGameController); gGameController = NULL; } break; } } currentTime = SDL_GetTicks(); checkAccel(); checkTurn(); SDL_Delay(5); } kill_child(SIGKILL); close(s); SDL_DestroyTexture(base_texture); SDL_FreeSurface(image); SDL_GameControllerClose(gGameController); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); }
int main () { int i, j; int result; //return value for function calls int failed, changed; //flags for tests int cardsBefore[treasure_map]; //cards in hand before shuffle int cardsAfter[treasure_map]; //cards in hand after shuffle struct gameState *g = malloc(sizeof(struct gameState)); //working game state struct gameState *pre = malloc(sizeof(struct gameState)); //reference game state; stored before functions run int selectedCards[10] = {adventurer, council_room, feast, gardens, mine, smithy, village, tribute, salvager, sea_hag}; int seed = 10; //---Test deckCount = 0 //---Expected result: error printf("*** Testing deckCount = 0 ***\n"); printf("Errors: "); //Create clean game if (initializeGame(NUM_PLAYERS, selectedCards, seed, g) == -1){ printf("\nCould not initialize game. Testing aborted."); return -1; } //Draw all cards in deck while (g->deckCount[whoseTurn(g)] > 0){ drawCard(whoseTurn(g), g); } //Save current state memcpy(pre, g, sizeof(struct gameState)); //Attempt to shuffle result = shuffle(whoseTurn(g), g); failed = 0; if (result != -1){ printf("\nReturn value: %d, Expected: %d", result, -1); failed = 1; } //Check game state is unchanged if (checkGameState(pre, g) < 0){ printf("\ngameState changed"); failed = 1; } //Final check if (failed){ printf("\nResult: FAIL\n\n"); } else { printf("none"); printf("\nResult: PASS\n\n"); } //---Test deckCount = 1 //---Expected result: gameState is unchanged printf("*** Testing deckCount = 1 ***\n"); printf("Errors: "); //Create clean game if (initializeGame(NUM_PLAYERS, selectedCards, seed, g) == -1){ printf("\nCould not initialize game. Testing aborted."); return -1; } //Draw all cards but 1 in deck while (g->deckCount[whoseTurn(g)] > 1){ drawCard(whoseTurn(g), g); } //Save current state memcpy(pre, g, sizeof(struct gameState)); //Run shuffle and check results failed = 0; result = shuffle(whoseTurn(g), g); if (result == -1){ printf("\nReturn value: %d, Expected: %d", result, 0); failed = 1; } //Check game state is unchanged if (checkGameState(pre, g) < 0){ printf("\ngameState changed"); failed = 1; } //Final check if (failed){ printf("\nResult: FAIL\n\n"); } else { printf("none"); printf("\nResult: PASS\n\n"); } //---Test deckCount > 1 /*---Expected results: - Game and turn settings are unchanged - Other player's cards (hand, deck, discard) are unchanged - Current player's hand and discard are unchanged - Played cards are unchanged - Current player's deck Count is unchanged - Current player's deck contains same cards as before shuffle - Current player's deck cards are in different order than before shuffle */ printf("*** Testing deckCount > 1 ***\n"); printf("Errors: "); //Create clean game if (initializeGame(NUM_PLAYERS, selectedCards, seed, g) == -1){ printf("\nCould not initialize game. Testing aborted."); return -1; } //Confirm deckCount > 1 if (g->deckCount[whoseTurn(g)] <= 1){ printf("\nCould not create suitable game. Testing aborted."); return -1; } //Save current state memcpy(pre, g, sizeof(struct gameState)); //Initialize card count arrays for (i = 0; i < treasure_map; i++){ cardsAfter[i] = 0; cardsBefore[i] = 0; } //Get before deck card counts for (i = 0; i < g->deckCount[(whoseTurn(g))]; i++){ cardsBefore[g->deck[whoseTurn(g)][i]]++; } //Run shuffle and check results failed = 0; result = shuffle(whoseTurn(g), g); if (result == -1){ printf("\nReturn value: %d, Expected: %d", result, 0); failed = 1; } //Check game/turn settings if (checkNumPlayers(pre, g) < 0){ printf("\nNumPlayers: %d, Expected: %d", g->numPlayers, pre->numPlayers); failed = 1; } result = checkSupply(pre, g); if (result != 0){ printf("\nSupply count for card %d: %d, Expected: %d", result, g->supplyCount[result], pre->supplyCount[result]); failed = 1; } result = checkEmbargo(pre, g); if (result != 0){ printf("\nEmbargo for card %d: %d, Expected: %d", result, g->embargoTokens[result], pre->embargoTokens[result]); failed = 1; } if (checkOutpost(pre, g) < 0){ printf("\noutpostPlayed: %d, Expected: %d", g->outpostPlayed, pre->outpostPlayed); printf("\noutpostTurn: %d, Expected: %d", g->outpostTurn, pre->outpostTurn); failed = 1; } if (checkTurn(pre, g) < 0){ printf("\nwhoseTurn: %d, Expected: %d", g->whoseTurn, pre->whoseTurn); failed = 1; } if (checkPhase(pre, g) < 0){ printf("\nphase: %d, Expected: %d", g->phase, pre->phase); failed = 1; } if (checkNumActions(pre, g) < 0){ printf("\nnumActions: %d, Expected: %d", g->numActions, pre->numActions); failed = 1; } if (checkCoins(pre, g) < 0){ printf("\ncoins: %d, Expected: %d", g->coins, pre->coins); failed = 1; } if (checkNumBuys(pre, g) < 0){ printf("\nnumBuys: %d, Expected: %d", g->numBuys, pre->numBuys); failed = 1; } //Check all hand, discard, play cards unchanged if (checkHands(pre, g) < 0){ printf("\nHands changed after shuffle\n"); //Print expected and actual hand for each player for (i = 0; i < NUM_PLAYERS; i++){ printf(" Hand for player %d:\n", i); printf(" Count: %d, Expected: %d\n", g->handCount[i], pre->handCount[i]); if (g->handCount[i] > 0){ printf(" Actual Cards: "); for (j = 0; j < g->handCount[i]; j++){ printf("%d ", g->hand[i][j]); } printf("\n Expected Cards: "); for (j = 0; j < pre->handCount[i]; j++){ printf("%d ", pre->hand[i][j]); } } } failed = 1; } if (checkDiscards(pre, g) < 0){ printf("\nDiscards changed after shuffle\n"); //Print expected and actual discards for each player for (i = 0; i < NUM_PLAYERS; i++){ printf(" Discard pile for player %d:\n", i); printf(" Count: %d, Expected: %d\n", g->discardCount[i], pre->discardCount[i]); if (g->discardCount[i] > 0){ printf(" Actual Cards: "); for (j = 0; j < g->discardCount[i]; j++){ printf("%d ", g->discard[i][j]); } printf("\n Expected Cards: "); for (j = 0; j < pre->discardCount[i]; j++){ printf("%d ", pre->discard[i][j]); } } } failed = 1; } if (checkPlayed(pre, g) < 0){ printf("\nPlayed cards changed after shuffle\n"); //Print expected and actual discards for each player printf(" Played count: %d, Expected: %d\n", g->playedCardCount, pre->playedCardCount); if (g->playedCardCount > 0){ printf(" Actual Cards: "); for (i = 0; i < g->playedCardCount; i++){ printf("%d ", g->playedCards[i]); } printf("\n Expected Cards: "); for (i = 0; i < pre->playedCardCount; i++){ printf("%d ", pre->playedCards[i]); } } failed = 1; } //Check deck unchanged for other players for (i = 0; i < g->numPlayers; i++){ //skip if current player if (i == whoseTurn(g)){ continue; } if (pre->deckCount[i] != g->deckCount[i]){ printf("\nPlayer %d's deckCount: %d, Expected: %d", i, g->deckCount[i], pre->deckCount[i]); failed = 1; } else { //check each card for (j = 0; j < g->deckCount[i]; j++){ if (pre->deck[i][j] != g->deck[i][j]){ printf("\nPlayer %d's deck[%d]: %d, Expected: %d", i, j, g->deck[i][j], pre->deck[i][j]); failed = 1; } } } } //Check current player's deck is same size if (pre->deckCount[whoseTurn(g)] != g->deckCount[whoseTurn(g)]){ printf("\nCurrent player's deckCount: %d, Expected: %d", g->deckCount[whoseTurn(g)], pre->deckCount[whoseTurn(g)]); failed = 1; } //Check current player's deck contains same cards as before for (i = 0; i < g->deckCount[(whoseTurn(g))]; i++){ cardsAfter[g->deck[whoseTurn(g)][i]]++; } for (i = 0; i < treasure_map; i++){ if (cardsBefore[i] != cardsAfter[i]){ printf("\nCount for card %d in deck: %d, Expected: %d", i, cardsAfter[i], cardsBefore[i]); failed = 1; break; } } //Check current player's deck has cards in different order than before if (pre->deckCount[whoseTurn(g)] == g->deckCount[whoseTurn(g)]){ changed = 0; for (i = 0; i < g->deckCount[whoseTurn(g)]; i++){ if (pre->deck[whoseTurn(g)][i] != g->deck[whoseTurn(g)][i]){ changed = 1; break; } } if (!changed){ printf("\nCards in current player's deck not shuffled"); failed = 1; } } //Final check if (failed){ printf("\nResult: FAIL\n"); } else { printf("none"); printf("\nResult: PASS\n"); } return 0; }
int main () { int i, j; //loop controls int result; //return value for function calls int failed; //flags for tests struct gameState *g = malloc(sizeof(struct gameState)); //working game state struct gameState *pre = malloc(sizeof(struct gameState)); //reference game state; stored before functions run int selectedCards[10] = {adventurer, council_room, feast, gardens, mine, smithy, village, tribute, salvager, sea_hag}; int seed = 10; //---Test numBuys = 0 //---Expected result: error printf("*** Testing numBuys = 0 ***\n"); printf("Errors: "); //Create clean game if (initializeGame(NUM_PLAYERS, selectedCards, seed, g) == -1){ printf("\nCould not initialize game. Testing aborted."); return -1; } //Set numBuys to 0 g->numBuys = 0; //Save current state memcpy(pre, g, sizeof(struct gameState)); //Attempt to buy card result = buyCard(TEST_CARD, g); failed = 0; if (result != -1){ printf("\nReturn value: %d, Expected: %d", result, -1); failed = 1; } //Check game state is unchanged if (checkGameState(pre, g) < 0){ printf("\ngameState changed"); failed = 1; } //Final check if (failed){ printf("\nResult: FAIL\n\n"); } else { printf("none"); printf("\nResult: PASS\n\n"); } //---Test no supply remaining //---Expected result: error printf("*** Testing supplyCount = 0 ***\n"); printf("Errors: "); //Create clean game if (initializeGame(NUM_PLAYERS, selectedCards, seed, g) == -1){ printf("\nCould not initialize game. Testing aborted."); return -1; } //Set supplyCount for test card to 0 g->supplyCount[TEST_CARD] = 0; //Save current state memcpy(pre, g, sizeof(struct gameState)); //Attempt to buy card result = buyCard(TEST_CARD, g); failed = 0; if (result != -1){ printf("\nReturn value: %d, Expected: %d", result, -1); failed = 1; } //Check game state is unchanged if (checkGameState(pre, g) < 0){ printf("\ngameState changed"); failed = 1; } //Final check if (failed){ printf("\nResult: FAIL\n\n"); } else { printf("none"); printf("\nResult: PASS\n\n"); } //---Test insufficient funds //---Expected result: error printf("*** Testing coins < cost ***\n"); printf("Errors: "); //Create clean game if (initializeGame(NUM_PLAYERS, selectedCards, seed, g) == -1){ printf("\nCould not initialize game. Testing aborted."); return -1; } //Set coins to cost of card - 1 g->coins = getCost(TEST_CARD) - 1; //Save current state memcpy(pre, g, sizeof(struct gameState)); //Attempt to buy card result = buyCard(TEST_CARD, g); failed = 0; if (result != -1){ printf("\nReturn value: %d, Expected: %d", result, -1); failed = 1; } //Check game state is unchanged if (checkGameState(pre, g) < 0){ printf("\ngameState changed"); failed = 1; } //Final check if (failed){ printf("\nResult: FAIL\n\n"); } else { printf("none"); printf("\nResult: PASS\n\n"); } //---Test invalid supply position //---Expected result: error printf("*** Testing invalid position ***\n"); printf("Errors: "); //Create clean game if (initializeGame(NUM_PLAYERS, selectedCards, seed, g) == -1){ printf("\nCould not initialize game. Testing aborted."); return -1; } //Save current state memcpy(pre, g, sizeof(struct gameState)); //Attempt to buy card at invalid position result = buyCard(treasure_map+1, g); failed = 0; if (result != -1){ printf("\nReturn value: %d, Expected: %d", result, -1); failed = 1; } //Check game state is unchanged if (checkGameState(pre, g) < 0){ printf("\ngameState changed"); failed = 1; } //Final check if (failed){ printf("\nResult: FAIL\n\n"); } else { printf("none"); printf("\nResult: PASS\n\n"); } //---Test valid buy /*---Expected results: - Game and turn settings are unchanged except as noted below - Phase set to 1 - numBuys decreased by 1 - coins decreased by card cost - Supply count for other cards are unchanged - Supply count for selected card is decreased by 1 - Other player's cards (hand, deck, discard) are unchanged - Played cards are unchanged - Current player's hand and deck are unchanged - Current player's discardCount is 1 more than before buyCard - Current player's discard is the same except additional card in last position */ printf("*** Testing valid buy ***\n"); printf("Errors: "); //Create clean game if (initializeGame(NUM_PLAYERS, selectedCards, seed, g) == -1){ printf("\nCould not initialize game. Testing aborted."); return -1; } //Add sufficient coins and numBuys g->coins = getCost(TEST_CARD); g->numBuys = 1; //Save current state memcpy(pre, g, sizeof(struct gameState)); //Run buyCard and check results failed = 0; result = buyCard(TEST_CARD, g); if (result == -1){ printf("\nReturn value: %d, Expected: %d", result, 0); failed = 1; } //Check game/turn settings if (checkNumPlayers(pre, g) < 0){ printf("\nNumPlayers: %d, Expected: %d", g->numPlayers, pre->numPlayers); failed = 1; } result = checkEmbargo(pre, g); if (result != 0){ printf("\nEmbargo for card %d: %d, Expected: %d", result, g->embargoTokens[result], pre->embargoTokens[result]); failed = 1; } if (checkOutpost(pre, g) < 0){ printf("\noutpostPlayed: %d, Expected: %d", g->outpostPlayed, pre->outpostPlayed); printf("\noutpostTurn: %d, Expected: %d", g->outpostTurn, pre->outpostTurn); failed = 1; } if (checkTurn(pre, g) < 0){ printf("\nwhoseTurn: %d, Expected: %d", g->whoseTurn, pre->whoseTurn); failed = 1; } if (checkNumActions(pre, g) < 0){ printf("\nnumActions: %d, Expected: %d", g->numActions, pre->numActions); failed = 1; } //Check phase = 1 if (g->phase != 1){ printf("\nphase: %d, Expected: %d", g->phase, 1); failed = 1; } //Check coins = coins_before - cost of card if (g->coins != pre->coins - getCost(TEST_CARD)){ printf("\ncoins: %d, Expected: %d", g->coins, pre->coins - getCost(TEST_CARD)); failed = 1; } //Check numBuys decreased by 1 if (g->numBuys != pre->numBuys - 1){ printf("\nnumBuys: %d, Expected: %d", g->numBuys, pre->numBuys - 1); failed = 1; } //Check supply for bought card is decreased by 1 if (g->supplyCount[TEST_CARD] != pre->supplyCount[TEST_CARD]-1){ printf("\nSupply count for card %d: %d, Expected: %d", TEST_CARD, g->supplyCount[TEST_CARD], pre->supplyCount[TEST_CARD]-1); failed = 1; } //Check supply for other cards remains the same for (i = 0; i <= treasure_map; i++){ //if test card, skip if (i == TEST_CARD){ continue; } if (pre->supplyCount[i] != g->supplyCount[i]){ printf("\nSupply count for card %d: %d, Expected: %d", i, g->supplyCount[i], pre->supplyCount[i]); failed = 1; } } //Check all hand, deck and played cards unchanged if (checkHands(pre, g) < 0){ printf("\nHands changed after buyCard\n"); //Print expected and actual hand for each player for (i = 0; i < NUM_PLAYERS; i++){ printf(" Hand for player %d:\n", i); printf(" Count: %d, Expected: %d\n", g->handCount[i], pre->handCount[i]); if (g->handCount[i] > 0){ printf(" Actual Cards: "); for (j = 0; j < g->handCount[i]; j++){ printf("%d ", g->hand[i][j]); } printf("\n Expected Cards: "); for (j = 0; j < pre->handCount[i]; j++){ printf("%d ", pre->hand[i][j]); } } } failed = 1; } if (checkDecks(pre, g) < 0){ printf("\nDecks changed after buyCard\n"); //Print expected and actual deck for each player for (i = 0; i < NUM_PLAYERS; i++){ printf(" Deck for player %d:\n", i); printf(" Count: %d, Expected: %d\n", g->deckCount[i], pre->deckCount[i]); if (g->deckCount[i] > 0){ printf(" Actual Cards: "); for (j = 0; j < g->deckCount[i]; j++){ printf("%d ", g->deck[i][j]); } printf("\n Expected Cards: "); for (j = 0; j < pre->deckCount[i]; j++){ printf("%d ", pre->deck[i][j]); } } } failed = 1; } if (checkPlayed(pre, g) < 0){ printf("\nPlayed cards changed after buyCard\n"); //Print expected and actual played cards printf(" Played count: %d, Expected: %d\n", g->playedCardCount, pre->playedCardCount); if (g->playedCardCount > 0){ printf(" Actual Cards: "); for (i = 0; i < g->playedCardCount; i++){ printf("%d ", g->playedCards[i]); } printf("\n Expected Cards: "); for (i = 0; i < pre->playedCardCount; i++){ printf("%d ", pre->playedCards[i]); } } failed = 1; } //Check discard unchanged for other players for (i = 0; i < g->numPlayers; i++){ //skip if current player if (i == whoseTurn(g)){ continue; } if (pre->discardCount[i] != g->discardCount[i]){ printf("\nPlayer %d's discardCount: %d, Expected: %d", i, g->discardCount[i], pre->discardCount[i]); failed = 1; } else { //check each card for (j = 0; j < g->discardCount[i]; j++){ if (pre->discard[i][j] != g->discard[i][j]){ printf("\nPlayer %d's discard[%d]: %d, Expected: %d", i, j, g->discard[i][j], pre->discard[i][j]); failed = 1; } } } } //Check current player's discardCount increased by 1 if (g->discardCount[whoseTurn(g)] != pre->discardCount[whoseTurn(g)]+1){ printf("\nCurrent player's discardCount: %d, Expected: %d", g->discardCount[whoseTurn(g)], pre->discardCount[whoseTurn(g)]+1); failed = 1; } //Check current player's discard contains same cards plus new card in last position for (i = 0; i < g->discardCount[whoseTurn(g)]-1; i++){ if (pre->discard[whoseTurn(g)][i] != g->discard[whoseTurn(g)][i]){ printf("\nCurrent player's discard[%d]: %d, Expected: %d", i, g->discard[whoseTurn(g)][i], pre->discard[whoseTurn(g)][i]); failed = 1; } } if (g->discard[whoseTurn(g)][g->discardCount[whoseTurn(g)]-1] != TEST_CARD){ printf("\nGained card: %d, Expected: %d", g->discard[whoseTurn(g)][g->discardCount[whoseTurn(g)]-1], TEST_CARD); failed = 1; } //Final check if (failed){ printf("\nResult: FAIL\n\n"); } else { printf("none"); printf("\nResult: PASS\n\n"); } //---Test buy with embargo /*---Expected results: - Game and turn settings are unchanged except as noted below - Phase set to 1 - numBuys decreased by 1 - coins decreased by card cost - Supply count for other cards are unchanged - Supply count for selected card is decreased by 1 - Supply count for curse card decreased by 1 - Embargo tokens for other cards are unchanged - Embargo tokens for selected card is decreased by 1 - Other player's cards (hand, deck, discard) are unchanged - Played cards are unchanged - Current player's hand and deck are unchanged - Current player's discardCount is 2 more than before buyCard - Current player's discard is the same except additional card and curse card */ printf("*** Testing valid buy with embargo ***\n"); printf("Errors: "); //Create clean game if (initializeGame(NUM_PLAYERS, selectedCards, seed, g) == -1){ printf("\nCould not initialize game. Testing aborted."); return -1; } //Add sufficient coins and numBuys g->coins = getCost(TEST_CARD); g->numBuys = 1; //Add embargo to test card g->embargoTokens[TEST_CARD] = 1; //Save current state memcpy(pre, g, sizeof(struct gameState)); //Run buyCard and check results failed = 0; result = buyCard(TEST_CARD, g); if (result == -1){ printf("\nReturn value: %d, Expected: %d", result, 0); failed = 1; } //Check game/turn settings if (checkNumPlayers(pre, g) < 0){ printf("\nNumPlayers: %d, Expected: %d", g->numPlayers, pre->numPlayers); failed = 1; } if (checkOutpost(pre, g) < 0){ printf("\noutpostPlayed: %d, Expected: %d", g->outpostPlayed, pre->outpostPlayed); printf("\noutpostTurn: %d, Expected: %d", g->outpostTurn, pre->outpostTurn); failed = 1; } if (checkTurn(pre, g) < 0){ printf("\nwhoseTurn: %d, Expected: %d", g->whoseTurn, pre->whoseTurn); failed = 1; } if (checkNumActions(pre, g) < 0){ printf("\nnumActions: %d, Expected: %d", g->numActions, pre->numActions); failed = 1; } //Check phase = 1 if (g->phase != 1){ printf("\nphase: %d, Expected: %d", g->phase, 1); failed = 1; } //Check coins = coins_before - cost of card if (g->coins != pre->coins - getCost(TEST_CARD)){ printf("\ncoins: %d, Expected: %d", g->coins, pre->coins - getCost(TEST_CARD)); failed = 1; } //Check numBuys decreased by 1 if (g->numBuys != pre->numBuys - 1){ printf("\nnumBuys: %d, Expected: %d", g->numBuys, pre->numBuys - 1); failed = 1; } //Check supply for bought card is decreased by 1 if (g->supplyCount[TEST_CARD] != pre->supplyCount[TEST_CARD]-1){ printf("\nSupply count for card %d: %d, Expected: %d", TEST_CARD, g->supplyCount[TEST_CARD], pre->supplyCount[TEST_CARD]-1); failed = 1; } //Check supply for curse card is decreased by 1 if (g->supplyCount[0] != pre->supplyCount[0]-1){ printf("\nSupply count for curse card: %d, Expected: %d", g->supplyCount[0], pre->supplyCount[0]-1); failed = 1; } //Check supply for other cards remains the same for (i = 1; i <= treasure_map; i++){ //if test card, skip if (i == TEST_CARD){ continue; } if (pre->supplyCount[i] != g->supplyCount[i]){ printf("\nSupply count for card %d: %d, Expected: %d", i, g->supplyCount[i], pre->supplyCount[i]); failed = 1; } } //Check embargo tokens for bought card is decreased by 1 if (g->embargoTokens[TEST_CARD] != pre->embargoTokens[TEST_CARD]-1){ printf("\nEmbargo tokens for card %d: %d, Expected: %d", TEST_CARD, g->embargoTokens[TEST_CARD], pre->embargoTokens[TEST_CARD]-1); failed = 1; } //Check embargo tokens for other cards remains the same for (i = 0; i <= treasure_map; i++){ //if test card, skip if (i == TEST_CARD){ continue; } if (pre->embargoTokens[i] != g->embargoTokens[i]){ printf("\nEmbargo tokens for card %d: %d, Expected: %d", i, g->embargoTokens[i], pre->embargoTokens[i]); failed = 1; } } //Check all hand, deck and played cards unchanged if (checkHands(pre, g) < 0){ printf("\nHands changed after buyCard\n"); //Print expected and actual hand for each player for (i = 0; i < NUM_PLAYERS; i++){ printf(" Hand for player %d:\n", i); printf(" Count: %d, Expected: %d\n", g->handCount[i], pre->handCount[i]); if (g->handCount[i] > 0){ printf(" Actual Cards: "); for (j = 0; j < g->handCount[i]; j++){ printf("%d ", g->hand[i][j]); } printf("\n Expected Cards: "); for (j = 0; j < pre->handCount[i]; j++){ printf("%d ", pre->hand[i][j]); } } } failed = 1; } if (checkDecks(pre, g) < 0){ printf("\nDecks changed after buyCard\n"); //Print expected and actual deck for each player for (i = 0; i < NUM_PLAYERS; i++){ printf(" Deck for player %d:\n", i); printf(" Count: %d, Expected: %d\n", g->deckCount[i], pre->deckCount[i]); if (g->deckCount[i] > 0){ printf(" Actual Cards: "); for (j = 0; j < g->deckCount[i]; j++){ printf("%d ", g->deck[i][j]); } printf("\n Expected Cards: "); for (j = 0; j < pre->deckCount[i]; j++){ printf("%d ", pre->deck[i][j]); } } } failed = 1; } if (checkPlayed(pre, g) < 0){ printf("\nPlayed cards changed after buyCard\n"); //Print expected and actual played cards printf(" Played count: %d, Expected: %d\n", g->playedCardCount, pre->playedCardCount); if (g->playedCardCount > 0){ printf(" Actual Cards: "); for (i = 0; i < g->playedCardCount; i++){ printf("%d ", g->playedCards[i]); } printf("\n Expected Cards: "); for (i = 0; i < pre->playedCardCount; i++){ printf("%d ", pre->playedCards[i]); } } failed = 1; } //Check discard unchanged for other players for (i = 0; i < g->numPlayers; i++){ //skip if current player if (i == whoseTurn(g)){ continue; } if (pre->discardCount[i] != g->discardCount[i]){ printf("\nPlayer %d's discardCount: %d, Expected: %d", i, g->discardCount[i], pre->discardCount[i]); failed = 1; } else { //check each card for (j = 0; j < g->discardCount[i]; j++){ if (pre->discard[i][j] != g->discard[i][j]){ printf("\nPlayer %d's discard[%d]: %d, Expected: %d", i, j, g->discard[i][j], pre->discard[i][j]); failed = 1; } } } } //Check current player's discardCount increased by 2 if (g->discardCount[whoseTurn(g)] != pre->discardCount[whoseTurn(g)]+2){ printf("\nCurrent player's discardCount: %d, Expected: %d", g->discardCount[whoseTurn(g)], pre->discardCount[whoseTurn(g)]+2); failed = 1; } //Check current player's discard contains same cards plus new card and curse in last two positions for (i = 0; i < g->discardCount[whoseTurn(g)]-2; i++){ if (pre->discard[whoseTurn(g)][i] != g->discard[whoseTurn(g)][i]){ printf("\nCurrent player's discard[%d]: %d, Expected: %d", i, g->discard[whoseTurn(g)][i], pre->discard[whoseTurn(g)][i]); failed = 1; } } if (g->discard[whoseTurn(g)][g->discardCount[whoseTurn(g)]-1] == TEST_CARD){ if (g->discard[whoseTurn(g)][g->discardCount[whoseTurn(g)]-2] != 0){ printf("\nGained card: %d, Expected: %d", g->discard[whoseTurn(g)][g->discardCount[whoseTurn(g)]-1], 0); failed = 1; } } else if (g->discard[whoseTurn(g)][g->discardCount[whoseTurn(g)]-1] == 0){ if (g->discard[whoseTurn(g)][g->discardCount[whoseTurn(g)]-2] != TEST_CARD){ printf("\nGained card: %d, Expected: %d", g->discard[whoseTurn(g)][g->discardCount[whoseTurn(g)]-1], TEST_CARD); failed = 1; } } else { printf("\nGained card: %d, Expected: %d or %d", g->discard[whoseTurn(g)][g->discardCount[whoseTurn(g)]-1], TEST_CARD, 0); failed = 1; } //Final check if (failed){ printf("\nResult: FAIL\n"); } else { printf("none"); printf("\nResult: PASS\n"); } return 0; }
int main () { int i, j; //loop controls int result; //return value for function calls int failed; //flags for tests struct gameState *g = malloc(sizeof(struct gameState)); //working game state struct gameState *pre = malloc(sizeof(struct gameState)); //reference game state; stored before functions run int selectedCards[10] = {adventurer, council_room, feast, gardens, mine, smithy, village, tribute, salvager, sea_hag}; int seed = 10; int choice1, choice2, choice3; //choices for card effect int coin_bonus; //coins added by card //---Test smithy (all choices = -1, no bonus) /*---Expected result: - Game and turn settings are unchanged - Other player's cards (hand, deck, discard) are unchanged - Current player's discard is unchanged - Played card count increased by 1 - Smithy card added to played pile (in last position) - Smithy card removed from hand - Last 3 cards of current player's deck moved to hand - Current player's deckCount is decreased by 3 - Current player's handCount is increased by 2 (+3 new cards, -1 smithy) - No bonus coins */ printf("*** Testing valid smithy ***\n"); printf("Errors: "); //Setup card options choice1 = -1; choice2 = -1; choice3 = -1; coin_bonus = 0; //Create clean game if (initializeGame(NUM_PLAYERS, selectedCards, seed, g) == -1){ printf("\nCould not initialize game. Testing aborted.\n"); return -1; } //Set deck to 5 copper cards g->deckCount[whoseTurn(g)] = 5; for (i = 0; i < 5; i++){ g->deck[whoseTurn(g)][i] = copper; } //Set test position in hand to test card g->hand[whoseTurn(g)][TEST_POS] = TEST_CARD; //Save current state memcpy(pre, g, sizeof(struct gameState)); //Attempt to execute card result = cardEffect(TEST_CARD, choice1, choice2, choice3, g, TEST_POS, &coin_bonus); failed = 0; if (result != 0){ printf("\nReturn value: %d, Expected: %d\n", result, 0); failed = 1; } //Check game/turn settings if (checkNumPlayers(pre, g) < 0){ printf("\nNumPlayers: %d, Expected: %d\n", g->numPlayers, pre->numPlayers); failed = 1; } result = checkSupply(pre, g); if (result != 0){ printf("\nSupply count for card %d: %d, Expected: %d\n", result, g->supplyCount[result], pre->supplyCount[result]); failed = 1; } result = checkEmbargo(pre, g); if (result != 0){ printf("\nEmbargo for card %d: %d, Expected: %d\n", result, g->embargoTokens[result], pre->embargoTokens[result]); failed = 1; } if (checkOutpost(pre, g) < 0){ printf("\noutpostPlayed: %d, Expected: %d\n", g->outpostPlayed, pre->outpostPlayed); printf("\noutpostTurn: %d, Expected: %d\n", g->outpostTurn, pre->outpostTurn); failed = 1; } if (checkTurn(pre, g) < 0){ printf("\nwhoseTurn: %d, Expected: %d\n", g->whoseTurn, pre->whoseTurn); failed = 1; } if (checkPhase(pre, g) < 0){ printf("\nphase: %d, Expected: %d\n", g->phase, pre->phase); failed = 1; } if (checkNumActions(pre, g) < 0){ printf("\nnumActions: %d, Expected: %d\n", g->numActions, pre->numActions); failed = 1; } if (checkCoins(pre, g) < 0){ printf("\ncoins: %d, Expected: %d\n", g->coins, pre->coins); failed = 1; } if (checkNumBuys(pre, g) < 0){ printf("\nnumBuys: %d, Expected: %d\n", g->numBuys, pre->numBuys); failed = 1; } //Check all player's discard unchanged if (checkDiscards(pre, g) < 0){ printf("\nDiscards changed after smithy\n"); //Print expected and actual discards for each player for (i = 0; i < NUM_PLAYERS; i++){ printf(" Discard pile for player %d:\n", i); printf(" Count: %d, Expected: %d\n", g->discardCount[i], pre->discardCount[i]); if (g->discardCount[i] > 0){ printf(" Actual Cards: "); for (j = 0; j < g->discardCount[i]; j++){ printf("%d ", g->discard[i][j]); } printf("\n Expected Cards: "); for (j = 0; j < pre->discardCount[i]; j++){ printf("%d ", pre->discard[i][j]); } } } failed = 1; } //Check deck unchanged for other players for (i = 0; i < g->numPlayers; i++){ //skip if current player if (i == whoseTurn(g)){ continue; } if (pre->deckCount[i] != g->deckCount[i]){ printf("\nPlayer %d's deckCount: %d, Expected: %d", i, g->deckCount[i], pre->deckCount[i]); failed = 1; } else { //check each card for (j = 0; j < g->deckCount[i]; j++){ if (pre->deck[i][j] != g->deck[i][j]){ printf("\nPlayer %d's deck[%d]: %d, Expected: %d", i, j, g->deck[i][j], pre->deck[i][j]); failed = 1; } } } } //Check hand unchanged for other players for (i = 0; i < g->numPlayers; i++){ //skip if current player if (i == whoseTurn(g)){ continue; } if (pre->handCount[i] != g->handCount[i]){ printf("\nPlayer %d's handCount: %d, Expected: %d", i, g->handCount[i], pre->handCount[i]); failed = 1; } else { //check each card for (j = 0; j < g->handCount[i]; j++){ if (pre->hand[i][j] != g->hand[i][j]){ printf("\nPlayer %d's hand[%d]: %d, Expected: %d", i, j, g->hand[i][j], pre->hand[i][j]); failed = 1; } } } } //Check smithy added to played pile if (g->playedCardCount != pre->playedCardCount + 1){ printf("\nPlayed card count: %d, Expected: %d", g->playedCardCount, pre->playedCardCount + 1); failed = 1; } else if (g->playedCards[g->playedCardCount - 1] != TEST_CARD){ printf("\nLast played card: %d, Expected: %d", g->playedCards[g->playedCardCount - 1], TEST_CARD); failed = 1; } //Check 3 cards added to hand and smithy removed if (g->handCount[whoseTurn(g)] != pre->handCount[whoseTurn(g)] + 2){ printf("\nCurrent player's hand count: %d, Expected: %d", g->handCount[whoseTurn(g)], pre->handCount[whoseTurn(g)] + 2); failed = 1; } //Check 3 cards removed from deck if (g->deckCount[whoseTurn(g)] != pre->deckCount[whoseTurn(g)] - 3){ printf("\nCurrent player's deck count: %d, Expected: %d", g->deckCount[whoseTurn(g)], pre->deckCount[whoseTurn(g)] - 3); failed = 1; } //Check cards removed from deck = cards added to hand (last 2 cards, plus position 0...last card replaces test card when discarded) if (g->hand[whoseTurn(g)][g->handCount[whoseTurn(g)] - 1] != pre->deck[whoseTurn(g)][pre->deckCount[whoseTurn(g)] - 1]){ printf("\nCurrent player's hand[%d]: %d, Expected: %d", g->handCount[whoseTurn(g)] - 1, g->hand[whoseTurn(g)][g->handCount[whoseTurn(g)] - 1], pre->deck[whoseTurn(g)][pre->deckCount[whoseTurn(g)] - 1]); failed = 1; } if (g->hand[whoseTurn(g)][g->handCount[whoseTurn(g)] - 2] != pre->deck[whoseTurn(g)][pre->deckCount[whoseTurn(g)] - 2]){ printf("\nCurrent player's hand[%d]: %d, Expected: %d", g->handCount[whoseTurn(g)] - 2, g->hand[whoseTurn(g)][g->handCount[whoseTurn(g)] - 2], pre->deck[whoseTurn(g)][pre->deckCount[whoseTurn(g)] - 2]); failed = 1; } if (g->hand[whoseTurn(g)][0] != pre->deck[whoseTurn(g)][pre->deckCount[whoseTurn(g)] - 3]){ printf("\nCurrent player's hand[0]: %d, Expected: %d", g->hand[whoseTurn(g)][0], pre->deck[whoseTurn(g)][pre->deckCount[whoseTurn(g)] - 3]); failed = 1; } //Check no bonus coins added if (coin_bonus != 0){ printf("\nCoin bonus after smithy: %d, Expected: %d", coin_bonus, 0); failed = 1; } //Final check if (failed){ printf("\nResult: FAIL\n"); } else { printf("none"); printf("\nResult: PASS\n"); } return 0; }
int main () { int i, j; //loop controls int result; //return value for function calls int failed; //flags for tests struct gameState *g = malloc(sizeof(struct gameState)); //working game state struct gameState *pre = malloc(sizeof(struct gameState)); //reference game state; stored before functions run int selectedCards[10] = {adventurer, council_room, feast, gardens, mine, smithy, village, tribute, salvager, sea_hag}; int seed = 10; int choice1, choice2, choice3; //choices for card effect int coin_bonus; //coins added by card //---Test adventurer with 2 treasure cards in deck //---(all choices = -1, no bonus) /*---Expected result: - Game and turn settings are unchanged - Other player's cards (hand, deck, discard) are unchanged - Played card count increased by 1 - Adventurer card added to played pile (in last position) - Adventurer card removed from hand - Current player's deck count decreased by 2 treasures + extras - Other cards in player's deck unchanged - Current player's discard count increased by extras - Current player's hand count increased by 1 (+2 treasures, -1 adventurer) - Cards added to player's hand are treasure - Cards added to player's discard are extras - No bonus coins */ printf("*** Testing 2 treasure cards in deck ***\n"); printf("Errors: "); //Setup card options choice1 = -1; choice2 = -1; choice3 = -1; coin_bonus = 0; //Create clean game if (initializeGame(NUM_PLAYERS, selectedCards, seed, g) == -1){ printf("\nCould not initialize game. Testing aborted.\n"); return -1; } //Set deck to 5 extras - 2 treasures - 5 extras g->deckCount[whoseTurn(g)] = 0; for (i = 1; i <= EXTRA_COUNT; i++){ g->deck[whoseTurn(g)][g->deckCount[whoseTurn(g)]] = EXTRA_CARD; g->deckCount[whoseTurn(g)]++; } for (i = 1; i <= 2; i++){ g->deck[whoseTurn(g)][g->deckCount[whoseTurn(g)]] = TREASURE; g->deckCount[whoseTurn(g)]++; } for (i = 1; i <= EXTRA_COUNT; i++){ g->deck[whoseTurn(g)][g->deckCount[whoseTurn(g)]] = EXTRA_CARD; g->deckCount[whoseTurn(g)]++; } //Set test position in hand to test card g->hand[whoseTurn(g)][TEST_POS] = TEST_CARD; //Save current state memcpy(pre, g, sizeof(struct gameState)); //Attempt to execute card result = cardEffect(TEST_CARD, choice1, choice2, choice3, g, TEST_POS, &coin_bonus); failed = 0; if (result != 0){ printf("\nReturn value: %d, Expected: %d", result, 0); failed = 1; } //Check game/turn settings if (checkNumPlayers(pre, g) < 0){ printf("\nNumPlayers: %d, Expected: %d", g->numPlayers, pre->numPlayers); failed = 1; } result = checkSupply(pre, g); if (result != 0){ printf("\nSupply count for card %d: %d, Expected: %d", result, g->supplyCount[result], pre->supplyCount[result]); failed = 1; } result = checkEmbargo(pre, g); if (result != 0){ printf("\nEmbargo for card %d: %d, Expected: %d", result, g->embargoTokens[result], pre->embargoTokens[result]); failed = 1; } if (checkOutpost(pre, g) < 0){ printf("\noutpostPlayed: %d, Expected: %d", g->outpostPlayed, pre->outpostPlayed); printf("\noutpostTurn: %d, Expected: %d", g->outpostTurn, pre->outpostTurn); failed = 1; } if (checkTurn(pre, g) < 0){ printf("\nwhoseTurn: %d, Expected: %d", g->whoseTurn, pre->whoseTurn); failed = 1; } if (checkPhase(pre, g) < 0){ printf("\nphase: %d, Expected: %d", g->phase, pre->phase); failed = 1; } if (checkNumActions(pre, g) < 0){ printf("\nnumActions: %d, Expected: %d", g->numActions, pre->numActions); failed = 1; } if (checkCoins(pre, g) < 0){ printf("\ncoins: %d, Expected: %d", g->coins, pre->coins); failed = 1; } if (checkNumBuys(pre, g) < 0){ printf("\nnumBuys: %d, Expected: %d", g->numBuys, pre->numBuys); failed = 1; } //Check discard unchanged for other players for (i = 0; i < g->numPlayers; i++){ //skip if current player if (i == whoseTurn(g)){ continue; } if (pre->discardCount[i] != g->discardCount[i]){ printf("\nPlayer %d's discardCount: %d, Expected: %d", i, g->discardCount[i], pre->discardCount[i]); failed = 1; } else { //check each card for (j = 0; j < g->discardCount[i]; j++){ if (pre->discard[i][j] != g->discard[i][j]){ printf("\nPlayer %d's discard[%d]: %d, Expected: %d", i, j, g->discard[i][j], pre->discard[i][j]); failed = 1; } } } } //Check deck unchanged for other players for (i = 0; i < g->numPlayers; i++){ //skip if current player if (i == whoseTurn(g)){ continue; } if (pre->deckCount[i] != g->deckCount[i]){ printf("\nPlayer %d's deckCount: %d, Expected: %d", i, g->deckCount[i], pre->deckCount[i]); failed = 1; } else { //check each card for (j = 0; j < g->deckCount[i]; j++){ if (pre->deck[i][j] != g->deck[i][j]){ printf("\nPlayer %d's deck[%d]: %d, Expected: %d", i, j, g->deck[i][j], pre->deck[i][j]); failed = 1; } } } } //Check hand unchanged for other players for (i = 0; i < g->numPlayers; i++){ //skip if current player if (i == whoseTurn(g)){ continue; } if (pre->handCount[i] != g->handCount[i]){ printf("\nPlayer %d's handCount: %d, Expected: %d", i, g->handCount[i], pre->handCount[i]); failed = 1; } else { //check each card for (j = 0; j < g->handCount[i]; j++){ if (pre->hand[i][j] != g->hand[i][j]){ printf("\nPlayer %d's hand[%d]: %d, Expected: %d", i, j, g->hand[i][j], pre->hand[i][j]); failed = 1; } } } } //Check adventurer added to played pile if (g->playedCardCount != pre->playedCardCount + 1){ printf("\nPlayed card count: %d, Expected: %d", g->playedCardCount, pre->playedCardCount + 1); failed = 1; } else if (g->playedCards[g->playedCardCount - 1] != TEST_CARD){ printf("\nLast played card: %d, Expected: %d", g->playedCards[g->playedCardCount - 1], TEST_CARD); failed = 1; } //Check 2 treasures + extras removed from deck; other cards remain if (g->deckCount[whoseTurn(g)] != pre->deckCount[whoseTurn(g)] - EXTRA_COUNT - 2){ printf("\nCurrent player's deck count: %d, Expected: %d", g->deckCount[whoseTurn(g)], pre->deckCount[whoseTurn(g)] - EXTRA_COUNT - 2); failed = 1; } for (i = 0; i < g->deckCount[whoseTurn(g)]; i++){ if (g->deck[whoseTurn(g)][i] != EXTRA_CARD){ printf("\nCurrent player's deck[%d]: %d, Expected: %d", i, g->deck[whoseTurn(g)][i], EXTRA_CARD); failed = 1; } } //Check 2 treasure cards added to hand, adventurer removed if (g->handCount[whoseTurn(g)] != pre->handCount[whoseTurn(g)] + 1){ printf("\nCurrent player's hand count: %d, Expected: %d", g->handCount[whoseTurn(g)], pre->handCount[whoseTurn(g)] + 1); failed = 1; } if ( (g->hand[whoseTurn(g)][g->handCount[whoseTurn(g)] - 1] != TREASURE) && (g->hand[whoseTurn(g)][g->handCount[whoseTurn(g)] - 2] != TREASURE) ){ printf("\nCurrent player's hand[%d]: %d, Expected: %d", g->handCount[whoseTurn(g)] - 1, g->hand[whoseTurn(g)][g->handCount[whoseTurn(g)] - 1], TREASURE); printf("\nCurrent player's hand[%d]: %d, Expected: %d", g->handCount[whoseTurn(g)] - 2, g->hand[whoseTurn(g)][g->handCount[whoseTurn(g)] - 2], TREASURE); failed = 1; } //Check extra cards added to discard if (g->discardCount[whoseTurn(g)] != pre->discardCount[whoseTurn(g)] + EXTRA_COUNT){ printf("\nCurrent player's discard count: %d, Expected: %d", g->discardCount[whoseTurn(g)], pre->discardCount[whoseTurn(g)] + EXTRA_COUNT); failed = 1; } for (i = 1; i <= EXTRA_COUNT; i++){ if (g->discard[whoseTurn(g)][g->discardCount[whoseTurn(g)] - i] != EXTRA_CARD){ printf("\nCurrent player's discard[%d]: %d, Expected: %d", g->discardCount[whoseTurn(g)] - i, g->discard[whoseTurn(g)][g->discardCount[whoseTurn(g)] - i], EXTRA_CARD); failed = 1; } } //Check no bonus coins added if (coin_bonus != 0){ printf("\nCoin bonus after adventurer: %d, Expected: %d", coin_bonus, 0); failed = 1; } //Final check if (failed){ printf("\nResult: FAIL\n\n"); } else { printf("none"); printf("\nResult: PASS\n\n"); } //---Test adventurer with 1 treasure card in deck, 1 in discard //---(all choices = -1, no bonus) /*---Expected result: - Game and turn settings are unchanged - Other player's cards (hand, deck, discard) are unchanged - Played card count increased by 1 - Adventurer card added to played pile (in last position) - Adventurer card removed from hand - Current player's deck count decreased by 1 treasure + extras - Current player's discard count increased by extras, decreased by 1 treasure - Current player's hand count increased by 1 (+2 treasures, -1 adventurer) - Cards added to player's hand are treasure - Cards added to player's discard are extras - No bonus coins ------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------- NOTICE: Test disabled because of bug in current code causing infinite loop. When deck is empty, adventurer code does not add discard pile to deck before shuffle. ------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------- */ printf("*** Testing 1 treasure in deck + 1 in discard ***\n"); //printf("Errors: "); printf("Test disabled because of infinite loop\n"); printf("Result: FAIL\n\n"); /* //Setup card options choice1 = -1; choice2 = -1; choice3 = -1; coin_bonus = 0; //Create clean game if (initializeGame(NUM_PLAYERS, selectedCards, seed, g) == -1){ printf("\nCould not initialize game. Testing aborted.\n"); return -1; } //Set deck to 1 treasure - 5 extras g->deckCount[whoseTurn(g)] = 0; g->deck[whoseTurn(g)][g->deckCount[whoseTurn(g)]] = TREASURE; g->deckCount[whoseTurn(g)]++; for (i = 1; i <= EXTRA_COUNT; i++){ g->deck[whoseTurn(g)][g->deckCount[whoseTurn(g)]] = EXTRA_CARD; g->deckCount[whoseTurn(g)]++; } //Set discard to 1 treasure g->discardCount[whoseTurn(g)] = 0; g->discard[whoseTurn(g)][g->deckCount[whoseTurn(g)]] = TREASURE; g->discardCount[whoseTurn(g)]++; //Set test position in hand to test card g->hand[whoseTurn(g)][TEST_POS] = TEST_CARD; //Save current state memcpy(pre, g, sizeof(struct gameState)); //Attempt to execute card result = cardEffect(TEST_CARD, choice1, choice2, choice3, g, TEST_POS, &coin_bonus); failed = 0; if (result != 0){ printf("\nReturn value: %d, Expected: %d", result, 0); failed = 1; } //Check game/turn settings if (checkNumPlayers(pre, g) < 0){ printf("\nNumPlayers: %d, Expected: %d", g->numPlayers, pre->numPlayers); failed = 1; } result = checkSupply(pre, g); if (result != 0){ printf("\nSupply count for card %d: %d, Expected: %d", result, g->supplyCount[result], pre->supplyCount[result]); failed = 1; } result = checkEmbargo(pre, g); if (result != 0){ printf("\nEmbargo for card %d: %d, Expected: %d", result, g->embargoTokens[result], pre->embargoTokens[result]); failed = 1; } if (checkOutpost(pre, g) < 0){ printf("\noutpostPlayed: %d, Expected: %d", g->outpostPlayed, pre->outpostPlayed); printf("\noutpostTurn: %d, Expected: %d", g->outpostTurn, pre->outpostTurn); failed = 1; } if (checkTurn(pre, g) < 0){ printf("\nwhoseTurn: %d, Expected: %d", g->whoseTurn, pre->whoseTurn); failed = 1; } if (checkPhase(pre, g) < 0){ printf("\nphase: %d, Expected: %d", g->phase, pre->phase); failed = 1; } if (checkNumActions(pre, g) < 0){ printf("\nnumActions: %d, Expected: %d", g->numActions, pre->numActions); failed = 1; } if (checkCoins(pre, g) < 0){ printf("\ncoins: %d, Expected: %d", g->coins, pre->coins); failed = 1; } if (checkNumBuys(pre, g) < 0){ printf("\nnumBuys: %d, Expected: %d", g->numBuys, pre->numBuys); failed = 1; } //Check discard unchanged for other players for (i = 0; i < g->numPlayers; i++){ //skip if current player if (i == whoseTurn(g)){ continue; } if (pre->discardCount[i] != g->discardCount[i]){ printf("\nPlayer %d's discardCount: %d, Expected: %d", i, g->discardCount[i], pre->discardCount[i]); failed = 1; } else { //check each card for (j = 0; j < g->discardCount[i]; j++){ if (pre->discard[i][j] != g->discard[i][j]){ printf("\nPlayer %d's discard[%d]: %d, Expected: %d", i, j, g->discard[i][j], pre->discard[i][j]); failed = 1; } } } } //Check deck unchanged for other players for (i = 0; i < g->numPlayers; i++){ //skip if current player if (i == whoseTurn(g)){ continue; } if (pre->deckCount[i] != g->deckCount[i]){ printf("\nPlayer %d's deckCount: %d, Expected: %d", i, g->deckCount[i], pre->deckCount[i]); failed = 1; } else { //check each card for (j = 0; j < g->deckCount[i]; j++){ if (pre->deck[i][j] != g->deck[i][j]){ printf("\nPlayer %d's deck[%d]: %d, Expected: %d", i, j, g->deck[i][j], pre->deck[i][j]); failed = 1; } } } } //Check hand unchanged for other players for (i = 0; i < g->numPlayers; i++){ //skip if current player if (i == whoseTurn(g)){ continue; } if (pre->handCount[i] != g->handCount[i]){ printf("\nPlayer %d's handCount: %d, Expected: %d", i, g->handCount[i], pre->handCount[i]); failed = 1; } else { //check each card for (j = 0; j < g->handCount[i]; j++){ if (pre->hand[i][j] != g->hand[i][j]){ printf("\nPlayer %d's hand[%d]: %d, Expected: %d", i, j, g->hand[i][j], pre->hand[i][j]); failed = 1; } } } } //Check adventurer added to played pile if (g->playedCardCount != pre->playedCardCount + 1){ printf("\nPlayed card count: %d, Expected: %d", g->playedCardCount, pre->playedCardCount + 1); failed = 1; } else if (g->playedCards[g->playedCardCount - 1] != TEST_CARD){ printf("\nLast played card: %d, Expected: %d", g->playedCards[g->playedCardCount - 1], TEST_CARD); failed = 1; } //Check 1 treasure + extras removed from deck if (g->deckCount[whoseTurn(g)] != pre->deckCount[whoseTurn(g)] - EXTRA_COUNT - 1){ printf("\nCurrent player's deck count: %d, Expected: %d", g->deckCount[whoseTurn(g)], pre->deckCount[whoseTurn(g)] - EXTRA_COUNT - 1); failed = 1; } //Check 2 treasure cards added to hand, adventurer removed if (g->handCount[whoseTurn(g)] != pre->handCount[whoseTurn(g)] + 1){ printf("\nCurrent player's hand count: %d, Expected: %d", g->handCount[whoseTurn(g)], pre->handCount[whoseTurn(g)] + 1); failed = 1; } if ( (g->hand[whoseTurn(g)][g->handCount[whoseTurn(g)] - 1] != TREASURE) && (g->hand[whoseTurn(g)][g->handCount[whoseTurn(g)] - 2] != TREASURE) ){ printf("\nCurrent player's hand[%d]: %d, Expected: %d", g->handCount[whoseTurn(g)] - 1, g->hand[whoseTurn(g)][g->handCount[whoseTurn(g)] - 1], TREASURE); printf("\nCurrent player's hand[%d]: %d, Expected: %d", g->handCount[whoseTurn(g)] - 2, g->hand[whoseTurn(g)][g->handCount[whoseTurn(g)] - 2], TREASURE); failed = 1; } //Check extra cards added to discard and treasure removed if (g->discardCount[whoseTurn(g)] != pre->discardCount[whoseTurn(g)] + EXTRA_COUNT - 1){ printf("\nCurrent player's discard count: %d, Expected: %d", g->discardCount[whoseTurn(g)], pre->discardCount[whoseTurn(g)] + EXTRA_COUNT - 1); failed = 1; } for (i = 1; i <= EXTRA_COUNT; i++){ if (g->discard[whoseTurn(g)][g->discardCount[whoseTurn(g)] - i] != EXTRA_CARD){ printf("\nCurrent player's discard[%d]: %d, Expected: %d", g->discardCount[whoseTurn(g)] - i, g->discard[whoseTurn(g)][g->discardCount[whoseTurn(g)] - i], EXTRA_CARD); failed = 1; } } //Check no bonus coins added if (coin_bonus != 0){ printf("\nCoin bonus after adventurer: %d, Expected: %d", coin_bonus, 0); failed = 1; } //Final check if (failed){ printf("\nResult: FAIL\n\n"); } else { printf("none"); printf("\nResult: PASS\n\n"); } */ //---Test adventurer with 1 treasure card in deck (only 1 treasure total) //---(all choices = -1, no bonus) /*---Expected result: - Game and turn settings are unchanged - Other player's cards (hand, deck, discard) are unchanged - Played card count increased by 1 - Adventurer card added to played pile (in last position) - Adventurer removed from hand - Current player's deck count decreased by 1 treasure + extras - Other cards in player's deck unchanged - Current player's discard count increased by extras - Current player's hand count is unchanged (+1 treasure, -1 adventurer) - Card added to player's hand is treasure - Cards added to player's discard are extras - No bonus coins ------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------- NOTICE: Test disabled because of bug in current code causing infinite loop. While loop does not have exit condition for case when only 1 treasure exists. ------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------- */ printf("*** Testing 1 treasure card in deck ***\n"); //printf("Errors: "); printf("Test disabled because of infinite loop\n"); printf("Result: FAIL\n"); /* //Setup card options choice1 = -1; choice2 = -1; choice3 = -1; coin_bonus = 0; //Create clean game if (initializeGame(NUM_PLAYERS, selectedCards, seed, g) == -1){ printf("\nCould not initialize game. Testing aborted.\n"); return -1; } //Set deck to 5 extras - 1 treasure - 5 extras g->deckCount[whoseTurn(g)] = 0; for (i = 1; i <= EXTRA_COUNT; i++){ g->deck[whoseTurn(g)][g->deckCount[whoseTurn(g)]] = EXTRA_CARD; g->deckCount[whoseTurn(g)]++; } g->deck[whoseTurn(g)][g->deckCount[whoseTurn(g)]] = TREASURE; g->deckCount[whoseTurn(g)]++; for (i = 1; i <= EXTRA_COUNT; i++){ g->deck[whoseTurn(g)][g->deckCount[whoseTurn(g)]] = EXTRA_CARD; g->deckCount[whoseTurn(g)]++; } //Set test position in hand to test card g->hand[whoseTurn(g)][TEST_POS] = TEST_CARD; //Save current state memcpy(pre, g, sizeof(struct gameState)); //Attempt to execute card result = cardEffect(TEST_CARD, choice1, choice2, choice3, g, TEST_POS, &coin_bonus); failed = 0; if (result != 0){ printf("\nReturn value: %d, Expected: %d", result, 0); failed = 1; } //Check game/turn settings if (checkNumPlayers(pre, g) < 0){ printf("\nNumPlayers: %d, Expected: %d", g->numPlayers, pre->numPlayers); failed = 1; } result = checkSupply(pre, g); if (result != 0){ printf("\nSupply count for card %d: %d, Expected: %d", result, g->supplyCount[result], pre->supplyCount[result]); failed = 1; } result = checkEmbargo(pre, g); if (result != 0){ printf("\nEmbargo for card %d: %d, Expected: %d", result, g->embargoTokens[result], pre->embargoTokens[result]); failed = 1; } if (checkOutpost(pre, g) < 0){ printf("\noutpostPlayed: %d, Expected: %d", g->outpostPlayed, pre->outpostPlayed); printf("\noutpostTurn: %d, Expected: %d", g->outpostTurn, pre->outpostTurn); failed = 1; } if (checkTurn(pre, g) < 0){ printf("\nwhoseTurn: %d, Expected: %d", g->whoseTurn, pre->whoseTurn); failed = 1; } if (checkPhase(pre, g) < 0){ printf("\nphase: %d, Expected: %d", g->phase, pre->phase); failed = 1; } if (checkNumActions(pre, g) < 0){ printf("\nnumActions: %d, Expected: %d", g->numActions, pre->numActions); failed = 1; } if (checkCoins(pre, g) < 0){ printf("\ncoins: %d, Expected: %d", g->coins, pre->coins); failed = 1; } if (checkNumBuys(pre, g) < 0){ printf("\nnumBuys: %d, Expected: %d", g->numBuys, pre->numBuys); failed = 1; } //Check discard unchanged for other players for (i = 0; i < g->numPlayers; i++){ //skip if current player if (i == whoseTurn(g)){ continue; } if (pre->discardCount[i] != g->discardCount[i]){ printf("\nPlayer %d's discardCount: %d, Expected: %d", i, g->discardCount[i], pre->discardCount[i]); failed = 1; } else { //check each card for (j = 0; j < g->discardCount[i]; j++){ if (pre->discard[i][j] != g->discard[i][j]){ printf("\nPlayer %d's discard[%d]: %d, Expected: %d", i, j, g->discard[i][j], pre->discard[i][j]); failed = 1; } } } } //Check deck unchanged for other players for (i = 0; i < g->numPlayers; i++){ //skip if current player if (i == whoseTurn(g)){ continue; } if (pre->deckCount[i] != g->deckCount[i]){ printf("\nPlayer %d's deckCount: %d, Expected: %d", i, g->deckCount[i], pre->deckCount[i]); failed = 1; } else { //check each card for (j = 0; j < g->deckCount[i]; j++){ if (pre->deck[i][j] != g->deck[i][j]){ printf("\nPlayer %d's deck[%d]: %d, Expected: %d", i, j, g->deck[i][j], pre->deck[i][j]); failed = 1; } } } } //Check hand unchanged for other players for (i = 0; i < g->numPlayers; i++){ //skip if current player if (i == whoseTurn(g)){ continue; } if (pre->handCount[i] != g->handCount[i]){ printf("\nPlayer %d's handCount: %d, Expected: %d", i, g->handCount[i], pre->handCount[i]); failed = 1; } else { //check each card for (j = 0; j < g->handCount[i]; j++){ if (pre->hand[i][j] != g->hand[i][j]){ printf("\nPlayer %d's hand[%d]: %d, Expected: %d", i, j, g->hand[i][j], pre->hand[i][j]); failed = 1; } } } } //Check adventurer added to played pile if (g->playedCardCount != pre->playedCardCount + 1){ printf("\nPlayed card count: %d, Expected: %d", g->playedCardCount, pre->playedCardCount + 1); failed = 1; } else if (g->playedCards[g->playedCardCount - 1] != TEST_CARD){ printf("\nLast played card: %d, Expected: %d", g->playedCards[g->playedCardCount - 1], TEST_CARD); failed = 1; } //Check 1 treasure + extras removed from deck; other cards remain if (g->deckCount[whoseTurn(g)] != pre->deckCount[whoseTurn(g)] - EXTRA_COUNT - 1){ printf("\nCurrent player's deck count: %d, Expected: %d", g->deckCount[whoseTurn(g)], pre->deckCount[whoseTurn(g)] - EXTRA_COUNT - 1); failed = 1; } for (i = 0; i < g->deckCount[whoseTurn(g)]; i++){ if (g->deck[whoseTurn(g)][i] != EXTRA_CARD){ printf("\nCurrent player's deck[%d]: %d, Expected: %d", i, g->deck[whoseTurn(g)][i], EXTRA_CARD); failed = 1; } } //Check 1 treasure card added to hand, adventurer removed if (g->handCount[whoseTurn(g)] != pre->handCount[whoseTurn(g)]){ printf("\nCurrent player's hand count: %d, Expected: %d", g->handCount[whoseTurn(g)], pre->handCount[whoseTurn(g)]); failed = 1; } if (g->hand[whoseTurn(g)][g->handCount[whoseTurn(g)] - 1] != TREASURE){ printf("\nCurrent player's hand[%d]: %d, Expected: %d", g->handCount[whoseTurn(g)] - 1, g->hand[whoseTurn(g)][g->handCount[whoseTurn(g)] - 1], TREASURE); failed = 1; } //Check extra cards added to discard if (g->discardCount[whoseTurn(g)] != pre->discardCount[whoseTurn(g)] + EXTRA_COUNT){ printf("\nCurrent player's discard count: %d, Expected: %d", g->discardCount[whoseTurn(g)], pre->discardCount[whoseTurn(g)] + EXTRA_COUNT); failed = 1; } for (i = 1; i <= EXTRA_COUNT; i++){ if (g->discard[whoseTurn(g)][g->discardCount[whoseTurn(g)] - i] != EXTRA_CARD){ printf("\nCurrent player's discard[%d]: %d, Expected: %d", g->discardCount[whoseTurn(g)] - i, g->discard[whoseTurn(g)][g->discardCount[whoseTurn(g)] - i], EXTRA_CARD); failed = 1; } } //Check no bonus coins added if (coin_bonus != 0){ printf("\nCoin bonus after adventurer: %d, Expected: %d", coin_bonus, 0); failed = 1; } //Final check if (failed){ printf("\nResult: FAIL\n"); } else { printf("none"); printf("\nResult: PASS\n"); } */ return 0; }