Java学习日记—模拟斗地主发牌(牌存在List)

需求:
Java学习日记—模拟斗地主发牌(牌存在List)

实现:

package com.day1104;

import java.util.ArrayList;
import java.util.Collections;

public class Game {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList<String> list = new ArrayList<String>();
		//把牌加入List
		for(int i = 0; i<54; i++) {
			if(i < 28) {   //3-10
				list.add(i, ""+(i/4+3));
			}else if(i < 32) { //10
				list.add(i, "X");
			}else if(i < 36) { //J
				list.add(i, "J");
			}else if(i < 40) {  //Q
				list.add(i, "Q");
			}else if(i < 44) {  //K
				list.add(i, "K");
			}else if(i < 48) {  //A
				list.add(i, "A");
			}else if(i < 52) {  //2
				list.add(i, "2");
			}else if(i == 52){
				list.add(i, "小王");
			}else {
				list.add(i, "大王");
			}
		}
		
		//洗牌
		Collections.shuffle(list);
//		for(String temp: list) {
//			System.out.print(temp);
//		}
		//摸牌
		ArrayList<String> list1 = new ArrayList<String>();
		ArrayList<String> list2 = new ArrayList<String>();
		ArrayList<String> list3 = new ArrayList<String>();
		for(int i =0; i<list.size()-3; ) {
				list1.add(list.get(i));
				i++;
				list2.add(list.get(i));
				i++;
				list3.add(list.get(i));
				i++;
		}
		
		//查看牌
		System.out.println("玩家1的牌:");
		Collections.sort(list1);
		for(String temp: list1) {
			System.out.print(temp);
		}
		System.out.println();
		
		System.out.println("玩家2的牌:");
		Collections.sort(list2);
		for(String temp: list2) {
			System.out.print(temp);
		}
		System.out.println();
		
		System.out.println("玩家3的牌:");
		Collections.sort(list3);
		for(String temp: list3) {
			System.out.print(temp);
		}
		System.out.println();
		
		System.out.println("底牌:");
		for(int i =list.size()-1; i>list.size()-4; i--) {
			System.out.print(list.get(i));
		}
		
	}

}

Java学习日记—模拟斗地主发牌(牌存在List)

匿名

发表评论

匿名网友