Skip to content

Instantly share code, notes, and snippets.

@Bahaaib
Last active March 21, 2019 10:23
Show Gist options
  • Select an option

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

Select an option

Save Bahaaib/bc35ff18eb69f9661f0051e0cd87c2b2 to your computer and use it in GitHub Desktop.
Java code to solve the Rock Paper Scissor problem
public class Solution {
private static int change;
private static int mIndex;
private static int mSubIndex;
private static int round;
private static boolean isMyTurn;
private static StringBuilder current, winner;
private static char player1 = 'x';
private static char player2 = 'x';
private static char player0 = 'x';
public static void main(String[] args) {
System.out.println("Changed: " + handFormationChange(5, 2, "RP") + " times");
}
private static int handFormationChange(int n, int a, String formation) {
//Check if the given string is Empty
if (formation.length() < 1) {
return 0;
} else {
//Extract all empty spaces from the given string (if any)
formation = formation.replaceAll("\\s+", "");
current = new StringBuilder(formation);
winner = new StringBuilder();
//Set the index to the given initial position
mIndex = a;
//insert arbitrary char in my position to complete the number of players
current.insert(a, 'x');
System.out.println(current);
//Determine my first formation by checking the status and position of my first opponent
if (((a + 1) % 2 == 0) && (a - 1 >= 0)) {
player0 = pickWinnerAgainst(current.charAt(a - 1));
} else if (((a + 1) % 2 != 0) && (a < current.length() - 1)) {
player0 = pickWinnerAgainst(current.charAt(a + 1));
}
//As long as no final winner yet..
while (current.length() > 1) {
round++;
//Start the competition..
for (int i = 0; i < current.length(); i += 2) {
//if the first player of any pairs is me
if (i == mIndex) {
//and I am the last player in the line - hasn't chosen a formation yet
if (player0 == 'x') {
//and there at least another opponent to play against yet
if (winner.length() > 0) {
//choose my first winning formation and qualify
player0 = pickWinnerAgainst(winner.charAt(0));
winner.append(player0);
continue;
} else {
//if I'm the only remaining player .. I win with any formation
player0 = 'S';
winner.append(player0);
continue;
}
} else {
//If I am the last player in the line with a formation chosen
player1 = player0;
//Ask to pick a winning formation for me as a first player in pair
mSubIndex = 1;
isMyTurn = true;
}
} else {
//If I'm not one of the pair
player1 = current.charAt(i);
}
//if the second player of any pairs is me
if ((i + 1) == mIndex) {
player2 = player0;
//Ask to pick a winning formation for me as a second player in pair
mSubIndex = 2;
isMyTurn = true;
} else {
//If I'm not one of the pair & there's enough players in the line, pick the second player
if ((i + 1) <= current.length() - 1) {
player2 = current.charAt(i + 1);
} else {
//if I'm the first player in pair and the last player
if (i == mIndex) {
//qualify me
winner.append(player1);
//save my new position for the next round
mIndex = winner.length() - 1;
isMyTurn = false;
continue;
//if the last player has no opponent and wasn't me, qualify him for the next round
} else {
winner.append(player1);
continue;
}
}
}
// if I'm in the pair
if (isMyTurn) {
//and I'm player1 in pair
if (mSubIndex == 1) {
insertWinner(player1, player2);
//and I'm player2 in pair
} else if (mSubIndex == 2) {
insertWinner(player2, player1);
}
//if I'm not in the pair
} else {
if (player1 == 'P' && player2 == 'R') {
winner.append(player1);
} else if (player1 == 'R' && player2 == 'P') {
winner.append(player2);
} else if (player1 == 'S' && player2 == 'P') {
winner.append(player1);
} else if (player1 == 'P' && player2 == 'S') {
winner.append(player2);
} else if (player1 == 'R' && player2 == 'S') {
winner.append(player1);
} else if (player1 == 'S' && player2 == 'R') {
winner.append(player2);
}
}
}
//start a new round with the previous round winners
current = new StringBuilder(winner);
winner.setLength(0);
winner = new StringBuilder();
}
return change;
}
}
//check if my current formation help me winning
private static boolean isWinning(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;
//in case of a draw, All lose!
} else {
return false;
}
}
//Advice me with the perfect formation to win
private static char pickWinnerAgainst(char mChar) {
if (mChar == 'P') {
return 'S';
} else if (mChar == 'S') {
return 'R';
} else {
return 'P';
}
}
//Do what's best for me and save my new position in the next round
private static void insertWinner(char playerA, char playerB) {
if (isWinning(playerA, playerB)) {
winner.append(playerA);
mIndex = winner.length() - 1;
isMyTurn = false;
} else {
player0 = pickWinnerAgainst(playerB);
winner.append(player0);
System.out.println(playerA + " -> " + player0 + " @R" + round);
change++;
mIndex = winner.length() - 1;
isMyTurn = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment