Created
May 3, 2013 04:45
-
-
Save banada/5507256 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 2A2B -- a game of dumb luck | |
* | |
* Authors: Nathaniel Chen | |
* Copyright Nathaniel Chen 2013 | |
* | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
struct details { | |
// target number set by opponent | |
// my current guess | |
// my current result (a's and b's) | |
} | |
// initialize data structures | |
// until a valid target is entered (4 unique numbers 0-9) | |
// user picks target for computer | |
// check that the entered target is valid | |
// computer generates a valid target for user | |
// do until someone wins | |
// until a valid guess is entered | |
// get a guess from user | |
// check that the entered numbers are unique and 0-9 | |
// from target and guess generate a's and b's | |
// if 4a's then | |
// user wins | |
// else | |
// computer generates new guess from previous (keeping any a's) | |
// if 4a then computer wins | |
// end do until | |
/* one struct for comp, one for user */ | |
struct keep { | |
int num[4]; | |
int guess[4]; | |
char a_or_b[4]; //a==correct number and position, b=correct number, blank=initial condition | |
}; | |
struct keep *user; | |
s)truct keep *comp; | |
int turn; | |
//can be called to generate a target or a guess | |
//returns -1 if error, 0 otherwise | |
//p is an array of 4 integers set only when 0 is returned | |
int getValidNumberFromUser (int (*p)) { | |
int i; | |
int k; | |
char c[4]; | |
int num[4]; | |
int j; | |
gets(c); | |
for (k=0; k<4; k++) { | |
num[k] = (int)c[k]; | |
num[k] -= '0'; | |
if (num[k] < 0 || num[k] > 9) { | |
printf("error! not an int\n"); | |
return -1; | |
} | |
/* all numbers must be different */ | |
for (j=0; j<k; j++) { | |
if (num[k] == num[j]) { | |
printf("error! numbers must all be different\n"); | |
return -1; | |
} | |
} | |
} | |
//everythings okay so copy the result to caller | |
for (k=0; k<4; k++) {p[k] = num[k];} | |
return 0; | |
} | |
// first time called a_or_b should be all blank | |
int computer_guess (void) { | |
int i; | |
int k; | |
/* next turns -- randomly guess what isn't A .. could be b or blank */ | |
/* randomly guess anything that isn't A right now */ | |
for (i=0; i<4; i++) { | |
if (comp->check[i] == 'A') | |
continue; | |
else { | |
comp->guess[i] = (rand() % 10); | |
/* all numbers must be different */ | |
if (i > 0) { | |
for (k=0;k<i;k++) { | |
while (comp->guess[i] == comp->guess[k]) { | |
comp->guess[i] = (rand() % 10); | |
} | |
} | |
} | |
} | |
} | |
turn++; /// take this out of here probably | |
} | |
int user_guess (void) { | |
int i; | |
int k; | |
printf("Take a guess:\n"); | |
get_numbers(user->guess); | |
return 0; | |
} | |
// if checkGuess(UserDetails.guess, UserDetails.target, | |
//how about bool checkGuess( int guess[], int target[], int aOrb[] ) | |
bool isPosUnique (int pos, int array[]) // is the number at pos anyywhere else in the array (of 4) | |
int check (struct keep *checker, struct keep *checkee) { | |
int i; | |
int k; | |
/* check for 2A2B */ | |
for (i=0; i<4; i++) { | |
if (checker->guess[i] == checkee->num[i]) | |
checker->check[i] = 'A'; | |
// else if (isNumInArray(guess[ | |
else { | |
for (k=0; k<4; k++) { | |
if (checker->guess[i] == checkee->num[k]) { | |
checker->check[i] = 'B'; | |
break; | |
} | |
else { | |
checker->check[i] = '0'; | |
} | |
} | |
} | |
} | |
return 0; // how about returning count of a's | |
} | |
int main (void) { | |
int i; | |
int k; | |
int counta; | |
int countb; | |
turn = 0; | |
user = calloc(1, sizeof(struct keep)); | |
comp = calloc(1, sizeof(struct keep)); | |
/* FIXME initialize check vals */ | |
comp->check[0] = '0'; | |
comp->check[1] = '0'; | |
comp->check[2] = '0'; | |
comp->check[3] = '0'; | |
user->check[0] = '0'; | |
user->check[1] = '0'; | |
user->check[2] = '0'; | |
user->check[3] = '0'; | |
comp->guess[0] = 0; | |
comp->guess[1] = 0; | |
comp->guess[2] = 0; | |
comp->guess[3] = 0; | |
user->guess[0] = 0; | |
user->guess[1] = 0; | |
user->guess[2] = 0; | |
user->guess[3] = 0; | |
printf("\n**************************************\n"); | |
printf("\n** WELCOME TO 2A2B -- GOOD LUCK **\n"); | |
printf("\n**************************************\n"); | |
printf("\nPick four numbers from 0-9 (e.g. 1234)\n"); | |
if (get_numbers(user->num) < 0) | |
return 0; | |
printf("Your numbers: "); | |
for (i=0; i<4; i++) { | |
printf("%d", user->num[i]); | |
if (i==3) | |
printf("\n"); | |
} | |
/* set random computer numbers */ | |
// printf("Computer's numbers: "); | |
/* seed */ | |
srand(time(NULL)); | |
for (i=0; i<4; i++) { | |
/* modulo 10 will always give us 0-9 */ | |
comp->num[i] = (rand() % 10); | |
/* all numbers must be different */ | |
// FIXME | |
if (i > 0) { | |
for (k=0;k<i;k++) { | |
while (comp->num[i] == comp->num[k]) { | |
comp->num[i] = (rand() % 10); | |
} | |
} | |
} | |
// printf("%d", comp->num[i]); | |
} | |
// printf("\n"); | |
/* FIXME this needs to be a separate function called in a while */ | |
while ((!((comp->check[0] == 'A') && (comp->check[1] == 'A') && (comp->check[2] == 'A') && (comp->check[3] == 'A'))) && | |
(!((user->check[0] == 'A') && (user->check[1] == 'A') && (user->check[2] == 'A') && (user->check[3] == 'A')))) { | |
printf("\nTake a guess:"); | |
if (get_numbers(user->guess) < 0) { | |
printf("error! failed to get your guess"); | |
return 0; | |
} | |
printf("\nUser's Guess: "); | |
for (i=0; i<4; i++) | |
printf("%d", user->guess[i]); | |
check(user, comp); | |
printf("\nResult: "); | |
counta = countb = 0; | |
for (i=0; i<4; i++) { | |
if (user->check[i] == 'A') | |
counta++; | |
else if (user->check[i] == 'B') | |
countb++; | |
} | |
printf("%dA%dB\n", counta, countb); | |
computer_guess(); | |
printf("\nYour numbers: "); | |
for (i=0; i<4; i++) { | |
printf("%d", user->num[i]); | |
if (i==3) | |
printf("\n"); | |
} | |
printf("Computer's Guess: "); | |
for (i=0; i<4; i++) | |
printf("%d", comp->guess[i]); | |
check(comp, user); | |
printf("\nResult: "); | |
counta = countb = 0; | |
for (i=0; i<4; i++) { | |
if (comp->check[i] == 'A') | |
counta++; | |
else if (comp->check[i] == 'B') | |
countb++; | |
} | |
printf("%dA%dB\n", counta, countb); | |
} | |
if ((comp->check[0] == 'A') && (comp->check[1] == 'A') && (comp->check[2] == 'A') && (comp->check[3] == 'A')) | |
printf("\nYou were bested by a machine in %d moves!\n", turn); | |
if ((user->check[0] == 'A') && (user->check[1] == 'A') && (user->check[2] == 'A') && (user->check[3] == 'A')) | |
printf("\nYou vanquished the computer in %d moves\n", turn); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment