java ----实现简单的21点纸牌游戏

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/angry_youth/article/details/74616597

- 游戏设计思路:
电脑和玩家的比拼:谁手中扑克牌的“点数和”首先达到21点为获胜方,或者在双方都叫停的情况下谁的“点数和”越接近21点且不大于21点为获胜方,凡是大于21点或者“点数和:小于对方的”点数和“的一方为输。
大小王的点数为0;
J,Q,K的点数为10;
其余的点数都与自己所表示的数字一样;

设计方法:
1.定义一个类:设置每一张扑克牌所具有的属性,将其点数,花色等等设置好;
2.定义一个类:设置一副扑克牌所具有的属性,比如洗牌,加载图片等;
3.定义一个游戏界面类:将整个游戏界面显示出来,其中包括一些游戏逻辑函数,计算得分,绘图等等操作;
4.定义一个游戏开始的入口类,加载游戏界面和一些基本的界面属性按键,设计游戏整体界面的格局;

说多了都是泪(类),直接上代码整!


Cards类:一张扑克牌的属性设置,比如点数,花色等;

package cords_21_game;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;

import javax.swing.JPanel;

public class Cards {//每一张扑克牌的对象属性和功能
    //创建每张牌的属性
    private String color;//花色
    private String value;//面值
    private Image cardImage;//每张牌的图像
    public int count;//每张牌的点数
    private int x = 0;
    private int y = 0;//定义好每张扑克牌的位置坐标
    private int cardWidth = 80;
    private int cardHeight = 100;
    private Image imagebackground = Toolkit.getDefaultToolkit().getImage("F:/Image/21_cards/background.png");//这是我的图片存储路径

    public Cards(String color, String value, Image cardImage)//正常牌初始化
    {
        this.color = color;
        this.value = value;
        this.cardImage = cardImage;
        if(value == "J" || value == "Q" || value == "K")
        {
            this.count = 10;
        }
        else if(value == "A")
        {
            this.count = 1;
        }
        else 
        {
            this.count = Integer.parseInt(value);
        }
    }
    public Cards(String value, Image cardImage)//大小王初始化
    {
        this.color = "王";
        this.value = value;//-----分为大王 小王
        this.cardImage = cardImage;
        this.count = 0;
    }

