Java纸牌小demo以及日历小demo

Posted 雲淡、風輕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java纸牌小demo以及日历小demo相关的知识,希望对你有一定的参考价值。

//卡牌类
public class Card {
    //定义卡牌的点数
    public static final String[] cardName = { "3", "4", "5", "6", "7", "8",
            "9", "10", "J", "Q", "K", "A", "2" };
    //定义卡牌的花色
    public static final String[] flowerColor = { "?", "?", "?", "?" };

    private int number;// 表示卡牌点数的下标 0-12;

    private int color;
    public Card(int number, int color) {
        this.number = number;
        this.color = color;
    }

    public int getNumber() {
        return number;
    }

    public int getColor() {
        return color;
    }

    // 显示牌的内容
    public String getCard() {
        return flowerColor[color] + cardName[number];
    }
}


import java.util.Random;
//游戏类
public class Game {
    //定义牌的张数
    private Card[] cards = new Card[52];
        
    public Game() {
        // 1. 创建牌
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 13; j++) {
                cards[i * 13 + j] = new Card(j, i);
            }
        }
    }
    //显示卡牌
    public void showCards() {
        for (int i = 0; i < cards.length; i++) {
            System.out.print(cards[i].getCard() + " "
                    + (i % 13 == 12 ? "\n" : ","));
        }
    }

    public Card[][] deal() {
        Card[][] cardss = new Card[4][13];
        for (int i = 0; i < 52; i++) {
            // 二维数组使用一维角标
            cardss[i / 13][i % 13] = cards[i];
        }
        return cardss;
    }

    public void shuffle() {
// List<Card> list = Arrays.asList(cards); // Collections.shuffle(list); // list.toArray(cards); // for (int i = 0; i < cards.length; i++) { // System.out.print(cards[i].getCard() + " " // + (i % 13 == 12 ? "\n" : ",")); // }
//2.排序 随机两两交换。 Random random = new Random(); for (int i = 0; i < cards.length; i++) { int index = random.nextInt(52); if (i != index) { Card temp = cards[i]; cards[i] = cards[index]; cards[index] = temp; } } } }

 

import java.util.Arrays;
import java.util.Comparator;
//玩家类
public class Player {

    private Card[] cards = new Card[13];
    private String name;

    public Player(String name, Card[] cards) {
        this.name = name;
        this.cards = cards;
    }

    public void showCard() {
        System.out.print("玩家" + name + ":");
        for (int i = 0; i < cards.length; i++) {
            System.out.print(cards[i].getCard() + ",");
        }
    }

    // 排序
    public void sort() {
        Arrays.sort(cards, new Comparator<Card>() {

            @Override
            public int compare(Card o1, Card o2) {
                return o1.getNumber() - o2.getNumber();
            }
        });
    }

}

 

public class Work {

    public static void main(String[] args) {
        // 1. 定义纸牌类 分别为A 2-10 J Q K 花色为???? 大王小王
        // 定义游戏类,其中有一副扑克,共52张牌 随机洗牌
        // 定义玩家类,每个玩家可以发到13张牌。创建4个玩家对象,打印他们手中的牌
        Game game = new Game();
        game.showCards();
        game.shuffle();
        System.out.println();
        game.showCards();
        Card[][] cardss = game.deal();
        
        Player[] players = new Player[4];
        
        for (int i = 0; i < 4; i++) {
            players[i] = new Player("玩家" + i, cardss[i]);
            players[i].sort();
        }
        for (int i = 0; i < players.length; i++) {
            players[i].showCard();
            System.out.println();
        }
    }
}

 

 

--石头剪刀布,三局两胜制

//玩家类
public class Player {

    private String name;

    public Player(String name) {
        this.name = name;
    }

    public static final int BU = 1;

    public static final int QUAN = 2;

    public static final int JIANDAO = 3;

    //随机出剪刀石头布
    public int chuQuan() {
        return (int) (Math.random() * 2 + 1);
    }

    public String getName() {
        return name;
    }
}

 

//裁判类
public class Referee {

    // 记录胜负
    private String[] win = new String[3];
    private int number = 0;

    // 裁判胜负
    public boolean judge(Player a, Player b) {
        int comeA = a.chuQuan();
        int comeB = b.chuQuan();
        int result = isWin(comeA, comeB);
        switch (result) {
        case 0:
            System.out.println("平手");
            break;
        case 1:
            // alt+shift+m
            showWin(a);
            break;
        case -1:
            showWin(b);
            break;
        }

        // 结束
        if (number == 3)
            return true;
        else if (number == 2 && win[0].equals(win[1]))
            return true;
        else
            return false;
    }

    private void showWin(Player a) {
        System.out.println(a.getName() + "获胜");
        win[number] = a.getName();
        number++;
    }

    // 0 平手
    // 1 comeA 胜利
    // -1 comeA 失败
    private int isWin(int comeA, int comeB) {
        if (comeA == comeB) {
            return 0;
        }
        switch (comeA) {
        case Player.BU:
            if (comeB == Player.JIANDAO)
                return -1;
            else
                return 1;
        case Player.JIANDAO:
            if (comeB == Player.QUAN)
                return -1;
            else
                return 1;
        case Player.QUAN:
            if (comeB == Player.BU)
                return -1;
            else {
                return 1;
            }
        }
        return 0;
    }
}

 

public class Test {

    public static void main(String[] args) {
        Referee referee = new Referee();
        Player playerA = new Player("张三");
        Player playerB = new Player("李四");
        System.out.println("游戏开始:");
        while (!referee.judge(playerA, playerB)) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("开始下一局");
        }
    }
}

 

