Swing을 이용하여 랜덤한 포커패 100세트를 생성하는 프로그램 만들어보기
코드 소개
- Swing을 이용하여 GUI 화면에 버튼 배열을 만들고, 그 버튼을 이미지로 채운 뒤, 버튼의 역할(클릭할 때 패를 뒤집는 기능)을 구현한 프로그램이다.
- 각 세트별로 카드르 모두 뒤집으면 스코어보드에 패의 결과(족보)가 기록된다.
프로그램 미리보기
코드
-
GUIPokerGrameMain.java
/**
* Created on Dec 18, 2017
* @author msJo -- hufs.ac.kr, Dept of CES
* Copy Right -- Free for Educational Purpose
*/
package guiPokerGame;
import java.awt.EventQueue;
import javax.swing.UIManager;
public class GUIPokerGrameMain {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
GUIPokerGameFrame frame = new GUIPokerGameFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
-
GUIPokerGameFrame.java
/**
* Created on Dec 18, 2017
* @author msJo -- hufs.ac.kr, Dept of CES
* Copy Right -- Free for Educational Purpose
*/
package guiPokerGame;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder
import javax.swing.table.DefaultTableModel;
public class GUIPokerGameFrame extends JFrame {
private static final long serialVersionUID = 1L;
private static final int FIVE = 5;
private static final int SUFFLECOUNT = 30;
private static final int GENCOUNT = 100;
private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private static final int myCardPanel_WIDTH = FIVE * 80;
private static final int myCardPanel_HEIGHT = 130;
private static final int controlBtnPanel_Width = 60;
private static final int controlBtnPanel_HEIGHT = myCardPanel_HEIGHT;
private static final int scoreTablePanel_HEIGHT = 100;
private static final int SCREEN_WIDTH = myCardPanel_WIDTH + controlBtnPanel_Width;
private static final int SCREEN_HEIGHT = myCardPanel_HEIGHT + scoreTablePanel_HEIGHT;
private static final String cardImageFilePath = "/cardimages/";
private static final String cardImageSize = "_70x98";
String[] header = {"Set", /*"", "", "", "", "",*/ "Family Tree"};
DefaultTableModel model = new DefaultTableModel(null, header);
private Random rand = new Random();
private int randomNumber = Math.abs(rand.nextInt()) % 2;
private JPanel contentPane;
private JPanel[] myCardPanel = new JPanel [GENCOUNT];
private JPanel curCardPanel;
private JPanel controlBtnPanel;
private JPanel scoreTablePanel;
private JButton[][] btnCards = new JButton [GENCOUNT][FIVE];
private JComboBox cmbbxCardSets;
private JTable scoreTable;
private JScrollPane scrollPane;
GUIPokerGameFrame thisClass = this;
ArrayList<FiveCards> fcs = generate5CardList(GENCOUNT);
/**
* Create the frame.
*/
public GUIPokerGameFrame() {
initialize();
}
void initialize() {
setTitle("GUI Poker Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
setResizable(false);
setLocation((screenSize.width - SCREEN_WIDTH) / 2, (screenSize.height - SCREEN_HEIGHT) / 2);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
for(int i = 0; i < GENCOUNT; i++) {
myCardPanel[i] = new JPanel();
myCardPanel[i].setLayout(new GridLayout(1, FIVE, 0, 0));
myCardPanel[i].setPreferredSize(new Dimension(myCardPanel_WIDTH, myCardPanel_HEIGHT));
}
curCardPanel = myCardPanel[0];
contentPane.add(curCardPanel, BorderLayout.CENTER);
curCardPanel.setVisible(true);
controlBtnPanel = new JPanel();
controlBtnPanel.setLayout(new BorderLayout(0, 0));
controlBtnPanel.setPreferredSize(new Dimension(controlBtnPanel_Width, controlBtnPanel_HEIGHT));
contentPane.add(controlBtnPanel, BorderLayout.EAST);
sortTest5Cards();
boolean[][] isOneCardOpen = new boolean [GENCOUNT][FIVE];
for(int i = 0; i < GENCOUNT; i++) {
for(int j = 0; j < FIVE; j++) {
isOneCardOpen[i][j] = false;
}
}
boolean[] isFiveCardsOpen = new boolean [GENCOUNT];
for(int i = 0; i < GENCOUNT; i++) {
isFiveCardsOpen[i] = false;
}
for(int i = 0; i < GENCOUNT; i++) {
for(int j = 0; j < FIVE; j++) {
int p = i;
int q = j;
btnCards[i][j] = new JButton();
btnCards[i][j].setIcon(new ImageIcon(GUIPokerGameFrame.class.getResource(
cardImageFilePath + getCardCoverImageFileName() + cardImageSize + ".png")));
btnCards[i][j].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnCards[p][q].setIcon(new ImageIcon(GUIPokerGameFrame.class.getResource(
cardImageFilePath + getCardImageFileName(fcs.get(p).pelem[q]) + cardImageSize + ".png")));
isOneCardOpen[p][q] = true;
if(isOneCardOpen[p][FIVE-5] && isOneCardOpen[p][FIVE-4] && isOneCardOpen[p][FIVE-3]
&& isOneCardOpen[p][FIVE-2] && isOneCardOpen[p][FIVE-1]) {
isOneCardOpen[p][FIVE-5] = false;
isOneCardOpen[p][FIVE-4] = false;
isOneCardOpen[p][FIVE-3] = false;
isOneCardOpen[p][FIVE-2] = false;
isOneCardOpen[p][FIVE-1] = false;
isFiveCardsOpen[p] = true;
}
if(isFiveCardsOpen[p]) {
JOptionPane.showMessageDialog(thisClass,
fcs.get(p).distinctionFamilyTree(),
"About", JOptionPane.INFORMATION_MESSAGE);
model.addRow(new Object[]{Integer.toString(p+1), fcs.get(p).distinctionFamilyTree()});
isFiveCardsOpen[p] = false;
}
}
});
myCardPanel[i].add(btnCards[i][j]);
}
}
String[] cardSets = new String [GENCOUNT];
for(int i = 0; i < GENCOUNT; i++) {
cardSets[i] = ("" + (i + 1));
}
cmbbxCardSets = new JComboBox(cardSets);
cmbbxCardSets.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
curCardPanel.setVisible(false);
contentPane.remove(curCardPanel);
curCardPanel = myCardPanel[Integer.parseInt(cmbbxCardSets.getSelectedItem().toString()) - 1];
contentPane.add(curCardPanel, BorderLayout.CENTER);
curCardPanel.setVisible(true);
}
});
controlBtnPanel.add(cmbbxCardSets, BorderLayout.NORTH);
scoreTablePanel = new JPanel();
scoreTablePanel.setLayout(new BorderLayout(0, 0));
scoreTablePanel.setPreferredSize(new Dimension(0, scoreTablePanel_HEIGHT));
contentPane.add(scoreTablePanel, BorderLayout.SOUTH);
scrollPane = new JScrollPane();
scoreTablePanel.add(scrollPane, BorderLayout.CENTER);
scoreTable = new JTable(model);
scrollPane.setViewportView(scoreTable);
}
void sortTest5Cards() {
System.out.println("Sort each 5 Cards -- in the order of Poker game");
pokerOrder5Card(fcs);
printFiveCardsListwithFamilyTree(fcs);
System.out.println();
}
void pokerOrder5Card(ArrayList<FiveCards> L) {
FiveCards[] tmp = new FiveCards [L.size()];
int i = 0;
for(FiveCards iter : L) { // copy from L to tmp
tmp[i++] = iter;
}
Card tmpCard = new Card();
for(int j = 0; j < L.size(); j++) {
for(int a = 0; a < FIVE; a++) {
for(int b = a + 1; b < FIVE; b++) {
if((tmp[j].pelem[a].getRank() > tmp[j].pelem[b].getRank())
|| ((tmp[j].pelem[a].getRank() == tmp[j].pelem[b].getRank())
&& (tmp[j].pelem[a].getSuit() > tmp[j].pelem[b].getSuit()))) { // Compare(poker order) and Swap
tmpCard = tmp[j].pelem[a];
tmp[j].pelem[a] = tmp[j].pelem[b];
tmp[j].pelem[b] = tmpCard;
}
}
}
}
Card tmpCard2 = new Card();
for(int m = 0; m < L.size(); m++) {
if((tmp[m].distinctionRSFMT() == "Royal Straight Flush")
|| (tmp[m].distinctionRSFMT() == "Mountain")) {
tmpCard2 = tmp[m].pelem[0];
tmp[m].pelem[0] = tmp[m].pelem[1];
tmp[m].pelem[1] = tmp[m].pelem[2];
tmp[m].pelem[2] = tmp[m].pelem[3];
tmp[m].pelem[3] = tmp[m].pelem[4];
tmp[m].pelem[4] = tmpCard2;
}
}
for(int j = 0; j < L.size(); j++)
{ // adjust L, copy from Dictionary ordered tmp to L
L.set(j, tmp[j]);
}
}
void pokerOrder5CardSets(ArrayList<FiveCards> L) {
FiveCards[] tmp = new FiveCards [L.size()];
int i = 0;
for(FiveCards iter : L) { // copy from L to tmp
tmp[i++] = iter;
}
FiveCards tmpCard = new FiveCards();
for(int a = 0; a < L.size(); a++)
{
for(int b = a + 1; b < L.size(); b++)
{
if(tmp[a].getFamilyTreeValue() < tmp[b].getFamilyTreeValue())
{ // Compare(poker order) and Swap
tmpCard = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpCard;
}
}
}
FiveCards tmpFiveCards = new FiveCards();
/* Royal Straight Flush가 100개의 5Card set중 2개 이상 나왔을 때, 그들을 정렬하는 방법 */
for(int a = 0; a < L.size(); a++) { // if the number of Royal Straight Flush >= 2
for(int b = a + 1; b < L.size(); b++) {
if((tmp[a].getFamilyTreeValue() == FAMILYTREE.RSF)
&& (tmp[b].getFamilyTreeValue() == FAMILYTREE.RSF)) { // Suit compare and Swap
if(tmp[a].pelem[0].getSuit() < tmp[b].pelem[0].getSuit()) { // Suit compare and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
}
}
}
/* Back Straight Flush가 100개의 5Card set중 2개 이상 나왔을 때, 그들을 정렬하는 방법 */
for(int a = 0; a < L.size(); a++) { // if the number of Back Straight Flush >= 2
for(int b = a + 1; b < L.size(); b++) {
if((tmp[a].getFamilyTreeValue() == FAMILYTREE.BSF)
&& (tmp[b].getFamilyTreeValue() == FAMILYTREE.BSF)) { // Suit compare and Swap
if(tmp[a].pelem[0].getSuit() < tmp[b].pelem[0].getSuit()) {
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
}
}
}
/* Straight Flush가 100개의 5Card set중 2개 이상 나왔을 때, 그들을 정렬하는 방법 */
for(int a = 0; a < L.size(); a++) { // if the number of Straight Flush >= 2
for(int b = a + 1; b < L.size(); b++) {
if((tmp[a].getFamilyTreeValue() == FAMILYTREE.SF)
&& (tmp[b].getFamilyTreeValue() == FAMILYTREE.SF)) { // Suit compare and Swap
if(tmp[a].pelem[4].cardcmp(tmp[b].pelem[4]) < 0) {
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
}
}
}
Card tmpSameCard1; // 5card set의 비교(left, right중 left), 같은 카드의 정보를 저장할 변수
Card tmpSameCard2; // 5card set의 비교(left, right중 right), 같은 카드의 정보를 저장할 변수
Card tmpRestCard1; // 5card set의 비교(left, right중 left), 같지 않은 나머지 카드의 정보를 저장할 변수
Card tmpRestCard2; // 5card set의 비교(left, right중 right), 같지 않은 나머지 카드의 정보를 저장할 변수
/* Four Card가 100개의 5Card set중 2개 이상 나왔을 때, 그들을 정렬하는 방법 */
for(int a = 0; a < L.size(); a++) { // if the number of Four Card >= 2
for(int b = a + 1; b < L.size(); b++) {
if((tmp[a].getFamilyTreeValue() == FAMILYTREE.FC)
&& (tmp[b].getFamilyTreeValue() == FAMILYTREE.FC)) {
if(tmp[a].pelem[0].getRank() == tmp[a].pelem[3].getRank()) {
tmpSameCard1 = tmp[a].pelem[3];
tmpRestCard1 = tmp[a].pelem[4];
}
else {
tmpSameCard1 = tmp[a].pelem[4];
tmpRestCard1 = tmp[a].pelem[0];
}
if(tmp[b].pelem[0].getRank() == tmp[b].pelem[3].getRank()) {
tmpSameCard2 = tmp[b].pelem[3];
tmpRestCard2 = tmp[b].pelem[4];
}
else {
tmpSameCard2 = tmp[b].pelem[4];
tmpRestCard2 = tmp[b].pelem[0];
}
if(tmpSameCard1.cardcmp(tmpSameCard2) < 0) { // Suit and Rank compare, and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
else if(tmpSameCard1.getRank() == tmpSameCard2.getRank()) {
if(tmpRestCard1.cardcmp(tmpRestCard2) < 0) { // Suit and Rank compare, and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
}
}
}
}
Card tmpFirstSameCard1; // 5card set의 비교(left, right중 left), 3장의 같은 카드의 정보를 저장할 변수
Card tmpFirstSameCard2; // 5card set의 비교(left, right중 left), 2장의 같은 카드의 정보를 저장할 변수
Card tmpSecondSameCard1; // 5card set의 비교(left, right중 right), 3장의 같은 카드의 정보를 저장할 변수
Card tmpSecondSameCard2; // 5card set의 비교(left, right중 right), 2장의 같은 카드의 정보를 저장할 변수
/* Full House가 100개의 5Card set중 2개 이상 나왔을 때, 그들을 정렬하는 방법 */
for(int a = 0; a < L.size(); a++) { // if the number of Full House >= 2
for(int b = a + 1; b < L.size(); b++) {
if((tmp[a].getFamilyTreeValue() == FAMILYTREE.FH)
&& (tmp[b].getFamilyTreeValue() == FAMILYTREE.FH)) {
if(tmp[a].pelem[0] == tmp[a].pelem[2]) {
tmpFirstSameCard1 = tmp[a].pelem[2];
tmpFirstSameCard2 = tmp[a].pelem[4];
}
else {
tmpFirstSameCard1 = tmp[a].pelem[4];
tmpFirstSameCard2 = tmp[a].pelem[1];
}
if(tmp[b].pelem[0] == tmp[b].pelem[2]) {
tmpSecondSameCard1 = tmp[b].pelem[2];
tmpSecondSameCard2 = tmp[b].pelem[4];
}
else {
tmpSecondSameCard1 = tmp[b].pelem[4];
tmpSecondSameCard2 = tmp[b].pelem[1];
}
if(tmpFirstSameCard1.cardcmp(tmpSecondSameCard1) < 0) { // Suit and Rank compare, and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
else if(tmpFirstSameCard1.getRank() == tmpSecondSameCard1.getRank()) {
if(tmpFirstSameCard2.cardcmp(tmpSecondSameCard2) < 0)
{ // Suit and Rank compare, and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
}
}
}
}
/* Flush가 100개의 5Card set중 2개 이상 나왔을 때, 그들을 정렬하는 방법 */
for(int a = 0; a < L.size(); a++) { // if the number of Flush >= 2
for(int b = a + 1; b < L.size(); b++) {
if((tmp[a].getFamilyTreeValue() == FAMILYTREE.F)
&& (tmp[b].getFamilyTreeValue() == FAMILYTREE.F)) {
if(tmp[a].pelem[4].cardcmp(tmp[b].pelem[4]) < 0) { // Suit and Rank compare, and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
}
}
}
/* Mountain가 100개의 5Card set중 2개 이상 나왔을 때, 그들을 정렬하는 방법 */
for(int a = 0; a < L.size(); a++) { // if the number of Mountain >= 2
for(int b = a + 1; b < L.size(); b++) {
if((tmp[a].getFamilyTreeValue() == FAMILYTREE.M)
&& (tmp[b].getFamilyTreeValue() == FAMILYTREE.M)) {
if(tmp[a].pelem[4].getSuit() < tmp[b].pelem[4].getSuit()) { // Suit compare and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
}
}
}
/* Back Straight가 100개의 5Card set중 2개 이상 나왔을 때, 그들을 정렬하는 방법 */
for(int a = 0; a < L.size(); a++) { // if the number of BackStraight >= 2
for(int b = a + 1; b < L.size(); b++) {
if((tmp[a].getFamilyTreeValue() == FAMILYTREE.BS)
&& (tmp[b].getFamilyTreeValue() == FAMILYTREE.BS)) {
if(tmp[a].pelem[0].getSuit() < tmp[b].pelem[0].getSuit()) { // Suit compare and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
}
}
}
/* Straight가 100개의 5Card set중 2개 이상 나왔을 때, 그들을 정렬하는 방법 */
for(int a = 0; a < L.size(); a++) { // if the number of Straight >= 2
for(int b = a + 1; b < L.size(); b++) {
if((tmp[a].getFamilyTreeValue() == FAMILYTREE.S)
&& (tmp[b].getFamilyTreeValue() == FAMILYTREE.S)) {
if(tmp[a].pelem[4].cardcmp(tmp[b].pelem[4]) < 0) { // Suit and Rank compare, and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
}
}
}
/* Triple가 100개의 5Card set중 2개 이상 나왔을 때, 그들을 정렬하는 방법 */
for(int a = 0; a < L.size(); a++) { // if the number of Triple >= 2
for(int b = a + 1; b < L.size(); b++) {
if((tmp[a].getFamilyTreeValue() == FAMILYTREE.T)
&& (tmp[b].getFamilyTreeValue() == FAMILYTREE.T)) { // Suit compare and Swap
if(tmp[a].pelem[0] == tmp[a].pelem[2]) {
tmpSameCard1 = tmp[a].pelem[2];
tmpRestCard1 = tmp[a].pelem[4];
}
else if(tmp[a].pelem[1] == tmp[a].pelem[3]) {
tmpSameCard1 = tmp[a].pelem[3];
tmpRestCard1 = tmp[a].pelem[4];
}
else {
tmpSameCard1 = tmp[a].pelem[4];
tmpRestCard1 = tmp[a].pelem[1];
}
if(tmp[b].pelem[0] == tmp[b].pelem[2]) {
tmpSameCard2 = tmp[b].pelem[2];
tmpRestCard2 = tmp[b].pelem[4];
}
else if(tmp[b].pelem[1] == tmp[b].pelem[3]) {
tmpSameCard2 = tmp[b].pelem[3];
tmpRestCard2 = tmp[b].pelem[4];
}
else {
tmpSameCard2 = tmp[b].pelem[4];
tmpRestCard2 = tmp[b].pelem[1];
}
if(tmpSameCard1.getRank() < tmpSameCard2.getRank()) { // Rank compare, and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
else if(tmpSameCard1.getRank() == tmpSameCard2.getRank()) {
if(tmpRestCard1.cardcmp(tmpRestCard2) < 0) { // Suit and Rank compare, and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
}
}
}
}
/* Two Pair가 100개의 5Card set중 2개 이상 나왔을 때, 그들을 정렬하는 방법 */
for(int a = 0; a < L.size(); a++) { // if the number of Two Pair >= 2
for(int b = a + 1; b < L.size(); b++) {
if((tmp[a].getFamilyTreeValue() == FAMILYTREE.TP)
&& (tmp[b].getFamilyTreeValue() == FAMILYTREE.TP)) { // Suit compare and Swap
if((tmp[a].pelem[0] == tmp[a].pelem[1]) && (tmp[a].pelem[2] == tmp[a].pelem[3])) {
tmpFirstSameCard1 = tmp[a].pelem[1];
tmpFirstSameCard2 = tmp[a].pelem[3];
tmpRestCard1 = tmp[a].pelem[4];
}
else if((tmp[a].pelem[1] == tmp[a].pelem[2]) && (tmp[a].pelem[3] == tmp[a].pelem[4])) {
tmpFirstSameCard1 = tmp[a].pelem[2];
tmpFirstSameCard2 = tmp[a].pelem[4];
tmpRestCard1 = tmp[a].pelem[0];
}
else {
tmpFirstSameCard1 = tmp[a].pelem[1];
tmpFirstSameCard2 = tmp[a].pelem[4];
tmpRestCard1 = tmp[a].pelem[2];
}
if((tmp[b].pelem[0] == tmp[b].pelem[1]) && (tmp[b].pelem[2] == tmp[b].pelem[3])) {
tmpSecondSameCard1 = tmp[b].pelem[1];
tmpSecondSameCard2 = tmp[b].pelem[3];
tmpRestCard2 = tmp[b].pelem[4];
}
else if((tmp[b].pelem[1] == tmp[b].pelem[2]) && (tmp[b].pelem[3] == tmp[b].pelem[4])) {
tmpSecondSameCard1 = tmp[b].pelem[2];
tmpSecondSameCard2 = tmp[b].pelem[4];
tmpRestCard2 = tmp[b].pelem[0];
}
else {
tmpSecondSameCard1 = tmp[b].pelem[1];
tmpSecondSameCard2 = tmp[b].pelem[4];
tmpRestCard2 = tmp[b].pelem[2];
}
if(tmpFirstSameCard2.getRank() < tmpSecondSameCard2.getRank()) { // Suit and Rank compare, and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
else if(tmpFirstSameCard2.getRank() == tmpSecondSameCard2.getRank()) {
if(tmpFirstSameCard1.getRank() != tmpSecondSameCard1.getRank()) {
if(tmpFirstSameCard1.getRank() < tmpSecondSameCard1.getRank()) { // Rank compare, and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
}
else {
if(tmpRestCard1.cardcmp(tmpRestCard2) < 0) { // Suit and Rank compare, and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
}
}
}
}
}
/* One Pair가 100개의 5Card set중 2개 이상 나왔을 때, 그들을 정렬하는 방법 */
for(int a = 0; a < L.size(); a++) { // if the number of One Pair >= 2
for(int b = a + 1; b < L.size(); b++) {
if((tmp[a].getFamilyTreeValue() == FAMILYTREE.OP)
&& (tmp[b].getFamilyTreeValue() == FAMILYTREE.OP)) { // Suit compare and Swap
if(tmp[a].pelem[0] == tmp[a].pelem[1]) {
tmpSameCard1 = tmp[a].pelem[1];
tmpRestCard1 = tmp[a].pelem[4];
}
else if(tmp[a].pelem[1] == tmp[a].pelem[2]) {
tmpSameCard1 = tmp[a].pelem[2];
tmpRestCard1 = tmp[a].pelem[4];
}
else if(tmp[a].pelem[2] == tmp[a].pelem[3]) {
tmpSameCard1 = tmp[a].pelem[3];
tmpRestCard1 = tmp[a].pelem[4];
}
else {
tmpSameCard1 = tmp[a].pelem[4];
tmpRestCard1 = tmp[a].pelem[2];
}
if(tmp[b].pelem[0] == tmp[b].pelem[1]) {
tmpSameCard2 = tmp[b].pelem[1];
tmpRestCard2 = tmp[b].pelem[4];
}
else if(tmp[b].pelem[1] == tmp[b].pelem[2]) {
tmpSameCard2 = tmp[b].pelem[2];
tmpRestCard2 = tmp[b].pelem[4];
}
else if(tmp[b].pelem[2] == tmp[b].pelem[3]) {
tmpSameCard2 = tmp[b].pelem[3];
tmpRestCard2 = tmp[b].pelem[4];
}
else {
tmpSameCard2 = tmp[b].pelem[4];
tmpRestCard2 = tmp[b].pelem[2];
}
if(tmpSameCard1.getRank() < tmpSameCard2.getRank()) { // Rank compare, and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
else if(tmpSameCard1.getRank() == tmpSameCard2.getRank()) {
if(tmpRestCard1.cardcmp(tmpRestCard2) < 0) { // Suit and Rank compare, and Swap
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
}
}
}
}
/* No Pair가 100개의 5Card set중 2개 이상 나왔을 때, 그들을 정렬하는 방법 */
for(int a = 0; a < L.size(); a++) { // if the number of No Pair >= 2
for(int b = a + 1; b < L.size(); b++) {
if((tmp[a].getFamilyTreeValue() == FAMILYTREE.NP)
&& (tmp[b].getFamilyTreeValue() == FAMILYTREE.NP)) { // 기본 문제의 정렬 방법과 같음 -- 교수님께 메일로 질문 했었습니다.
if(tmp[a].pelem[0].over(tmp[b].pelem[0])) {
tmpFiveCards = tmp[a];
tmp[a] = tmp[b];
tmp[b] = tmpFiveCards;
}
}
}
}
for(int j = 0; j < L.size(); j++)
{ // adjust L, copy from Dictionary ordered tmp to L
L.set(j, tmp[j]);
}
}
void suffle(ArrayList<Card> deck) {
int oneOrzero = 0;
int deckSize = deck.size();
Card iter = deck.get(0);
for(int i = 1; i <= SUFFLECOUNT * deckSize; i++) {
oneOrzero = Math.abs(rand.nextInt()) % 2;
if (oneOrzero == 0) { // move to back
deck.add(iter);
iter = deck.remove(deck.indexOf(iter));
}
else {
iter = deck.get(i % deckSize);
if(iter == deck.get(deckSize-1)) {
iter = deck.get(0);
}
}
}
}
ArrayList<Card> makeCardDeck() { // Make a Card Deck.
ArrayList<Card> deck = new ArrayList<Card>();
for(int s = SUIT.CLUB; s <= SUIT.SPADE; s++) {
for(int r = PIP.ACE; r <= PIP.KING; r++) {
deck.add(new Card(s, r));
}
}
return deck;
}
ArrayList<FiveCards> generate5CardList(int gencount) {
// Generate List<FiveCards>
ArrayList<FiveCards> p5CardsList = new ArrayList<FiveCards>();
ArrayList<Card> deckCard = makeCardDeck();
//int deckSize = deckCard.size();
for(int i = 1, j = 1; i <= gencount; i++) {
suffle(deckCard);
// Make a Card Deck
Card iter = deckCard.get(0);
FiveCards fc = new FiveCards();
int ipos = 0;
while(true) {
if(ipos != 5) {
fc.pelem[ipos] = iter;
iter = deckCard.get(++j);
ipos++;
}
else {
p5CardsList.add(fc);
ipos = 0;
j = 0;
break;
}
}
}
return p5CardsList;
}
void printFiveCardsArrayList(ArrayList<FiveCards> L) {
for(int i = 0; i < L.size(); i++) {
System.out.printf("%d ", (i+1));
System.out.println(L.get(i));
}
}
void printFiveCardsListwithFamilyTree(ArrayList<FiveCards> L) {
for(int i = 0; i < L.size(); i++) {
System.out.printf("%d ", (i+1));
System.out.print(L.get(i));
System.out.println(" ---- " + L.get(i).distinctionFamilyTree());
}
}
final String getCardCoverImageFileName() {
return "back_" + (randomNumber == 0 ? "black" : "color");
}
final String getCardImageFileName(Card card) {
return card.toString().substring(0, 1) + card.getRank();
}
}
-
SUIT.java
package guiPokerGame;
public class SUIT {
final static int CLUB = 1;
final static int HEART = 2;
final static int DIAMOND = 3;
final static int SPADE = 4;
int value;
SUIT(int value) {
this.value = value;
}
}
-
PIP.java
package guiPokerGame;
public class PIP {
final static int ACE = 1;
final static int JACK = 11;
final static int QUEEN = 12;
final static int KING = 13;
int value;
PIP(int value) {
this.value = value;
}
}
-
Card.java
/**
* Created on Dec 18, 2017
* @author msJo -- hufs.ac.kr, Dept of CES
* Copy Right -- Free for Educational Purpose
*/
package guiPokerGame;
public class Card {
private int suit;
private int rank;
public Card() { // default constructor
suit = SUIT.SPADE;
rank = PIP.ACE;
}
public Card(int s, int r) { // constructor
suit = s;
rank = r;
}
public final String toString() {
String image = null;
switch (suit) {
case SUIT.CLUB: image = "CL "; break;
case SUIT.DIAMOND: image = "DI "; break;
case SUIT.HEART: image = "HE "; break;
case SUIT.SPADE: image = "SP "; break;
}
switch (rank) {
case PIP.ACE: image += "A "; break;
case PIP.JACK: image += "J "; break;
case PIP.QUEEN: image += "Q "; break;
case PIP.KING: image += "K "; break;
case 10: image += "10"; break;
default: image += (char)('0'+rank) + " ";
}
return image;
}
final int compare(final Card right) {
int lrank, rrank;
lrank = this.rank;
rrank = right.rank;
int diff = lrank - rrank;
return diff;
}
final int cardcmp(final Card right)
{
if(this.getRank() > right.getRank())
return 1;
else if(this.getRank() < right.getRank())
return -1;
else{
if(this.getSuit() > right.getSuit())
return 1;
else if(this.getSuit() < right.getSuit())
return -1;
else
return 0;
}
}
final Card getHighValue() { // New
return (new Card(1,15));
}
int getRank() {
return rank;
}
int getSuit() {
return suit;
}
boolean under(final Card right) {
return this.compare(right) < 0;
}
boolean orUnder(final Card right) {
return this.compare(right) <= 0;
}
boolean equal(final Card right) {
return this.compare(right) == 0;
}
boolean unequal(final Card right) {
return this.compare(right) != 0;
}
boolean over(final Card right) {
return this.compare(right) > 0;
}
boolean orOver(final Card right) {
return this.compare(right) >= 0;
}
}
-
FiveCards.java
/**
* Created on Dec 18, 2017
* @author msJo -- hufs.ac.kr, Dept of CES
* Copy Right -- Free for Educational Purpose
*/
package guiPokerGame;
public class FiveCards {
private static final int FIVE = 5;
Card FIVEHIGHCARD[] = { new Card(1,15), new Card(1,15), new Card(1,15), new Card(1,15), new Card(1,15) };
public FiveCards() {
pelem = new Card [FIVE];
}
public FiveCards(final FiveCards right) {
pelem = new Card [FIVE];
for(int i = 0; i < FIVE; ++i) {
pelem[i] = right.pelem[i];
}
}
public final int compare(final FiveCards right) {
return pelem[0].compare(right.pelem[0]);
}
public final String toString() {
String result = "[";
for(int i = 0; i < FIVE; ++i) {
if(i != 0) {
result += ", ";
}
result += pelem[i].toString();
}
result += "]";
return result;
}
public final String distinctionRSFMT() {
String familyTree = "";
if(((pelem[0].getRank() == PIP.ACE) && (pelem[0].getSuit() == pelem[1].getSuit()))
&& ((pelem[1].getRank() == 10) && (pelem[1].getSuit() == pelem[2].getSuit()))
&& ((pelem[2].getRank() == PIP.JACK) && (pelem[2].getSuit() == pelem[3].getSuit()))
&& ((pelem[3].getRank() == PIP.QUEEN) && (pelem[3].getSuit() == pelem[4].getSuit()))
&& ((pelem[4].getRank() == PIP.KING) && (pelem[4].getSuit() == pelem[0].getSuit()))) {
familyTree = "Royal Straight Flush";
}
else if(((pelem[0].getRank() == PIP.ACE))
&& ((pelem[1].getRank() == 10))
&& ((pelem[2].getRank() == PIP.JACK))
&& ((pelem[3].getRank() == PIP.QUEEN))
&& ((pelem[4].getRank() == PIP.KING))) {
familyTree = "Mountain";
}
return familyTree;
}
public final String distinctionFamilyTree() {
String familyTree = "";
if(((pelem[0].getRank() == 10) && (pelem[0].getSuit() == pelem[1].getSuit()))
&& ((pelem[1].getRank() == PIP.JACK) && (pelem[1].getSuit() == pelem[2].getSuit()))
&& ((pelem[2].getRank() == PIP.QUEEN) && (pelem[2].getSuit() == pelem[3].getSuit()))
&& ((pelem[3].getRank() == PIP.KING) && (pelem[3].getSuit() == pelem[4].getSuit()))
&& ((pelem[4].getRank() == PIP.ACE) && (pelem[4].getSuit() == pelem[0].getSuit()))) {
familyTree = "Royal Straight Flush";
}
else if(((pelem[0].getRank() == PIP.ACE) && (pelem[0].getSuit() == pelem[1].getSuit()))
&& ((pelem[1].getRank() == 2) && (pelem[1].getSuit() == pelem[2].getSuit()))
&& ((pelem[2].getRank() == 3) && (pelem[2].getSuit() == pelem[3].getSuit()))
&& ((pelem[3].getRank() == 4) && (pelem[3].getSuit() == pelem[4].getSuit()))
&& ((pelem[4].getRank() == 5) && (pelem[4].getSuit() == pelem[0].getSuit()))) {
familyTree = "Back Straight Flush";
}
else if(((pelem[0].getRank() == pelem[1].getRank() - 1) && (pelem[0].getSuit() == pelem[1].getSuit()))
&& ((pelem[1].getRank() == pelem[2].getRank() - 1) && (pelem[1].getSuit() == pelem[2].getSuit()))
&& ((pelem[2].getRank() == pelem[3].getRank() - 1) && (pelem[2].getSuit() == pelem[3].getSuit()))
&& ((pelem[3].getRank() == pelem[4].getRank() - 1) && (pelem[3].getSuit() == pelem[4].getSuit()))) {
familyTree = "Straight Flush";
}
else if((pelem[0].getRank() == pelem[3].getRank()) || (pelem[1].getRank() == pelem[4].getRank())) {
familyTree = "Four Card";
}
else if(((pelem[0].getRank() == pelem[1].getRank()) && (pelem[2].getRank() == pelem[4].getRank()))
|| ((pelem[0].getRank() == pelem[2].getRank()) && (pelem[3].getRank() == pelem[4].getRank()))) {
familyTree = "Full House";
}
else if((pelem[0].getSuit() == pelem[1].getSuit())
&& (pelem[1].getSuit() == pelem[2].getSuit())
&& (pelem[2].getSuit() == pelem[3].getSuit())
&& (pelem[3].getSuit() == pelem[4].getSuit())) {
familyTree = "Flush";
}
else if(((pelem[0].getRank() == 10))
&& ((pelem[1].getRank() == PIP.JACK))
&& ((pelem[2].getRank() == PIP.QUEEN))
&& ((pelem[3].getRank() == PIP.KING))
&& ((pelem[4].getRank() == PIP.ACE))) {
familyTree = "Mountain";
}
else if(((pelem[0].getRank() == PIP.ACE))
&& ((pelem[1].getRank() == 2))
&& ((pelem[2].getRank() == 3))
&& ((pelem[3].getRank() == 4))
&& ((pelem[4].getRank() == 5))) {
familyTree = "Back Straight";
}
else if(((pelem[0].getRank() == pelem[1].getRank() - 1))
&& ((pelem[1].getRank() == pelem[2].getRank() - 1))
&& ((pelem[2].getRank() == pelem[3].getRank() - 1))
&& ((pelem[3].getRank() == pelem[4].getRank() - 1))) {
familyTree = "Straight";
}
else if((pelem[0].getRank() == pelem[2].getRank())
|| (pelem[1].getRank() == pelem[3].getRank())
|| (pelem[2].getRank() == pelem[4].getRank())) {
familyTree = "Triple";
}
else if(((pelem[0].getRank() == pelem[1].getRank()) && (pelem[2].getRank() == pelem[3].getRank()))
|| ((pelem[1].getRank() == pelem[2].getRank()) && (pelem[3].getRank() == pelem[4].getRank()))
|| ((pelem[0].getRank() == pelem[1].getRank()) && (pelem[3].getRank() == pelem[4].getRank()))) {
familyTree = "Two Pair";
}
else if((pelem[0].getRank() == pelem[1].getRank())
|| (pelem[1].getRank() == pelem[2].getRank())
|| (pelem[2].getRank() == pelem[3].getRank())
|| (pelem[3].getRank() == pelem[4].getRank())) {
familyTree = "One Pair";
}
else {
familyTree = "No Pair";
}
return familyTree;
}
public FiveCards getHighValue() {
FiveCards p5Cards = new FiveCards();
p5Cards.pelem = FIVEHIGHCARD;
return p5Cards;
}
public int getFamilyTreeValue() {
if(distinctionFamilyTree() == "Royal Straight Flush")
return FAMILYTREE.RSF;
else if(distinctionFamilyTree() == "Back Straight Flush")
return FAMILYTREE.BSF;
else if(distinctionFamilyTree() == "Straight Flush")
return FAMILYTREE.SF;
else if(distinctionFamilyTree() == "Four Card")
return FAMILYTREE.FC;
else if(distinctionFamilyTree() == "Full House")
return FAMILYTREE.FH;
else if(distinctionFamilyTree() == "Flush")
return FAMILYTREE.F;
else if(distinctionFamilyTree() == "Mountain")
return FAMILYTREE.M;
else if(distinctionFamilyTree() == "Back Straight")
return FAMILYTREE.BS;
else if(distinctionFamilyTree() == "Straight")
return FAMILYTREE.S;
else if(distinctionFamilyTree() == "Triple")
return FAMILYTREE.T;
else if(distinctionFamilyTree() == "Two Pair")
return FAMILYTREE.TP;
else if(distinctionFamilyTree() == "One Pair")
return FAMILYTREE.OP;
else if(distinctionFamilyTree() == "No Pair")
return FAMILYTREE.NP;
else
return 0;
}
public int fcscmp(final FiveCards left, final FiveCards right) {
// this function is similar to STL-cstring's strcmp(in C++)
int i = 0;
int j = 0;
while(i != FIVE - 1)
{
if((left.pelem[i].getRank() != right.pelem[j].getRank())
|| ((left.pelem[i].getRank() == right.pelem[j].getRank())
&& (left.pelem[i].getSuit() != right.pelem[j].getSuit()))) {
break;
}
i++;
j++;
}
if(left.pelem[i].getRank() > right.pelem[j].getRank())
return 1;
else if(left.pelem[i].getRank() < right.pelem[j].getRank())
return -1;
else{
if(left.pelem[i].getSuit() > right.pelem[j].getSuit())
return 1;
else if(left.pelem[i].getSuit() < right.pelem[j].getSuit())
return -1;
else
return 0;
}
}
public void assign(final FiveCards right) {
if(this != right) {
pelem = new Card [FIVE];
for(int i = 0; i < FIVE; ++i)
pelem[i] = right.pelem[i];
}
}
boolean under(final FiveCards left, final FiveCards right) {
return left.compare(right) < 0;
}
boolean equal(final FiveCards left, final FiveCards right) {
return left.compare(right) == 0;
}
boolean over(final FiveCards left, final FiveCards right) {
return left.compare(right) > 0;
}
public Card[] pelem;
}
-
FAMILYTREE.java
/**
* Created on Dec 18, 2017
* @author msJo -- hufs.ac.kr, Dept of CES
* Copy Right -- Free for Educational Purpose
*/
package guiPokerGame;
public class FAMILYTREE {
final static int NP = 1;
final static int OP = 2;
final static int TP = 3;
final static int T = 4;
final static int S = 5;
final static int BS = 6;
final static int M = 7;
final static int F = 8;
final static int FH = 9;
final static int FC = 10;
final static int SF = 11;
final static int BSF = 12;
final static int RSF = 13;
int value;
FAMILYTREE(int value) {
this.value = value;
}
}