theADOLESCENT Posted September 6, 2013 Posted September 6, 2013 Hey everyone. It's been about 3 months since I've programmed and at that time I had just finished a semester of Software Design in Java.Now I'm in a C++ class and was asked to create this program. Mind you, we haven't been taught anything yet, first day was full of introductions. So we were told to read a few chapters and do this program. So between being rusty with my programming and never really programming in C++ before, I'm having a little trouble. On Wednesday he assigned 180 pages of reading along with 2 programming assignments which are due tonight. Just started the program this morning since I've been reading a lot so please don't think I was being lazy haha.The part I'm having trouble with is programming the "smart" computer.Here's what the assignment says.In smart mode the computer takes off enough marbles to make the size of the pile a power of two minus 1 - that is, 3, 7, 15, 31, or 63 since the max pile size will be 100. That is always a legal move, except if the size of the pile is currently one less than a power of two. In that case, the computer makes a random legal move.I've just kind of been messing around trying to figure out something for the smart computer but have not been able to get it. I'm trying to code "start at pile size and move down each iteration, test to see if 2 raised to a number equals the pile size, then do (2^number)-1". I'm having trouble with figuring out the "number". This also may be way off, too.Any help is appreciated.Here it is: #include <iostream> using namespace std; int main() { int pileSize = 10 + rand() % 91; // generates a random pile size between 10 and 100 int turn = rand() % 2; // generates a 0 or 1 to determine who goes first, computer or player int marbleGrab = 0; // how many marbles are being taken out int smartOrStupid = rand() % 2; //0 for smart, 1 for stupid bool smartComputer; // setting level of "intelligence" of computer if(smartOrStupid == 0) { smartComputer = true; std::cout << "Computer is smart.\n"; } else { smartComputer = false; std::cout << "Computer is not so smart.\n"; } pileSize = 100; //take out before turn in std::cout << "Pile size is " << pileSize << "\n"; while(pileSize > 1) { if(turn == 0) // computer turn { std::cout << "Computer turn.\n"; if(!smartComputer) { marbleGrab = 1 + (rand() % pileSize)/2;; } else { for(int i = pileSize; i >= pileSize/2; i--) { for(int x = 10; x > 0; x--) { if(pow(2, x) == pileSize); marbleGrab = pow(2,x) - 1; } } } pileSize -= marbleGrab; std::cout << "The computer took " << marbleGrab << " marbles.\nThere are " << pileSize << " marbles left.\n"; } else // player turn { std::cout << "Player turn\nHow many marbles would you like to take?\n"; cin >> marbleGrab; while(!(marbleGrab <= pileSize/2 && marbleGrab > 0)) { std::cout << "You are trying to take too many or too little marbles.\nHow many marbles would you like to take?\n"; cin >> marbleGrab; } pileSize -= marbleGrab; std::cout << "The player took " << marbleGrab << " marbles.\nThere are " << pileSize << " marbles left.\n"; } turn = (turn + 1) % 2; } std::cout << "Pile size is " << pileSize << "\n"; if (turn == 0) cout << "Computer won.\n"; else cout << "Player won.\n"; return 0; }
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now