• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
daily pastebin goal
58%
SHARE
TWEET

# Untitled

a guest Mar 26th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. public class Solution {
2.
3.     private static int change;
4.     private static int mIndex;
5.     private static int mSubIndex;
6.     private static int round;
7.     private static boolean isMyTurn;
8.     private static StringBuilder current, winner;
9.     private static char player1 = 'x';
10.     private static char player2 = 'x';
11.     private static char player0 = 'x';
12.
13.
14.     public static void main(String[] args) {
15.
16.         System.out.println("Changed: " + handFormationChange(5, 2, "RP") + " times");
17.
18.
19.     }
20.
21.     private static int handFormationChange(int n, int a, String formation) {
22.
23.         //Check if the given string is Empty
24.         if (formation.length() < 1) {
25.             return 0;
26.         } else {
27.             //Extract all empty spaces from the given string (if any)
28.             formation = formation.replaceAll("\\s+", "");
29.             current = new StringBuilder(formation);
30.             winner = new StringBuilder();
31.
32.             //Set the index to the given initial position
33.             mIndex = a;
34.
35.             //insert arbitrary char in my position to complete the number of players
36.             current.insert(a, 'x');
37.             System.out.println(current);
38.             //Determine my first formation by checking the status and position of my first opponent
39.             if (((a + 1) % 2 == 0) && (a - 1 >= 0)) {
40.                 player0 = pickWinnerAgainst(current.charAt(a - 1));
41.             } else if (((a + 1) % 2 != 0) && (a < current.length() - 1)) {
42.                 player0 = pickWinnerAgainst(current.charAt(a + 1));
43.             }
44.
45.             //As long as no final winner yet..
46.             while (current.length() > 1) {
47.                 round++;
48.
49.                 //Start the competition..
50.                 for (int i = 0; i < current.length(); i += 2) {
51.
52.                     //if the first player of any pairs is me
53.                     if (i == mIndex) {
54.                         //and I am the last player in the line - hasn't chosen a formation yet
55.                         if (player0 == 'x') {
56.                             //and there at least another opponent to play against yet
57.                             if (winner.length() > 0) {
58.                                 //choose my first winning formation and qualify
59.                                 player0 = pickWinnerAgainst(winner.charAt(0));
60.                                 winner.append(player0);
61.                                 continue;
62.                             } else {
63.                                 //if I'm the only remaining player .. I win with any formation
64.                                 player0 = 'S';
65.                                 winner.append(player0);
66.                                 continue;
67.                             }
68.
69.                         } else {
70.                             //If I am the last player in the line with a formation chosen
71.                             player1 = player0;
72.                             //Ask to pick a winning formation for me as a first player in pair
73.                             mSubIndex = 1;
74.                             isMyTurn = true;
75.                         }
76.
77.                     } else {
78.                         //If I'm not one of the pair
79.                         player1 = current.charAt(i);
80.                     }
81.
82.                     //if the second player of any pairs is me
83.                     if ((i + 1) == mIndex) {
84.                         player2 = player0;
85.                         //Ask to pick a winning formation for me as a second player in pair
86.                         mSubIndex = 2;
87.                         isMyTurn = true;
88.                     } else {
89.                         //If I'm not one of the pair & there's enough players in the line, pick the second player
90.                         if ((i + 1) <= current.length() - 1) {
91.                             player2 = current.charAt(i + 1);
92.                         } else {
93.                             //if I'm the first player in pair and the last player
94.                             if (i == mIndex) {
95.                                 //qualify me
96.                                 winner.append(player1);
97.                                 //save my new position for the next round
98.                                 mIndex = winner.length() - 1;
99.                                 isMyTurn = false;
100.                                 continue;
101.                                 //if the last player has no opponent and wasn't me, qualify him for the next round
102.                             } else {
103.                                 winner.append(player1);
104.                                 continue;
105.                             }
106.                         }
107.                     }
108.
109.                     // if I'm in the pair
110.                     if (isMyTurn) {
111.                         //and I'm player1 in pair
112.                         if (mSubIndex == 1) {
113.                             insertWinner(player1, player2);
114.                             //and I'm player2 in pair
115.                         } else if (mSubIndex == 2) {
116.                             insertWinner(player2, player1);
117.                         }
118.                         //if I'm not in the pair
119.                     } else {
120.                         if (player1 == 'P' && player2 == 'R') {
121.                             winner.append(player1);
122.                         } else if (player1 == 'R' && player2 == 'P') {
123.                             winner.append(player2);
124.                         } else if (player1 == 'S' && player2 == 'P') {
125.                             winner.append(player1);
126.                         } else if (player1 == 'P' && player2 == 'S') {
127.                             winner.append(player2);
128.                         } else if (player1 == 'R' && player2 == 'S') {
129.                             winner.append(player1);
130.                         } else if (player1 == 'S' && player2 == 'R') {
131.                             winner.append(player2);
132.                         }
133.                     }
134.
135.                 }
136.
137.                 //start a new round with the previous round winners
138.                 current = new StringBuilder(winner);
139.                 winner.setLength(0);
140.                 winner = new StringBuilder();
141.             }
142.
143.             return change;
144.         }
145.
146.     }
147.
148.     //check if my current formation help me winning
149.     private static boolean isWinning(char m, char n) {
150.         if (m == 'P' && n == 'R') {
151.             return true;
152.         } else if (m == 'R' && n == 'P') {
153.             return false;
154.         } else if (m == 'S' && n == 'P') {
155.             return true;
156.         } else if (m == 'P' && n == 'S') {
157.             return false;
158.         } else if (m == 'R' && n == 'S') {
159.             return true;
160.         } else if (m == 'S' && n == 'R') {
161.             return false;
162.             //in case of a draw, All lose!
163.         } else {
164.             return false;
165.         }
166.     }
167.
168.     //Advice me with the perfect formation to win
169.     private static char pickWinnerAgainst(char mChar) {
170.         if (mChar == 'P') {
171.             return 'S';
172.
173.         } else if (mChar == 'S') {
174.             return 'R';
175.         } else {
176.             return 'P';
177.         }
178.
179.
180.     }
181.
182.     //Do what's best for me and save my new position in the next round
183.     private static void insertWinner(char playerA, char playerB) {
184.         if (isWinning(playerA, playerB)) {
185.             winner.append(playerA);
186.             mIndex = winner.length() - 1;
187.             isMyTurn = false;
188.         } else {
189.             player0 = pickWinnerAgainst(playerB);
190.             winner.append(player0);
191.             System.out.println(playerA + " -> " + player0 + " @R" + round);
192.             change++;
193.             mIndex = winner.length() - 1;
194.             isMyTurn = false;
195.         }
196.     }
197.
198. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top