Skip to content

Instantly share code, notes, and snippets.

@hidenba
Created March 25, 2016 04:02
Show Gist options
  • Save hidenba/4732b6a786dc9d4149c4 to your computer and use it in GitHub Desktop.
Save hidenba/4732b6a786dc9d4149c4 to your computer and use it in GitHub Desktop.
public class Game {
public static Player matchUp(Player player1, Player player2) {
String left = player1.getStrategy(player2.getStrategySize());
String right = player2.getStrategy(player1.getStrategySize());
for(int i=0; i < left.length(); i++) {
char left_hand = left.charAt(i);
char right_hand = right.charAt(i);
if( left_hand == 'R' && right_hand =='S') {
return player1;
} else if( left_hand == 'S' && right_hand == 'P') {
return player1;
} else if( left_hand == 'P' && right_hand == 'R') {
return player1;
} else if( left_hand == right_hand) {
} else {
return player2;
}
}
return player1;
}
}
public class Player {
private String strategy;
public Player(String strategy) {
this.strategy = strategy;
}
public String getStrategy(int count) {
StringBuilder sb = new StringBuilder(this.strategy);
for(int i=1; i < count; i++) {
sb.append(this.strategy);
}
return sb.toString();
}
public int getStrategySize() {
return this.strategy.length();
}
public String toString() {
return "(" + this.strategy + ")";
}
}
import java.util.regex.*;
import java.util.*;
public class RSP {
public static void main(String[] args) {
test("(RSP)(R)(RPS)(SP)", "(RPS)");
test("(RPS)(R)(RSP)(SP)(RSSP)", "(RSSP)");
test("(RRS)(S)(PSSRP)(PRP)(PSS)", "(PRP)");
test("(PRS)(PSPP)(PRSP)(S)(RR)(SSPR)", "(PRS)");
test("(PSRP)(PR)(RPRPR)(PSSPP)(SP)(SRPP)(PR)", "(SP)");
test("(SPS)(R)(RP)(RRS)(PPRRS)(R)(RS)(RRRRP)", "(PPRRS)");
test("(PPSRPSPRR)(SP)(PPPRSSR)(PS)(P)(PRSPS)(PP)(RSSR)", "(SP)");
test("(SRPRS)(SRPSRS)(SPP)(RSPRS)(S)(SRPSPS)(RSPPSSS)(SRRPRRPSSP)", "(RSPPSSS)");
test("(SRSPSPRS)(RRPRRS)(PRRRRS)(RSSPSSRPS)(PPSSPPRR)(PPSPPS)(PSPSPSSSP)(RPPRPS)", "(PRRRRS)");
test("(S)(PRS)(RSRP)(S)(PPRR)(PP)(RSSS)(P)(RSR)", "(PP)");
test("(RPR)(P)(PSPR)(SRSRP)(SR)(RPPR)(RRS)(S)(SSPR)(PRPR)", "(RPPR)");
test("(PSR)(PPPRR)(S)(SP)(S)(PR)(SPSRP)(PPSRR)(PRPPR)(RRRSP)(SR)", "(S)");
test("(PPRPP)(RSS)(PRS)(R)(RPRP)(SPSSS)(RR)(PPRP)(RSSS)(RSRS)(RP)", "(PPRPP)");
test("(P)(PPPRR)(RRRS)(RR)(RPRSS)(PRSPS)(PP)(R)(PSR)(RPPP)(RP)(SSSR)", "(PSR)");
test("(SR)(P)(RRPRP)(RSPS)(PSS)(SPPSP)(RRPS)(PR)(RRRSR)(PRR)(SSS)(RRRSS)(P)", "(SR)");
test("(PS)(RS)(RR)(RPR)(SR)(SP)(PRP)(PPS)(R)(PRSP)(SSPRR)(SP)(PPR)(RSRR)", "(SSPRR)");
test("(RRRRS)(SRPRR)(PPSS)(SSPPS)(R)(R)(P)(P)(PSSPR)(S)(RRPP)(SPRR)(S)(RR)(S)", "(PSSPR)");
test("(RRPSSRP)(SSSSSP)(RRSPSS)(PRSRRSRP)(SSRRRRR)(SS)(SSSSSSPPRP)(R)(SRRSR)(PPPSRSP)(RPRS)(RSRPPRS)(RPPPPRPR)(PRRSR)(RPRRSR)", "(PPPSRSP)");
test("(SSSRS)(SRPSS)(RSPRP)(RPPPP)(S)(PPRPS)(RRR)(PS)(RPSPS)(SPP)(PSRS)(P)(P)(RR)(S)(PSP)", "(RSPRP)");
test("(SPP)(PR)(SR)(SRPSP)(P)(RR)(SSPP)(RS)(RRRPP)(R)(PRSPS)(RRPP)(RRRSS)(RRRSS)(RSP)(SRPR)(PPS)", "(SPP)");
test("(SSS)(SSPR)(SSRR)(P)(PRRSP)(RRRPP)(PR)(P)(PS)(PPR)(R)(SRPSR)(R)(S)(SSPRS)(SRPR)(PPPR)(SRS)", "(SSRR)");
test("(PR)(R)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PPPSSP)(RRRPSR)(PSR)(SRSRSSR)(RPSSSRP)(RRSPSSSPPR)(RS)(SRRRSPRP)(PR)(RSSRPSSS)(PPRRRRRR)(RRSRP)(RRR)(PSPRSSPRP)(PRPPRSSRP)(SPPSPSS)(PSS)(RPS)(P)(RRSRSP)(PS)(RRPSSSRR)(RR)(PPPSPRPR)(PS)(PRSSRPR)(RRP)(PSRPR)(PS)(R)(RRPP)(SSPPSS)(SRPSSS)(RRSRRPRPP)", "(SPPSPSS)") ;
}
public static void test(String str, String expect) {
Player winer = Tournament.start(parse(str));
System.out.println(winer.toString().equals(expect));
}
public static ArrayList<Player> parse(String str) {
Pattern p = Pattern.compile("[\\w]+");
Matcher m = p.matcher(str);
ArrayList<Player> players = new ArrayList<Player>();
while(m.find()) {
players.add(new Player(m.group()));
}
return players;
}
}
import java.util.*;
public class Tournament {
public static Player start(List<Player> players) {
if (players.size() == 1) {
return players.get(0);
}
double z = (Math.log(players.size()) / Math.log(2));
int count = (int)(Math.pow(2, Math.ceil(z)) - (double)players.size());
List<Player> left = players.subList(0, count);
List<Player> right = players.subList(count, players.size());
left.addAll(game(right));
Tournament.start(left);
return players.get(0);
}
public static List<Player> game(List<Player> players) {
List<Player> winer = new ArrayList<Player>();
for(int i=0; i < players.size(); i+=2) {
winer.add(Game.matchUp(players.get(i), players.get(i+1)));
}
return winer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment