码迷,mamicode.com
首页 > 编程语言 > 详细

Java纸牌小demo以及日历小demo

时间:2017-06-24 17:18:29      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:card   next   ons   bre   方法   mon   etc   input   math   

//卡牌类
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

标签:card   next   ons   bre   方法   mon   etc   input   math   

原文地址:http://www.cnblogs.com/tawny-tw/p/7071522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!