    public void setPos(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public void drawCards(Graphics g, JPanel i)//在面板i上面画扑克牌对象
    {
        g.drawImage(cardImage, x, y, cardWidth, cardHeight, (ImageObserver) i);
    }

    public void drawBackgroundCards(Graphics g, JPanel i)
    {
        g.drawImage(imagebackground, x, y, cardWidth, cardHeight, (ImageObserver) i);
    }

    public String getColor()//得到当前对象的花色
    {
        return color;
    }

    public String getValue()//得到当前对象的面值-----“A,2,3,4,5,6。。。”
    {
        return value;
    }

    public int getCount()//得到当前对象的点数
    {
        return count;
    }

    public void print()
    {
        System.out.print("花色"+color);
        System.out.print("  面值"+value);
        System.out.println("  点数"+count);

    }
}


Poker类:一副扑克牌的一些基本属性,比如牌的图片,牌的获取,洗牌等操作;

package cords_21_game;

import java.awt.Image;
import java.awt.Toolkit;
import java.util.Random;
//import java.util.Random;

public class Poker {
    static final int COUNT = 54;
    static String color[] = {"红桃", "黑桃", "方片", "梅花"};
    static String value[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    private Image cardsImage[] = new Image[COUNT];
    static Cards cards[] = new Cards[COUNT];
    public void getcardsImage()//获取图片信息-----在创建对象以后
    {
        for(int i = 0; i < color.length; i++)//花色
        {
            for(int j = 0; j < value.length; j++)//面值
            {
                cardsImage[i*value.length + j] = Toolkit.getDefaultToolkit().getImage("F:/Image/21_cards\\"+(i+1)+"-"+(j+1)+".png");
            }
        }
        cardsImage[COUNT-2] = Toolkit.getDefaultToolkit().getImage("F:/Image/21_cards/5-0.png");//小王
        cardsImage[COUNT-1] = Toolkit.getDefaultToolkit().getImage("F:/Image/21_cards/5-1.png");//大王
    }

    public Poker()
    {
        this.getcardsImage();
        for(int i = 0; i < color.length; i++)
        {
             for(int j = 0; j < value.length; j++)
            {
                cards[i*13+j] = new Cards(color[i], value[j], cardsImage[i*13+j]);
            }
        }
        cards[COUNT-2] = new Cards("小王", cardsImage[COUNT-2]);
        cards[COUNT-1] = new Cards("大王", cardsImage[COUNT-1]);
    }

    public void WashCards()
    {
        Random random = new Random();
        for(int i = 0; i < cards.length; i++)
        {
            int p = random.nextInt(cards.length);
            Cards temp = cards[i];
            cards[i] = cards[p];
            cards[p] = temp;
        }
    }


    public Cards[] getCard()//获取一副扑克对象
    {
        return Poker.cards;
    }
    public Cards getOneCard(int n)//获取一张扑克牌对象
    {
        return Poker.cards[n-1];
    }
    public void show()//显示所有的牌对象color 和 value信息
    {
        for(int i = 0; i < COUNT; i++)
        {
            cards[i].print();
        }
        System.out.println();
    }
}


PorkerPanel类:游戏界面类,真正的游戏显示界面设计,也包含了游戏的计算得分,逻辑判断等;
package cords_21_game;

import java.awt.Graphics;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class PokerPanel extends JPanel{
    Poker poker = new Poker();//创建一个扑克牌对象
    int myScore = 0;
    int computerScore = 0;//创建玩家得分和电脑得分初始化为0
    ArrayList myCardsList = new ArrayList();
    ArrayList computerCardsList = new ArrayList();//创建两个得分机制数组,一个存放玩家得分,一个存放电脑得分
    boolean mylose = false;
    boolean computerlose = false;//创建两个变量判断玩家电脑谁输谁赢
    static int n = 1;//n用来表示当前发的牌的ID
    public boolean ComputerContinue = true;

    public PokerPanel()//构造函数
    {
        poker.WashCards();//先洗牌
        poker.show();//后台显示一下牌
        this.setVisible(true);
        repaint();
    }

    public void paint(Graphics g)
    {
        //super.paint(g);
        g.clearRect(0, 0, this.getWidth(), this.getHeight());//清屏
        g.drawString("电脑牌", 400, 100);
        for(int i = 0; i < computerCardsList.size(); i++)
        {
            Cards card = (Cards) computerCardsList.get(i);
            card.setPos(i*50, 100);//设置扑克牌图像的宽度为50
            card.print();
            if(computerlose == true || mylose == true)
            {
                card.drawCards(g, this);
            }
            else
            {
                card.drawBackgroundCards(g, this);
            }
        }
        g.drawString("玩家牌", 400, 300);
        for(int i = 0; i < myCardsList.size(); i++)
        {
            Cards card = (Cards) myCardsList.get(i);
            card.setPos(i*50, 300);
            card.print();
            card.drawCards(g, this);
        }
        System.out.println();
    }


    public void myPlayer()//给玩家发牌
    {
        myCardsList.add(poker.getOneCard(n));//获取一张扑克牌
        computeMyScore();//计算玩家得分
        repaint();
        IsOrNoWashCards();
        if(myScore > 21)
        {
            mylose = true;
//          repaint();
            JOptionPane.showMessageDialog(null, "玩家输!电脑赢!\n电脑得分:"+computerScore+"\n玩家得分:"+myScore, "提示", JOptionPane.OK_OPTION);
            repaint();
        }
    }
    public void computeMyScore()
    {
        myScore = 0;
        for(int i = 0; i < myCardsList.size(); i++)
        {
            Cards card = (Cards) myCardsList.get(i);
            myScore += card.count;
        }
    }

    public void computerPlayer()
    {
        computerCardsList.add(poker.getOneCard(n));
        computeComputerScore();
        repaint();
        IsOrNoWashCards();
        if(computerScore > 21)
        {
            computerlose = true;
            ComputerContinue = false;
//          repaint();
            JOptionPane.showMessageDialog(null, "玩家赢,电脑输!\n电脑得分:"+computerScore+"\n玩家得分:"+myScore, "提示", JOptionPane.OK_OPTION);
            repaint();
        }
        else if(computerScore > 18)//电脑停牌不叫
        {
            ComputerContinue = false;
            JOptionPane.showMessageDialog(null, "电脑停牌不叫!", "提示", JOptionPane.OK_OPTION);
        }

    }
    public void computeComputerScore()
    {
        computerScore = 0;
        for(int i = 0; i < computerCardsList.size(); i++)
        {
            Cards card = (Cards) computerCardsList.get(i);
            computerScore += card.count;
        }
    }

    public void ComputeAllScore()
    {
        this.computeMyScore();
        this.computeComputerScore();
        if(myScore > computerScore)
        {
            computerlose = true;
            JOptionPane.showMessageDialog(null, "玩家赢了,电脑输了!\n电脑得分:"+computerScore+"\n玩家得分:"+myScore, "赢了", JOptionPane.OK_OPTION);
        }
        else if(myScore < computerScore)
        {
            JOptionPane.showMessageDialog(null, "电脑赢了,玩家输了!\n电脑得分:"+computerScore+"\n玩家得分:"+myScore, "输了", JOptionPane.OK_OPTION);
            mylose = true;
        }
        else
        {
            JOptionPane.showMessageDialog(null, "平局!\n电脑得分:"+computerScore+"\n玩家得分:"+myScore, "平局", JOptionPane.OK_OPTION);
            mylose = true;
        }
        repaint();
    }

    public void ReGame()
    {
        myScore = 0;
        computerScore = 0;
        mylose = false;
        computerlose = false;
        ComputerContinue = true;
        myCardsList.clear();
        computerCardsList.clear();
        for(int i = 0; i < 2; i++)
        {
            myPlayer();
            computerPlayer();
            repaint();
        }
    }

    public void IsOrNoWashCards()
    {
        if(n >= 54)
        {
            n = 1;
            poker.WashCards();
        }
        else
            n++;
    }

}


Game类:整个游戏的入口,存放游戏面板和一些基本的面板属性;

package cords_21_game;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Game extends JFrame{
    PokerPanel pokerpanel = new PokerPanel();
    JButton button1 = new JButton("玩家要牌");
    JButton button2 = new JButton("玩家停牌");
    JButton button3 = new JButton("重新开局");
    JButton button4 = new JButton("退出游戏");

    public Game()//构造函数
    {
        JPanel paneone = new JPanel();
        JPanel panetwo = new JPanel();

        panetwo.setLayout(new FlowLayout());//第三个面板
        panetwo.add(button1);
        panetwo.add(button2);
        panetwo.add(button3);
        panetwo.add(button4);

        Container container = this.getContentPane();
        container.setLayout(new BorderLayout());
        container.add(pokerpanel,BorderLayout.CENTER);
        container.add(panetwo,BorderLayout.SOUTH);

        this.setTitle("21点纸牌游戏");
        this.setVisible(true);
        this.setBounds(100, 100, 500, 600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button1.addMouseListener(new MouseAdapter()//玩家要牌的阶段
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                System.out.println("您点击了玩家要牌模式");
                pokerpanel.myPlayer();
                if(pokerpanel.ComputerContinue && pokerpanel.mylose == false)
                {
                    pokerpanel.computerPlayer();
                }
            }

        });
        button2.addMouseListener(new MouseAdapter()//玩家停牌阶段
        {
            public void mouseClicked(MouseEvent e)
            {
                System.out.println("您点击了玩家停牌模式");
                while(pokerpanel.ComputerContinue)
                {
                    pokerpanel.computerPlayer();
                }
                //计算总成绩
                if(pokerpanel.mylose == false && pokerpanel.computerlose == false)
                {
                    pokerpanel.ComputeAllScore();
                }
            }
        });
        button3.addMouseListener(new MouseAdapter()//游戏重新开始
        {
            public void mouseClicked(MouseEvent e)
            {
                System.out.println("您点击了重新开始模式");
                pokerpanel.ReGame();
            }
        });
        button4.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent e)
            {
                System.exit(1);
            }
        });

    }

    public static void main(String[] args) {
        new Game();
    }

}

游戏运行结果显示:
一开始运行程序后就出现如下界面,点击玩家要牌开始游戏!
这里写图片描述

大小王的得分是0,J,Q,K都是10点,谁的牌数和越接近21点越容易赢,玩家根据点数判断自己会否要牌还是停牌!
这里写图片描述

这里写图片描述

展开阅读全文

没有更多推荐了,返回首页