啊哈算法,小猫钓鱼,Java实现

啊哈算法,小猫钓鱼,Java实现

接口

public interface IKittenService {int fishing(List<Integer> one, List<Integer> two);}

实现类

public class KittenServiceImpl implements IKittenService {private final Logger log = LoggerFactory.getLogger(IKittenService.class);/*** 小猫钓鱼实现** @param one 玩家1的牌* @param two 玩家2的牌* @return 获胜者*/@Overridepublic int fishing(List<Integer> one, List<Integer> two) {// 玩家手上的牌Queue<Integer> queueOne = new LinkedList<>(one);Queue<Integer> queueTwo = new LinkedList<>(two);// 桌子上的牌Stack<Integer> stack = new Stack<>();int[] boot = new int[14]; // 扑克共14张while (!queueOne.isEmpty() && !queueTwo.isEmpty()) {this.playCards(queueOne, stack, boot);this.playCards(queueTwo, stack, boot);}log.info("玩家1剩余牌:{}", queueOne);log.info("玩家2剩余牌:{}", queueTwo);log.info("桌面剩余牌:{}", stack);return queueOne.isEmpty() ? 2 : 1;}/*** 出牌方法** @param queue 当前出牌人的所有手牌* @param stack 桌面上的所有牌* @param boot 牌面标记*/private void playCards(Queue<Integer> queue, Stack<Integer> stack, int[] boot) {Integer playingCards = queue.peek();if (boot[playingCards] == 1) {while (!stack.peek().equals(playingCards)) {Integer collectedCard = stack.pop();boot[collectedCard] = 0;queue.add(collectedCard);}} else {stack.push(queue.poll());boot[playingCards] = 1;}}}

测试

@Test
public void testFishMan() {IKittenService kittenService = new KittenServiceImpl();Integer[] one = new Integer[] {1, 2, 3, 4, 5, 6};List<Integer> listOne = Arrays.asList(one);Integer[] two = new Integer[] {1, 4, 2, 3, 5, 6};List<Integer> listTwo = Arrays.asList(two);int fishMan = kittenService.fishing(listOne, listTwo);log.info("最后的获胜者是第{}位选手!", fishMan);}

啊哈算法,小猫钓鱼,Java实现

啊哈算法,小猫钓鱼,Java实现

接口

public interface IKittenService {int fishing(List<Integer> one, List<Integer> two);}

实现类

public class KittenServiceImpl implements IKittenService {private final Logger log = LoggerFactory.getLogger(IKittenService.class);/*** 小猫钓鱼实现** @param one 玩家1的牌* @param two 玩家2的牌* @return 获胜者*/@Overridepublic int fishing(List<Integer> one, List<Integer> two) {// 玩家手上的牌Queue<Integer> queueOne = new LinkedList<>(one);Queue<Integer> queueTwo = new LinkedList<>(two);// 桌子上的牌Stack<Integer> stack = new Stack<>();int[] boot = new int[14]; // 扑克共14张while (!queueOne.isEmpty() && !queueTwo.isEmpty()) {this.playCards(queueOne, stack, boot);this.playCards(queueTwo, stack, boot);}log.info("玩家1剩余牌:{}", queueOne);log.info("玩家2剩余牌:{}", queueTwo);log.info("桌面剩余牌:{}", stack);return queueOne.isEmpty() ? 2 : 1;}/*** 出牌方法** @param queue 当前出牌人的所有手牌* @param stack 桌面上的所有牌* @param boot 牌面标记*/private void playCards(Queue<Integer> queue, Stack<Integer> stack, int[] boot) {Integer playingCards = queue.peek();if (boot[playingCards] == 1) {while (!stack.peek().equals(playingCards)) {Integer collectedCard = stack.pop();boot[collectedCard] = 0;queue.add(collectedCard);}} else {stack.push(queue.poll());boot[playingCards] = 1;}}}

测试

@Test
public void testFishMan() {IKittenService kittenService = new KittenServiceImpl();Integer[] one = new Integer[] {1, 2, 3, 4, 5, 6};List<Integer> listOne = Arrays.asList(one);Integer[] two = new Integer[] {1, 4, 2, 3, 5, 6};List<Integer> listTwo = Arrays.asList(two);int fishMan = kittenService.fishing(listOne, listTwo);log.info("最后的获胜者是第{}位选手!", fishMan);}