#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.ignore(80, '\n'); cin >> peg[0] >> peg[1] >> peg[2] >> peg[3]; } while(!verify(peg)); } 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; } } 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; } } void compare(pegs code, pegs &guess){ int i = 0, c = 0; char ref[5] = {'r','b','o','y','g'}; int colors[2][5] = {{0,0,0,0,0},{0,0,0,0,0}}; for(i = 0; i < 4; i++){ if(code.peg[i] == guess.peg[i]) guess.black++; else{ colors[0][indexof(code.peg[i], ref)]++; colors[1][indexof(guess.peg[i], ref)]++; } } for(i = 0; i < 5; i++){ if(colors[0][i] + colors[1][i] != 0){ c = (colors[0][i] <= colors[1][i]) ? 0 : 1; guess.white += colors[c][i]; } } } 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; } // rules. no input, outputs text void rules(){ system("cls"); cout << "how to play: " << endl << "coming to a theatre near you" << 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. 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; }