Feelings:
At first do not understand or is not familiar with the characteristics of Java, do not know the relationship between classes and classes, Java encapsulation, inheritance, polymorphism and no better understanding, in the future learning process to master and familiar with Java development habits,
In the process of writing code, there are many things not mastered, relying on the help of teachers and classmates to complete this study, a preliminary understanding of Java object-oriented development of the basic essentials. Write this blog so that you can learn in the future, but also a good memory of it!
Above is a class diagram that represents the idea of writing this program.
Card class: His subordinate is CardView Superior is the player, has a certain relationship
Package Com.cqvie;
public class Card
{
public int rank,suit;
public int cardidx;
Public CardView CardView;
Public Player owner;
public Boolean selected=false;
public static String suits[]
=new string[]{"", "♠", "♥", "♣", "♦"};
public static String ranks[]
=new string[]{"J", "Q", "K", "A", "2", "Xiao Wang", "King"};
Public Card (int rank,int suit)
{
This.rank=rank; This.suit=suit;
This.cardview=new CardView (this); To generate the CardView object synchronously when the card object is generated
}
Public String toString ()
{
if (rank<=10)//10 pips or less
return Suits[suit]+rank;
else//over 10 pips
return suits[suit]+ ranks[rank-11];
}
}
CardView class:
Package Com.cqvie;
Import Java.awt.Font;
Import Java.awt.Insets;
Import Java.awt.Panel;
Import Java.awt.event.MouseAdapter;
Import java.awt.event.MouseEvent;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
public class CardView {
Private card card; The associated card
Private JButton btn;
private static JFrame win; The window of attachment
Public Card Getcard () {
return card;
}
public void Setcard (card) {
This.card = Card;
}
Public JButton getbtn () {
return BTN;
}
public void setbtn (JButton btn) {
THIS.BTN = BTN;
}
Public CardView (final card) {
This.card = Card;
BTN = new JButton ();
Btn.setsize (50, 80);
Btn.setmargin (New Insets (0, 0, 0, 0)); Margins for text and controls
Btn.setfont (New Font (Btn.getfont (). GetName (), Font.Bold, 16)); Font settings
Btn.addmouselistener (New Mouseadapter () {
@Override
public void mouseclicked (MouseEvent e) {
System.out.println (Card.tostring ());
card.selected=!card.selected;
JButton b= (JButton) E.getsource ();
if (card.selected) b.setlocation (B.getx (), B.gety ()-20);
Else B.setlocation (B.getx (), b.gety () +20);
}
});
}
public void Show ()
{
Btn.settext (Card.tostring ());
int x=card.cardidx*btn.getwidth () +50;
y=card.owner.payeridx*150+50;
Btn.setlocation (x,20);
This.card.owner.playerView.panel.add (BTN);
}
public void Refresh ()//Refresh button position
{
int x=card.cardidx*btn.getwidth () +50;
Btn.setlocation (x,20);
}
}
Player class:
Package Com.cqvie;
Import java.util.Collections;
Import Java.util.Comparator;
Import java.util.LinkedList;
Import java.util.List;
public class Player {
public String ID;
public int payeridx;
public Boolean Islandlord;
public list<card> cards;
Public Playerview Playerview;
Public Player (int payeridx)
{
This.payeridx=payeridx;
Islandlord=false;
Cards=new linkedlist<card> ();
This.playerview=new Playerview (this);
}
public void Putcards ()//card
{
for (int i=cards.size () -1;i>=0;i--)
if (Cards.get (i). Selected)
{
Remove a card object from the Cards collection
Card c= Cards.remove (i);
This.playerView.panel.remove (C.CARDVIEW.GETBTN ());
C.CARDVIEW.GETBTN (). setvisible (false);
}
Adjust the position of the button after the card
for (int i=0;i<cards.size (); i++)//Update Cardidx
{
Cards.get (i). Cardidx=i;
Cards.get (i). Cardview.refresh ();
}
This.playerView.panel.repaint (); Refresh Panel
}
public void Sortcards ()//Sort
{
Collections.sort (Cards,new cardcomparetor ());
for (int i=0;i<cards.size (); i++)//Update Cardidx
Cards.get (i). Cardidx=i;
}
}
Class Cardcomparetor implements Comparator<card>
{
@Override
public int Compare (card O1, card O2) {
if (O1.rank!=o2.rank) return o2.rank-o1.rank;
else return o1.suit-o2.suit;
}
}
Playerview class:
Package Com.cqvie;
Import Java.awt.Color;
Import Java.awt.event.MouseAdapter;
Import java.awt.event.MouseEvent;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JPanel;
public class Playerview {
public player player;
public JPanel Panel;
Public Playerview (Player p)
{
This.player=p;
Panel=new JPanel ();
Panel.setlayout (NULL); Absolute layout
Panel.setsize (1300, 160);
Panel.setbackground (New Color (255, 255, 0));
}
public void Show ()
{
Panel.setlocation (Panel.getheight () +5) *player.payeridx);
GameManager.win.add (panel);
for (int i=0;i<this.player.cards.size (); i++)
This.player.cards.get (i). Cardview.show ();
Show Player Info
JLabel label=new JLabel ();
Label.setsize (100,50);
Label.setlocation (50, 100);
Label.settext ("Player" + (this.player.payeridx+1));
Panel.add (label);
Add a "card" button
JButton b=new JButton ();
B.setsize (80,40);
B.setlocation (150, 120);
B.settext ("card");
Panel.add (b);
B.addmouselistener (
New Mouseadapter () {
@Override
public void mouseclicked (MouseEvent e) {
Player.putcards (); Player is an attribute of an external class
}
});
}
}
Gamemanager class:
Package Com.cqvie;
Import java.util.LinkedList;
Import java.util.List;
Import Java.util.Random;
Import Javax.swing.JFrame;
public class Gamemanager {
public static JFrame win; The owning window (public)
Public player[] Players=null; Player Collection
Public Gamemanager ()
{
Win=new Mywindow ();
Players=new Player[3];
for (int i=0;i<players.length;i++)
Players[i]=new Player (i);
}
public void Startgame ()
{
List<card> cards=new linkedlist<card> (); Poker
for (int i=3;i<=15;i++)
for (int j=1;j<=4;j++)
Cards.add (New Card (I,J));
Cards.add (New Card (16,0));
Cards.add (New Card (17,0));
Random r=new random (); Random number generator
for (int i=0;i<players.length;i++)
{
for (int j=0;j<17;j++)
{
int K=r.nextint (cards.size ());
Cards.get (k). Owner=players[i];
Cards.get (k). Cardidx=j;
Players[i].cards.add (Cards.get (k));
Cards.remove (k);
}
Players[i].sortcards ();
}
int t = (int) (Math.random () *players.length);
Players[t].islandlord=true;
if (Players[t].islandlord)
for (int m=17;m<=19;m++) {
int k = (int) (Math.random () * cards.size ());
Cards.get (k). Owner=players[t];
Cards.get (k). Cardidx=m;
Players[t].cards.add (Cards.get (k));
Cards.remove (k);
}
Players[t].sortcards ();
for (int i=0;i<players.length;i++)
// {
System.out.print (players[i].id+players[i].islandlord+ "the card is");
Players[i].cards.print ();
System.out.print ("\ n------------------------\ n");
// }
}
public void Show ()
{
for (int i=0;i<players.length;i++)
for (int j=0;j<players[i].cards.size (); j + +)
// {
Players[i].cards.get (j). Cardview.show ();
// }
for (int i=0;i<players.length;i++)
Players[i].playerview.show ();
Win.setvisible (TRUE);
}
}
Window (Win) class:
Package Com.cqvie;
Import javax.swing.*;
public class Mywindow extends JFrame {
Public Mywindow () {
This.setsize (1500, 800);
This.setlayout (NULL);
This.setdefaultcloseoperation (Jframe.exit_on_close);
}
}
Test class:
Package Com.cqvie;
public class Test {
public static void Main (string[] args) {
Gamemanager gm=new Gamemanager ();
Gm.startgame ();
Gm.show ();
}
}
Java Learning Object-oriented poker (at the beginning)