I am working on a Minesweeper which I have pretty much complete.
The only thing missing is the detection of winning. What would be the best way to implement this? I know its going to be part of the actionlistener that detects clicks, at some point the last click should detect a winner.
Could anyone give me some ideas thanks!
The player has won if
numUnopenedCells == numBombs
(where a cell is unopened if it is in its initial state, or flagged as a mine).
If numUnopenedCells > numBombs then the player has unopened cells which are not bombs (i.e. some work left to do)
If numUnopenedCells < numBombs then the player has necessarily "opened" a bomb cell and already lost.
I know its going to be part of the actionlistener that detects clicks, at some point the last click should detect a winner.
Yes, this snippet would be executed directly or indirectly by the action listener. I'd suggest you have a model of the game state, and in the openCell(int x, int y) method you check the above, and take the appropriate action.
If the opened fields are #(all fields) - #(bomb fields).
Related
For our purposes, let's think of it as a chess game. So I touch the queen and click it, then I touch the square that I'd like to move the queen to and the queen moves to that square.
I think this is a pretty simple task, but I've searched high and low for an answer but yet to find one.
Original I was using an OnTouchListener and Drag and Drop, but it will work much better with OnClick so that the user clicks twice to move the object. (I think)
Thanks for the help.
Setup an OnClickListener and a variable to hold the selected field. onClick, check if there is already a field selected, if not, store the clicked field in the variable. If there is already a selected field stored, move the piece from the stored field to the clicked field and clear the variable to allow a new move. You can apply your game logic/rules in the second branch too.
First of all I'm new to Java and you'll probably be able to guess. Thanks in advance.
I'm currently writing a board game using Java for an assignment. There are human and computer player classes, and the GUI only in the human player class. In the human class I use a class called 'UI' extending JFrame where the user selects a piece.
At the moment this UI class waits for the enter button to be pressed then sets a 'done' variable to true. In the human player class I create this UI then wait in a while loop continuously checking this boolean before getting the X/Y position of the move.
while (!input.moveEntered());
move.setPosition(input.getX(), input.getY());
This only seems to work if the while loop is not empty. If I add a print statement in there it works fine.
It seems like there is a much better way to go about this. I've looked into dialogs but I don't close the window every time a move is entered.
Thanks for reading.
You should never be busy-waiting; in your case, I would show a modal JDialog. When the user has entered their input, the setVisible(true) method will end without busy-waiting.
Or maybe I misunderstood your problem and you only need to define an event listener in your main JFrame in order to handle user input. Have a look at
http://www.tutorialspoint.com/swing/swing_event_listeners.htm
I'd like to know what object a swing MouseMotionEvent (or MouseReleased) ends in. The problem is that both the MousePressed and MouseReleased events go to the object that was under the "press", not the release.
Here's a contrived example that may explain better:
The user sees a screen with some balls and some baskets and is told to drag a ball to a basket. Each ball represents some entity in the application space and each basket represents some action to take in the application space. From the Swing point of view, the balls and baskets are implemented separately as highly-overridden JButtons. On mousePressed the ball stores its identity in a known place. I'd like mouseReleased to be caught by a MouseListener in the basket which checks up the balls identity in the known place and then goes off into program logic and do the task represented by that basket.
But as I understand Swing (actually the AWT) the mouseReleased event goes to the component that contained the mousePressed event (ie the ball). Other than looking at X and Y (which seems an atrocious kludge) how do I figure out which basket the mouseReleased happened in? (If the mouseRelease happened outside of any basket, I'll need to take some sort of default reset action. Than can be done by a mouseEvent handler in the underlying JPanel).
(Please don't tell me this is a poor interface. The example I've given is not real. It abstracts out the problem I have in a way that I think is easy to visualize and understand.)
If the mouseRelease happened outside of any basket, I'll need to take some sort of default reset action -
Use the Drag and Drop API and then you will only be able to drop on components that support your drop.
Other than looking at X and Y (which seems an atrocious kludge)
Why? The event doesn't have the information so you need to get it somehow. So if you don't want to use the DnD API then you need to do this yourself.
There are methods in the API to help you do this:
Window window = SwingUtilities.windowForComponent( e.getComponent() );
Point dropPoint = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), window);
Component dropComponent = SwingUtilities.getDeepestComponentAt(window, dropPoint.x, dropPoint.y);
I've been playing with the Robot class recently and I have it doing what I want, but I haven't figured out how to interrupt/stop its actions via user input.
For example: I want it to click the desktop a hundred times but I decide forty clicks in that I want to make it quit (or pause).
I'd like to be able to do something simple like press a certain key or press the middle mouse button in order to tell it to stop. In order to do this, it must be able to listen for input outside of the Java application, since the actions the Robot is performing are in other programs.
As edward said, there doesn't seem to be a way to do exactly what I was looking for. So this answer is to explain how I achieved an acceptable substitute.
The other question edward linked to had stated that
MouseInfo.getPointerInfo().getLocation()
is capable of fetching the mouse coordinates regardless of what the mouse is doing. My program uses the robot class to control the mouse within a specific range of coordinates. I also wanted to be able to disable the program via user input.
To achieve this result, I compared the x and y coordinates of the mouse to the x and y coordinates that the robot last set it to. If the two do not match, the program exits.
Pausing the program by this method would be impractical because resuming would require going back to the original x and y coordinate prior to pausing, but it does at least give an example of how to achieve stopping without actually being focused on the java parent program.
In order to pause the program, you would instead compare the coordinates to a range of coordinates (have the coordinates create an imaginary, 2D box). If the mouse's coordinates are within that range: pause. To resume, do the opposite check (mouse not being in that range).
You may be able to use some of the code from this answer:
Simulate a key held down in Java
And then add a Listener to whatever action, component, whatever to call the stop method on the robot command.
Does that provide you with some inspiration?
Edit After further discussion the real question is:
How to respond to external Mouse Events (Outside the Java Application) inside a Java application?
It Seems that you can't without native code and Mouse Hooks as it's OS Dependent.
For Further Discussion see Is it possible to have a MouseMotionListener listen to all system mouse motion events?
I am programming a simple game in Java but I am trying to do it 'right' with a nice clean design and no hacks.
I have two classes GamePanel that receives clicks and keypresses and Model which contains all the Entities and is responsible for their updates. The Model needs to know where the user's mouse is but I can't decide on the 'right' way to do it.
Should the Model and every Entity within keep a reference to the GamePanel or just cache the last known mouse position and receive updates from the GamePanel regularly. With the first option when the model is created it will need to be given a reference to the GamePanel and with the second the last mouse position will be sent as a parameter to the World.update() method.
Neither of these solutions seem elegant so I was wondering if there is a 'right' way to do this that I have missed.
Thanks,
Ben.
In my opinion, it would depend on how your classes are interacting. Is a change in the mouse position triggering the entities in the Model class? or Is the Model class independent of the GamePanel and only works on the current values of Mouse position?
If the later and in that case I agree with what jeff has said before me. Pass a handle on the GamePanel to the Model class when creating it and let every entity use the handle to access the mouse position whenever needed. That way the updated mouse position is always used.
If the former I would suggest use Observers to let the Model class know when the mouse position value has changed. Then Model class could use the same design (i.e let the Model class have an handle on the GamePanel class always) and access the current values in the GamePanel.
To sum up my answer to your question, I think it is only logical and conforming to OO concepts to let the GamePanel hold the values of the mouse position and let other classes access that information using a GamePanel instance.
Thanks,
Rohan.
A common solution is to fire events. In this case GamePanel would fire an event informing all interested (subscribed to the event) objects (Entity) in Model of the new mouseposition.
In this way, GamePanel does not need to know explicitly which Entities to inform, and the Entities do not need to keep an instance of GamePanel around.
My advice: read up on event listeners. (Java.util)
You could always have the current coordinates be a public property of the GamePanel, so that the Entitys/Models can simply access it from there.
Does the model always need to know where the mouse is. IE: Do you need the mouse position at every update, or only at a specific points.
If you need it at every point or during every update then either solution that you have documented, or anyone solution here should be solid.
If you only need it at specific points then only grab it then. (IE: if your game responds to mouse clicks then do something on click.) Meaning Read about Event Listeners.
Good luck! Hope that helps.
If you want to poll the current mouse position, you could run this code snipplet in the Event Dispatch Thread at any time you wish:
Point pt = MouseInfo.getPointerInfo().getLocation();
Point cpt = GamePanel.getLocationOnScreen();
Point rel = new Point(pt.x - cpt.x, pt.y - cpt.y);
And rel contains the relative position of the mouse.
Alternatively, you could have a look at JInput, as it is designed to cope with keyboard/mouse/whatever inputs.