Skip to content

Instantly share code, notes, and snippets.

@vchatela
Created February 10, 2016 18:12
Show Gist options
  • Select an option

  • Save vchatela/c9a28e7dd489519adb09 to your computer and use it in GitHub Desktop.

Select an option

Save vchatela/c9a28e7dd489519adb09 to your computer and use it in GitHub Desktop.
Codingame : Winamax Sponsored Contest
import java.util.*;
import java.io.*;
import java.math.*;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
class Solution {
private static final String NOBODY_WINS = "PAT";
private static final String PLAYER_1_WIN = "1";
private static final String PLAYER_2_WIN = "2";
private static class Player{
private final Queue<Card> cardsToPlay;
private final Queue<Card> cardsOnStack;
private Player(Queue<Card> cards){
this.cardsToPlay = cards;
this.cardsOnStack = new LinkedList<>();
}
public Card playCard(){
Card card = cardsToPlay.poll();
cardsOnStack.offer(card);
return card;
}
public boolean hasCardsToPlay(){
return cardsToPlay.size() >0;
}
public boolean hasEnoughCardsForWar() {
return cardsToPlay.size() >= 4;
}
public void giveUp() {
cardsToPlay.clear();
}
public void prepareForWar() {
for (int i = 0; i < 3; i++) {
playCard();
}
}
public void collectWinningsFromPlayer(Player looser) {
while (!looser.cardsOnStack.isEmpty()) {
this.cardsToPlay.offer(looser.cardsOnStack.poll());
}
}
}
private static class Card{
private final int value;
private Card(int val){
this.value = val;
}
private Card(String sValue){
String val = sValue.substring(0,sValue.length() -1);
switch(val){
case"J" : this.value = 11;
break;
case"Q" : this.value = 12;
break;
case"K" : this.value = 13;
break;
case"A" : this.value = 14;
break;
default : this.value = Integer.parseInt(val);
}
}
private int getValue(){
return value;
}
}
private static class GameEngine{
private int rounds;
private final Player player1;
private final Player player2;
private GameEngine(Player player1, Player player2) {
this.player1 = player1;
this.player2 = player2;
}
public String play() {
while (player1.hasCardsToPlay() && player2.hasCardsToPlay()) {
playBattle();
rounds++;
}
return getWinnerAndRounds();
}
private void playBattle() {
Card player1Card = player1.playCard();
Card player2Card = player2.playCard();
fight(player1Card, player2Card);
}
private void fight(Card player1Card, Card player2Card) {
if (player1Card.getValue() > player2Card.getValue()) {
assignWinningsToPlayer(player1);
} else if (player1Card.getValue() < player2Card.getValue()) {
assignWinningsToPlayer(player2);
} else {
playWar();
}
}
private void assignWinningsToPlayer(Player winner) {
winner.collectWinningsFromPlayer(player1);
winner.collectWinningsFromPlayer(player2);
}
private void playWar(){
if(isWarDoable()){
prepareForWar();
playBattle();
}
else {
forceGameToEnd();
}
}
private boolean isWarDoable() {
return player1.hasEnoughCardsForWar() && player2.hasEnoughCardsForWar();
}
private void prepareForWar() {
player1.prepareForWar();
player2.prepareForWar();
}
private void forceGameToEnd() {
player1.giveUp();
player2.giveUp();
}
private String getWinnerAndRounds() {
if (player1.hasCardsToPlay()) {
return PLAYER_1_WIN + " " + rounds;
} else if (player2.hasCardsToPlay()) {
return PLAYER_2_WIN + " " + rounds;
} else {
return NOBODY_WINS;
}
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
Queue<Card> play1Cards = new LinkedList<>();
int n = in.nextInt(); // the number of cards for player 1
for (int i = 0; i < n; i++) {
String cardp1 = in.next(); // the n cards of player 1
play1Cards.offer(new Card(cardp1));
}
Queue<Card> play2Cards = new LinkedList<>();
int m = in.nextInt(); // the number of cards for player 2
for (int i = 0; i < m; i++) {
String cardp2 = in.next(); // the m cards of player 2
play2Cards.offer(new Card(cardp2));
}
Player play1 = new Player(play1Cards);
Player play2 = new Player(play2Cards);
GameEngine game = new GameEngine(play1,play2);
System.out.println(game.play());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment