#include <iostream.h>#include <stdlib.h>#include <time.h>// pegs game[11]// game[10] = original code// game[9-0] attempts and resultstruct 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 intint 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 booleanbool 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.ignore(80, '\n'); 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 dependso 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 aivoid 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: textvoid 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 outputvoid 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' multi-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. 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 resultvoid 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;}// rules. no input, outputs textvoid rules(){ system("cls"); cout << "how to play: " << endl << "coming to a theatre near you" << endl;}// menu function. no input, outputs text to screenvoid menu(){ bool repeat = true; do{ int choice; cout << endl << "1. player vs player" << endl << "2. player vs computer" << endl << "3. rules" << endl << "4. exit" << endl; cin.ignore(80, '\n'); cin >> choice; switch(choice){ case 2: play(true); break; case 3: rules(); 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;}