I am currently building a game where I would need to do the following:
New currentControls(defualt);
User selects an object on the board
If (selectedObj instanceof typeOfObj) { currentControls.setControls(typeOfObjControls); }
Is there a good structure I could use to implement this? I've looked at a few tutorials but haven't found a good modular way to do it.
Any ideas?
I figured out the answer on my own.
When creating my Controls Class I needed to over write the constructor to contain the object the Player had selected and update in the main game loop which object is currently selected. On instantiation of Controls the Keyboard, mouse, and game controller would be set the same way via the passed selectedObj The Keyboard class would then in turn would only do methods appropriate to the Obj:
if Dog is selected { A: left, D: right}
if bird is selected {A: fly left, D: fly right}
Related
I'm making a Java chess game as a uni project, and I'm basically getting the move from a player using a GUI.
My game has several classes, but the main classes are the Pieces, HumanPlayer and GraphicalDisplay classes.
What I'm basically doing is, when the HumanPlayer wants to make a move, it's currently using a class called PieceController, which is using a PieceModel class and the GraphicalDisplay classes as the model and view.
The problem is that I'm having to write code to set, for example, MouseListeners to certain cells in the chess grid (contained in an two dimensional array called cellHolder) in the Model class. This is because the code that contains adding listeners to cells also change state of the data, which is then used to display the game in the GUI.
This is causing a problem. The cellHolder object is created inside the GraphicalDisplay (GUI) class, but it's also used in the model and therefore the model is using data from the view.
I can't really think of another way of doing this without having to share (or pass as an argument) the cellHolder.
Any suggestions on how to improve the current MVC design?
Check this link http://www.tutorialspoint.com/design_pattern/adapter_pattern.htm
About Design Patterns In Java this one is called "the Adapter Pattern" .. It's basically a software design pattern that allows the interface of an existing class to be used from another interface
I wrote a simple little maze game for a terminal which repeatedly asks the user to do something (e.g. "In which direction would you like to go? [N/E/S/W]"). I have a navigate() method running in a loop that fires off these questions, stores their answers and does something depending on the answer.
public enum Dir (N, E, S, W);
public void navigate() {
Dir nextDir = utils.askDirection("Which way do you want to go?");
// Do stuff with answer, like changing position of user in maze
}
Now, I've written a simple GUI for my game. I deliberately put all the references to the terminal in a ConsoleUtils class which implements a Utils interface (this has methods like askQuestion()) - the idea being that I could create a GuiUtils class and have my game either as a terminal game or as a GUI game.
The problem is that the navigate method asks the user a question and then "waits" for the response, which the Utils class gives it by using a Scanner to read the newest line of input. However if I use Event Listeners for the new N/E/S/W buttons in my GUI, they fire off events regardless whether the navigate method has asked for one or not.
--> Image of GUI
Is there any way I can combine this or do I need to write a new navigate method for the GUI?
(To be honest, I'm also not entirely sure whether my GUI class should instantiate a game class, in which case the logic for navigate could end up in a GUI method anyway, or whether the game should have a GUI. I haven't written any code for the event listener either yet, since I'm not sure which class should be calling which. This is probably a separate question.)
Your text based game has a loop that repeatedly asks questions to gather user input. Swing provides this loop for you by continually executing Runnable blocks of code that have been posted to the EventQueue. For example, when the user presses a button labeled E, code is posted to the queue that invokes your ActionEvent implementation to handle your game's interpretation of the move east command.
For reference, a complete example of a very simple guessing game is examined here. In pseudocode, the corresponding text based game might look like this:
initialize
loop
prompt "Guess what color!"
get chosenColor
if chosenColor = actualColor
say "You win!"
reset game
else
say "Keep trying."
end loop
A more elaborate game cited there includes the original text-based source.
Hi everyone I am making a simple Libgdx game on Android that involves touching the screen and hitting things. However I have came across a slight problem. I have a small selection of weapons to choose from but I don't know what the best way is to actually draw/access the selected weapons. They are all inherited from a basic weapon class. Right now I have a normal hammer class and a fast hammer class which extends hammer.
The fast hammer has some special methods and swings faster. When I just instantiate the classes I want to test it works fine. However I want to do a check to see which hammer has been selected beforehand and then access it and draw it. I can't think of a very elegant way to do this other than a whole mass of if statements.
I originally tried giving a check variable an int. Then If int 1 then hammer = new normalhammer(); else if int 2 then hammer = new fasthammer(); but this clearly won't work because my hammer variable is assigned to the normalhammer class i.e. Hammer hammer; What is the best way to do this thanks.
Hammer hammer;
FastHammer hammer;//this obviously won't work because duplicate names
if(selected==1){//this was the plan but again won't work because duplication
hammer = new Hammer();
}
else if (selected==2){
hammer = new FastHammer();
}
hammerframe = hammer.HammerAnimation.getKeyFrame(hammer.hammerTime+=delta, false);
//then accessing the class variables won't work because again hammer is a duplicate field. I basically want to check what the check int is and then set hammer to the right class based on that int and the rest of the code will automatically retrieve what I need. Is this possible?
Well, here's how I would do it.
Make all your weapon classes implement some interface/superclass that will help with rendering
Keep an "armory" of weapon objects, one for each weapon type
When the player selects a weapon, set the representative existing object into a "current weapon" variable
For rendering, use the existing "current weapon" variable
Then you don't need to deal with the different types in the renderer - it's abstracted behind the interface!
first off apologies for the question title I wasn't to sure what to name this.
anyway I'm working on a game, where there can be a number of players who can each have a number of pets. I have developed the main structure to the game, e.g. player class, pet class and a main class .. From there I have been working on the GUI, where I ask how many players, and how many pets each player would like.. Where I am getting stuck is how to to create the pets for each player.
I have created a pretty basic form that asks for the player to choose a pet type, give it a name and then create the pet..
public void createPets( final Player player){
//various buttons,comboBox and labels go here
//layout managers
//add it all to a frame
JButton jbCreatePet = new JButton("Create Pet");
jbCreatePet.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String name = jtfName.toString();
if (cbSpecies.getSelectedIndex() == 0){
Alien alien = new Alien();
alien.setName(name);
player.getAllPets.add(alien);
}
else if(cbSpecies.getSelectedIndex() == 1){
create other pet2
}
else{
create other pet3
}
}
});
player is a Player object passed into the method using a for loop..
for (Player player: allPlayers){
createPets(player);
}
Now I know that its NOT correct to use the for loop e.g. the form will simply skip to the last player and none of the other players will get to create a pet..
So I have a couple of questions:
When I assigned the created pet to the players list of all pets, eclipse told me I had to create it final. I somewhat understand why but what I am wondering is by making the player parameter final does that mean i wont be able to create pets for other plays, only the first player..
How can I show my form to each player e.g. 2 players in the game both with 2 pets, player 1 chooses a pet and gives it a name then creates it, he will then be told he needs to create another pet (form shows again) so he creates another pet, then its player 2s turn to choose and create 2 pets... I guess I am trying to figure out how to pass the right player argument into the createPet method at the right time...
Please let me know if you would like me to clarify anything else...
Big thanks to whoever can help me with this!!!!
When I assigned the created pet to the players list of all pets, eclipse told me I had to create it final. I somewhat understand why but what I am wondering is by making the player parameter final does that mean i wont be able to create pets for other plays, only the first player..
Eclipse isn't requiring this -- Java is because you're using the Player parameter within an anonymous inner class and so it must be final. This will not prevent you using this same method for other players.
How can I show my form to each player e.g. 2 players in the game both with 2 pets, player 1 chooses a pet and gives it a name then creates it, he will then be told he needs to create another pet (form shows again) so he creates another pet, then its player 2s turn to choose and create 2 pets... I guess I am trying to figure out how to pass the right player argument into the createPet method at the right time...
The main Game object will control all of the above, correct? I suppose you could use a for loop, one that say displays a modal dialog such as a JOptionPane inside of the loop.
Another option is to create JPanel view that allows all players to enter their pets. It's all up to you, and I recommend that you experiment with different approaches.
One main thing that you'll want to be sure to do early on is to strongly separate your program's logic from the GUI. For instance your Player and Pet classes should have no knowledge about the GUI, should have no Swing code whatsoever, so that the code to logically add Pets is non-GUI (but can and will be used by the GUI).
Edit
Consider giving your Game class a registerPlayer(Player player) or editPlayer(Player player) method that any Player can call to register their name, their Pets, and any other property that may be needed to play the game. Then have this method called once when a JButton is pressed. Don't allow the game to progress unless all Players have been properly registered.
I am making a knight's tour implementation in java, and currently I have everything jumbled into one giant mess of code.
I have a class called MainFrame which includes the code to solve the knights tour as well as methods for the menu, etc.
I want to create a new class called KT (for knights tour) which will contain the code for the algorithm, but I'm having lots of issues doing that.
I don't want to post code here because i dont want someone from my class copying or something, so I will just briefly explain.
In class KT, I have declared the variables, arrays, etc. I have methods such as printSolution, move, redo (the backtracking), etc.
However I am unsure how to tie in the code for the buttons (which is declared in MainFrame). For example, I have a loop in the print method that prints the correct solution on the 8x8 board. Right now I'm being asked to create a new method for the button even though I have the button in class MainFrame.
I have a KT k = new KT(); and then I'm launching MainFrame. Is that where I am doing it wrong or is it something really simple that I'm being too dumb to figure out?
tl;dr program works well when i have everything in one class, but i want to make two classes and make everything "look" nice
First of all, give your KnightTour class an actual name. Like, you know, KnightTour. If you were coding this for cash money, the next guy who had to read your code would want to punch the guy who called a class something like KT.
Think about creating this class so that you can use it from a GUI controller like your button and menu laden applet. But so that it can ALSO be used from, say, a character based application where you type commands at a prompt and have your new class evaluate those commands.
The reason I suggest this is because it will force you to create your KnightTour class so that it is PURELY the "business logic" for your app. By that I mean that your KnightTour class should not know anything about buttons, menus, GUIs, text interfaces, or anything like that. You need to think about your KnightTour class in terms of what it must accomplish.
I don't really know what KnightTour does. So I'll just throw out some ideas of what kind of functionality it might need to support. I'm assuming everything takes place on a chess board?
Get the state (occupied, unoccupied) for a given board location (x,y)
Put a chess piece (piece enumerator) on a given location (x,y)
Validate placement of a piece (piece enumerator, location x,y)
Suggest a move, returning a Suggestion object with a piece enumerator and location
Reset the board to start all over.
Now, when you push a button that says "place a piece on 5,5" then you'll handle that event in your GUI controller, and then call the "set piece" method (#2 above) to do that work. If you have a character based application, when you type "put knight at 5,5" then you'll parse that text, and then invoke #2 above. The key point is that both of those user interfaces access the same KnightTour methods to do the same work.
In the actionPerformed method of your MainFrame class just call the appropriate methods to get the solution from KT (which, by the way, I would rename to KnightsTour...readability counts).
Ideally you want all your logic (the model) broken up into sensible methods in KnightsTour, and all your display and button-handling code (the view and controller) in MainFrame. If that's difficult, it's a good sign that you need to rethink how you divided things into methods (and what you're doing with global state...which is frowned upon).
I'm sorry I can't be more specific--I'm kind of hand-tied since you didn't post code.