/* the following program will let the user play the game "Mastermind" the user can pick between two options of play; first, against a user codemaker, and the computer codemaker. the core of this program is the compare function, and it allowes the computer to compare the user's code guessing attempt, and the codemaker's orignal code. this is done by isolating the letters that do not match, adding them up, and comparing them, hence the name 'compare.' this orignal algorithm took me forever to figure out, leading to many, many, many, different endings of fairy tales, and dream relationships. for those lucky few, they lost their minds at the fact that forever actually arrived. the initalize function initalizes the whole game array and gets the codemaker's code, while filling the game board. once again, hence the name 'initialize'. this simply loops through, putting in a '.' for the color pegs, and a 0 in for the black/white pegs. the verify function makes sure that the codebreaker put in valid color letters. the indexof function will find the int value that a character is in a character array. as the function comment says, it's a "neat luttle function that i needed...". and the pegs struct is vital to the game integrety. thanks for a fun class. /////////////////////////////////////////////////////////////////////////////// row 9 | . | . | . | . :: 0 | 0 row 8 | . | . | . | . :: 0 | 0 row 7 | . | . | . | . :: 0 | 0 row 6 | . | . | . | . :: 0 | 0 row 5 | . | . | . | . :: 0 | 0 row 4 | . | . | . | . :: 0 | 0 row 3 | . | . | . | . :: 0 | 0 row 2 | . | . | . | . :: 0 | 0 row 1 | . | . | . | . :: 0 | 0 row 0 | . | . | . | . :: 0 | 0 (r)ed, (b)lue, (o)range, (y)ellow, (g)reen type in your code, seperated by spaces: row 9 | . | . | . | . :: 0 | 0 row 8 | . | . | . | . :: 0 | 0 row 7 | . | . | . | . :: 0 | 0 row 6 | . | . | . | . :: 0 | 0 row 5 | r | r | o | b :: 4 | 0 row 4 | b | o | r | r :: 0 | 4 row 3 | r | r | b | o :: 2 | 2 row 2 | o | o | o | o :: 1 | 0 row 1 | b | b | b | b :: 1 | 0 row 0 | r | r | r | r :: 2 | 0 Congrats, You won on turn 5! 1. player vs player 2. player vs computer 3. help 4. exit */ #include #include #include // pegs game[11] // game[10] = original code // game[9-0] attempts and result struct pegs{ char peg[4]; int white, black; }; // neat little function that i needed and couldnt find any built in one // function will find the place of a character in a char array (of varying size) // input character and char array, output int int indexof(char x, char *y){ int size, i = 0; // sizeof will return the number of bytes used. // y / *y will return the number of elements in the array size = sizeof(y)/sizeof(*y); while(i < size && x != y[i]) i++; return i; } // function verifys user input. input char array[4], output boolean bool verify(char x[4]){ bool valid = true; char ref[5] = {'r','b','o','y','g'}; int i = 0; while(i < 4 && valid == true){ if(ref[indexof(x[i], ref)] != x[i]) valid = false; i++; } return valid; } // function gets user input for code guess. input/output char array[4], void getcode(char peg[4]){ cout << "(r)ed, (b)lue, (o)range, (y)ellow, (g)reen" << endl; do{ cout << "type in your code, seperated by spaces: "; cin >> peg[0] >> peg[1] >> peg[2] >> peg[3]; } while(!verify(peg)); } // function gets the code to break and initializes the game array // method of original code depends on one input, the bool ai // if positive, use a randomized combination, if not, user makes it up // input: (and output) pegs game array[11], bool ai void initialize(pegs game[11], bool ai){ int i = 0, j = 0; char ref[5] = {'r','b','o','y','g'}; if(ai){ srand(time(NULL)); for(i = 0; i < 4; i++) game[10].peg[i] = ref[rand() % 5]; } else{ cout << "Make sure the codebreaker isnt looking!" << endl; getcode(game[10].peg); } for(i = 0; i < 10; i++){ for(j = 0; j < 4; j++) game[i].peg[j] = '.'; game[i].black = 0; game[i].white = 0; } } // function prints game board to screen. // input: struct pegs array[11] output: text void printmm(pegs game[11]){ int i = 9, j = 0; system("cls"); for(i; i >= 0; i--){ cout << "row " << i << " "; j = 0; for(j; j < 4; j++) cout << "| " << game[i].peg[j] << " "; cout << " :: " << game[i].black << " | " << game[i].white << endl; } } // function compares the values in the original code, and the user's attempt // more info inside function // input: pegs code, and the attempt (guess), which is also the output void compare(pegs code, pegs &guess){ int i = 0, c = 0; // reference array, to be used for the indexof function char ref[5] = {'r','b','o','y','g'}; // keeps track of the number of colors that do not match int colors[2][5] = {{0,0,0,0,0},{0,0,0,0,0}}; for(i = 0; i < 4; i++){ // if they match, add one to the black 'pegs' placeholder if(code.peg[i] == guess.peg[i]) guess.black++; // if not, add to correct position in the 'colors' m-d array else{ colors[0][indexof(code.peg[i], ref)]++; colors[1][indexof(guess.peg[i], ref)]++; } } // complex part that took much time to figure out for(i = 0; i < 5; i++){ // i could have done "if they both are zero", but logically this one-step // comparison should save processing time (for when my game becomes the new halo) if(colors[0][i] + colors[1][i] != 0){ // whichever one has less, add that value to the white 'pegs' c = (colors[0][i] <= colors[1][i]) ? 0 : 1; guess.white += colors[c][i]; } } } // function cycles through the game process // inputs ai boolean, output depends on game result void play(bool ai){ pegs game[11]; int turn = 0; bool victory = false; system("cls"); initialize(game, ai); printmm(game); while(!victory && turn < 10){ getcode(game[turn].peg); compare(game[10], game[turn]); if(game[turn].black == 4) victory = true; else turn++; printmm(game); } if(victory) cout << endl << "Congrats, You won on turn " << turn << "!" << endl; else cout << endl << "Sorry, but you just lost the game" << endl; } // help no input, outputs text void help(){ system("cls"); cout << "the game screen: " << endl << "//////////////////////////////////////////////////////" << endl << "// --game rows-- black peg column //" << endl << "// | | | | | //" << endl << "// row N | . | . | . | . :: 0 | 0 //" << endl << "// | | //" << endl << "// game turn white peg column //" << endl << "//////////////////////////////////////////////////////" << endl; } // menu function. no input, outputs text to screen void menu(){ bool repeat = true; do{ int choice; cout << endl << "1. player vs player" << endl << "2. player vs computer" << endl << "3. help" << endl << "4. exit" << endl; cin >> choice; switch(choice){ case 2: play(true); break; case 3: help(); break; case 4: repeat = false; break; default: play(false); break; } } while(repeat); } // i dont like my main function. at all. i never have. it's too dependant :( int main(){ menu(); return 0; }