中级农民
- 积分
- 100
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-4-28
- 最后登录
- 1970-1-1
|
- package cardsplay;
- /*54 张牌, 每张牌上 1-9, A-E 各一个数字 + 一个 Letter, 比如 3D, 4F, 5A, 7C; 有一个
- * string 输入,
- 写一个 program 每次读 4 张牌, 1 到 4,如果发现 1 和 4 letter 一样, 丢弃 中间 2 张,在重新读
- 1 号 位置 到 4 号 4 张牌, 如果 1 和 4, number 一样, 4 张全部不要, 如果 又没有 number
- 一样,也没有 letter 一样, 读 2 到 5 号 新的 4 张牌,再比较 first card 和 last card ,
- 做同样的
- 事情, 最后如果全部扔掉, print You Win, 如果手里还剩牌,print You lose 和牌的个数;如果
- 数字相同 就全部丢掉, 如果数字不同,比较字母 ;如果字母相同, 丢中间的 2 张; 如果 字母
- 数字 全部不同, index 到 2 3 4 5 张 card; 做一样的事情; 但是只要丢牌 index 就 重新 开始
- */
- import java.util.*;
- public class CardsPlay {
- public static void play(String heap){
- if (heap.length()!=54*2 || heap==null ) return;
- List<Card> hand = new ArrayList<Card>();
- for (int i=0; i<54; i++){
- String curr_str = heap.substring(2*i,2*i+2);
- hand.add(new Card(Integer.parseInt(curr_str.substring(0,1)),curr_str.charAt(1)));
- }
- // till now, we get an array of cards.
- int begin=0;
- boolean change=true;
- while (begin+3<hand.size() || change){
- int action = compare(hand,begin);
- switch(action){
- case 0:
- change=false;
- begin++;
- break;
- case 1:
- for (int i=0; i<4; i++){
- hand.remove(begin);
- }
- change=true;
- begin=0;
- break;
- case 2:
- hand.remove(begin+1);
- hand.remove(begin+1);
- begin=0;
- change=true;
- break;
- }
- }
- if (hand.size()>0){
- System.out.println("You Lose!");
- System.out.println("There are "+hand.size()+" cards left.");
- }
- else System.out.println("You Win!");
- }
-
- public static int compare(List<Card> hand, int begin){
- if (hand.size()<4) return 0;
- Card card1 = hand.get(begin);
- Card card4 = hand.get(begin+3);
- if (card1.getNUm()==card4.getNUm()) return 1;
- if (card1.getLetter()==card4.getLetter()) return 2;
- else return 0;
- }
-
- public static String generate(){
- StringBuilder sb = new StringBuilder();
- boolean[][] candidate = new boolean[6][9];
- for (int i=0; i<6; i++) {Arrays.fill(candidate[i],false);}
- while (sb.length()<108){
- int letter=(int)(Math.random()*10);
- int num = (int)(Math.random()*10);
- if (letter<6 && num>=1 && num<=9 && !candidate[letter][num-1]){
- sb.append(num);
- sb.append((char)('A'+letter));
- }
- }
- return sb.toString();
- }
-
- public static void main(String[]args){
- String test = generate();
- play(test);
- }
-
- public static boolean compare_discard(List<Card> hand, int begin){
- Card card1 = hand.get(begin);
- Card card4 = hand.get(begin+3);
- if (card1.getNUm()==card4.getNUm()){
- for (int i=0; i<4; i++){
- hand.remove(begin); // attention: the capacity always change when removed.
- }
- return true;
- }
- else if (card1.getLetter()==card4.getLetter()){
- hand.remove(begin+1);
- hand.remove(begin+1);
- return true;
- }
- else return false;
- }
- }
- class Card{
- private int num;
- private char letter;
- public Card(int num, char letter){
- this.num=num;
- this.letter=letter;
- }
- public int getNUm(){
- return this.num;
- }
- public char getLetter(){
- return this.letter;
- }
- }
复制代码 |
|