2

こんにちは皆さん、飲酒カードゲームのキングスをシミュレートするプログラムを書きました。これは私の 3 番目の Java プロジェクトです

playGame()GUIによって処理されるプレイヤーのターン中、getChoice()(時間の 5% のように) 正しいタイトルで JPanel をレンダリングしますが、それ以外は空白です! なぜこうなった?

写真:

JPANEL フリーズ/レンダリングの問題

ゲームの説明は、ここのコード コメント ボックスにあります。

 /**
 * @author              :KyleMHB
 * Project Number       :0003
 * Project Name         :Kings
 * IDE                  :NETBEANS
 * Goal of Project      - 
 * Kings is a rule based drinking game using cards for 4+ players.
 * The Rules are read in from a rules.txt so that one can easily change the rules.
 * How the game works:
 * Players shuffle a deck of cards, place a glass between them and circle the
 * cards around the base of the glass.
 * The players then take turns picking cards, each card has its own rule associated to it.
 * Most importantly, there are 4 Kings, each time a King is picked, 
 * the player who picked it can pour as much of his/her drink into the glass between
 * them as they wish.
 * The game ends when the fourth and final King is picked.
 * The player to pick the final King must down the glass in the center of table.
 */

package kings;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.swing.JOptionPane;

public class Kings { 

    public static class Card {
        private int rank, suit;

        private  String[] suits = { "Hearts", "Spades", "Diamonds", "Clubs" };
        private  String[] ranks  = { "Ace", "2", "3", "4", "5", "6", "7", "8",
                                     "9", "10", "Jack", "Queen", "King" };

        Card(int suit, int rank){
                this.rank=rank;
                this.suit=suit;
        }
        public @Override String toString(){
                  return ranks[rank] + " of " + suits[suit];
        }
        public int getRank() {
                 return rank;
        }
        public int getSuit() {
                return suit;
        }
    }

    public static class Deck {
        private static ArrayList<Card> cards;
         Deck() {
             cards=new ArrayList<Card>();
                for (int a=0; a<=3; a++){
                        for (int b=0; b<=12; b++){
                           cards.add( new Card(a,b) );
                         }
                }
                Collections.shuffle(cards, new Random());
                Collections.shuffle(cards, new Random(System.nanoTime()));
         }
         public Card drawFromDeck(){       
                return cards.remove( 0 );
         }
         public int getTotalCards(){
                return cards.size();
         }
    }

    private static List<String> rules;

    public static void main(String[] args) throws FileNotFoundException, IOException {     
        setRules(new File("rules.txt"));
        playGame(getNum("How many people are going to play","Number of Players"));
    }

/**
* Rules are not hard-coded because people often have different ones,
* Therefore I made an easily editable rules.txt file.
* Also my rule file has formatting in using the \n,
* However when the file is read it is read as \ and n
* Hence why I used the replaceAll( "\\\\n","\n");
*/ 

    private static void setRules(File f) throws FileNotFoundException, IOException {
        rules = Files.readAllLines(f.toPath(), Charset.defaultCharset());
        for(int i=0; i!=rules.size(); i++){
            rules.set(i, rules.get(i).replaceAll( "\\\\n","\n"));
        }
    }

    private static int getNum(String prompt,String title) {
        return Integer.parseInt(JOptionPane.showInputDialog(null,prompt,title,3));
    }

    private static void playGame(int players) {
        int playerTurn;
        int choice;
        int kings=0;
        Card cardDrawn;
        Deck deck=new Deck();
        while(true){
            playerTurn=0;
            while (playerTurn!=players){
                choice=getChoice("Player "+(playerTurn+1),
                        "Would you like to skip or draw?",
                        "Draw","Skip","Exit");

                if (choice==0){
                    cardDrawn=deck.drawFromDeck();
                    System.out.println(cardDrawn);
                    kings+=showCard(cardDrawn,kings,playerTurn+1);     
                    playerTurn++;
                }                   
                else if(choice==1)
                    playerTurn++;              
                else
                    System.exit(0);                                    
             }//Turn reset loop
        }//continuous loop
    }

    private static int getChoice(String title, String prompt,
                                 String a, String b, String c) {
        Object[] options = { a, b, c};
        return JOptionPane.showOptionDialog(null, prompt, title,0,2,
                                            null,options,options[0]);
        }

    private static int showCard(Card a, int kings, int player) {
        if(a.rank==12)
            if(kings==3){
                JOptionPane.showMessageDialog(null,
                        "Player "+player+" has Drawn the Final King\n\n"
                        + "Restart the Program to Play again",a.toString(),1);
                System.exit(0);
                return 0;
            }
            else{
                JOptionPane.showMessageDialog(null,"Player "+player+
                        " has drawn the "+a.toString()+"\n"
                        + "Which is King "+(kings+1)+"/4\n\n"
                        +rules.get(a.rank),a.toString(),1);
                return 1;
            }
        else{
            JOptionPane.showMessageDialog(null,"Player "+player+
                    " has drawn the "+a.toString()+"\n\n"
                    + rules.get(a.rank),a.toString(),1);
            return 0;
        }
    }
}
4

0 に答える 0