逻辑非常简单,但我不知道为什么我的代码会获得WA。如果你能指出逻辑上的错误,请帮助。
来源:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
public class UVa_00162 {
static Map<Character, Integer> faceCards = new HashMap<Character, Integer>();
static {
faceCards.put('A', 4);
faceCards.put('K', 3);
faceCards.put('Q', 2);
faceCards.put('J', 1);
}
static Stack<String> dealerHand = new Stack<>();
static Stack<String> nonDealerHand = new Stack<>();
static Stack<String> table = new Stack<>();
static enum GameStatus {
CONTINUE, DEALER_WIN, NONDEALER_WIN
};
static GameStatus status = GameStatus.CONTINUE;
static void play() {
String curCard;
boolean nonDealerTurn = true;
int penalty;
char key;
while (true) {
if (nonDealerHand.size() == 0) {
status = GameStatus.DEALER_WIN;
break;
}
if (dealerHand.size() == 0) {
status = GameStatus.NONDEALER_WIN;
break;
}
curCard = nonDealerTurn ? nonDealerHand.pop() : dealerHand.pop();
table.push(curCard);
nonDealerTurn = !nonDealerTurn;
key = curCard.charAt(1);
if (faceCards.containsKey(key)) {
penalty = faceCards.get(key);
while (penalty > 0) {
if (nonDealerHand.size() == 0 || dealerHand.size() == 0) break;
curCard = nonDealerTurn ? nonDealerHand.pop() : dealerHand.pop();
--penalty;
table.push(curCard);
key = curCard.charAt(1);
if (faceCards.containsKey(key)) {
penalty = faceCards.get(key);
nonDealerTurn = !nonDealerTurn;
}
}
if (nonDealerHand.size() == 0) {
status = GameStatus.DEALER_WIN;
break;
}
if (dealerHand.size() == 0) {
status = GameStatus.NONDEALER_WIN;
break;
}
// place the heap face down under current hand
if (nonDealerTurn) {
while (!table.isEmpty())
dealerHand.insertElementAt(table.remove(0), 0);
nonDealerTurn = false;
} else {
while (!table.isEmpty())
nonDealerHand.insertElementAt(table.remove(0), 0);
nonDealerTurn = true;
}
}
}
}
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s;
while(true) {
s = input.next();
if (s.equals("#")) break;
nonDealerHand.push(s);
dealerHand.push(input.next());
int count = 3;
while (count <= 52) {
nonDealerHand.push(input.next());
++count;
dealerHand.push(input.next());
++count;
}
play();
if (status==GameStatus.DEALER_WIN)
System.out.println("1 " + dealerHand.size());
else
System.out.println("2 " + nonDealerHand.size());
nonDealerHand.clear();
dealerHand.clear();
table.clear();
status = GameStatus.CONTINUE;
}
}
}