Note that there are some explanatory texts on larger screens.

plurals
  1. POJPanel freeze/Render issue randomly in my program Kings
    text
    copied!<p>Hi guys I wrote a program to simulate the drinking card game Kings. This is my third Java Project</p> <p><strong>During my <code>playGame()</code> on a players turn to choose, which is handled by <code>getChoice()</code> the GUI SOMETIMES (like 5% of the time) renders a JPanel with the correct title but otherwise blank!</strong> Why is this happening?</p> <p>Pic:</p> <p><img src="https://i.stack.imgur.com/3Xb2J.png" alt="JPANEL FREEZE/RENDER ISSUE"></p> <p>The explanation of the game is in the code comment box here:</p> <pre><code> /** * @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&lt;Card&gt; cards; Deck() { cards=new ArrayList&lt;Card&gt;(); for (int a=0; a&lt;=3; a++){ for (int b=0; b&lt;=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&lt;String&gt; 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; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload