/* All of the below was authored by me. By using any part of this source code, you agree to waive all liabilities from the author. Furthermore, you agree that you will not use this source code for personal (or corporate) gain. Finally, should you change any part of this source code, you agree to give credit to the author where such credit is due. This code utilizes pointers, and creates one-dimensional arrays using "pointer = (cast) malloc (arguments);". When modifying this code, it is important to keep in mind that variables declared within a function, and variables declared inside a function arguement list are function specific (local). */ #include #include void config(int *colors, int *size, int *guesses); void gen_arr(int *arr, int colors, int size); void get_arr(int *arr, int colors, int size); void arr_copy(int *arr, int *arrcopy, int size); int pos_chk(int *gss, int *brd, int size); int clr_chk(int *gss, int *gss_cpy, int *brd, int size); int main() { int *brd, *gss, *gss_cpy, i, exit, colors, size, guesses, won, lost, win, pos_cor, clr_cor; printf("---=== Mastermind ===---\n"); for (won=lost=0,exit=1;exit;) { do { config(&colors, &size, &guesses); /* sizeof(int) == 4 */ gss = (int *) malloc(size * 4); gss_cpy = (int *) malloc(size * 4); brd = (int *) malloc(size * 4); if (brd==NULL || gss==NULL || gss_cpy==NULL) printf("Uh Oh! Your computer doesn't like me. Try setting FEWER positions."); } while (brd==NULL || gss==NULL || gss_cpy==NULL); printf("\n\n\n%d colors to choose from, each represented by POSITIVE integers (1 to %d).\nEnter 0 to change previous position.\nYou have %d tries.\nGood Luck!\n\n\n",colors,colors,guesses); gen_arr(brd,colors,size); // This initializes the "brd" array created using "pointer = malloc(arguments);" for (i=1; i<=guesses;i++) { get_arr(gss,colors,size); // This initializes the "gss" array pos_cor = pos_chk(gss,brd,size); // Checks if gss and brd arrays are identical; if not, how many values are the same if (pos_cor!=4) { clr_cor = clr_chk(gss,gss_cpy,brd,size); printf("\n\n\n%d colors correct with %d in proper position.\n%d tries remaining.\n\n\n",clr_cor,pos_cor,guesses-i); win=0; } else { win=1; break; } } if(!win) { lost++; putchar(10); // ASCII character 10 is the newline character for (i=0; icolors || *(arr+i)<0) printf("Invalid Color Integer. Please choose between 1 and %d inclusive.\n",colors); else if (!*(arr+i)) i-=2; } while (*(arr+i)>colors || *(arr+i)<0); return; } void arr_copy(int *arr, int *arrcopy, int size) { int i; for (i=0;i