/* The following program plays the game 'Nim' with the user. +++ there is a two player option and a one player option. when the computer plays it will look for certain patterns in the numbers and decide what to do, depending on the pattern. there will be no option of the computer going first, however. */ #include #include // function checks to see if all are 0. if they are, then they lost // input: three ints. output: boolean bool check_loss(int a, int b, int c){ if(a == 0 && b == 0 && c == 0) return true; else return false; } // the function prints the current turn, and the current game stats // input: the nim array void print_nim(int nim[4]){ cout << "Player " << (nim[0] % 2) + 1 << ", it is your turn" << endl; cout << endl; cout << "-1- -2- -3-" << endl; cout << " :: " << endl; cout << " " << nim[1] << " " << nim[2] << " " << nim[3] << " " << endl; cout << endl; } void ai(int nim[4]){ int sum, nsum, pile, greatest, nxor = 1, out = 1; sum = (nim[1] ^ nim[2] ^ nim[3]);// % 4; if(nim[3] >= nim[2] && nim[3] >= nim[1]) greatest = 3; else if(nim[2] >= nim[3] && nim[2] >= nim[1]) greatest = 2; else if(nim[1] >= nim[3] && nim[1] >= nim[2]) greatest = 1; if(sum == 0) // computer lost. nim[greatest]--; else{ if(((nim[3] - sum) ^ nim[2] ^ nim[1]) == 0){ pile = 3; out = sum; } else if((nim[3] ^ (nim[2] - sum) ^ nim[1]) == 0){ pile = 2; out = sum; } else if((nim[3] ^ nim[2] ^ (nim[1] - sum)) == 0){ pile = 1; out = sum; } nxor = nim[pile] ^ nim[((pile + 1) % 3) + 1] ^ nim[((pile + 2) % 3) + 1]; nsum = nim[1] + nim[2] + nim[3]; if(nsum <= 3 && nim[pile] == 2){ nim[pile] = nim[pile] - 2; out = 2; } else if((nim[pile] - sum) >= 0) nim[pile] = nim[pile] - out; else{ nim[greatest]--; pile = greatest; out = 1; } } cout << "The computer took " << out << " out of pile " << pile << endl; if(check_loss(nim[1], nim[2], nim[3])){ system("cls"); cout << "Computer lost!" << endl << endl; nim[0] = -1; } else nim[0]++; } // the function below actually is the game, for pvp. recursive function until // a winner is declared. input: the nim array. output: prints to console void play(int nim[4]){ //int nim[4] = {0,3,5,8}; int pile, out; // nim[0] is turns system("cls"); print_nim(nim); do{ cout << "Pick a pile: "; cin >> pile; } while(!(pile >= 1 && pile <= 3 && nim[pile] > 0)); do{ cout << "take out: "; cin >> out; } while(!(out <= nim[pile] && out >= 1)); nim[pile] = nim[pile] - out; if(check_loss(nim[1], nim[2], nim[3])){ system("cls"); cout << "Player " << (nim[0] % 2) + 1 << " lost!" << endl << endl; nim[0] = -1; } else nim[0]++; } // this function prints the menu and handles the choice // no input, output is printed to console int menu(){ int choice; do{ int nim[4] = {0,3,5,8}; cout << "1. pvp" << endl << "2. pvc" << endl << "3. exit" << endl; cin >> choice; switch(choice){ case 1: while(nim[0] >= 0) play(nim); break; case 2: while(nim[0] >= 0) if(nim[0] % 2 == 0) play(nim); else ai(nim); break; case 3: cout << "Thanks for playing!" << endl; break; } } while(choice != 3); return choice; } // a pointless function, no? // just outputs 'nim' in ascii. just once. void splash(){ cout << "nn nn ii mm mm mm" << endl << "nn nn nn mm mm mm mm mm" << endl << "nnn nn ii mmm mmm mm" << endl << "nn nn ii mm mm mm" << endl << "nn nn ii mm mm mm" << endl << "nn nn ii mm mm mm" << endl << "nn nn ii mm mm mm" << endl << endl; } int main(){ splash(); while(menu() != 3); return 0; }