目录
扫描二维码关注公众号,回复:
17193989 查看本文章
登录注册界面
登录注册界面,是游戏开始时弹出的界面。玩家必须正确输入用户名,密码以及验证码才能开始游戏。或者玩家注册一个账号,然后再进行登录,登录成功后可以开始游戏。
package com.doudizhu.game;
import com.doudizhu.domain.User;
import com.doudizhu.utils.CodeUtils;
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 {
//记得换成自己的路径哦
private static final String basePath = "D:\\develop\\[a04]\\";
static ArrayList<User> allUsers = new ArrayList<>();
static {
allUsers.add(new User("张三", "123"));
allUsers.add(new User("李四", "456"));
}
static String usernameInput = "";
static String passwordInput = "";
JButton login = new JButton();
JButton register = new JButton();
JTextField username = new JTextField();
JTextField password = new JTextField();
JTextField code = new JTextField();
//正确的验证码
JLabel rightCode = new JLabel();
public LoginJFrame() {
//设置图标
setIconImage(Toolkit.getDefaultToolkit().getImage(basePath + "doudizhu\\image\\dizhu.png"));
//初始化界面
initJFrame();
//在这个界面中添加内容
initView();
//让当前界面显示出来
this.setVisible(true);
}
public void initView() {
//1.添加用户名文字
Font usernameFont = new Font(null, Font.BOLD, 16);
JLabel usernameText = new JLabel("用户名");
usernameText.setForeground(Color.white);
usernameText.setFont(usernameFont);
usernameText.setBounds(200, 55, 55, 22);
this.getContentPane().add(usernameText);
//2.添加用户名输入框
username.setBounds(263, 55, 200, 30);
this.getContentPane().add(username);
//3.添加密码文字
JLabel passwordText = new JLabel("密码");
Font passwordFont = new Font(null, Font.BOLD, 16);
passwordText.setForeground(Color.white);
passwordText.setFont(passwordFont);
passwordText.setBounds(200, 95, 40, 22);
this.getContentPane().add(passwordText);
//4.密码输入框
password.setBounds(263, 95, 160, 30);
this.getContentPane().add(password);
//验证码提示
JLabel codeText = new JLabel("验证码");
Font codeFont = new Font(null, Font.BOLD, 16);
codeText.setForeground(Color.white);
codeText.setFont(codeFont);
codeText.setBounds(200, 135, 55, 22);
this.getContentPane().add(codeText);
//验证码的输入框
code.setBounds(263, 135, 100, 30);
this.getContentPane().add(code);
//获取正确的验证码
String codeStr = CodeUtils.getCode();
Font rightCodeFont = new Font(null, Font.BOLD, 15);
//设置颜色
rightCode.setForeground(Color.BLACK);
//设置字体
rightCode.setFont(rightCodeFont);
//设置内容
rightCode.setText(codeStr);
//绑定鼠标事件
rightCode.addMouseListener(this);
//位置和宽高
rightCode.setBounds(370, 130, 100, 40);
//添加到界面
this.getContentPane().add(rightCode);
//5.添加登录按钮
login.setBounds(165, 310, 128, 47);
login.setIcon(new ImageIcon(basePath + "doudizhu\\image\\login\\登录按钮.png"));
//去除按钮的边框
login.setBorderPainted(false);
//去除按钮的背景
login.setContentAreaFilled(false);
//给登录按钮绑定鼠标事件
login.addMouseListener(this);
this.getContentPane().add(login);
//6.添加注册按钮
register.setBounds(330, 310, 128, 47);
register.setIcon(new ImageIcon(basePath + "doudizhu\\image\\login\\注册按钮.png"));
//去除按钮的边框
register.setBorderPainted(false);
//去除按钮的背景
register.setContentAreaFilled(false);
//给注册按钮绑定鼠标事件
register.addMouseListener(this);
this.getContentPane().add(register);
//7.添加背景图片
JLabel background = new JLabel(new ImageIcon(basePath + "doudizhu\\image\\login\\background.png"));
background.setBounds(-15, -10, 633, 423);
this.getContentPane().add(background);
}
public void initJFrame() {
this.setSize(633, 423);//设置宽高
this.setTitle("斗地主游戏 V1.0登录");//设置标题
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置关闭模式
this.setLocationRelativeTo(null);//居中
this.setAlwaysOnTop(true);//置顶
this.setLayout(null);//取消内部默认布局
}
//点击
@Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() == login) {
System.out.println("点击登录");
action();
//判断集合中是否包含当前用户对象
//其实就是验证用户名和密码是否相同
//contains底层是依赖equals方法判断的,所以需要重写equals方法
User userInfo = new User(usernameInput, passwordInput);
if (allUsers.contains(userInfo) && !usernameInput.equals("") && !passwordInput.equals("")) {
System.out.println("登录成功");
//关闭当前登录界面
this.setVisible(false);
//打开游戏的主界面
new GameJFrame();
} else {
showJDialog("用户名或密码错误!");
}
} else if (e.getSource() == register) {
System.out.println("点击注册");
action();
//添加用户
allUsers.add(new User(usernameInput, passwordInput));
User userInfo = new User(usernameInput, passwordInput);
if (allUsers.contains(userInfo) && !usernameInput.equals("") && !passwordInput.equals("")) {
System.out.println("注册成功");
showJDialog("注册成功,请登录!");
} else {
showJDialog("注册失败,请重新尝试注册!");
}
} else if (e.getSource() == rightCode) {
System.out.println("更换验证码");
//获取一个新的验证码
String code = CodeUtils.getCode();
rightCode.setText(code);
}
}
public void action() {
//获取两个文本输入框中的内容
usernameInput = username.getText();
passwordInput = password.getText();
//获取用户输入的验证码
String codeInput = code.getText();
//判断用户名是否为空
if (usernameInput.length() == 0) {
showJDialog("用户名不能为空!");
return;
}
//判断密码是否为空
if (passwordInput.length() == 0) {
showJDialog("密码不能为空!");
return;
}
//判断验证码是否为空
if (codeInput.length() == 0) {
showJDialog("验证码不能为空!");
return;
}
//判断验证码是否正确
if (!codeInput.equalsIgnoreCase(rightCode.getText())) {
showJDialog("验证码错误!");
}
}
public void showJDialog(String content) {
//创建一个弹框对象
JDialog jDialog = new JDialog();
//给弹框设置大小
jDialog.setSize(180, 90);
//让弹框置顶
jDialog.setAlwaysOnTop(true);
//让弹框居中
jDialog.setLocationRelativeTo(null);
//弹框不关闭永远无法操作下面的界面
jDialog.setModal(true);
//创建JLabel对象管理文字并添加到弹框当中
JLabel warning = new JLabel(content);
warning.setBounds(0, 0, 180, 90);
jDialog.getContentPane().add(warning);
//让弹框展示出来
jDialog.setVisible(true);
}
//按下不松
@Override
public void mousePressed(MouseEvent e) {
if (e.getSource() == login) {
login.setIcon(new ImageIcon(basePath + "doudizhu\\image\\login\\登录按下.png"));
} else if (e.getSource() == register) {
register.setIcon(new ImageIcon(basePath + "doudizhu\\image\\login\\注册按下.png"));
}
}
//松开按钮
@Override
public void mouseReleased(MouseEvent e) {
if (e.getSource() == login) {
login.setIcon(new ImageIcon(basePath + "doudizhu\\image\\login\\登录按钮.png"));
} else if (e.getSource() == register) {
register.setIcon(new ImageIcon(basePath + "doudizhu\\image\\login\\注册按钮.png"));
}
}
//鼠标划入
@Override
public void mouseEntered(MouseEvent e) {
}
//鼠标划出
@Override
public void mouseExited(MouseEvent e) {
}
}
游戏界面
进入游戏界面,开始发牌,并且抢地主。抢到地主的一方会额外获得三张牌,且这三张牌会展示给玩家。在抢地主和出牌的时候都会有时间限制,超过时间限制后将会由系统自动代替玩家做出对应的选择。在任意一方出完所有的牌后获得胜利,并且游戏结束。如果还想进行游戏的话,需要重新启动程序,登录或注册并登录成功后开始游戏。
package com.doudizhu.game;
import com.doudizhu.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;
public class GameJFrame extends JFrame implements ActionListener {
//记得换成自己的路径哦
private static final String basePath = "D:\\develop\\[a04]\\";
//获取界面中的隐藏容器
//现在统一获取了,后面直接用就可以了
public Container container = null;
//管理抢地主和不抢两个按钮
JButton[] landlord = new JButton[2];
//管理出牌和不要两个按钮
JButton[] publishCard = new JButton[2];
int diZhuFlag;
int turn;
//游戏界面中地主的图标
JLabel diZhu;
//集合嵌套集合
//大集合中有三个小集合
//小集合中装着每一个玩家当前要出的牌
//0索引:左边的电脑玩家
//1索引:中间的自己
//2索引:右边的电脑玩家
ArrayList<ArrayList<Poker>> currentList = new ArrayList<>();
//集合嵌套集合
//大集合中有三个小集合
//小集合中装着每一个玩家的牌
//0索引:左边的电脑玩家
//1索引:中间的自己
//2索引:右边的电脑玩家
ArrayList<ArrayList<Poker>> playerList = new ArrayList<>();
//底牌
ArrayList<Poker> lordList = new ArrayList<>();
//牌盒,装所有的牌
ArrayList<Poker> pokerList = new ArrayList<>();
//三个玩家前方的文本提示
//0索引:左边的电脑玩家
//1索引:中间的自己
//2索引:右边的电脑玩家
JTextField[] time = new JTextField[3];
//用户操作,涉及多线程的知识
PlayerOperation po;
boolean nextPlayer = false;
public GameJFrame() {
//设置图标
setIconImage(Toolkit.getDefaultToolkit().getImage(basePath + "doudizhu\\image\\dizhu.png"));
//设置界面
initJFrame();
//添加组件
initView();
//界面显示出来
//先展示界面再发牌,因为发牌里面有动画,界面不展示出来,动画无法展示
this.setVisible(true);
//初始化牌
//准备牌,洗牌,发牌
new Thread(this::initCard).start();
//打牌之前的准备工作
//展示抢地主和不抢地主两个按钮并且再创建三个集合用来装三个玩家准备要出的牌
initGame();
}
//初始化牌
//准备牌,洗牌,发牌
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(this, i + "-" + j, false);
poker.setLocation(350, 150);
pokerList.add(poker);
container.add(poker);
}
}
}
//洗牌
Collections.shuffle(pokerList);
//创建三个集合用来装三个玩家的牌,并把三个小集合放到大集合中方便管理
ArrayList<Poker> player0 = new ArrayList<>();
ArrayList<Poker> player1 = new ArrayList<>();
ArrayList<Poker> player2 = new ArrayList<>();
for (int i = 0; i < pokerList.size(); i++) {
//获取当前遍历的牌
Poker poker = pokerList.get(i);
//发三张底牌
if (i <= 2) {
//移动牌
Common.move(poker, poker.getLocation(), new Point(270 + (75 * i), 10));
//把底牌添加到集合中
lordList.add(poker);
continue;
}
//给三个玩家发牌
if (i % 3 == 0) {
//给左边的电脑发牌
Common.move(poker, poker.getLocation(), new Point(50, 60 + i * 5));
player0.add(poker);
} else if (i % 3 == 1) {
//给中间的自己发牌
Common.move(poker, poker.getLocation(), new Point(180 + i * 7, 450));
player1.add(poker);
//把自己的牌展示正面
poker.turnFront();
} else {
//给右边的电脑发牌
Common.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++) {
//排序
Common.order(playerList.get(i));
//重新摆放顺序
Common.rePosition(this, playerList.get(i), i);
}
}
//打牌之前的准备工作
private void initGame() {
//创建三个集合用来装三个玩家准备要出的牌
for (int i = 0; i < 3; i++) {
ArrayList<Poker> list = new ArrayList<>();
//添加到大集合中方便管理
currentList.add(list);
}
//展示抢地主和不抢地主两个按钮
landlord[0].setVisible(true);
landlord[1].setVisible(true);
//展示自己前面的倒计时文本
time[1].setVisible(true);
//倒计时10秒
po = new PlayerOperation(this, 30);
//开启倒计时
po.start();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == landlord[0]) {
//点击抢地主
time[1].setText("抢地主");
po.isRun = false;
} else if (e.getSource() == landlord[1]) {
//点击不抢
time[1].setText("不抢");
po.isRun = false;
} else if (e.getSource() == publishCard[1]) {
//点击不要
this.nextPlayer = true;
currentList.get(1).clear();
time[1].setText("不要");
} else if (e.getSource() == publishCard[0]) {
//点击出牌
//创建一个临时的集合,用来存放当前要出的牌
ArrayList<Poker> c = new ArrayList<>();
//获取中自己手上所有的牌
ArrayList<Poker> player2 = playerList.get(1);
//遍历手上的牌,把要出的牌都放到临时集合中
for (Poker poker : player2) {
if (poker.isClicked()) {
c.add(poker);
}
}
int flag = 0;
//判断,如果左右两个电脑玩家是否都不要
if (time[0].getText().equals("不要") && time[2].getText().equals("不要")) {
if (Common.judgeType(c) != PokerType.c0) {
flag = 1;
}
} else {
flag = Common.checkCards(c, currentList, this);
}
if (flag == 1) {
//把当前要出的牌,放到大集合中统一管理
currentList.set(1, c);
//在手上的牌中,去掉已经出掉的牌
player2.removeAll(c);
//计算坐标并移动牌
//移动的目的是要出的牌移动到上方
Point point = new Point();
point.x = (770 / 2) - (c.size() + 1) * 15 / 2;
point.y = 300;
for (Poker poker : c) {
Common.move(poker, poker.getLocation(), point);
point.x += 15;
}
//重新摆放剩余的牌
Common.rePosition(this, player2, 1);
//赢藏文本提示
time[1].setVisible(false);
//下一个玩家可玩
this.nextPlayer = true;
}
}
}
//添加组件
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(basePath + "doudizhu\\image\\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);
}
}
出牌规则
枚举出所有出牌的情况。
package com.doudizhu.game;
public enum PokerType {
c0,//不能出牌
c1,//单牌。
c2,//对子。
c3,//3不带。
c4,//炸弹。
c31,//3带1。
c32,//3带2。
c411,//4带2个单,或者一对
c422,//4带2对
c123,//连子。
c112233,//连对。
c111222,//飞机。
c11122234,//飞机带单排.
c1112223344//飞机带对子.
}
游戏规则
利用多线程实现轮流出牌,并且按照游戏的规则进行相应的判断。最先出完牌的一方获胜,游戏结束,结束进程。
package com.doudizhu.game;
import com.doudizhu.domain.Poker;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class PlayerOperation extends Thread {
//游戏主界面
GameJFrame gameJFrame;
//是否能走
boolean isRun = true;
//倒计时
int i;
public PlayerOperation(GameJFrame m, int i) {
this.gameJFrame = m;
this.i = i;
}
@Override
public void run() {
while (i > -1 && isRun) {
gameJFrame.time[1].setText("倒计时:" + i--);
sleep(1);
}
if (i == -1) {
gameJFrame.time[1].setText("不抢");
}
gameJFrame.landlord[0].setVisible(false);
gameJFrame.landlord[1].setVisible(false);
for (Poker poker2 : gameJFrame.playerList.get(1)) {
poker2.setCanClick(true);// 可被点击
}
if (gameJFrame.time[1].getText().equals("抢地主")) {
gameJFrame.playerList.get(1).addAll(gameJFrame.lordList);
openLord(true);
sleep(2);
Common.order(gameJFrame.playerList.get(1));
Common.rePosition(gameJFrame, gameJFrame.playerList.get(1), 1);
gameJFrame.publishCard[1].setEnabled(false);
setLord(1);
} else {
if (Common.getScore(gameJFrame.playerList.get(0)) < Common.getScore(gameJFrame.playerList.get(2))) {
gameJFrame.time[2].setText("抢地主");
gameJFrame.time[2].setVisible(true);
setLord(2);
openLord(true);
sleep(3);
gameJFrame.playerList.get(2).addAll(gameJFrame.lordList);
Common.order(gameJFrame.playerList.get(2));
Common.rePosition(gameJFrame, gameJFrame.playerList.get(2), 2);
openLord(false);
} else {
gameJFrame.time[0].setText("抢地主");
gameJFrame.time[0].setVisible(true);
setLord(0);
openLord(true);
sleep(3);
gameJFrame.playerList.get(0).addAll(gameJFrame.lordList);
Common.order(gameJFrame.playerList.get(0));
Common.rePosition(gameJFrame, gameJFrame.playerList.get(0), 0);
openLord(false);
}
}
gameJFrame.landlord[0].setVisible(false);
gameJFrame.landlord[1].setVisible(false);
turnOn(false);
for (int i = 0; i < 3; i++) {
gameJFrame.time[i].setText("不要");
gameJFrame.time[i].setVisible(false);
}
gameJFrame.turn = gameJFrame.diZhuFlag;
while (true) {
if (gameJFrame.turn == 1) {
gameJFrame.publishCard[1].setEnabled(!gameJFrame.time[0].getText().equals("不要") || !gameJFrame.time[2].getText().equals("不要"));
turnOn(true);
timeWait(30, 1);
turnOn(false);
gameJFrame.turn = (gameJFrame.turn + 1) % 3;
if (win()) {
gameJFrame.setVisible(false);
System.exit(0);
}
}
if (gameJFrame.turn == 0) {
computer0();
gameJFrame.turn = (gameJFrame.turn + 1) % 3;
if (win()) {
gameJFrame.setVisible(false);
System.exit(0);
}
}
if (gameJFrame.turn == 2) {
computer2();
gameJFrame.turn = (gameJFrame.turn + 1) % 3;
if (win()) {
gameJFrame.setVisible(false);
System.exit(0);
}
}
}
}
//定义一个方法用来暂停N秒
//参数为等待的时间
//因为线程中的sleep方法有异常,直接调用影响阅读
public void sleep(int i) {
try {
Thread.sleep((long) i * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void openLord(boolean is) {
for (int i = 0; i < 3; i++) {
if (is)
gameJFrame.lordList.get(i).turnFront();
else {
gameJFrame.lordList.get(i).turnRear();
}
gameJFrame.lordList.get(i).setCanClick(true);
}
}
public void setLord(int i) {
Point point = new Point();
if (i == 1) {
point.x = 80;
point.y = 430;
gameJFrame.diZhuFlag = 1;
}
if (i == 0) {
point.x = 80;
point.y = 20;
gameJFrame.diZhuFlag = 0;
}
if (i == 2) {
point.x = 700;
point.y = 20;
gameJFrame.diZhuFlag = 2;
}
gameJFrame.diZhu.setLocation(point);
gameJFrame.diZhu.setVisible(true);
}
public void turnOn(boolean flag) {
gameJFrame.publishCard[0].setVisible(flag);
gameJFrame.publishCard[1].setVisible(flag);
}
public void computer0() {
timeWait(1, 0);
ShowCard(0);
}
public void computer2() {
timeWait(1, 2);
ShowCard(2);
}
public void ShowCard(int role) {
int[] orders = new int[]{4, 3, 2, 1, 5};
Model model = Common.getModel(gameJFrame.playerList.get(role), orders);
ArrayList<String> list = new ArrayList<>();
if (gameJFrame.time[(role + 1) % 3].getText().equals("不要") && gameJFrame.time[(role + 2) % 3].getText().equals("不要")) {
if (model.a123.size() > 0) {
list.add(model.a123.get(model.a123.size() - 1));
} else if (model.a3.size() > 0) {
if (model.a1.size() > 0) {
list.add(model.a1.get(model.a1.size() - 1));
} else if (model.a2.size() > 0) {
list.add(model.a2.get(model.a2.size() - 1));
}
list.add(model.a3.get(model.a3.size() - 1));
} else if (model.a112233.size() > 0) {
list.add(model.a112233.get(model.a112233.size() - 1));
} else if (model.a111222.size() > 0) {
String[] name = model.a111222.get(0).split(",");
if (name.length / 3 <= model.a1.size()) {
list.add(model.a111222.get(model.a111222.size() - 1));
for (int i = 0; i < name.length / 3; i++)
list.add(model.a1.get(i));
} else if (name.length / 3 <= model.a2.size()) {
list.add(model.a111222.get(model.a111222.size() - 1));
for (int i = 0; i < name.length / 3; i++)
list.add(model.a2.get(i));
}
} else if (model.a2.size() > (0)) {
list.add(model.a2.get(model.a2.size() - 1));
} else if (model.a1.size() > (0)) {
list.add(model.a1.get(model.a1.size() - 1));
} else if (model.a4.size() > 0) {
list.add(model.a4.get(0));
}
} else {
if (role != gameJFrame.diZhuFlag) {
int f = 0;
if (gameJFrame.time[gameJFrame.diZhuFlag].getText().equals("不要")) {
f = 1;
}
if ((role + 1) % 3 == gameJFrame.diZhuFlag) {
if ((Common.judgeType(gameJFrame.currentList.get((role + 2) % 3)) != PokerType.c1
|| Common.judgeType(gameJFrame.currentList.get((role + 2) % 3)) != PokerType.c2)
&& gameJFrame.currentList.get(gameJFrame.diZhuFlag).size() < 1)
f = 1;
if (gameJFrame.currentList.get((role + 2) % 3).size() > 0
&& Common.getValue(gameJFrame.currentList.get((role + 2) % 3).get(0)) > 13)
f = 1;
}
if (f == 1) {
gameJFrame.time[role].setVisible(true);
gameJFrame.time[role].setText("不要");
return;
}
}
int can = 0;
if (role == gameJFrame.diZhuFlag) {
if (gameJFrame.playerList.get((role + 1) % 3).size() <= 5 || gameJFrame.playerList.get((role + 2) % 3).size() <= 5)
can = 1;
} else {
if (gameJFrame.playerList.get(gameJFrame.diZhuFlag).size() <= 5)
can = 1;
}
ArrayList<Poker> player;
if (gameJFrame.time[(role + 2) % 3].getText().equals("不要"))
player = gameJFrame.currentList.get((role + 1) % 3);
else
player = gameJFrame.currentList.get((role + 2) % 3);
PokerType cType = Common.judgeType(player);
if (cType == PokerType.c1) {
if (can == 1)
model = Common.getModel(gameJFrame.playerList.get(role), new int[]{1, 4, 3, 2, 5});
AI_1(model.a1, player, list, role);
} else if (cType == PokerType.c2) {
if (can == 1)
model = Common.getModel(gameJFrame.playerList.get(role), new int[]{2, 4, 3, 5, 1});
AI_1(model.a2, player, list, role);
} else if (cType == PokerType.c3) {
AI_1(model.a3, player, list, role);
} else if (cType == PokerType.c4) {
AI_1(model.a4, player, list, role);
} else if (cType == PokerType.c31) {
if (can == 1)
model = Common.getModel(gameJFrame.playerList.get(role), new int[]{3, 1, 4, 2, 5});
AI_2(model.a3, model.a1, player, list, role);
} else if (cType == PokerType.c32) {
if (can == 1)
model = Common.getModel(gameJFrame.playerList.get(role), new int[]{3, 2, 4, 5, 1});
AI_2(model.a3, model.a2, player, list, role);
} else if (cType == PokerType.c411) {
AI_5(model.a4, model.a1, player, list, role);
} else if (cType == PokerType.c422) {
AI_5(model.a4, model.a2, player, list, role);
} else if (cType == PokerType.c123) {
if (can == 1)
model = Common.getModel(gameJFrame.playerList.get(role), new int[]{5, 3, 2, 4, 1});
AI_3(model.a123, player, list, role);
} else if (cType == PokerType.c112233) {
if (can == 1)
model = Common.getModel(gameJFrame.playerList.get(role), new int[]{2, 4, 3, 5, 1});
AI_3(model.a112233, player, list, role);
} else if (cType == PokerType.c11122234) {
AI_4(model.a111222, model.a1, player, list, role);
} else if (cType == PokerType.c1112223344) {
AI_4(model.a111222, model.a2, player, list, role);
}
if (list.size() == 0 && can == 1) {
int len4 = model.a4.size();
if (len4 > 0)
list.add(model.a4.get(len4 - 1));
}
}
gameJFrame.currentList.get(role).clear();
if (list.size() > 0) {
Point point = new Point();
if (role == 0)
point.x = 200;
if (role == 2)
point.x = 550;
if (role == 1) {
point.x = (770 / 2) - (gameJFrame.currentList.get(1).size() + 1) * 15 / 2;
point.y = 300;
}
point.y = (400 / 2) - (list.size() + 1) * 15 / 2;
ArrayList<Poker> temp = new ArrayList<>();
for (String s : list) {
List<Poker> pokers = getCardByName(gameJFrame.playerList.get(role), s);
temp.addAll(pokers);
}
temp = Common.getOrder2(temp);
for (Poker poker : temp) {
Common.move(poker, poker.getLocation(), point);
point.y += 15;
gameJFrame.container.setComponentZOrder(poker, 0);
gameJFrame.currentList.get(role).add(poker);
gameJFrame.playerList.get(role).remove(poker);
}
Common.rePosition(gameJFrame, gameJFrame.playerList.get(role), role);
} else {
gameJFrame.time[role].setVisible(true);
gameJFrame.time[role].setText("不要");
}
for (Poker poker : gameJFrame.currentList.get(role))
poker.turnFront();
}
public List<Poker> getCardByName(List<Poker> list, String n) {
String[] name = n.split(",");
ArrayList<Poker> cardsList = new ArrayList<>();
int j = 0;
for (int i = 0, len = list.size(); i < len; i++) {
if (j < name.length && list.get(i).getName().equals(name[j])) {
cardsList.add(list.get(i));
i = 0;
j++;
}
}
return cardsList;
}
public void AI_3(List<String> model, List<Poker> player, List<String> list, int role) {
for (String value : model) {
String[] s = value.split(",");
if (s.length == player.size() && getValueInt(value) > Common.getValue(player.get(0))) {
list.add(value);
return;
}
}
}
public void AI_4(List<String> model1, List<String> model2, List<Poker> player, List<String> list, int role) {
player = Common.getOrder2(player);
int len1 = model1.size();
int len2 = model2.size();
if (len1 < 1 || len2 < 1)
return;
for (String value : model1) {
String[] s = value.split(",");
String[] s2 = model2.get(0).split(",");
if ((s.length / 3 <= len2) && (s.length * (3 + s2.length) == player.size())
&& getValueInt(value) > Common.getValue(player.get(0))) {
list.add(value);
for (int j = 1; j <= s.length / 3; j++)
list.add(model2.get(len2 - j));
}
}
}
public void AI_5(List<String> model1, List<String> model2, List<Poker> player, List<String> list, int role) {
player = Common.getOrder2(player);
int len1 = model1.size();
int len2 = model2.size();
if (len1 < 1 || len2 < 2)
return;
for (String s : model1) {
if (getValueInt(s) > Common.getValue(player.get(0))) {
list.add(s);
for (int j = 1; j <= 2; j++)
list.add(model2.get(len2 - j));
}
}
}
public void AI_1(List<String> model, List<Poker> player, List<String> list, int role) {
for (int len = model.size(), i = len - 1; i >= 0; i--) {
if (getValueInt(model.get(i)) > Common.getValue(player.get(0))) {
list.add(model.get(i));
break;
}
}
}
public void AI_2(List<String> model1, List<String> model2, List<Poker> player, List<String> list, int role) {
player = Common.getOrder2(player);
int len1 = model1.size();
int len2 = model2.size();
if (len1 > 0 && model1.get(0).length() < 10) {
list.add(model1.get(0));
System.out.println("王炸");
return;
}
if (len1 < 1 || len2 < 1)
return;
for (int i = len1 - 1; i >= 0; i--) {
if (getValueInt(model1.get(i)) > Common.getValue(player.get(0))) {
list.add(model1.get(i));
break;
}
}
list.add(model2.get(len2 - 1));
if (list.size() < 2)
list.clear();
}
public void timeWait(int n, int player) {
if (gameJFrame.currentList.get(player).size() > 0)
Common.hideCards(gameJFrame.currentList.get(player));
if (player == 1) {
int i = n;
while (!gameJFrame.nextPlayer && i >= 0) {
gameJFrame.time[player].setText("倒计时:" + i);
gameJFrame.time[player].setVisible(true);
sleep(1);
i--;
}
if (i == -1) {
ShowCard(1);
}
gameJFrame.nextPlayer = false;
} else {
for (int i = n; i >= 0; i--) {
sleep(1);
gameJFrame.time[player].setText("倒计时:" + i);
gameJFrame.time[player].setVisible(true);
}
}
gameJFrame.time[player].setVisible(false);
}
public int getValueInt(String n) {
String[] name = n.split(",");
String s = name[0];
int i = Integer.parseInt(s.substring(2));
if (s.charAt(0) == '5')
i += 3;
if (s.substring(2).equals("1") || s.substring(2).equals("2"))
i += 13;
return i;
}
public boolean win() {
for (int i = 0; i < 3; i++) {
if (gameJFrame.playerList.get(i).size() == 0) {
String s;
if (i == 1) {
s = "恭喜你赢了!";
} else {
s = "你输了!";
}
for (int j = 0; j < gameJFrame.playerList.get((i + 1) % 3).size(); j++)
gameJFrame.playerList.get((i + 1) % 3).get(j).turnFront();
for (int j = 0; j < gameJFrame.playerList.get((i + 2) % 3).size(); j++)
gameJFrame.playerList.get((i + 2) % 3).get(j).turnFront();
JOptionPane.showMessageDialog(gameJFrame, s);
return true;
}
}
return false;
}
}
牌的处理
实现发牌,对拿到的牌进行处理,根据花色和大小进行排序。
package com.doudizhu.game;
import com.doudizhu.domain.Poker;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class Common {
//判断牌型
public static PokerType judgeType(ArrayList<Poker> list) {
int len = list.size();
if (len <= 4) {
if (list.size() > 0 && Common.getValue(list.get(0)) == Common.getValue(list.get(len - 1))) {
switch (len) {
case 1 -> {
return PokerType.c1;
}
case 2 -> {
return PokerType.c2;
}
case 3 -> {
return PokerType.c3;
}
case 4 -> {
return PokerType.c4;
}
}
}
if (len == 2 && Common.getColor(list.get(1)) == 5)
return PokerType.c2;
if (len == 4 && ((Common.getValue(list.get(0)) == Common.getValue(list.get(len - 2)))
|| Common.getValue(list.get(1)) == Common.getValue(list.get(len - 1))))
return PokerType.c31;
else {
return PokerType.c0;
}
}
PokerIndex pokerIndex = new PokerIndex();
ArrayList<ArrayList<Integer>> indexList = pokerIndex.indexList;
for (int i = 0; i < 4; i++) {
indexList.add(new ArrayList<>());
}
Common.getMax(pokerIndex, list);
if (indexList.get(2).size() == 1 && indexList.get(1).size() == 1 && len == 5)
return PokerType.c32;
if (indexList.get(3).size() == 1 && len == 6)
return PokerType.c411;
if (indexList.get(3).size() == 1 && indexList.get(1).size() == 2 && len == 8)
return PokerType.c422;
if ((Common.getColor(list.get(0)) != 5) && (indexList.get(0).size() == len)
&& (Common.getValue(list.get(0)) - Common.getValue(list.get(len - 1)) == len - 1))
return PokerType.c123;
if (indexList.get(1).size() == len / 2 && len % 2 == 0
&& (Common.getValue(list.get(0)) - Common.getValue(list.get(len - 1)) == (len / 2 - 1)))
return PokerType.c112233;
if (indexList.get(2).size() == len / 3 && (len % 3 == 0)
&& (Common.getValue(list.get(0)) - Common.getValue(list.get(len - 1)) == (len / 3 - 1)))
return PokerType.c111222;
if (indexList.get(2).size() == len / 4 && (indexList.get(2).get(len / 4 - 1)
- (indexList.get(2).get(0)) == len / 4 - 1))
return PokerType.c11122234;
if (indexList.get(2).size() == len / 5 && indexList.get(2).size() == len / 5
&& ((indexList.get(2).get(len / 5 - 1)) - (indexList.get(2).get(0)) == len / 5
- 1))
return PokerType.c1112223344;
return PokerType.c0;
}
//移动牌(有移动的动画效果)
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 order(ArrayList<Poker> list) {
//此时可以改为lambda表达式
list.sort((o1, o2) -> {
//获取花色
//1-黑桃 2-红桃 3-梅花 4-方块 5-大小王
int a1 = Integer.parseInt(o1.getName().substring(0, 1));
int a2 = Integer.parseInt(o2.getName().substring(0, 1));
//获取牌上的数字,同时也是牌的价值
//1-A ... 11-J 12-Q 13-K
int b1 = Integer.parseInt(o1.getName().substring(2));
int b2 = Integer.parseInt(o2.getName().substring(2));
//计算牌的价值,利用牌的价值进行排序
//牌上的数字在3~10之间,价值就是3~10
//3:价值3
//...
//10:价值10
//J:价值11
//Q:价值12
//K:价值13
//A:1 + 20 = 21
//2:2 + 30 = 32
//小王:1 + 100 = 101
//大王:2 + 100 = 102
//计算大小王牌的价值
if (a1 == 5) {
b1 += 100;
}
if (a2 == 5) {
b2 += 100;
}
//计算A的价值
if (b1 == 1) {
b1 += 20;
}
if (b2 == 1) {
b2 += 20;
}
//计算2的价值
if (b1 == 2) {
b1 += 30;
}
if (b2 == 2) {
b2 += 30;
}
//倒序排列
int flag = b2 - b1;
//如果牌的价值一样,则按照花色排序
if (flag == 0) {
return a2 - a1;
} else {
return flag;
}
});
}
//重新摆放牌
public static void rePosition(GameJFrame m, ArrayList<Poker> 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 (Poker poker : list) {
Common.move(poker, poker.getLocation(), p);
m.container.setComponentZOrder(poker, 0);
if (flag == 1)
p.x += 21;
else
p.y += 15;
}
}
public static int getScore(ArrayList<Poker> list) {
int count = 0;
for (Poker poker : list) {
if (poker.getName().charAt(0) == '5') {
count += 5;
}
if (poker.getName().substring(2).equals("2")) {
count += 2;
}
}
return count;
}
public static int getColor(Poker poker) {
return Integer.parseInt(poker.getName().substring(0, 1));
}
public static int getValue(Poker poker) {
int i = Integer.parseInt(poker.getName().substring(2));
if (poker.getName().substring(2).equals("2"))
i += 13;
if (poker.getName().substring(2).equals("1"))
i += 13;
if (Common.getColor(poker) == 5)
i += 2;
return i;
}
public static void getMax(PokerIndex pokerIndex, ArrayList<Poker> list) {
int[] count = new int[14];
for (int i = 0; i < 14; i++)
count[i] = 0;
for (Poker poker : list) {
if (Common.getColor(poker) == 5)// 王
count[13]++;
else
count[Common.getValue(poker) - 1]++;
}
ArrayList<ArrayList<Integer>> indexList = pokerIndex.indexList;
for (int i = 0; i < 14; i++) {
switch (count[i]) {
case 1 -> indexList.get(0).add(i + 1);
case 2 -> indexList.get(1).add(i + 1);
case 3 -> indexList.get(2).add(i + 1);
case 4 -> indexList.get(3).add(i + 1);
}
}
}
public static Model getModel(ArrayList<Poker> list, int[] orders) {
ArrayList<Poker> list2 = new ArrayList<>(list);
Model model = new Model();
for (int order : orders) showOrders(order, list2, model);
return model;
}
public static void get123(ArrayList<Poker> list, Model model) {
ArrayList<Poker> del = new ArrayList<>();
if (list.size() > 0 && (Common.getValue(list.get(0)) < 7 || Common.getValue(list.get(list.size() - 1)) > 10))
return;
if (list.size() < 5)
return;
ArrayList<Poker> list2 = new ArrayList<>();
ArrayList<Poker> temp = new ArrayList<>();
ArrayList<Integer> integers = new ArrayList<>();
for (Poker poker : list2) {
if (!integers.contains(Common.getValue(poker))) {
integers.add(Common.getValue(poker));
temp.add(poker);
}
}
Common.order(temp);
for (int i = 0, len = temp.size(); i < len; i++) {
int k = i;
for (int j = i; j < len; j++) {
if (Common.getValue(temp.get(i)) - Common.getValue(temp.get(j)) == j - i) {
k = j;
}
}
if (k - i >= 4) {
String s = "";
for (int j = i; j < k; j++) {
s += temp.get(j).getName() + ",";
del.add(temp.get(j));
}
s += temp.get(k).getName();
del.add(temp.get(k));
model.a123.add(s);
i = k;
}
}
list.removeAll(del);
}
public static void getTwoTwo(ArrayList<Poker> list, Model model) {
ArrayList<String> del = new ArrayList<>();
ArrayList<String> l = model.a2;
if (l.size() < 3)
return;
common(del, l, model);
l.removeAll(del);
}
public static void getPlane(ArrayList<Poker> list, Model model) {
ArrayList<String> del = new ArrayList<>();
ArrayList<String> l = model.a3;
if (l.size() < 2)
return;
common(del, l, model);
l.removeAll(del);
}
public static void common(ArrayList<String> del, ArrayList<String> l, Model model) {
Integer[] s = new Integer[l.size()];
for (int i = 0, len = l.size(); i < len; i++) {
String[] name = l.get(i).split(",");
s[i] = Integer.parseInt(name[0].substring(2));
}
for (int i = 0, len = l.size(); i < len; i++) {
int k = i;
for (int j = i; j < len; j++) {
if (s[i] - s[j] == j - i)
k = j;
}
if (k - i >= 2) {
String ss = "";
for (int j = i; j < k; j++) {
ss += l.get(j) + ",";
del.add(l.get(j));
}
ss += l.get(k);
model.a112233.add(ss);
del.add(l.get(k));
i = k;
}
}
}
public static void getBomb(ArrayList<Poker> list, Model model) {
ArrayList<Poker> del = new ArrayList<>();
if (list.size() < 1)
return;
if (list.size() >= 2 && Common.getColor(list.get(0)) == 5 && Common.getColor(list.get(1)) == 5) {
model.a4.add(list.get(0).getName() + "," + list.get(1).getName());
del.add(list.get(0));
del.add(list.get(1));
}
if (Common.getColor(list.get(0)) == 5 && Common.getColor(list.get(0)) != 5) {
del.add(list.get(0));
model.a1.add(list.get(0).getName());
}
list.removeAll(del);
for (int i = 0, len = list.size(); i < len; i++) {
if (i + 3 < len && Common.getValue(list.get(i)) == Common.getValue(list.get(i + 3))) {
String s = list.get(i).getName() + ",";
s += list.get(i + 1).getName() + ",";
s += list.get(i + 2).getName() + ",";
s += list.get(i + 3).getName();
model.a4.add(s);
for (int j = i; j <= i + 3; j++)
del.add(list.get(j));
i = i + 3;
}
}
list.removeAll(del);
}
public static void getThree(ArrayList<Poker> list, Model model) {
ArrayList<Poker> del = new ArrayList<>();
for (int i = 0, len = list.size(); i < len; i++) {
if (i + 2 < len && Common.getValue(list.get(i)) == Common.getValue(list.get(i + 2))) {
String s = list.get(i).getName() + ",";
s += list.get(i + 1).getName() + ",";
s += list.get(i + 2).getName();
model.a3.add(s);
for (int j = i; j <= i + 2; j++)
del.add(list.get(j));
i = i + 2;
}
}
list.removeAll(del);
}
public static void getTwo(ArrayList<Poker> list, Model model) {
ArrayList<Poker> del = new ArrayList<>();
for (int i = 0, len = list.size(); i < len; i++) {
if (i + 1 < len && Common.getValue(list.get(i)) == Common.getValue(list.get(i + 1))) {
String s = list.get(i).getName() + ",";
s += list.get(i + 1).getName();
model.a2.add(s);
for (int j = i; j <= i + 1; j++)
del.add(list.get(j));
i = i + 1;
}
}
list.removeAll(del);
}
public static void getSingle(ArrayList<Poker> list, Model model) {
ArrayList<Poker> del = new ArrayList<>();
for (Poker poker : list) {
model.a1.add(poker.getName());
del.add(poker);
}
list.removeAll(del);
}
public static void hideCards(ArrayList<Poker> list) {
for (Poker poker : list) {
poker.setVisible(false);
}
}
public static int checkCards(ArrayList<Poker> c, ArrayList<ArrayList<Poker>> current, GameJFrame m) {
ArrayList<Poker> currentList;
if (m.time[0].getText().equals("不要"))
currentList = current.get(2);
else
currentList = current.get(0);
PokerType cType = Common.judgeType(c);
PokerType cType2 = Common.judgeType(currentList);
if (cType != PokerType.c4 && c.size() != currentList.size())
return 0;
if (cType != PokerType.c4 && Common.judgeType(c) != Common.judgeType(currentList)) {
return 0;
}
if (cType == PokerType.c4) {
if (c.size() == 2)
return 1;
if (cType2 != PokerType.c4) {
return 1;
}
}
if (cType == PokerType.c1 || cType == PokerType.c2 || cType == PokerType.c3 || cType == PokerType.c4) {
if (Common.getValue(c.get(0)) <= Common.getValue(currentList.get(0))) {
return 0;
} else {
return 1;
}
}
if (cType == PokerType.c123 || cType == PokerType.c112233 || cType == PokerType.c111222) {
if (Common.getValue(c.get(0)) <= Common.getValue(currentList.get(0)))
return 0;
else
return 1;
}
if (cType == PokerType.c31 || cType == PokerType.c32 || cType == PokerType.c411 || cType == PokerType.c422
|| cType == PokerType.c11122234 || cType == PokerType.c1112223344) {
List<Poker> a1 = Common.getOrder2(c);
List<Poker> a2 = Common.getOrder2(currentList);
if (Common.getValue(a1.get(0)) < Common.getValue(a2.get(0)))
return 0;
}
return 1;
}
public static ArrayList<Poker> getOrder2(List<Poker> list) {
ArrayList<Poker> list2 = new ArrayList<>(list);
ArrayList<Poker> list3 = new ArrayList<>();
int len = list2.size();
int[] a = new int[20];
for (int i = 0; i < 20; i++)
a[i] = 0;
for (int i = 0; i < len; i++) {
a[Common.getValue(list2.get(i))]++;
}
int max = 0;
for (int i = 0; i < 20; i++) {
max = 0;
for (int j = 19; j >= 0; j--) {
if (a[j] > a[max])
max = j;
}
for (int k = 0; k < len; k++) {
if (Common.getValue(list2.get(k)) == max) {
list3.add(list2.get(k));
}
}
list2.remove(list3);
a[max] = 0;
}
return list3;
}
public static void showOrders(int i, ArrayList<Poker> list, Model model) {
switch (i) {
case 1 -> Common.getSingle(list, model);
case 2 -> {
Common.getTwo(list, model);
Common.getTwoTwo(list, model);
}
case 3 -> {
Common.getThree(list, model);
Common.getPlane(list, model);
}
case 4 -> Common.getBomb(list, model);
case 5 -> Common.get123(list, model);
}
}
}
class PokerIndex {
ArrayList<ArrayList<Integer>> indexList = new ArrayList<>();
}
实体类
1.牌的实体类
package com.doudizhu.domain;
import com.doudizhu.game.GameJFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Poker extends JLabel implements MouseListener {
//记得换成自己的路径哦
private static final String basePath = "D:\\develop\\[a04]\\";
//游戏的主界面
GameJFrame gameJFrame;
//牌的名字
String name;
//牌显示正面还是反面
boolean up;
//是否可点击
boolean canClick = false;
//当前状态,是否已经被点击
boolean clicked = false;
public Poker(GameJFrame m, String name, boolean up) {
this.gameJFrame = m;
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 Poker() {
}
public Poker(GameJFrame gameJFrame, String name, boolean up, boolean canClick, boolean clicked) {
this.gameJFrame = gameJFrame;
this.name = name;
this.up = up;
this.canClick = canClick;
this.clicked = clicked;
}
//显示正面
public void turnFront() {
this.setIcon(new ImageIcon(basePath + "doudizhu\\image\\poker\\" + name + ".png"));
this.up = true;
}
//显示背面
public void turnRear() {
this.setIcon(new ImageIcon(basePath + "doudizhu\\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;
Point to = new Point(from.x, from.y + step);
this.setLocation(to);
}
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
public void mousePressed(MouseEvent e) {
}
/**
* 获取
*/
public GameJFrame getGameJFrame() {
return gameJFrame;
}
/**
* 设置
*/
public void setGameJFrame(GameJFrame gameJFrame) {
this.gameJFrame = gameJFrame;
}
/**
* 获取
*/
public String getName() {
return name;
}
/**
* 设置
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
*/
public boolean isUp() {
return up;
}
/**
* 设置
*/
public void setUp(boolean up) {
this.up = up;
}
/**
* 获取
*/
public boolean isCanClick() {
return canClick;
}
/**
* 设置
*/
public void setCanClick(boolean canClick) {
this.canClick = canClick;
}
/**
* 获取
*/
public boolean isClicked() {
return clicked;
}
/**
* 设置
*/
public void setClicked(boolean clicked) {
this.clicked = clicked;
}
public String toString() {
return "Poker{gameJFrame = " + gameJFrame + ", name = " + name + ", up = " + up + ", canClick = " + canClick + ", clicked = " + clicked + "}";
}
}
2.玩家的实体类
package com.doudizhu.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;
}
/**
* 获取
*/
public String getUsername() {
return username;
}
/**
* 设置
*/
public void setUsername(String username) {
this.username = username;
}
/**
* 获取
*/
public String getPassword() {
return 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 + "}";
}
}
CodeUtils工具类
package com.doudizhu.utils;
import java.util.ArrayList;
import java.util.Random;
public class CodeUtils {
public static String getCode() {
//1.创建一个集合
ArrayList<Character> list = new ArrayList<>();//52 索引的范围:0 ~ 51
//2.添加字母 a - z A - Z
for (int i = 0; i < 26; i++) {
list.add((char) ('a' + i));//a - z
list.add((char) ('A' + i));//A - Z
}
//3.打印集合
//System.out.println(list);
//4.生成4个随机字母
String result = "";
Random r = new Random();
for (int i = 0; i < 4; i++) {
//获取随机索引
int randomIndex = r.nextInt(list.size());
char c = list.get(randomIndex);
result = result + c;
}
//System.out.println(result);//长度为4的随机字符串
//5.在后面拼接数字 0~9
int number = r.nextInt(10);
//6.把随机数字拼接到result的后面
result = result + number;
//System.out.println(result);//ABCD5
//7.把字符串变成字符数组
char[] chars = result.toCharArray();//[A,B,C,D,5]
//8.在字符数组中生成一个随机索引
int index = r.nextInt(chars.length);
//9.拿着4索引上的数字,跟随机索引上的数字进行交换
char temp = chars[4];
chars[4] = chars[index];
chars[index] = temp;
//10.把字符数组再变回字符串
String code = new String(chars);
//System.out.println(code);
return code;
}
}
Model类
列举出所有的出牌情况,用集合表示每一种可能的情况。
package com.doudizhu.game;
import java.util.ArrayList;
public class Model {
int value; //权值
int num;// 手数 (几次能够走完,没有挡的情况下)
ArrayList<String> a1 = new ArrayList<>(); //单张
ArrayList<String> a2 = new ArrayList<>(); //对子
ArrayList<String> a3 = new ArrayList<>(); //3带
ArrayList<String> a123 = new ArrayList<>(); //连子
ArrayList<String> a112233 = new ArrayList<>(); //连牌
ArrayList<String> a111222 = new ArrayList<>(); //飞机
ArrayList<String> a4 = new ArrayList<>(); //炸弹
}
App游戏启动类
游戏启动的入口,运行即可开始游戏。
package com.doudizhu;
import com.doudizhu.game.GameJFrame;
import com.doudizhu.game.LoginJFrame;
public class App {
public static void main(String[] args) {
System.out.println("斗地主,启动!");
new LoginJFrame();
//new GameJFrame();
}
}
完整的代码会放在评论区里哦,快去自己试试吧!