目录
一、斗地主游戏1 -- 准洗发(控制台版)
1、准备牌
2、洗牌
3、发牌lord:地主牌(3张)
4、看牌
编辑
二、斗地主游戏2 -- 给牌排序1(利用序号进行排序)
1、准备牌
2、洗牌
3、发牌
4、看牌
三、斗地主游戏2 -- 给牌排序2(给每一张牌计算价值)
1、准备牌
2、洗牌
3、发牌
4、排序
5、看牌
四、斗地主游戏3 (页面版)
1、登录页面
2、游戏界面
3、面向对象设计
4、游戏完善
PokerGame():
//牌盒
//♥3 ♣3
static ArrayList list = new ArrayList<>();
//静态代码块
//特点:随着类的加载而在加载的,而且只执行一次。
static {
//准备牌
// "♦", "♣", "♥", "♠"
// "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"
String[] color = {"♦", "♣", "♥", "♠" };
String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
for (String c : color) {
//c依次表示每一种花色
for (String n : number) {
//n 依次表示每一个数字
list.add(c + n);
}
}
list.add("小王");
list.add("大王");
}
//洗牌
Collections.shuffle(list);
//发牌
ArrayList lord = new ArrayList<>();
ArrayList player1 = new ArrayList<>();
ArrayList player2 = new ArrayList<>();
ArrayList player3 = new ArrayList<>();
//遍历牌盒得到每一张牌
for (int i = 0; i < list.size(); i++) {
//i:索引
String poker = list.get(i);
if(i <= 2){
lord.add(poker);
continue;
}
//给三个玩家轮流发牌
if(i % 3 == 0){
player1.add(poker);
}else if(i % 3 == 1){
player2.add(poker);
}else{
player3.add(poker);
}
}
//看牌
lookPoker("底牌",lord);
lookPoker("钢脑壳",player1);
lookPoker("大帅比",player2);
lookPoker("蛋筒",player3);
}
/*
* 参数一:玩家的名字
* 参数二:每位玩家的牌
* */
public void lookPoker(String name, ArrayList list){
System.out.print(name + ": ");
for (String poker : list) {
System.out.print(poker + " ");
}
System.out.println();
}
总代码:
import java.util.ArrayList;
import java.util.Collections;
public class PokerGame {
//牌盒
//♥3 ♣3
static ArrayList list = new ArrayList<>();
//静态代码块
//特点:随着类的加载而在加载的,而且只执行一次。
static {
//准备牌
// "♦", "♣", "♥", "♠"
// "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"
String[] color = {"♦", "♣", "♥", "♠" };
String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
for (String c : color) {
//c依次表示每一种花色
for (String n : number) {
//n 依次表示每一个数字
list.add(c + n);
}
}
list.add("小王");
list.add("大王");
}
public PokerGame(){
//洗牌
Collections.shuffle(list);
//发牌
ArrayList lord = new ArrayList<>();
ArrayList player1 = new ArrayList<>();
ArrayList player2 = new ArrayList<>();
ArrayList player3 = new ArrayList<>();
//遍历牌盒得到每一张牌
for (int i = 0; i < list.size(); i++) {
//i:索引
String poker = list.get(i);
if(i <= 2){
lord.add(poker);
continue;
}
//给三个玩家轮流发牌
if(i % 3 == 0){
player1.add(poker);
}else if(i % 3 == 1){
player2.add(poker);
}else{
player3.add(poker);
}
}
//看牌
lookPoker("底牌",lord);
lookPoker("钢脑壳",player1);
lookPoker("大帅比",player2);
lookPoker("蛋筒",player3);
}
/*
* 参数一:玩家的名字
* 参数二:每位玩家的牌
* */
public void lookPoker(String name, ArrayList list){
System.out.print(name + ": ");
for (String poker : list) {
System.out.print(poker + " ");
}
System.out.println();
}
}
以谁为主导,谁作为键
先让序号和数据产生对应关系,一一对应,打乱序号,再通过序号找到原始数据即可
package com.yaqi.doudizhu2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class PokerGame {
//牌盒 Map
//此时我们只要把牌跟序号产生对应关系就可以了,不需要按照序号进行排序,所以只要HashMap就可以了
static HashMap hm = new HashMap<>();
static ArrayList list = new ArrayList<>();
static {
String[] color = {"♦", "♣", "♥", "♠"};
String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
//序号
int serialNumber = 1;
//细节
for (String n : number) {
//依次表示每一个数字
for (String c : color) {
//依次表示每一个花色
hm.put(serialNumber, c + n);
list.add(serialNumber);
serialNumber++;
}
}
hm.put(serialNumber, "小王");
list.add(serialNumber);
serialNumber++;
hm.put(serialNumber, "大王");
list.add(serialNumber);
}
public PokerGame() {
//洗牌
Collections.shuffle(list);
//发牌
TreeSet lord = new TreeSet<>();
TreeSet player1 = new TreeSet<>();
TreeSet player2 = new TreeSet<>();
TreeSet player3 = new TreeSet<>();
for (int i = 0; i < list.size(); i++) {
//i :依次表示集合中的每一个索引
//list.get(i)元素:牌的序号
int serialNumber = list.get(i);
if(i <= 2){
lord.add(serialNumber);
continue;
}
if(i % 3 == 0){
player1.add(serialNumber);
}else if(i % 3 == 1){
player2.add(serialNumber);
}else{
player3.add(serialNumber);
}
}
//看牌
lookPoker("底牌",lord);
lookPoker("钢脑壳",player1);
lookPoker("大帅比",player2);
lookPoker("蛋筒",player3);
}
/*
* 参数一:玩家的名字
* 参数二:牌的序号
* */
public void lookPoker(String name, TreeSet ts){
System.out.print(name + ": ");
//遍历TreeSet集合得到每一个序号,再拿着序号到Map集合中去找真正的牌
for (int serialNumber : ts) {
String poker = hm.get(serialNumber);
System.out.print(poker + " ");
}
System.out.println();
}
}
//洗牌
Collections.shuffle(list);
TreeSet
//发牌
TreeSet lord = new TreeSet<>();
TreeSet player1 = new TreeSet<>();
TreeSet player2 = new TreeSet<>();
TreeSet player3 = new TreeSet<>();
for (int i = 0; i < list.size(); i++) {
//i :依次表示集合中的每一个索引
//list.get(i)元素:牌的序号
int serialNumber = list.get(i);
if(i <= 2){
lord.add(serialNumber);
continue;
}
if(i % 3 == 0){
player1.add(serialNumber);
}else if(i % 3 == 1){
player2.add(serialNumber);
}else{
player3.add(serialNumber);
}
}
//看牌
lookPoker("底牌",lord);
lookPoker("钢脑壳",player1);
lookPoker("大帅比",player2);
lookPoker("蛋筒",player3);
}
/*
* 参数一:玩家的名字
* 参数二:牌的序号
* */
public void lookPoker(String name, TreeSet ts){
System.out.print(name + ": ");
//遍历TreeSet集合得到每一个序号,再拿着序号到Map集合中去找真正的牌
for (int serialNumber : ts) {
String poker = hm.get(serialNumber);
System.out.print(poker + " ");
}
System.out.println();
}
//牌盒
static ArrayList list = new ArrayList<>();
//创建一个集合,用来添加牌的价值
static HashMap hm = new HashMap<>();
static {
//准备牌
String[] color = {"♦", "♣", "♥", "♠"};
String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
for (String c : color) {
for (String n : number) {
list.add(c + n);
}
}
list.add(" 小王");
list.add(" 大王");
//指定牌的价值
//牌上的数字到Map集合中判断是否存在
//存在,获取价值
//不存在,本身的数字就是价值
hm.put("J", 11);
hm.put("Q", 12);
hm.put("K", 13);
hm.put("A", 14);
hm.put("2", 15);
hm.put("小王", 50);
hm.put("大王", 100);
}
//洗牌
Collections.shuffle(list);
//发牌
ArrayList lord = new ArrayList<>();
ArrayList player1 = new ArrayList<>();
ArrayList player2 = new ArrayList<>();
ArrayList player3 = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
String poker = list.get(i);
//发底牌
if (i <= 2) {
lord.add(poker);
continue;
}
//给三个玩家轮流发牌
if (i % 3 == 0) {
player1.add(poker);
} else if (i % 3 == 1) {
player2.add(poker);
} else {
player3.add(poker);
}
}
//排序
order(lord);
order(player1);
order(player2);
order(player3);
//看牌
lookPoker("底牌",lord);
lookPoker("钢脑壳",player1);
lookPoker("大帅比",player2);
lookPoker("蛋筒",player3);
}
/*
* 参数一:玩家的名字
* 参数二:每位玩家的牌
* */
public void lookPoker(String name, ArrayList list){
System.out.print(name + ": ");
for (String poker : list) {
System.out.print(poker + " ");
}
System.out.println();
}
//利用牌的价值进行排序
//参数:集合
//♥5 ♥3 ♥6 ♥7 ♥9
public void order(ArrayList list){
Collections.sort(list, new Comparator() {
//Array.sort (插入排序 + 二分查找)
@Override
public int compare(String o1, String o2) {
//o1:表示当前要插入到有序序列中的牌
//o2:表示已经在有序序列中存在的牌
//负数:o1小 插入到前面
//正数:o1大 插入到后面
//0:o1的数字跟o2的数字是一样的,需要按照花色再次排序
//1.计算o1的花色和价值 大王
String color1 = o1.substring(0, 1);
int value1 = getValue(o1);
//2.计算o2的花色和价值
String color2 = o2.substring(0, 1);
int value2 = getValue(o2);
//3.比较o1和o2的价值 ♥3 ♠3
int i = value1 - value2;
return i == 0 ? color1.compareTo(color2) : i;
}
});
}
//计算牌的价值
//参数:牌
//返回值:价值
public int getValue(String poker){//♥3
//获取牌上的数字
String number = poker.substring(1);//把这里截取出来的结果,让这个结果再Map集合中存在 “ 大王”
//拿着数字到map集合中判断是否存在
if(hm.containsKey(number)){
//存在,获取价值
return hm.get(number);
}else{
//不存在,类型转换
return Integer.parseInt(number);
}
}
package com.yaqi.domain;
import java.util.Objects;
public class User {
private String username;
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
/**
* 获取
* @return username
*/
public String getUsername() {
return username;
}
/**
* 设置
* @param username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* 获取
* @return password
*/
public String getPassword() {
return password;
}
/**
* 设置
* @param password
*/
public void setPassword(String password) {
this.password = password;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(username, user.username) && Objects.equals(password, user.password);
}
@Override
public int hashCode() {
return Objects.hash(username, password);
}
public String toString() {
return "User{username = " + username + ", password = " + password + "}";
}
}
LoginJFrame:
package com.yaqi.game;
import com.yaqi.domain.User;
import com.yaqi.util.CodeUtil;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
public class LoginJFrame extends JFrame implements MouseListener {
static ArrayList allUsers = new ArrayList<>();
static {
allUsers.add(new User("zhangsan","123"));
allUsers.add(new User("lisi","1234"));
}
JButton login = new JButton();
JButton register = new JButton();
JTextField username = new JTextField();
JPasswordField password = new JPasswordField();
JTextField code = new JTextField();
//正确的验证码
JLabel rightCode = new JLabel();
public LoginJFrame() {
//初始化界面
initJFrame();
//初始化组件,在这个界面中添加内容
initView();
//让当前界面显示出来
this.setVisible(true);
}
//在这个界面中添加内容
public void initView() {
//1. 添加用户名文字
Font usernameFont = new Font(null,1,16);
JLabel usernameText = new JLabel("用户名");
usernameText.setForeground(Color.white);
usernameText.setFont(usernameFont);
usernameText.setBounds(140, 55, 55, 22);
this.getContentPane().add(usernameText);
//2.添加用户名输入框
username.setBounds(223, 46, 200, 30);
this.getContentPane().add(username);
//3.添加密码文字
JLabel passwordText = new JLabel("密码");
Font passwordFont = new Font(null,1,16);
passwordText.setForeground(Color.white);
passwordText.setFont(passwordFont);
passwordText.setBounds(197, 95, 40, 22);
this.getContentPane().add(passwordText);
//4.密码输入框
password.setBounds(263, 87, 160, 30);
this.getContentPane().add(password);
//验证码提示
JLabel codeText = new JLabel("验证码");
Font codeFont = new Font(null,1,16);
codeText.setForeground(Color.white);
codeText.setFont(codeFont);
codeText.setBounds(215, 142, 55, 22);
this.getContentPane().add(codeText);
//验证码的输入框
code.setBounds(291, 133, 100, 30);
this.getContentPane().add(code);
//获取正确的验证码
String codeStr = CodeUtil.getCode();
Font rightCodeFont = new Font(null,1,15);
//设置颜色
rightCode.setForeground(Color.RED);
//设置字体
rightCode.setFont(rightCodeFont);
//设置内容
rightCode.setText(codeStr);
//绑定鼠标事件
rightCode.addMouseListener(this);
//位置和宽高
rightCode.setBounds(400, 133, 100, 30);
//添加到界面
this.getContentPane().add(rightCode);
//5.添加登录按钮
login.setBounds(123, 310, 128, 47);
login.setIcon(new ImageIcon("farmerandlord\\image\\login\\登录按钮.png"));
//去除按钮的边框
login.setBorderPainted(false);
//去除按钮的背景
login.setContentAreaFilled(false);
//给登录按钮绑定鼠标事件
login.addMouseListener(this);
this.getContentPane().add(login);
//6.添加注册按钮
register.setBounds(256, 310, 128, 47);
register.setIcon(new ImageIcon("farmerandlord\\image\\login\\注册按钮.png"));
//去除按钮的边框
register.setBorderPainted(false);
//去除按钮的背景
register.setContentAreaFilled(false);
//给注册按钮绑定鼠标事件
register.addMouseListener(this);
this.getContentPane().add(register);
//7.添加背景图片
JLabel background = new JLabel(new ImageIcon("farmerandlord\\image\\login\\background.png"));
background.setBounds(0, 0, 633, 423);
this.getContentPane().add(background);
}
//初始化组件,在这个界面中添加内容
public void initJFrame() {
this.setSize(633, 423);//设置宽高
this.setTitle("斗地主游戏 V1.0登录");//设置标题
this.setDefaultCloseOperation(3);//设置关闭模式
this.setLocationRelativeTo(null);//居中
this.setAlwaysOnTop(true);//置顶
this.setLayout(null);//取消内部默认布局
}
//点击
@Override
public void mouseClicked(MouseEvent e) {
Object obj = e.getSource();
if (obj == login) {
//获取两个文本输入框中的内容
String usernameInput = username.getText();
String passwordInput = password.getText();
//获取用户输入的验证码
String codeInput = code.getText();
//判断验证码是否为空
if (codeInput.length() == 0) {
showJDialog("验证码不能为空");
return;
}
//判断用户名和密码是否为空
if (usernameInput.length() == 0 || passwordInput.length() == 0) {
showJDialog("用户名或者密码为空");
return;
}
//判断验证码是否正确
if (!codeInput.equalsIgnoreCase(rightCode.getText())) {
showJDialog("验证码输入错误");
return;
}
//判断集合中是否包含当前用户对象
//其实就是验证用户名和密码是否相同
//contains底层是依赖equals方法判断的,所以需要重写equals方法
User userInfo = new User(usernameInput, passwordInput);
if (allUsers.contains(userInfo)) {
//关闭当前登录界面
this.setVisible(false);
//打开游戏的主界面
new GameJFrame();
} else {
showJDialog("用户名或密码错误");
}
} else if (obj == register) {
System.out.println("点击了注册按钮");
} else if (obj == rightCode) {
//获取一个新的验证码
String code = CodeUtil.getCode();
rightCode.setText(code);
}
}
//展示弹框
public void showJDialog(String content) {
//创建一个弹框对象
JDialog jDialog = new JDialog();
//给弹框设置大小
jDialog.setSize(200, 150);
//让弹框置顶
jDialog.setAlwaysOnTop(true);
//让弹框居中
jDialog.setLocationRelativeTo(null);
//弹框不关闭永远无法操作下面的界面
jDialog.setModal(true);
//创建Jlabel对象管理文字并添加到弹框当中
JLabel warning = new JLabel(content);
warning.setBounds(0, 0, 200, 150);
jDialog.getContentPane().add(warning);
//让弹框展示出来
jDialog.setVisible(true);
}
//按下不松
@Override
public void mousePressed(MouseEvent e) {
if (e.getSource() == login) {
login.setIcon(new ImageIcon("farmerandlord\\image\\login\\登录按下.png"));
} else if (e.getSource() == register) {
register.setIcon(new ImageIcon("farmerandlord\\image\\login\\注册按下.png"));
}
}
//松开按钮
@Override
public void mouseReleased(MouseEvent e) {
if (e.getSource() == login) {
login.setIcon(new ImageIcon("farmerandlord\\image\\login\\登录按钮.png"));
} else if (e.getSource() == register) {
register.setIcon(new ImageIcon("farmerandlord\\image\\login\\注册按钮.png"));
}
}
//鼠标划入
@Override
public void mouseEntered(MouseEvent e) {
}
//鼠标划出
@Override
public void mouseExited(MouseEvent e) {
}
}
GameJFrame:
package com.yaqi.game;
import com.yaqi.domain.Poker;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class GameJFrame extends JFrame implements ActionListener {
//获取界面中的隐藏容器,现在统一获取了,后面直接用就可以了
public static Container container = null;
//管理抢地主和不抢两个按钮
JButton landlord[] = new JButton[2];
//管理出牌和不要两个按钮
JButton publishCard[] = new JButton[2];
//游戏界面中地主的图标
JLabel dizhu;
//集合嵌套集合
//大集合中有三个小集合
//小集合中装着每一个玩家当前要出的牌
//0索引:左边的电脑玩家
//1索引:中间的自己
//2索引:右边的电脑玩家
ArrayList> currentList = new ArrayList<>();
//集合嵌套集合
//大集合中有三个小集合
//小集合中装着每一个玩家手上的牌
//0索引:左边的电脑玩家
//1索引:中间的自己
//2索引:右边的电脑玩家
ArrayList> playerList = new ArrayList<>();
//底牌
ArrayList lordList = new ArrayList<>();
//牌盒,装所有的牌
ArrayList pokerList = new ArrayList();
//三个玩家前方的文本提示
//0索引:左边的电脑玩家
//1索引:中间的自己
//2索引:右边的电脑玩家
JTextField time[] = new JTextField[3];
public GameJFrame() {
//设置任务栏的图标
setIconImage(Toolkit.getDefaultToolkit().getImage("farmerandlord\\image\\poker\\dizhu.png"));
//设置界面
initJframe();
//添加组件
initView();
//界面显示出来
//先展示界面再发牌,因为发牌里面有动画,界面不展示出来,动画无法展示
this.setVisible(true);
//初始化牌
//准备牌,洗牌,发牌
initCard();
//打牌之前的准备工作
//展示抢地主和不抢地主两个按钮并且再创建三个集合用来装三个玩家准备要出的牌
initGame();
}
//打牌之前的准备工作
private void initGame() {
//创建三个集合用来装三个玩家准备要出的牌
for (int i = 0; i < 3; i++) {
ArrayList list = new ArrayList<>();
//添加到大集合中方便管理
currentList.add(list);
}
//展示抢地主和不抢地主两个按钮
landlord[0].setVisible(true);
landlord[1].setVisible(true);
//展示自己前面的倒计时文本
for (JTextField field : time) {
field.setText("倒计时30秒");
field.setVisible(true);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == landlord[0]) {
//点击抢地主
time[1].setText("抢地主");
ArrayList player1 = playerList.get(1);
player1.addAll(lordList);
lordList.clear();
order(playerList.get(1));
for (Poker poker : player1) {
poker.turnFront();
}
rePosition(playerList.get(1),1);
} else if (e.getSource() == landlord[1]) {
//点击不抢
time[1].setText("不抢");
} else if (e.getSource() == publishCard[1]) {
//点击不要
time[1].setText("不要");
} else if (e.getSource() == publishCard[0]) {
time[1].setText("出牌");
}
}
//初始化牌(准备牌,洗牌,发牌)
public void initCard() {
//准备牌
//把所有的牌,包括大小王都添加到牌盒cardList当中
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 13; j++) {
if ((i == 5) && (j > 2)) {
break;
} else {
Poker poker = new Poker(i + "-" + j, false);
poker.setLocation(350, 150);
pokerList.add(poker);
container.add(poker);
}
}
}
//洗牌
Collections.shuffle(pokerList);
//创建三个集合用来装三个玩家的牌,并把三个小集合放到大集合中方便管理
ArrayList player0 = new ArrayList<>();
ArrayList player1 = new ArrayList<>();
ArrayList player2 = new ArrayList<>();
//发牌
for (int i = 0; i < pokerList.size(); i++) {
//获取当前遍历的牌
Poker poker = pokerList.get(i);
//发三张底牌
if (i <= 2) {
//把底牌添加到集合中
lordList.add(poker);
move(poker, poker.getLocation(), new Point(270 + (75 * i), 10));
continue;
}
//给三个玩家发牌
if (i % 3 == 0) {
//给左边的电脑发牌
move(poker, poker.getLocation(), new Point(50, 60 + i * 5));
player0.add(poker);
} else if (i % 3 == 1) {
//给中间的自己发牌
move(poker, poker.getLocation(), new Point(180 + i * 7, 450));
player1.add(poker);
//把自己的牌展示正面
poker.turnFront();
} else if (i % 3 == 2) {
//给右边的电脑发牌
move(poker, poker.getLocation(), new Point(700, 60 + i * 5));
player2.add(poker);
}
//把三个装着牌的小集合放到大集合中方便管理
playerList.add(player0);
playerList.add(player1);
playerList.add(player2);
//把当前的牌至于最顶端,这样就会有牌依次错开且叠起来的效果
container.setComponentZOrder(poker, 0);
}
//排序
for (int i = 0; i < 3; i++) {
order(playerList.get(i));
}
}
//排序
public void order(ArrayList list) {
//此处可以改为lambda表达式
Collections.sort(list, new Comparator() {
@Override
public int compare(Poker o1, Poker o2) {
//获取o1的花色和价值
int color1 = Integer.parseInt(o1.getName().substring(0, 1));
int value1 = getValue(o1);
//获取o2的花色和价值
int color2 = Integer.parseInt(o2.getName().substring(0, 1));
int value2 = getValue(o2);
//倒序排列
int flag = value2 - value1;
//如果牌的价值一样,则按照花色排序
if (flag == 0){
return color2 - color1;
}else {
return flag;
}
}
});
}
//获取每一张牌的价值
public int getValue(Poker poker) {
//获取牌的名字 1-1
String name = poker.getName();
//获取牌的花色
int color = Integer.parseInt(poker.getName().substring(0, 1));
//获取牌对应的数字,同时也是牌的价值
int value = Integer.parseInt(name.substring(2));
//在本地文件中,每张牌的文件名为:数字1-数字2
//数字1表示花色,数字2表示牌的数字
//其中3~K对应的数字2,可以视为牌的价值
//所以,我们单独判断大小王,A,2即可
//计算大小王牌的价值
if (color == 5){
//小王的初始价值为1,在1的基础上加100,小王价值为:101
//大王的初始价值为2,在2的基础上加100,大王价值为:102
return value += 100;
}
//计算A的价值
if (value == 1){
//A的初始价值为1,在1的基础上加20,大王价值为:21
return value += 20;
}
//计算2的价值
if (value == 2){
//2的初始价值为2,在2的基础上加30,大王价值为:32
return value += 30;
}
//如果不是大小王,不是A,不是2,牌的价值就是牌对应的数字
return value;
}
//添加组件
public void initView() {
//创建抢地主的按钮
JButton robBut = new JButton("抢地主");
//设置位置
robBut.setBounds(320, 400, 75, 20);
//添加点击事件
robBut.addActionListener(this);
//设置隐藏
robBut.setVisible(false);
//添加到数组中统一管理
landlord[0] = robBut;
//添加到界面中
container.add(robBut);
//创建不抢的按钮
JButton noBut = new JButton("不 抢");
//设置位置
noBut.setBounds(420, 400, 75, 20);
//添加点击事件
noBut.addActionListener(this);
//设置隐藏
noBut.setVisible(false);
//添加到数组中统一管理
landlord[1] = noBut;
//添加到界面中
container.add(noBut);
//创建出牌的按钮
JButton outCardBut = new JButton("出牌");
outCardBut.setBounds(320, 400, 60, 20);
outCardBut.addActionListener(this);
outCardBut.setVisible(false);
publishCard[0] = outCardBut;
container.add(outCardBut);
//创建不要的按钮
JButton noCardBut = new JButton("不要");
noCardBut.setBounds(420, 400, 60, 20);
noCardBut.addActionListener(this);
noCardBut.setVisible(false);
publishCard[1] = noCardBut;
container.add(noCardBut);
//创建三个玩家前方的提示文字:倒计时
//每个玩家一个
//左边的电脑玩家是0
//中间的自己是1
//右边的电脑玩家是2
for (int i = 0; i < 3; i++) {
time[i] = new JTextField("倒计时:");
time[i].setEditable(false);
time[i].setVisible(false);
container.add(time[i]);
}
time[0].setBounds(140, 230, 60, 20);
time[1].setBounds(374, 360, 60, 20);
time[2].setBounds(620, 230, 60, 20);
//创建地主图标
dizhu = new JLabel(new ImageIcon("images/dizhu.png"));
dizhu.setVisible(false);
dizhu.setSize(40, 40);
container.add(dizhu);
}
//设置界面
public void initJframe() {
//设置标题
this.setTitle("斗地主");
//设置大小
this.setSize(830, 620);
//设置关闭模式
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗口无法进行调节
this.setResizable(false);
//界面居中
this.setLocationRelativeTo(null);
//获取界面中的隐藏容器,以后直接用无需再次调用方法获取了
container = this.getContentPane();
//取消内部默认的居中放置
container.setLayout(null);
//设置背景颜色
container.setBackground(Color.LIGHT_GRAY);
}
//移动牌(有移动的动画效果)
public static void move(Poker poker, Point from, Point to) {
if (to.x != from.x) {
double k = (1.0) * (to.y - from.y) / (to.x - from.x);
double b = to.y - to.x * k;
int flag = 0;
if (from.x < to.x)
flag = 20;
else {
flag = -20;
}
for (int i = from.x; Math.abs(i - to.x) > 20; i += flag) {
double y = k * i + b;
poker.setLocation(i, (int) y);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
poker.setLocation(to);
}
//重新摆放牌
public static void rePosition(ArrayList list, int flag) {
Point p = new Point();
if (flag == 0) {
p.x = 50;
p.y = (450 / 2) - (list.size() + 1) * 15 / 2;
}
if (flag == 1) {
p.x = (800 / 2) - (list.size() + 1) * 21 / 2;
p.y = 450;
}
if (flag == 2) {
p.x = 700;
p.y = (450 / 2) - (list.size() + 1) * 15 / 2;
}
int len = list.size();
for (int i = 0; i < len; i++) {
Poker poker = list.get(i);
move(poker, poker.getLocation(), p);
container.setComponentZOrder(poker, 0);
if (flag == 1)
p.x += 21;
else
p.y += 15;
}
}
}
正反面:
牌是否可以被点击 && 是否已经被点击:
Poker.java:
package com.yaqi.domain;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Poker extends JLabel implements MouseListener {
//牌的名字
private String name;
//牌显示正面还是反面
private boolean up;
//是否可点击
private boolean canClick = false;
//当前状态,是否已经被点击
private boolean clicked = false;
public Poker(String name, boolean up) {
this.name = name;
this.up = up;
//判断当前的牌是显示正面还是背面
if (this.up){
this.turnFront();
}else {
this.turnRear();
}
//设置牌的宽高大小
this.setSize(71, 96);
//把牌显示出来
this.setVisible(true);
//给每一张牌添加鼠标监听
this.addMouseListener(this);
}
//显示正面
public void turnFront() {
this.setIcon(new ImageIcon("farmerandlord\\image\\poker\\" + name + ".png"));
this.up = true;
}
//显示背面
public void turnRear() {
this.setIcon(new ImageIcon("farmerandlord\\image\\poker\\rear.png"));
this.up = false;
}
//出牌时,需要点击牌
//被点击之后,牌向上移动20个像素
//再次被点击,牌回落20个像素
@Override
public void mouseClicked(MouseEvent e) {
if (canClick) {
Point from = this.getLocation();
int step;
if (clicked)
step = -20;
else {
step = 20;
}
clicked = !clicked;
//。。。牌被点击之后,还需要写移动牌的代码
}
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
public void mousePressed(MouseEvent e) {
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return up
*/
public boolean isUp() {
return up;
}
/**
* 设置
* @param up
*/
public void setUp(boolean up) {
this.up = up;
}
/**
* 获取
* @return canClick
*/
public boolean isCanClick() {
return canClick;
}
/**
* 设置
* @param canClick
*/
public void setCanClick(boolean canClick) {
this.canClick = canClick;
}
/**
* 获取
* @return clicked
*/
public boolean isClicked() {
return clicked;
}
/**
* 设置
* @param clicked
*/
public void setClicked(boolean clicked) {
this.clicked = clicked;
}
}
移动牌与重新摆放牌:
Common.java:
//移动牌(有移动的动画效果)
public static void move(Poker poker, Point from, Point to) {
if (to.x != from.x) {
double k = (1.0) * (to.y - from.y) / (to.x - from.x);
double b = to.y - to.x * k;
int flag = 0;
if (from.x < to.x)
flag = 20;
else {
flag = -20;
}
for (int i = from.x; Math.abs(i - to.x) > 20; i += flag) {
double y = k * i + b;
poker.setLocation(i, (int) y);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
poker.setLocation(to);
}
//重新摆放牌
public static void rePosition(ArrayList list, int flag) {
Point p = new Point();
if (flag == 0) {
p.x = 50;
p.y = (450 / 2) - (list.size() + 1) * 15 / 2;
}
if (flag == 1) {
p.x = (800 / 2) - (list.size() + 1) * 21 / 2;
p.y = 450;
}
if (flag == 2) {
p.x = 700;
p.y = (450 / 2) - (list.size() + 1) * 15 / 2;
}
int len = list.size();
for (int i = 0; i < len; i++) {
Poker poker = list.get(i);
move(poker, poker.getLocation(), p);
container.setComponentZOrder(poker, 0);
if (flag == 1)
p.x += 21;
else
p.y += 15;
}
}
}