I was wondering if someone coule help me on how to implement a thread which runs infinitely in the background until the user presses p at which point it pauses the other threads and upon pressing r resumes other threads. This is some of the code
Some of the main object
public class CardGame
{
static Player[] players;
static int handSize;
static Queue<Card>[] playingDeckArray;
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.println( "\nHello, how many players would you like" );
int playersNum = Integer.parseInt(Checks.userInputCheck( "\\d" ));
System.out.println( "\nHow many cards should each player begin with" );
int handSize = Integer.parseInt(Checks.userInputCheck( "\\d" ));
System.out.println( "\nWhich strategy would you like to use 1 or 2" );
int strategy = Integer.parseInt(Checks.userInputCheck( "[12]$" ));
Logger.createDeck( playersNum, handSize );
makePlayers( playersNum, handSize, strategy );
makePlayingDecks( playersNum );
dealInitialHand( playersNum, players, handSize );
makePlayerOutputs();
for ( int i = 0; i < players.length; i++){
logInitialHand(players[i]);
}
CardGame cG = new CardGame();
cG.startPauseThread();
for ( int i = 0; i < players.length; i++){
new Thread(players[i]).start();
}
}
public void startPauseThread(){
Thread add = new Thread( pauseInputThread );
add.start();
}
Thread pauseInputThread = new Thread(){
public void run(){
int i = 0;
for(;;){
System.out.println("i'm still here" );
Scanner reader = new Scanner(System.in);
String result = Checks.userInputCheck( "[pPrR]$" );
i++;
System.out.println(i);
}
}
};
}
The player object which are the threads to be paused
public class Player implements Runnable
{
Card[] hand;
String playerName;
int strategyChosen;
public void run(){
System.out.println( "les do dis" );
}
private Player(){
}
public Player( int strategy, int cardsInHand, int playerNumber ){
hand = new Card[cardsInHand];
strategyChosen = strategy;
playerName = "Player " + playerNumber;
}
public String getPlayerName(){
return playerName;
}
public void fillHand(){
for ( int i = 0; i < hand.length; i++){
hand[i] = new Card(0);
}
}
public void setHand( int value, int index ){
hand[index].setCardValue( value );
}
public void seeHand(){
for ( int i = 0; i < hand.length; i++){
System.out.println( hand[i].getCardValue() );
}
}
public String getHand(){
String result = "";
for ( int i = 0; i < hand.length; i++ ){
result = result + hand[i].getCardValue() + " \n" ;
}
return result;
}
public int getHandValue( Card card ){
return card.getCardValue();
}
}
The players will be 'playing a game' where they draw and discard objects from arrays, but the user should be able to pause and resume the programm at any point during the game. i just dont quite understand how to go about that, using events and listners.
Help would be appreciated. Thank you.