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();
        }

    }
}
时间: 2024-10-25 01:59:23

Java纸牌小demo以及日历小demo的相关文章

微信小程序-整理各种小程序源码和资料免费下载

微信小程序整理下载 [小程序源码]微信小程序-车源宝微信版 [小程序源码]小程序-微赞社区(论坛demo) [小程序源码]微信小程序-收支账单 [小程序工具]微信小程序-日历 [小程序源码]小程序-在线聊天功能 [小程序源码]微信小程序-大好商城(新增功能天气查询和2048游戏) [小程序源码]微信小程序-查询号码归属地 [小程序源码]微信小程序-备忘录2 [小程序源码]微信小程序-QQ音乐 [小程序源码]小程序-货币汇率 [小程序源码]微信小程序-大学图书馆 [小程序源码]小程序-积分商城 [

AndroidStudio制作一个简易的订餐交易小demo【日常小练习】

AndroidStudio模拟制作一个简易的订餐交易小demo[日常小练习]     ————安德风 一.最终效果图: 二.布局设计activity_main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/a

小程序源码下载[demo整理自github]

摘自:https://www.cnblogs.com/ytkah/p/9003620.html 微信小程序的火热程度大家都有所了解,也有很多牛人写了不错的小程序,今天ytkah就整理一些github上的小程序开源项目,源码可以直接下载来用,感兴趣的朋友赶紧去看看吧!以下小程序排名按star的数量来排,数字动态变化中,请参考具体的项目 weapp-demo ★727 - 仿豆瓣电影微信小程序 wechat-weapp-gank ★537 - Gank微信小程序 SmallAppForQQ ★460

Calendar日历小程序

//有待完善,有点bugpackage com.sunshine.framework.calendar.model; import java.util.Calendar; /** * * <p> * 描述该类情况 {@link 代表跟谁有关系} * </p> *  * @author 王超 * @since 1.0 * @date 2016年10月20日 下午8:19:15 * @see 新建|修改|放弃 * @see com.sunshine.framework.calendar

Java游戏编程的几个小经验

1.输出图片,最直接的方式是 Image img = Toolkit.getDefaultToolkit().getImage("images/cheer1.jpg"); graphics.drawImage(img, 0, 0, 500, 500, null); 注意:路径是相对于工程根目录的,不是相对于src的 2.深刻理解 repaint()  update()   paint() 三者之间的关系 每一次输出图片,都要先重新获得一次画笔graphics,(通过getGraphic

java 反射和暴力反射 两个DEMO

该类为反射函数 获取和暴力获取ReflectPoin类中的属性 package com.tuozou.test; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class ReflectTest { public ReflectTest() { // TODO Auto-generated con

Java内存管理的9个小技巧

Java内存管理的9个小技巧很多人都说“Java完了,只等着衰亡吧!”,为什么呢?最简单的的例子就是Java做的系统时非常占内存!一听到这样的话,一定会有不少人站出来为Java辩护,并举出一堆的性能测试报告来证明这一点.其实从理论上来讲Java做的系统并不比其他语言开发出来的系统更占用内存,那么为什么却有这么多理由来证明它确实占内存呢?两个字,陋习. 1.别用new Boolean(). 在很多场景中Boolean类型是必须的,比如JDBC中boolean类型的set与get都是通过Boolea

Java 绘制环形的文字 (Circle Text Demo)

1. [代码]CircleTextDemo.java     import java.awt.*;import java.awt.event.*;import java.awt.geom.*; /** * A demo class that illustrates drawing text * along the outside of a circle. */public class CircleTextDemo extends Canvas {    Frame myframe;    Tex

java几个容易出错的小程序

把基本知识过了一遍,发现了几个自己容易 出错的小程序,记录下来.... 1,关于try-catch异常 1,关于try-catch异常 package chapter5; public class p101 { public static void main(String args[]) { int a[]=new int[3]; try{ a[0]=1; a[1]=2; a[2]=3; a[3]=3/0; }catch(ArrayIndexOutOfBoundsException e) { S