Skip to content

Instantly share code, notes, and snippets.

@Bahaaib
Last active March 27, 2019 15:28
Show Gist options
  • Select an option

  • Save Bahaaib/4baa5cd386f05c19a08ae3b920c17912 to your computer and use it in GitHub Desktop.

Select an option

Save Bahaaib/4baa5cd386f05c19a08ae3b920c17912 to your computer and use it in GitHub Desktop.
Cpp code to solve the Rock Paper Scissor problem
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
char mPlayer = 'x';
int change = 0;
int position;
int pairPosition;
int round_num;
bool isMyTurn;
std::vector<char> currentForm;
std::vector<char> winningForm;
std::vector<char>::iterator it;
void testVector(const vector<char>& mVector){
for(int i=0; i < mVector.size(); i++){
cout<<mVector[i];
}
cout << endl;
}
bool isWinningForm(char m, char n){
if (m == 'P' && n == 'R') {
return true;
} else if (m == 'R' && n == 'P') {
return false;
} else if (m == 'S' && n == 'P') {
return true;
} else if (m == 'P' && n == 'S') {
return false;
} else if (m == 'R' && n == 'S') {
return true;
} else if (m == 'S' && n == 'R') {
return false;
} else {
return false;
}
}
char playForMeAgainst(char ch){
if (ch == 'P') {
return 'S';
} else if (ch == 'S') {
return 'R';
} else {
return 'P';
}
}
void decideWinner(char playerA, char playerB){
if (isWinningForm(playerA, playerB)) {
winningForm.push_back(playerA);
position = winningForm.size() - 1;
isMyTurn = false;
} else {
mPlayer = playForMeAgainst(playerB);
winningForm.push_back(mPlayer);
cout << playerA << " -> " << mPlayer << " @R" << round_num <<endl;
change++;
position = winningForm.size() - 1;
isMyTurn = false;
}
}
void provideInitialWinner(int initIndex){
if(((initIndex + 1) % 2 == 0) && (initIndex - 1 >= 0)){
//pick winner
mPlayer = playForMeAgainst(currentForm.at(initIndex - 1));
}else if(((initIndex + 1) % 2 != 0) && (initIndex < currentForm.size() - 1)){
//pick winner
mPlayer = playForMeAgainst(currentForm.at(initIndex + 1));
}
}
void comparePlayers(char player1, char player2){
if (player1 == 'P' && player2 == 'R') {
winningForm.push_back(player1);
} else if (player1 == 'R' && player2 == 'P') {
winningForm.push_back(player2);
} else if (player1 == 'S' && player2 == 'P') {
winningForm.push_back(player1);
} else if (player1 == 'P' && player2 == 'S') {
winningForm.push_back(player2);
} else if (player1 == 'R' && player2 == 'S') {
winningForm.push_back(player1);
} else if (player1 == 'S' && player2 == 'R') {
winningForm.push_back(player2);
}
}
void playMyTurn(char player1, char player2, int pairIndex){
if(pairIndex == 1){
decideWinner(player1, player2);
}else{
decideWinner(player2, player1);
}
}
int handFormationChange(int n, int a, string formations){
char player1;
char player2;
char formationArray[formations.length()];
strcpy(formationArray, formations.c_str());
std::vector<char> formationVector(formationArray, formationArray + sizeof(formationArray) / sizeof formationArray[0]);
currentForm = formationVector;
position = a;
it = currentForm.begin();
currentForm.insert(it + position, 'x');
testVector(currentForm);
provideInitialWinner(a);
while(currentForm.size() > 1){
round_num++;
for(int i=0; i < currentForm.size(); i += 2){
if(i == position){
if(mPlayer == 'x'){
if(winningForm.size() > 0){
//pick winner
mPlayer = playForMeAgainst(winningForm.at(0));
winningForm.push_back(mPlayer);
continue;
}else{
mPlayer = 'S';
winningForm.push_back(mPlayer);
continue;
}
}else{
player1 = mPlayer;
pairPosition = 1;
isMyTurn = true;
}
}else{
player1 = currentForm.at(i);
}
if((i + 1) == position){
player2 = mPlayer;
pairPosition = 2;
isMyTurn = true;
}else{
if((i + 1) <= currentForm.size() - 1){
player2 = currentForm.at(i + 1);
}else{
if(i == position){
winningForm.push_back(player1);
position = winningForm.size() - 1;
isMyTurn = false;
continue;
}else{
winningForm.push_back(player1);
continue;
}
}
}
if(isMyTurn){
playMyTurn(player1, player2, pairPosition);
}else{
comparePlayers(player1, player2);
}
}
currentForm = winningForm;
winningForm.clear();
}
return change;
}
int main()
{
int result = handFormationChange(5, 3, "RPSR");
cout << "Changed: " << result << " times"<< endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment