行业知识
十四、综合项目(斗地主)
2026-10-20 01:50:57
来源:互联网转载
综合项目(斗地主)
- 1.准备牌、洗牌、发牌、看牌
- 2.对每人手中的牌进行排序
- 2.1 排序方法1(利用序号进行排序)
- 2.2排序方法2(给每一张牌计算价值)
- 3.两个实体类
- 3.1 User
- 3.2 Poker
- 4.登录页面
- 4.1 验证码代码
- 4.2 登录界面
- 5.���戏界面
1.准备牌、洗牌、发牌、看牌
public class PokerGame { static ArrayList list = new ArrayList(); //准备牌 static { String[] color = {"♠", "♥", "♦", "♣"}; String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"}; for (String s : color) { for (String n : number) { list.add(s + 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; i4.2 登录界面
package farmerAndLoder.game;import farmerAndLoder.domain.User;import farmerAndLoder.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); } //初始化组件,在这个界面中添加内容 private void InitView() { //0.添加用户名文字 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); //1.添加用户名输入框 username.setBounds(236, 46, 200, 30); this.getContentPane().add(username); //2.添加密码文字 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); //3.密码输入框 password.setBounds(263, 87, 160, 30); this.getContentPane().add(password); //4.验证码提示 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); //5.验证码的输入框 code.setBounds(291, 133, 100, 30); this.getContentPane().add(code); //6.获取正确的验证码 String codeStr = CodeUtil.getCode3(); Font rightCodeFont = new Font(null, 1, 15); rightCode.setForeground(Color.RED); rightCode.setFont(rightCodeFont); rightCode.setText(codeStr); rightCode.setBounds(400, 133, 100, 30); this.getContentPane().add(rightCode); //登录按钮 login.setBounds(170, 310, 128, 47); login.setIcon(new ImageIcon("day25_code\src\farmerAndLoder\image\login/登录按钮.png")); login.setBorderPainted(false); login.setContentAreaFilled(false); login.addMouseListener(this); this.getContentPane().add(login); //7.添加注册按钮 register.setBounds(310, 310, 128, 47); register.setIcon(new ImageIcon("day25_code\src\farmerAndLoder\image\login/注册按钮.png")); register.setBorderPainted(false); register.setContentAreaFilled(false); register.addMouseListener(this); this.getContentPane().add(register); //设置背景 JLabel background = new JLabel(new ImageIcon("day25_code\src\farmerAndLoder\image\login/background.png")); background.setBounds(0, 0, 633, 423); this.getContentPane().add(background); } //初始化组件 private void InitJFrame() { this.setSize(633, 423);//设置宽高 this.setTitle("斗地主游戏 V1.0 登录");//设置标题 this.setDefaultCloseOperation(EXIT_ON_CLOSE);//设置关闭模式 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 (userNameInput.length() == 0 || passwordInput.length() == 0) { showJDialog("用户名或者密码为空"); return; } //判断验证码是否为空 if (codeInput.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) { showJDialog("点击了注册按钮"); } else if (obj == rightCode) { //获取一个新的验证码 String code = CodeUtil.getCode3(); System.out.println(code); rightCode.setText(code); } } private void showJDialog(String content) { JDialog jDialog = new JDialog(); jDialog.setSize(200, 150); jDialog.setAlwaysOnTop(true); jDialog.setLocationRelativeTo(null); jDialog.setModal(true); JLabel warning = new JLabel(content); warning.setBounds(0, 0, 200, 150); jDialog.getContentPane().add(warning); jDialog.setVisible(true); } //按下不松 @Override public void mousePressed(MouseEvent e) { Object obj = e.getSource(); if (obj==login){ login.setIcon(new ImageIcon("day25_code\src\farmerAndLoder\image\login/登录按下.png")); } else if (obj==register) { register.setIcon(new ImageIcon("day25_code\src\farmerAndLoder\image\login/注册按下.png")); } } //按下释放 @Override public void mouseReleased(MouseEvent e) { Object obj = e.getSource(); if (obj==login){ login.setIcon(new ImageIcon("day25_code\src\farmerAndLoder\image\login/登录按钮.png")); } else if (obj==register) { register.setIcon(new ImageIcon("day25_code\src\farmerAndLoder\image\login/注册按钮.png")); } } //鼠标划入 @Override public void mouseEntered(MouseEvent e) { Object obj = e.getSource(); if (obj == rightCode){ System.out.println("刷新验证码"); } } //鼠标划出 @Override public void mouseExited(MouseEvent e) { }}5.游戏界面
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