Skip to content

Instantly share code, notes, and snippets.

@hirosof
Created December 9, 2014 17:41
Show Gist options
  • Save hirosof/ccd28d1c835895f646ca to your computer and use it in GitHub Desktop.
Save hirosof/ccd28d1c835895f646ca to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <time.h>
#include "HSRand/CHSRand.h"
#include "HSRand/HSRandSupport.h"
#define PROB_LENGTH 3
bool IsNumberChar(char c);
int main(void) {
CHSRand mRand;
mRand.SetSeed((unsigned)time(NULL));
unsigned int tVal[PROB_LENGTH];
char cVal[PROB_LENGTH];
unsigned int inVal[PROB_LENGTH];
int HitCount = 0;
int BlowCount = 0;
int TurnCounter = 0;
int ProblemCounter = 0;
bool bNextProblem = false;
char NextFlagChar;
do {
ProblemCounter++;
TurnCounter = 0;
HSRandSupport::GenerateNonOverlapArrayRandom(&mRand, tVal, PROB_LENGTH, 0, 9);
while (true) {
TurnCounter++;
printf("<<Turn %u - %u>>\n",ProblemCounter, TurnCounter);
for (int i = 0; i < PROB_LENGTH; i++) {
scanf("%c", cVal + i);
}
while (getchar() != '\n');
bool bIsAllNumber;
bIsAllNumber = IsNumberChar(cVal[0]);
for (int i = 1; i < PROB_LENGTH; i++) {
bIsAllNumber &= IsNumberChar(cVal[i]);
}
if (bIsAllNumber) {
HitCount = BlowCount = 0;
for (int i = 0; i < PROB_LENGTH; i++) {
inVal[i] = cVal[i] - '0';
}
for (int i = 0; i < PROB_LENGTH; i++) {
for (int j = 0; j < PROB_LENGTH; j++) {
if (tVal[i] == inVal[j]) {
if (i == j) HitCount++;
else BlowCount++;
}
}
}
if (HitCount == PROB_LENGTH) {
printf("正解です。\n\n");
break;
} else {
printf("Hit:%u Blow:%u\n\n", HitCount, BlowCount);
}
} else {
if ((cVal[0] == 'G') || (cVal[0] == 'g')) {
if ((cVal[1] == 'I') || (cVal[1] == 'i')) {
if ((cVal[2] == 'V') || (cVal[2] == 'v')) {
printf("正解は");
for (int i = 0; i < PROB_LENGTH; i++) {
printf("%u", tVal[i]);
}
printf("でした\n\n");
break;
}
}
}
printf("不正な入力です。\n\n");
}
}
printf("[次のうちどれかを入力してください。]\n");
printf("Y or y ・・・次の問題をする\n");
printf("それ以外 ・・・次の問題をしない\n");
scanf("%c", &NextFlagChar);
while (getchar() != '\n');
if ((NextFlagChar == 'Y') || (NextFlagChar == 'y')) {
bNextProblem = true;
} else {
bNextProblem = false;
}
} while (bNextProblem);
return 0;
}
bool IsNumberChar(char c) {
return (c >= '0') && (c <= '9');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment