Java Swing Dice Rolling Animation - java

I'm coding up a GUI game of craps. There is a JButton called "roll" which when clicked rolls the dice for the game. The GUI then displays what you rolled using jpeg's of die faces.
Everything works great, except I'm supposed to now add an animation to the GUI. My idea was to somehow rapidly display different face values for a short period of time (simulating a "roll") using the same method of displaying the jpeg's. However, as I'm sure you all know, that doesn't work.
I'm familiar with the idea of EDT and the Timer class, but I'm not sure exactly how to use them. Basically I want this animation to happen when I hit the "roll" button, and when the animation is finished, I want it to display what was actually rolled like it did before.
Any help would be greatly appreciated. Here's the code I have thus far:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/* This is the GUI declaration */
public class NCrapsGUI extends JFrame {
//code...
/* Action when "roll" is clicked */
private void rollActionPerformed(java.awt.event.ActionEvent evt){
game.rollDice();
//Rolls both die
sumOfDice.setText(Integer.toString(game.getSum()));
//Displays the sum of the die
numRolls.setText(Integer.toString(game.getNumRolls()));
//Displays the number of rolls in each game
// <editor-fold defaultstate="collapsed" desc="Die JPEG's">
// If statements display the die face based on the number rolled
if (game.getDie1Value() == 1) {
die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face1.jpg")));
}
if (game.getDie1Value() == 2) {
die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face2.jpg")));
}
if (game.getDie1Value() == 3) {
die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face3.jpg")));
}
if (game.getDie1Value() == 4) {
die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face4.jpg")));
}
if (game.getDie1Value() == 5) {
die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face5.jpg")));
}
if (game.getDie1Value() == 6) {
die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face6.jpg")));
}
if (game.getDie2Value() == 1) {
die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face1.jpg")));
}
if (game.getDie2Value() == 2) {
die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face2.jpg")));
}
if (game.getDie2Value() == 3) {
die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face3.jpg")));
}
if (game.getDie2Value() == 4) {
die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face4.jpg")));
}
if (game.getDie2Value() == 5) {
die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face5.jpg")));
}
if (game.getDie2Value() == 6) {
die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face6.jpg")));
}
//</editor-fold>
/*
* If the game is beyond the first roll, it checks to see if the sum of the
* values rolled equal 7 or the point value for a loss or win respectively.
* If it is a win, it adds a win. Likewise for a loss.
*/
if (game.getGameStatus() == 2) {
if (game.getSum() == game.getPoint()) {
game.addWin();
numWins.setText(Integer.toString(game.getWins()));
game.setGameStatus(1);
game.setPoint(0);
game.resetRolls();
return;
}
if (game.getSum() == 7) {
game.addLoss();
numLosses.setText(Integer.toString(game.getLosses()));
game.setGameStatus(1);
game.setPoint(0);
game.resetRolls();
return;
}
}
/*
* This checks to see if the game is on the first roll. If it is, it checks
* if the sum of the die is 7 or 11 for a win, or 2, 3, or 12 for a loss. If
* not, it passes it on to the next roll and sets the point value to the sum
*/
if (game.getGameStatus() == 1) {
game.setPoint(game.getSum());
dieSum.setText(Integer.toString(game.getPoint()));
if (((game.getSum() == 7) || ((game.getSum() == 11)))) {
game.addWin();
numWins.setText(Integer.toString(game.getWins()));
game.setPoint(0);
dieSum.setText(Integer.toString(game.getPoint()));
game.resetRolls();
return;
}
if (((game.getSum() == 2) || ((game.getSum()) == 3)) || (game.getSum()) == 12) {
game.addLoss();
numLosses.setText(Integer.toString(game.getLosses()));
game.setPoint(0);
dieSum.setText(Integer.toString(game.getPoint()));
game.resetRolls();
return;
} else {
game.setGameStatus(2);
}
}
}
EDIT WITH UPDATED CODE!!!
Here's where the Timer and array are declared:
public class NCrapsGUI extends JFrame
{
private Timer timer;
private int numPlayers;
private int totalIcons = 6;
private ImageIcon imageArray[];`
/* CODE */
And here is where the array is populated inside the NCrapsGUI constructor:
imageArray = new ImageIcon[totalIcons];
for (int i = 0; i < 6 ;i++)
{
int temp = i + 1;
imageArray[i] = new ImageIcon("face" + temp + ".jpg");
}
initComponents();`
This is the entire rollActionPerformed method. I'm guessing the Timer gets started right
at the beginning, but whenever I try to start it I get loads of error. However, when I
I made a new JPanel seperately, and made it implement action listener, I got no
errors. I tried adding implements ActionListner to this declaration, but NetBeans literally
would not let me type anything in.
private void rollActionPerformed(java.awt.event.ActionEvent evt) {
game.rollDice();
//Rolls both die
sumOfDice.setText(Integer.toString(game.getSum()));
//Displays the sum of the die
numRolls.setText(Integer.toString(game.getNumRolls()));
//Displays the number of rolls in each game
// <editor-fold defaultstate="collapsed" desc="Die JPEG's">
// If statements display the die face based on the number rolled
if (game.getDie1Value() == 1)
{
die1Disp.setIcon(imageArray[0]);
}
if (game.getDie1Value() == 2)
{
die1Disp.setIcon(imageArray[1]);
}
if (game.getDie1Value() == 3)
{
die1Disp.setIcon(imageArray[2]);
}
if (game.getDie1Value() == 4) {
die1Disp.setIcon(imageArray[3]);
}
if (game.getDie1Value() == 5) {
die1Disp.setIcon(imageArray[4]);
}
if (game.getDie1Value() == 6)
{
die1Disp.setIcon(imageArray[5]);
}
if (game.getDie2Value() == 1)
{
die2Disp.setIcon(imageArray[0]);
}
if (game.getDie2Value() == 2)
{
die2Disp.setIcon(imageArray[1]);
}
if (game.getDie2Value() == 3)
{
die2Disp.setIcon(imageArray[2]);
}
if (game.getDie2Value() == 4)
{
die2Disp.setIcon(imageArray[3]);
}
if (game.getDie2Value() == 5)
{
die2Disp.setIcon(imageArray[4]);
}
if (game.getDie2Value() == 6)
{
die2Disp.setIcon(imageArray[5]);
}
//</editor-fold>
/*
* If the game is beyond the first roll, it checks to see if the sum of the
* values rolled equal 7 or the point value for a loss or win respectively.
* If it is a win, it adds a win. Likewise for a loss.
*/
if (game.getGameStatus() == 2) {
if (game.getSum() == game.getPoint()) {
game.addWin();
numWins.setText(Integer.toString(game.getWins()));
game.setGameStatus(1);
game.setPoint(0);
game.resetRolls();
return;
}
if (game.getSum() == 7) {
game.addLoss();
numLosses.setText(Integer.toString(game.getLosses()));
game.setGameStatus(1);
game.setPoint(0);
game.resetRolls();
return;
}
}
`

Your basic idea behind the animation is a good one I think, but whether it works or not is all in the implementation details of course. I suggest
That you read in your images and make ImageIcons once, probably at the start of the program.
That you put the icons into an ImageIcon array with a length of 7 -- but you'll put an icon into the 1-6 slots, leaving the 0th item null.
That you use a Swing Timer to swap these icons randomly with some appropriate delay, say 200 or 300 msec.
That you use a a Random object to get a random number between 1 and 6, and then with this number as your array index, get the Icon out of the array.
That you display the ImageIcons in a JLabel (or two JLabels if you're displaying 2 die) and swap Icons by simply calling the JLabel's setIcon(...) method.
Edit
You state in your comment that you tried:
timer = new Timer(100,this);
And that's your problem -- your use of this. You shouldn't try to use the same ActionListner for everything. Instead create an ActionListener right there, where you need it. Something like,
timer = new Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent actionEvt) {
// ... put your ActionListener's code here
}
});

Related

JPanel doesn't get new values (anymore)

So, I'm trying to program a Game of Life simulation (Conway), and I want to show it in a JFrame.
For this purpose, I've created a JPanel, and it works perfectly, until I try to actually show a new generation. With prints, I've figured out, that the list is actually correct inside the newGeneration() method, but when paint(Graphics g) gets called (aka, when I try to repaint the JFrame), the list isn't updating.
I'm sure I've missed something obvious, and I'm not well versed in Java, but it's just getting so annoying. I'd really appreciate your help.
Here's my code;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Main {
public static void main(String[] args) {
new GameOfLife();
}
}
class GameOfLife {
// Initialising all class wide variables; sorted by type
JFrame frame = new JFrame("Game of Life");
JPanel panel;
Scanner gameSize = new Scanner(System.in);
String dimensions;
String splitHorizontal;
String splitVertical;
String confirmation;
Boolean accepted = false;
Integer split;
Integer horizontal;
Integer vertical;
Integer livingNeighbours;
int[][] cells;
int[][] newCells;
public GameOfLife() {
// Prompt for game Size
System.out.println("Please enter your game size in the following format; 'Horizontal,Vertical'");
// Run until viable game Size has been chosen
while (!accepted) {
dimensions = gameSize.nextLine();
// Check for correct format
if (dimensions.contains(",")) {
split = dimensions.indexOf(",");
splitHorizontal = dimensions.substring(0, split);
splitVertical = dimensions.substring(split + 1);
// Check for validity of inputs
if (splitHorizontal.matches("[0-9]+") && splitVertical.matches("[0-9]+")) {
horizontal = Integer.parseInt(dimensions.substring(0, split));
vertical = Integer.parseInt(dimensions.substring(split + 1));
// Check for game Size
if (horizontal > 1000 || vertical > 1000) {
System.out.println("A game of this Size may take too long to load.");
} else {
// Confirmation Prompt
System.out.println("Your game will contain " + horizontal + " columns, and " + vertical + " rows, please confirm (Y/N)");
confirmation = gameSize.nextLine();
// Check for confirmation, anything invalid is ignored
if (confirmation.matches("Y")) {
accepted = true;
System.out.println("Thank you for your confirmation. Please select live cells. Once your happy with your game, press Spacebar to start the Simulation.");
// Setting parameters depending on Size
frame.setSize(horizontal * 25 + 17, vertical * 25 + 40);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
}
// Prompt asking for new dimensions in case of invalid dimensions or non confirmation
if (!accepted) {
System.out.println("Please enter different dimensions.");
}
}
// Creating list of cells
cells = new int[horizontal][vertical];
// Showing the empty panel for selection of live cells
panel = new PaintCells(horizontal, vertical, cells);
frame.add(panel);
// Select live cells
panel.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if (cells[(int) Math.ceil(e.getX() / 25)][(int) Math.ceil(e.getY() / 25)] == 1) {
cells[(int) Math.ceil(e.getX() / 25)][(int) Math.ceil(e.getY() / 25)] = 0;
} else {
cells[(int) Math.ceil(e.getX() / 25)][(int) Math.ceil(e.getY() / 25)] = 1;
}
frame.repaint();
}
});
// Simulation start
frame.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == ' ') {
newGeneration();
}
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
});
}
// Generating new generations
void newGeneration() {
newCells = new int[horizontal][vertical];
// Pause inbetween generations
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
/*
* Way of Life Rules:
* Living cells with 2 or 3 living neighbours live on to the next generation.
* Dead cells with exactly 3 living neighbours become living cells in the next generation.
* Every other living cell dies.
*/
// iterate through every cell
for (int l = 0; l < vertical; l++) {
for (int k = 0; k < horizontal; k++) {
livingNeighbours = 0;
// check amount of neighbours
if (k - 1 > -1) {
if (l - 1 > -1) {
if (cells[k - 1][l - 1] == 1) {
livingNeighbours++;
}
}
if (l + 1 < vertical) {
if (cells[k - 1][l + 1] == 1) {
livingNeighbours++;
}
}
if (cells[k - 1][l] == 1) {
livingNeighbours++;
}
}
if (k + 1 < horizontal) {
if (l - 1 >= 0) {
if (cells[k + 1][l - 1] == 1) {
livingNeighbours++;
}
}
if (l + 1 < vertical) {
if (cells[k + 1][l + 1] == 1) {
livingNeighbours++;
}
}
if (cells[k + 1][l] == 1) {
livingNeighbours++;
}
}
if (l - 1 >= 0) {
if (cells[k][l - 1] == 1) {
livingNeighbours++;
}
}
if (l + 1 < vertical) {
if (cells[k][l + 1] == 1) {
livingNeighbours++;
}
}
// change cell value depending on amount of neighbours
if (cells[k][l] == 1) {
if (livingNeighbours < 2 || livingNeighbours > 3) {
newCells[k][l] = 0;
} else {
newCells[k][l] = 1;
}
} else {
if (livingNeighbours == 3) {
newCells[k][l] = 1;
}
}
}
}
cells = newCells;
frame.validate();
frame.paint(frame.getGraphics());
newGeneration();
}
}
// Our canvas
class PaintCells extends JPanel {
private Integer horizontal;
private Integer vertical;
private int[][] newOriginalCells;
// Get our X and Y from the original prompts
public PaintCells(Integer originalHorizontal, Integer originalVertical, int[][] originalCells) {
this.horizontal = originalHorizontal;
this.vertical = originalVertical;
this.newOriginalCells = originalCells;
}
#Override
public void paint(Graphics g) {
for (int i = 0; i < vertical; i++) {
for (int j = 0; j < horizontal; j++) {
// Check cell value
if (newOriginalCells[j][i] == 1) {
g.setColor(Color.black);
} else {
g.setColor(Color.white);
}
// paint according to value
g.fillRect(j * 25, i * 25, 25, 25);
if (newOriginalCells[j][i] == 1) {
g.setColor(Color.white);
} else {
g.setColor(Color.black);
} // maybe change style?
g.drawRect(j * 25, i * 25, 25, 25);
}
}
}
}
I'm guessing, the problem is somewhere in newGeneration(), but other than that, I really have no idea anymore.
You have a common problem which I had myself a few months ago.
Java Swing GUI system works in thread called Event Dispatch Thread (EDT). This thread handle events like mouse clicks, typing etc. and paint the components to the screen. You should use this thread not as your main thread, but as sub-thread which working only once a certain time/when event happens, and not let him run continuously. In your code, since the user choose the cell to live, this thread run non-stop (because you started the program inside a listener, which is part of the EDT), and your GUI stuck, because it's updating only at the end of the thread.
You can solve this by using javax.swing.Timer. Timer is an object that allows you do tasks once a while, and it is perfect to this problem.
Use code like this:
ActionListener actionListaner = new ActionListener(){
public void actionPerformed(ActionEvent e){
//Put here you ne genration repeating code
}
};
int delay = 1000;//You delay between generations in millis
Timer timer = new timer(delay, actionListener);
The code in the actionPerformed method will repeat every second (or any other time you want it to repeat), and every operation of the timer will recall EDT instead of let it run non-stop.

Java: Listen For Second KeyPress

I have a list of colors with numbers associated with them. I'd like to make it so that, if the user types in "2", it selects "2: Green"; if they type in "21", it selects "21: Yellow"; etc. I'm trying to use KeyPressed, and I think I need some way for the program to listen for the first number pressed and then wait a second to see if another is pressed. For example, something like this:
// The integer pressed will always be zero or positive.
private void jComboBox1KeyPressed(KeyEvent evt) {
int code = -1;
for(int i = 0; i < 10; i++) {
if (evt.getKeyCode() == i) {
if (code == -1) {
code = i;
} else {
code += i;
}
break;
}
}
// PSEUDO-CODE: if (KeyEvent newEvt is pressed within 1 sec) {
jComboBox1KeyPressed(newEvt);
}
}
Then, I suppose I'd have a key-value map with an index and the color numbers, and I'd set the selected item for jComboBox1 based on code (the selected color number).
I've got it working for a single keyPressed:
private void jComboBox1KeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_0) {
jComboBox1.setSelectedItem("2: Green");
}
else if (evt.getKeyCode() == KeyEvent.VK_1) {
jComboBox1.setSelectedItem("21: Yellow");
}
else if (evt.getKeyCode() == KeyEvent.VK_2) {
jComboBox1.setSelectedItem("13: Blue");
}
else if (evt.getKeyCode() == KeyEvent.VK_3) {
jComboBox1.setSelectedItem("2041: Red");
}
}
Here's the interface:
PLEASE NOTE: This is an example I quickly made and not the actual app I'm creating. My app involves many more number options, but as with this example, the numbers will always be positive.

Game of Nim, computer will not make move

I am attempting to make a Game of Nim for a school project, but whenever the computer attempts to make its move, it will not actually do anything unless there is three or less rocks left. My temp code will also execute in the console, so it runs through it, its just not working.
These are the custom voids:
public void winnerCheck(){
if(rocksLeft == 0 && lastPlayer == 0){
logBox.append("Plose gameOver");
}else if(rocksLeft == 0 && lastPlayer == 1){
logBox.append("Close gameOver");
}
//temp
System.out.println("winnerCheck() successful");
}
public void playersMove() throws BadLocationException{
lastPlayer = 0;
//used to gather players input and attempt to make a move
try{
playersRocks = Integer.parseInt(txtfPlayer.getText());
if(playersRocks <= 3 && playersRocks>=1){
rocksLeft -= playersRocks;
logBox.append("You have taken "+playersRocks+" rocks.\nThere are: "+rocksLeft+" rocks left.\nIt is the computer's turn.\n\n");
}else{
isValid = false;
}
}catch(NumberFormatException e){
isValid = false;
}
//temp
System.out.println("playersMove() successful");
}
public void computerAttempt(int computersRocks){
//this void is a snippet for the computer trying to make a play.
//contains only outputs and rocksLeft altering equation
logBox.append("\nThe computer is making a play.\n");
try {
//makes the game feel more realistic, as the computer takes time to make a move.
Thread.sleep(1600);
} catch (InterruptedException ex) {
Logger.getLogger(GameOfNim.class.getName()).log(Level.SEVERE, null, ex);
}
rocksLeft -= computersRocks;
logBox.append("The computer has taken "+computersRocks+" rocks.\nThere are: "+rocksLeft+" rocks left.\nIt is your turn!\n\n");
}
public void computersMove() throws BadLocationException{
lastPlayer = 1;
//computer will attempt to win, if not possible at the time it will take a random number
if(rocksLeft == 3){
computerAttempt(2);
}else if(rocksLeft == 2){
computerAttempt(1);
}else if(rocksLeft == 1){
logBox.append("The computer takes 1 rock.\n There are: 0 rocks left.\n\n Y O U H A V E W O N\n\n");
}else if(rocksLeft > 3){
computersRocks = (int) (Math.random()*(3-1+1)+1);
}
//temp
System.out.println("computersMove() succesful");
}
These are the two buttons:
private void buttonStartActionPerformed(java.awt.event.ActionEvent evt) {
//starts/resets game
rocksLeft = (int)(Math.random()*(30-15+1)+15);
buttonStart.setText("Reset");
buttonPlay.setEnabled(true);
logBox.setText("T h e g a m e h a s b e g u n !\n\nThere are: "+rocksLeft+" rocks left.\nIt is your turn!\n\n");
}
private void buttonPlayActionPerformed(java.awt.event.ActionEvent evt) {
//check if game is over
if(rocksLeft == 0){
logBox.append("The game has completed!\n Press the reset button to play again!");
System.out.println("gameOver succesful");
}else{
try {
//allows each player to move then checks if the player won or not
playersMove();
winnerCheck();
computersMove();
winnerCheck();
//temp
System.out.println("buttonPlay() succesful\n");
} catch (BadLocationException ex) {
Logger.getLogger(GameOfNim.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I don't know the game you are making, but here is your mistake I think.
If there are more than 3 rocks left, you call
}else if(rocksLeft > 3){
computersRocks = (int) (Math.random()*(3-1+1)+1);
}
Which, I think, should be
}else if(rocksLeft > 3){
computerAttempt((int) (Math.random()*(3-1+1)+1));
}
Because you just changed the variable without calling the method on the value.(And I presume the important variable is the 'rocksLeft' which never got changed in your code, when the value was > 3.

Java label set icon with getComponent

I have a frame, in this frame I have 10 labels.
If I click on label then it icon should be set to "zoldgomb.jpg", if I click a second time it should be set to "sargagomb.jpg".
This part is working, now my question is: How can it be written so that I don't have to write this part down ten times for each of the 10 labels (label name A1-A11)?
if (event.getSource()==A1) {
if (x==1) {
A1.setIcon(new ImageIcon("zoldgomb.jpg"));
x=2;
}else if (x==2) {
A1.setIcon(new ImageIcon("sargagomb.jpg"));
x=1;
} }
event.getSource() return reference to your JLabel, you can use something like next:
if (event.getSource() instanceof JLabel) {
if (x == 1) {
((JLabel)event.getSource()).setIcon(new ImageIcon("zoldgomb.jpg"));
x = 2;
} else if (x == 2) {
((JLabel)event.getSource()).setIcon(new ImageIcon("sargagomb.jpg"));
x = 1;
}
}

Selecting buttons at random in java

How can I randomly select a java button, I am trying to create a tic-tac-toe game where the user plays the cpu or another player. I have it working fine for 2 players but am stuck on 1 player game, I dont know if it can be done but my idea is that I just randomly select a button for the cpu check to see if its been selected previously and then assign the appropiate x or o to the selected square .
public void buttonSelected(ActionEvent click) {
Object source = click.getSource();
// loop through to see which button has been selected
if(onePlayer){
// User Vs CPU
/*if((turn % 2 == 0)){// CPU Turn
int selected;
do {
selected = new Random().nextInt(btnEmpty.length );
if (chosen[selected -1] == false){
chosen[selected -1] = true;
}
}while (chosen[selected -1] == true);
source = Integer.valueOf(selected);
for(int i=1; i<= btnNotSelected.length; i++) {
if(source == btnNotSelected[i] && turn < 10) {
btnClicked = true; // user has selected a button
// Check which user selected button and insert appropriate x or y
btnNotSelected[i].setText("X");
btnNotSelected[i].setEnabled(false); // disable selected button
pnlPlayingField.requestFocus(); // highlight selected panel
}
}
}
else{ //User Turn
for(int i=1; i<=9; i++) {
if(source == btnNotSelected[i] && turn < 10) {
btnClicked = true; // user has selected a button
// Check which user selected button and insert O
btnNotSelected[i].setText("O");
btnNotSelected[i].setEnabled(false);
chosen[i] = true;// disable selected button
pnlPlayingField.requestFocus(); // highlight selected panel
}
}
} */
turn++;
}
else if(twoPlayer){
for(int i=1; i<=9; i++) {
if(source == btnNotSelected[i] && turn < 10) {
btnClicked = true; // user has selected a button
// Check which user selected button and insert appropriate x or y
if(turn % 2 == 0){
btnNotSelected[i].setText("X");
}
else{
btnNotSelected[i].setText("O");
}
btnNotSelected[i].setEnabled(false); // disable selected button
pnlPlayingField.requestFocus(); // highlight selected panel
turn++;
}`
A one player tic-tac-toe game can certainly be done, and your strategy of selecting at random is fine. The first specific error in your commented out one player code is an infinite do-while loop. The condition on the loop always evaluates to true since chosen[selected - 1] is always true (if it is false, you set it to true right before the condition check), and therefore loops again.
Your do-while should look like this:
do {
selected = new Random().nextInt(btnEmpty.length);
} while (chosen[selected - 1] == true);
chosen[selected - 1] = true;
That way, you are setting the chosen flag after the while loop condition.
A couple of additional issues I see with the onePlayer block:
in the CPU turn block, the comparison between source (here an Integer) and btnNotSelected[i] (assuming a java button based on the working code in the twoPlayer block) will not work as you expect it to
this method is called in response to a user input, which is clicking one of the buttons. The computer will not provide any such input, so you should have another trigger that calls running the code for the computer's turn. The easiest one is just to run it after the user's turn
Without making any drastic changes to your overall coding style and strategy, I'll attempt to translate the onePlayer portion into something more functional:
public void buttonSelected(ActionEvent click) {
Object source = click.getSource();
if (onePlayer) {
// User's turn first
source.setText("O");
source.setEnabled(false);
pnlPlayingField.requestFocus();
// Mark that button as chosen
for (int i = 0; i < btnNotSelected.length; i++) {
if (source == btnNotSelected[i]) {
chosen[i] = true;
break;
}
}
// Increment turn counter
turn++;
// Check if game is over
if (turn > 9) return;
// CPU turn
int selected;
do {
selected = new Random().nextInt(btnNotSelected.length);
} while (chosen[selected]);
chosen[selected] = true;
btnNotSelected[selected].setText("X");
btnNotSelected[selected].setEnabled(false);
pnlPlayingField.requestFocus();
turn++;
} else if (twoPlayer) {
/* your preexisting twoPlayer code */
}
}
int selected;
do {
selected = new Random().nextInt(btnEmpty.length );
if (chosen[selected -1] == false){
chosen[selected -1] = true;
}
}while (chosen[selected -1] == true);
code above is an endless loop, change it to:
int selected;
do {
selected = new Random().nextInt(btnEmpty.length);
}while (chosen[selected] == true);
chosen[selected] == true;
remove -1, because nextInt(n) will give you a number "between 0 (inclusive) and n (exclusive)"
I, personally, would start out with a List of the buttons, removing each on as it become selected, this would make easier to determine what has being selected and what hasn't, but lets work with what we've got...
List<Integer> free = new ArrayList<Integer>(chosen.length);
for (int index = 0; index < chosen.length; index++) {
if (!chosen[index]) {
free.add(index);
}
}
if (!free.isEmpty()) {
Collections.shuffle(free);
int selected = free.get(0);
// Continue with game logic
}
Basically, this places an Integer in a List which represents the "free" slots. We then use Collections.shuffle to randomise the list, then we grab the first element (for want of something to grab) and continue with the game logic...
This eliminates the possibility of a infinite loop trying to find free slots that don't exist...

Categories