int main(int argc, char* argv[]) {
	int test;
	int seed = atoi(argv[1]);
	
	//Create gameStates with random properties
	for (test = 0; test < NUMTESTS; test++) {
		
		int i, acard, init, command, numPlayers, playerNum, outcome, currentPlayer, turnNum;
		int kCards[10];
		struct gameState * g;
		
		char line[MAX_STRING_LENGTH];
		char cardName[MAX_STRING_LENGTH];
		int players[MAX_PLAYERS];
		
		int gameOver = FALSE;
		int arg0 = UNUSED;
		int arg1 = UNUSED;
		int arg2 = UNUSED;
		int arg3 = UNUSED;
		
		printf("------===================******* Game %d of %d ********==================--------\n", test + 1, NUMTESTS);
		
		//Pick random cards
		for (i = 0; i < 10; i++) {
			acard = floor(Random() * 20 + 7);
			if (ValInArr(acard, kCards, 10) || acard == feast)
				i--;
			else
				kCards[i] = acard;
		}
		kCards[6] = mine; //force choose mine
		printf("Cards chosen for game %d:\n", test + 1);
		for (i = 0; i < 10; i++) {
			strcpy(cardName,"");
			cardNumToName(kCards[i], cardName);
			printf("  (%d) - %s\n", kCards[i], cardName);
		}
		
		//Create game state
		g = (struct gameState*)malloc(sizeof(struct gameState));
		numPlayers = floor(Random() * (2 + 1) + 2);
		init = initializeGame(numPlayers, kCards, seed, g);
		assert(init > -1);
		
		
		currentPlayer = whoseTurn(g);
		PrintGameInfo(g, turnNum);
		PrintTurnInfo(g, turnNum);
		while(1) {
			
			outcome = FAILURE;
			strcpy(line,"");
			strcpy(cardName,"");
			
			gameOver = isGameOver(g); 		
			if(gameOver == TRUE)
				break; //Exit out of the game
			
			//Choose action for turn
			command = GetCommand();
			
			if (command == cmdPlay) {
				arg0 = floor(Random() * g->handCount[currentPlayer]);
				arg1 = floor(Random() * (treasure_map + 1));
				arg2 = floor(Random() * (treasure_map + 1));
				arg3 = floor(Random() * (1 + 1));
				strcpy(cardName,"");
			    cardNumToName(g->hand[currentPlayer][arg0], cardName);
				outcome = playCard(arg0, arg1, arg2, arg3, g);
				if (outcome == SUCCESS){
					printf("-Player %d plays %s...args: %d, %d, %d, %d\n\n", currentPlayer, cardName, arg0, arg1, arg2, arg3);
					PrintTurnInfo(g, turnNum);
				} else {
					printf("-Player %d cannot play card %s...args: %d, %d, %d, %d\n\n", currentPlayer, cardName, arg0, arg1, arg2, arg3);
					PrintTurnInfo(g, turnNum);
				}
			} else
			if (command == cmdBuy) {
				arg0 = floor(Random() * (treasure_map + 1));
				outcome = buyCard(arg0, g);
				strcpy(cardName,"");
			    cardNumToName(arg0, cardName);
				if(outcome == SUCCESS){
					printf("-Player %d buys card %d: %s...args: %d, %d, %d, %d\n\n", currentPlayer, arg0, cardName, arg0, arg1, arg2, arg3);
					PrintTurnInfo(g, turnNum);
				} else {
					printf("-Player %d cannot buy card %d: %s...args: %d, %d, %d, %d\n\n", currentPlayer, arg0, cardName, arg0, arg1, arg2, arg3);
					PrintTurnInfo(g, turnNum);
				}
			} else
			if (command == cmdEnd) {
				printf("-Player %d ends turn %d...args: %d, %d, %d, %d\n\n", whoseTurn(g), turnNum, arg0, arg1, arg2, arg3);
				turnNum++;
				endTurn(g);
				currentPlayer = whoseTurn(g);
				PrintGameInfo(g, turnNum);
			}
		}
		
		printScores(g);
		getWinners(players, g);
		printf("After %d turns, the winner(s) are:\n", turnNum);
		for(playerNum = 0; playerNum < g->numPlayers; playerNum++)
			if(players[playerNum] == WINNER) printf("Player %d\n", playerNum);
		PrintGameInfo(g, turnNum);
		free(g);
		printf("Game %d of %d complete\n", test + 1, NUMTESTS);
	}
	
	printf("All games complete\n\n");
	//printf("%d of %d test were successful.\n", NUMTESTS * (NUMPLAYERS-1) - numFailed, NUMTESTS * (NUMPLAYERS-1));
	
	return 0;
}
示例#2
0
int main(int argc, char *argv[]) {
  char *add = "add";
  char *buyC = "buy";
  char *endT = "end";
  char *exit = "exit";
  char *help = "help";
  char *init = "init";
  char *numH = "num";
  char *play = "play";
  char *resign = "resi";
  char *show = "show";
  char *stat = "stat";
  char *supply = "supp";
  char *whos = "whos";

  char command[MAX_STRING_LENGTH];
  char line[MAX_STRING_LENGTH];
  char cardName[MAX_STRING_LENGTH];

  //Array to hold bot presence
  int isBot[MAX_PLAYERS] = {0, 0, 0, 0};

  int players[MAX_PLAYERS];
  int playerNum;
  int outcome;
  int currentPlayer;
  int gameOver = FALSE;
  int gameStarted = FALSE;
  int turnNum = 0;

  int randomSeed = atoi(argv[1]);

  //Default cards, as defined in playDom
  int kCards[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, sea_hag, tribute, smithy};

  struct gameState g;
  struct gameState *game = &g;

  memset(game, 0, sizeof(struct gameState));

  if (argc != 2) {
    printf("Usage: player [integer random number seed]\n");
    return EXIT_SUCCESS;
  }

  if (randomSeed <= 0) {
    printf("Usage: player [integer random number seed]\n");
    return EXIT_SUCCESS;
  }

  initializeGame(2, kCards, randomSeed, game);

  printf("Please enter a command or \"help\" for commands\n");


  while (TRUE) {
    int arg0 = UNUSED;
    int arg1 = UNUSED;
    int arg2 = UNUSED;
    int arg3 = UNUSED;

    outcome = FAILURE;
    strcpy(line, "");
    strcpy(command, "");
    strcpy(cardName, "");

    currentPlayer = whoseTurn(game);

    //If you are getting a seg fault comment this if block out
    gameOver = isGameOver(game);
    if (gameStarted == TRUE && gameOver == TRUE) {
      printScores(game);
      getWinners(players, game);
      printf("After %d turns, the winner(s) are:\n", turnNum);
      for (playerNum = 0; playerNum < game->numPlayers; playerNum++) {
        if (players[playerNum] == WINNER) printf("Player %d\n", playerNum);
      }
      for (playerNum = 0; playerNum < game->numPlayers; playerNum++) {
        printHand(playerNum, game);
        printPlayed(playerNum, game);
        printDiscard(playerNum, game);
        printDeck(playerNum, game);
      }

      break; //Exit out of the game/while loop
    }


    if (isBot[currentPlayer] == TRUE) {
      executeBotTurn(currentPlayer, &turnNum, game);
      continue;
    }

    printf("$ ");
    fgets(line, MAX_STRING_LENGTH, stdin);
    sscanf(line, "%s %d %d %d %d", command, &arg0, &arg1, &arg2, &arg3);


    if (COMPARE(command, add) == 0) {
      outcome = addCardToHand(currentPlayer, arg0, game);
      cardNumToName(arg0, cardName);
      printf("Player %d adds %s to their hand\n\n", currentPlayer, cardName);
    } else if (COMPARE(command, buyC) == 0) {
      outcome = buyCard(arg0, game);
      cardNumToName(arg0, cardName);
      if (outcome == SUCCESS) {
        printf("Player %d buys card %d, %s\n\n", currentPlayer, arg0, cardName);
      } else {
        printf("Player %d cannot buy card %d, %s\n\n", currentPlayer, arg0, cardName);
      }
    } else if (COMPARE(command, endT) == 0) {
      if (gameStarted == TRUE) {
        if (currentPlayer == (game->numPlayers - 1)) turnNum++;
        endTurn(game);
        currentPlayer = whoseTurn(game);
        printf("Player %d's turn number %d\n\n", currentPlayer, turnNum);
      }

    } else if (COMPARE(command, exit) == 0) {
      break;
    } else if (COMPARE(command, help) == 0) {
      printHelp();
    } else if (COMPARE(command, init) == 0) {
      int numHuman = arg0 - arg1;
      for (playerNum = numHuman; playerNum < arg0; playerNum++) {
        isBot[playerNum] = TRUE;
      }
      //		selectKingdomCards(randomSeed, kCards);  //Comment this out to use the default card set defined in playDom.
      outcome = initializeGame(arg0, kCards, randomSeed, game);
      printf("\n");
      if (outcome == SUCCESS) {
        gameStarted = TRUE;
        currentPlayer = whoseTurn(game);
        printf("Player %d's turn number %d\n\n", currentPlayer, turnNum);
      }

    } else if (COMPARE(command, numH) == 0) {
      int numCards = numHandCards(game);
      printf("There are %d cards in your hand.\n", numCards);
    } else if (COMPARE(command, play) == 0) {
      int card = handCard(arg0, game);
      outcome = playCard(arg0, arg1, arg2, arg3, game);
      cardNumToName(card, cardName);
      if (outcome == SUCCESS) {
        printf("Player %d plays %s\n\n", currentPlayer, cardName);
      } else {
        printf("Player %d cannot play card %d\n\n", currentPlayer, arg0);
      }

    } else if (COMPARE(command, resign) == 0) {
      endTurn(game);
      printScores(game);
      break;
    } else if (COMPARE(command, show) == 0) {
      if (gameStarted == FALSE) continue;
      printHand(currentPlayer, game);
      printPlayed(currentPlayer, game);
      //printDiscard(currentPlayer, game);
      //printDeck(currentPlayer, game);
    } else if (COMPARE(command, stat) == 0) {
      if (gameStarted == FALSE) continue;
      printState(game);
    } else if (COMPARE(command, supply) == 0) {
      printSupply(game);
    } else if (COMPARE(command, whos) == 0) {
      int playerNum = whoseTurn(game);
      printf("Player %d's turn\n", playerNum);
    }
  }

  return EXIT_SUCCESS;

}
int main (int argc, char** argv) {
    int i;
    int j;
    int valid;
    int numPlayers;
    int k[10];
    int result;
    int seed;
    char cardName[100];
    struct gameState *state = newGame();
    int currentPlayer;



    printf ("------------------ Starting game --------------------\n");
    //set the seed
    srand(1);
    seed = rand();
    //2-4 players
    numPlayers = rand() % 3 + 2;

    printf("Number of Players: %d\n", numPlayers);

    for (i = 0; i < 10; i++) {
        valid = 0;
        while (k[i] == feast || k[i] == tribute || k[i] == council_room || valid == 0) {
            k[i] = rand() % (treasure_map - adventurer + 1) + adventurer;
            valid = 1;
            for(j = 0; j < i; j++) {
                if(k[i] == k[j]) {
                    valid = 0;
                }
            }
        }
    }

    printf("Kingdom Cards:\n");
    for(i = 0; i < 10; i++) {
        printf("Test.\n");
        cardNumToName(k[i], cardName);
        printf("Test..\n");
        printf("%s\n", cardName);
    }

    if(initializeGame(numPlayers, k, seed, state) != 0) {
        printf("Error when calling initializeGame\n");
        exit(1);
    }

    int money = 0;
    int actionCard = 0;
    int currentCard = 0;

    while (!isGameOver(state)) {

        currentPlayer = state->whoseTurn;
        printf("Player %d's Turn:\n", currentPlayer);

        printf("----------- Action phase -----------\n");

        actionCard = 0;
        for(i = 0; i < numHandCards(state); i++) {
            if((handCard(i, state) < treasure_map) && (handCard(i, state) >= adventurer) && (handCard(i, state) != feast) && (handCard(i, state) != tribute)) {
                actionCard = i;
                break;
            }
        }

        while(state->numActions > 0 && actionCard != 0) {
            currentCard = handCard(actionCard, state);
            cardNumToName(currentCard, cardName);

            if(playCard(actionCard, -1, -1, -1, state) == -1) {
                discardCard(actionCard, currentPlayer, state, 0);
            }
            else {
                printf("Played Card: %s, %d\n", cardName, currentCard );
            }

            if(state->numActions <= 0) {
                printf("All actions used, move to buy phase\n");
                break;
            }
            for(i = actionCard; i < numHandCards(state); i++) {
                if(handCard(i, state) < treasure_map && handCard(i, state) >= adventurer && handCard(i, state) != feast && handCard(i, state) != tribute) {
                    actionCard = i;
                    break;
                }
                else {
                    actionCard = 0;
                }
            }
        }

        printf("----------- Buy phase -----------\n");
        money = countHandCoins(state->whoseTurn, state);
        int buy_card;

        while(state->numBuys > 0 && money > 0) {
            do {
                if(money >= 8) {
                    buy_card = province;
                }
                else {
                    buy_card = rand() % treasure_map;
                    if(buy_card == 4) {
                        if(rand() % 4 != 0) {
                            buy_card++;
                        }
                    }
                }

            } while(buyCard(buy_card, state) != 0);

            cardNumToName(buy_card, cardName);
            printf("Buying card: %s\n", cardName);
        }
        printf("End Turn\n");
        endTurn(state);

    }

    printScores(state);

    return 0;
}