So I have a massive project due soon, and for whatever reason my JButton's in my grid will call my incMoves() method which updates the private state of the move counter, but on the other hand, will not update the grid as it should be until i click the same button again.
The incMoves() method is called both times, so my move counter is completely out of whack.
Here is the code for my button:
class genWindow:
http://pastebin.com/SJ4wzYX0
class Jam: http://pastebin.com/87nqPBYP
Here is my test file: (txt)
6 5
4
0 3 2 3
3 1 3 3
3 0 5 0
2 0 2 1
Let me know if more information is needed.
Other than debugging the program with breakpoints within the ActionListener inner class to check whether it was actually making it in to the actionPerformed method, I don't know what else to attempt to fix it. I'm reasonably new to swing, and am rather confused by this, considering no errors or noticeable changes happen, besides the colors not switching.
The car buttons should be shifting depending on the large if statements in the actionPerformed method, and then updating grid with the addGrid function. The move counter is incremented, although the buttons don't actually shift unless clicked again.
Related
I'm really new to JAVA and I'm making tic tac toe game i finished player vs player and now i want to do player vs cpu what i want to do is when player click on a button for X to appear then cpu will select their button randomly for O to appear but i don't know the code to randomly perform action
I was searching on internet for hour and still no result
here's my code
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
//my code here to set text and background etc
if(jButton3.getActionCommand().equals("X") && jButton1.getActionCommand().equals("") && jButton2.getActionCommand().equals("")){
//this where i wanna random between 2 button action
}
I want the CPU to choose between
jButton1ActionPerformed(evt);
jButton2ActionPerformed(evt);
but i really have no idea how to do it
My Understanding
I am interpreting your question as How do I write a function which randomly marks one of the remaining squares with the letter 'O', after the user has performed a move. Correct me with a comment if I am mistaken.
Problem Approach
Because I do not know the exact nature of the code representing your cpu player I will provide a high level solution that you can implement.
1. Firstly, after a player marks a square with the letter 'X' you must check which squares still remain unmarked. You can do this by initializing an ArrayList of Integers, 1 through 9, at the start of the game which represent the squares (buttons) which are still unmarked.
Figure: Numbered Tic-Tac-Toe Board
2. Second, each time one of the squares is marked, either by the player or the cpu, remove the corresponding Integer from the list.
3. Watch for button action events in the way you currently are and add the following code. (our ArrayList of Integers is named unmarked_boxes).
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
//my code here to set text and background etc
if(jButton3.getActionCommand().equals("X") && jButton1.getActionCommand().equals("") && jButton2.getActionCommand().equals("")){
Random rand_gen = new Random();
int index_selected = rand_gen.nextInt(unmarked_boxes.size());
box_selected = unmarked_boxes.remove(index_selected);
/*
here you select and mark the button which corresponds to the box
selected. i.e. if the box_selected = 3, then find and mark button3 (not sure
how you access your buttons).
*/
}
In the code I inserted we instantiate and Object of type Random and call its member function nextInt(int bound). This function will generate a number between 0 and the int 'bound'.
In this case we want to select a square from the entire list of unmarked squares.
Hence we generated a number between 0 and the number of remaining squares.
Then we grab (and simultaneously remove) the number which is at 'index_selected' in our list of unmarked_boxes and mark the corresponding button with an 'O'.
After this point, you need to implement your code to mark the chosen button.
Note: If you are dead set on selecting between only 2 squares, then forget about the ArrayList approach I have described. Just call rand_gen.nextBoolean() and insert an if statement which picks one button if true and the other if false.
This should be enough to get you started implementing your solution,
Good Luck
I am programming a lottery program in JavaFX (6 numbers of a 6x7 field + 1 lucky number) which allows the user to create max 10 tips at once. Then the program evaluates if he won.
My current problem is to delete a Tip that has been added. I have a Class "Tip" which consists of an HBox with 7 labels inside (for the 7 chosen numbers) and a button to delete this Tip (so every Tip has its own delete button).
My function to add a Tip works (even though, I don't really know if this is really clean. So if you see any improvements, I'd be really thankful! :-)).
Under the following link, you can find my entire code:
https://github.com/Lucindor/Lottery.git
The main issue or part that I'm stuck is in the Controller from line 73 to 80 (deleteTip method).
My main idea was to removethe parent of the button (which should be the Tip(?)), which should have removed the entire tip...but this didn't work. I couldn't get any ideas more.
In the end, I want to be able to click on any delete button which removes this tip and moves all the tips below (So that it's like a list).
Thanks a lot!
Spent the last 1-2 hours looking for the answer and haven't quite got it, I'm hoping it's something simple.
I have created an array of a custom class with just a small number of values like so (where Person is a Class):
Person person[] = new Person[2];
(Note I'll be replacing the 2 with a variable like numberOfPeople)
I need to create a method that I can call when needed (on a button press for example) that will move from the current person to the next in the array (going back to the start when it reaches the end).
In other words: On button press - move from player0 to player1.
I've been looking at how to use For, ForEach, If etc to do this but can't work it out.
SOLVED VIA Vincent Ugenti's ANSWER.
In case other noobs come across this to solve their problem, had a few subsequent noob issues where I was trying to define the number of values in the array with a variable which wasn't given a value until after the array was made (causing instant crashing) and then I forgot doing nothing more with the code than this won't run each objects Constructor which needs to be done separately.
Create a variable such as "currentPerson"
In your event handler (whenever you want to advance to the next person), currentPerson = (currentPerson + 1) % numberOfPeople
The current person is then always person[currentPerson]
Keeping track of the current index and hooking a button event is what you want to do.
On button press check your variable, increment it by 1 and check if that value is not greater than the length of the array.
If its not greater than the array length you can grab the element, else reset the count to 0 and do what you need to do.
If youre only doing this on button press (event handler) you shouldnt need to use any loops.
I have a JSlider with a minimum of 0 and maximum of 100. Its current value is 0.
At runtime how do I get every value that slider is moved across? For example, with my current code if I change the slider very fast from 0 to 16, it shows like output like:
0
1
2
3
4
7
16
It skipped 8 to 15 because I moved the slider directly from 7 to 16 very fast. But I do not want that to happen. I want each and every value to be printed that the slider came across in its movement.
I also want the program to capture the last value the slider took in its movement.
This is my code:
p1.addChangeListener(new ChangeListener()
{
#Override public void stateChanged(ChangeEvent e)
{
if (p1.getValueIsAdjusting())
{
System.out.println(p1.getValue());
}
}
});
Can anyone explain why this isn't printing every value?
Most people have the opposite problem in that they just want the final JSlider value and not the intermediate values.
There's no guarantee in Swing that your change listener is going to be called for every intermediate value.
Keep the previous JSlider value, determine the final value, and calculate the intermediate results yourself.
If I'm not mistaken, the getValueIsAdjusting() is responsible for the behaviour you see. It is there to avoid needless code execution while the users drags the slider.
If you remove the if clause, you should see every value.
At runtime how do I get every value that slider is moved across?
Since a JSlider is effectively 1 dimensional, I'd suggest:
Get the starting value
Get the end value
get every value in between from the model used for the slider.
The result of point 3. would seem to be the answer to your question.
Of course, since it is such a trivial answer, it then raises the $64 question of..
What application feature are you trying to offer the user? Explain it as you might explain it to an end user, for better answers.
I wanted to make a "memories" or "find pairs" game for Java, just to start with a basic game before starting something more difficult. My game works very well, but I just have 1 big problem. I don't really know how to hide the 2 images x seconds after the user clicks on the second one.
This is what is done:
The game is created with 4x4 buttons in a array
The values of the cards are distributed (name, position, icon)
The user then clicks on the first card, which shows up immediately
The user clicks on the second one, if the first is equal to the second one then disable the 2, if not then re-hide the 2.
But I don't know how I can make the program show the second card and then after x seconds hide the 2...
How can I solve this?
Depending on what framework you are using, there should be a timer utility available to you.
For instance, if you are using Swing, then you should be able to use javax.swing.Timer as suggested in the above comments. Follow this link for a simple Swing Timer tutorial. As they say on that page, swing timers can be used in one of two ways and one of those ways is:
To perform a task once, after a delay
This sounds like exactly what you are trying to achieve.
You could also try a library like Joda Time which has lots of features and options.
you should make match function and another unmatch function and inside the unmatch function
you can use setTimeout and give the two cards class(flip) when you choose them and after certain time and when they didn't match remove this flip class.