Example #1
0
bool MessageFormatter::mapArgs(std::vector<icu::Formattable>& types,
                               std::vector<icu::UnicodeString>& names,
                               const Array& args) {
  int32_t count = args.size(), arg_num = 0;
  types.resize(count);
  names.resize(count);
  auto idx_limit = args->iter_end();
  for (auto idx = args->iter_begin(); idx != idx_limit;
       idx = args->iter_advance(idx), ++arg_num) {
    auto key = args->getKey(idx);
    icu::Formattable::Type type = icu::Formattable::kObject; // unknown
    if (key.isString()) {
      UErrorCode error = U_ZERO_ERROR;
      icu::UnicodeString key16(u16(key.toString(), error));
      if (U_FAILURE(error)) {
        setError(U_ILLEGAL_ARGUMENT_ERROR,
                 "Invalid UTF-8 data in argument key: '%s'",
                 key.toString().c_str());
        return false;
      }
      if (m_namedParts.find(key16) != m_namedParts.end()) {
        type = m_namedParts[key16];
      }
      names[arg_num] = key16;
    } else {
      auto num = key.toInt64();
      if (m_numericParts.find(num) != m_numericParts.end()) {
        type = m_numericParts[num];
      }
      char buffer[12];
      int32_t len = snprintf(buffer, sizeof(buffer), "%d", (int)num);
      UErrorCode error = U_ZERO_ERROR;
      icu::UnicodeString numName(u16(String(buffer, len, CopyString), error));
      names[arg_num] = numName;
    }

    auto val = args->getValue(idx);
    icu::Formattable formattable;
    switch(type) {
      case icu::Formattable::kString: {
string_val:
        UErrorCode error = U_ZERO_ERROR;
        icu::UnicodeString ustr(u16(val.toString(), error));
        if (U_FAILURE(error)) {
          setError(error, "Invalid UTF-8 data in string argument: '%s'",
                          val.toString().c_str());
          return false;
        }
        formattable.adoptString(new icu::UnicodeString(ustr));
        break;
      }
      case icu::Formattable::kDouble:
        formattable.setDouble(val.toDouble());
        break;
      case icu::Formattable::kLong:
        formattable.setLong(val.toInt64());
        break;
      case icu::Formattable::kInt64:
        formattable.setInt64(val.toInt64());
        break;
      case icu::Formattable::kDate:
        formattable.setDate(VariantToMilliseconds(val));
        break;
      default:
        // No context for arg, so make assupmtion based on value
        if (val.isDouble()) {
          formattable.setDouble(val.toDouble());
        } else if (val.isNull() || val.isBoolean() || val.isInteger()) {
          formattable.setInt64(val.toInt64());
        } else if (val.isString() || val.isObject()) {
          goto string_val;
        } else {
          setError(U_ILLEGAL_ARGUMENT_ERROR,
                   "No strategy to convert the "
                   "value given for the argument with key '%s' "
                   "is available", val.toString().c_str());
          return false;
        }
    }
    types[arg_num] = formattable;
  }
  return true;
}
int main() {
	struct gameState G;
	FILE *out;
	int nplayers = 0, ntests=0,i=0,j=0,k[10],redo = 1,seed, currentp=0, actions =0, *played, turn, ncoins=0, buyA = -1;
	out = fopen("gameResults.out","w");
	srand(time(NULL));
	
	nplayers = rand() % 3 + 2;
	// iterate through multiple tests
	while(ntests < MAX_TESTS) {
		turn = 1;
		seed = rand();
		printf("random seed %d ", seed);
		printf("in tests\n");
		fprintf(out, "Initializing game: %d\n", ntests);
		//pick cards
		for(i=0;i < 10; i++){
			printf("stuck picking cards\n");
			//kingdom cards unique no repeats
			do {
			redo = 0;
			k[i] = rand() % 20 + 7;
			printf("%d ", k[i]);
				for(j=0; j < i; j++) {
					if(k[i] == k[j]){
						redo = 1;
					} 
				}
			} while(redo);
		}
		fprintf(out, "Kingdom cards: \n");
		for(i=0; i < 10; i++) {
			fprintf(out,"%s, ", numName(k[i]));
		}
		fprintf(out,"\n");
		initializeGame(nplayers, k, seed, &G);
		printf("past init games\n");
		//Print player hands
		/*fprintf(out, "Player hands: \n");
		for(i=0; i < nplayers; i++) {
			fprintf(out,"P%d: {\n",i);
			for(j=0; j < numHandCards(&G); j++) {
				//fprintf(out, "\tC%d: %s\n", j, numName(handCard(j, &G)));
				//debug
				fprintf(out, "\tC%d: %d\n", j, handCard(j, &G));
			}
			fprintf(out, "}\n");
		}
		*/
		
		while(!isGameOver(&G)) {
			//printf("stuck in game\n");
			currentp = whoseTurn(&G);
			//Print turn player cards and info
			fprintf(out, "-- P%d turn %d --\n Current hand: {", currentp,turn);
			for(i=0; i < numHandCards(&G); i++) {
				fprintf(out, "C%d: %s ", i, numName(handCard(i, &G)));
			}
			fprintf(out, "}\n <-- Action Phase -->\n");
			//start action phase
			actions = 0;
			ncoins = coins(&G);
			G.coins = ncoins;
			for(i=0;i < G.numActions;i++) {
				fprintf(out, "#A: %d\n", G.numActions);
				played = playing(k, &G);
				if(played == 0) {
					fprintf(out, "No playable cards\n");
					break;
				}
				fprintf(out, "Playing: %s\n Choices: %d, %d, %d\n", numName(played[0]), played[1], played[2], played[3]);
				
				if(playCard(played[0], played[1], played[2], played[3], &G) == -1)
					fprintf(out, "\tFailure to play %s\n", numName(played[0]));
			}
			
			//start buy phase
			fprintf(out, "<-- Buy Phase --> \n");
			for(i=0; i < G.numBuys; i++) {
				buyA = canBuy(k, &G);
				fprintf(out, "Buying a %s\n", numName(buyA));
				if(buyCard(buyA, &G) == -1)
					fprintf(out, "\tFailed to buy\n");
				G.coins -= getCost(buyA);
			}
			
			//end of turn
			//fprintf(out, "-- End P%d turn --\n", currentp);
			endTurn(&G);
			free(played);
			turn++;
		}
		
	
	ntests++;
	turn = 1;
	}
	fclose(out);
	return 0;
}