Dec 27, 2010 at 1:31pm UTC
I wrote a Rocks papers Scissors game with an assumption that
-when the opponent wins the last game, he uses the same move the next game
-when the opponent looses the last game, he chooses the weaopen which would have defeated the computer in previous game.
Dumb assumption, but hey!
Anyway, For some reason - the computer always selects rock no matter what the previous move is. I don't understand why that is!
Here's the relevant code
Interface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
enum move{rock,paper,scissor};
class RocksPapersScissors
{
public :
RocksPapersScissors();//Possibly problematic
move getMove();//Fine
move counterAttack();//Possibly Problematic
void decideWinner();//Fine
void printOutcome();//Fine
private :
void previousify();//Checked..Fine
bool ComputerWonPrevious;
move Random();//For now, just returns rock
move PreviousComputerMove;
move PreviousHumanMove;
move CurrentHumanMove;
move CurrentComputerMove;
string WinStatus;
};
Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
RocksPapersScissors::RocksPapersScissors()
{
ComputerWonPrevious=false ;
PreviousHumanMove=Random();
PreviousComputerMove=Random();
WinStatus="No Value" ;
}
move RocksPapersScissors::counterAttack()
{
if (ComputerWonPrevious)
{
switch (PreviousComputerMove)
{
case rock:{CurrentComputerMove=scissor;}
case paper:{CurrentComputerMove=rock;}
case scissor:{CurrentComputerMove=paper;}
default : {CurrentComputerMove=Random();}
}
}
else
{
switch (PreviousHumanMove)
{
case rock:{CurrentComputerMove=paper;}
case paper:{CurrentComputerMove=scissor;}
case scissor:{CurrentComputerMove=rock;}
default : {CurrentComputerMove=Random();}
}
}
}
void RocksPapersScissors::printOutcome()
{ previousify(); /*the rest...*/ }
}
void RocksPapersScissors::previousify()
{
PreviousComputerMove = CurrentComputerMove;
PreviousHumanMove = CurrentHumanMove;
}
move RocksPapersScissors::Random()
{ return rock; }
Main
1 2 3 4 5 6 7 8 9 10 11 12
int main()
{
RocksPapersScissors player;
while (1)
{
player.getMove();
player.counterAttack();
player.decideWinner();
player.printOutcome();
}
return 0;
}
BTW: Are long function names acceptable? I use em cuz they're kinda more readable.
Last edited on Dec 27, 2010 at 1:35pm UTC
Dec 27, 2010 at 2:13pm UTC
Last edited on Dec 27, 2010 at 2:17pm UTC
Dec 27, 2010 at 3:25pm UTC
Can't believe I missed that, Thanks!!
:D