--打印日历

import java.util.Calendar;
//日历类
public class Solar {

    private int year, month, day;
    Calendar calendar = Calendar.getInstance();

    public Solar(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public void showCal() {
        // 填充方法
        System.out.println("*******" + year + "-" + month + "*******");
        System.out.println("周日\t周一\t周二\t周三\t周四\t周五\t周六");
        // 上月的最后几天
        int upMonth = upMonthLastDay(month);// 31 4 28
        int week = monthOneDayOfWeek();
        int monthLength = upMonthLastDay(month + 1);
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                if (i * 7 + j < week) {
                    // 抬头
                    int upMothValue = upMonth - week + 1 + j;
                    System.out.print(upMothValue + "\t");
                } else {
                    int monthValue = i * 7 + j - week + 1;
                    if (monthValue <= monthLength) {
                        // 正文
                        System.out.print(monthValue + "\t");
                    } else {
                        // 尾部
                        System.out.print((monthValue - monthLength) + "\t");
                    }
                }

            }
            System.out.println();
        }

    }

    // 这个月的第一天是周几
    private int monthOneDayOfWeek() {
        // set用来修改日历
        calendar.set(year, month - 1, 1);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        return week - 1;
    }

    // 上一月有多少天
    private int upMonthLastDay(int month) {
        if (month == 1) {
            return 31;
        } else if (month == 3) {
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                return 29;
            else {
                return 28;
            }
        } else if (month == 2 || month == 4 || month == 6 || month == 8
                || month == 9 || month == 11)
            return 31;
        else
            return 30;
    }

}

 

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // System.out.println("请输入需要查询的年月日 格式1970-01-01");
        // Scanner input = new Scanner(System.in);
        // String str = input.next();
        // String[] array = str.split("-");
        // // 转换int 包装类
        // int year = Integer.parseInt(array[0]);
        // int month = Integer.parseInt(array[1]);
        // int day = Integer.parseInt(array[2]);

        for (int i = 1; i < 13; i++) {
            Solar solar = new Solar(2017, i, 1);
            solar.showCal();
            System.out.println();
        }

    }
}

 

以上是关于Java纸牌小demo以及日历小demo的主要内容,如果未能解决你的问题,请参考以下文章

每天一个JS 小demo之日历制作。主要知识点:日期函数和对于函数封装的灵活运用

JAVA的程序代码小细节,变量的使用,以及一些细节的面试题

java之Spring学习之前的一个小demo

正则中关于修饰符g以及exec和match区别的一个小demo

微信小程序横版日历,tab栏

数组的使用eclipse调试程序练习小demo以及方法的定义和重载