Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Java Predicament, classes within classes?
I'm making TicTacToe in java...
So far, I've made it fully functional, but only for two players. Now that I've added a CPU, I found that I need to entirely restructure my program.
The only problem I ever have is taking values out if methods, and putting them into others. I finally figured that out, but now I'm messing with events, and private classes, that won't allow me to easily move values around.
Upon realizing this, I tried to put everything into one class.
Something like this
private static class Menu implements ActionListener {
public void actionPerformed(ActionEvent evt) {
}
}
This worked out great, but then when I started making the CPU, the program got confused because there were multiple if statements that were the same, but with different commands...
So I decided, I have to make multiple classes. I figured out that I could, make multiple classes, and I learned alot by figuring this out.
Now though, my code will not run almost at all. Before doing this, I could atleast play TicTacToe, but now I can't; I can only choose the options.
So basically, here's the question
How do I make the user able to choose to play against the CPU or another player? Do I need to make a whole bunch of classes, and then one class that has an if statement, that runs all the classes? That's what I'm thinking, but I need some help doing it. I already tried making a huge if statement, going through each possible scenario of options, but it didn't work b/c of the nature of if statements and b/c there are two options to choose from.
This will probably involve emailing back and forth, unless my mistake is simple, and I am on the right track.
Here's the code
Good Luck reading it, it's full of unfinished thoughts, and most definitely stupid mistakes.
I just don't know if I'm on the right path, or if I'm straying away.
I don't want too much help. I've done all of this so far on my own
also, just so I don't look too dumb, here's the working PvP. The difficulty selection doesn't make a difference, it all makes PvP TicTacToe
Thanks in advance (try not to take too long answering EX: writing the code for me), I'm going to bed soon, and I don't want much help. Just some syntax of how to run classes in other classes (which I've attempted using
className arbitrary = new clasName();)
I'm having problems with letter and turn to be more clear, sorry
1 Answer
- deonejuanLv 78 years agoFavorite Answer
You are locking up your options with 'private' and 'static'. Those modifiers serve a purpose unrelated to your current efforts. I usually do something like this to generate an Object. When I have the Object in RAM, then I can utilize all the public properties of the Object.
public class Game {
public static void main( String[] args ) {
// generate an Object, one of two ways
new Game().init();
// or
Game game = new Game();
game.init();
...
Then, I would have other classes, such as GameTile, GameEngine. The use of external classes then makes 'private' 'protected' 'default' 'public' access modifiers invaluable. If I make 10 pizza Objects from the class Pizza, then having a private property price ensures that each pizza in RAM will have THE price that reflects that custom pizza.
Link below is for an applet of TicTacToe. Briefly, if the 9 squares of the board could be 000000000 or 111111111 that can be an int number that instantly determines a winner. The first part of the code, the .isWon() is checking the winning boards for each cycle of play. If you don't understand bit-shifting on an int, then try and design an int[] = new int[ 9]; or boolean[] = new boolean[9]