简体   繁体   中英

adding another class to my java applet program

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

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.

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?

  1. Get the state (occupied, unoccupied) for a given board location (x,y)
  2. Put a chess piece (piece enumerator) on a given location (x,y)
  3. Validate placement of a piece (piece enumerator, location x,y)
  4. Suggest a move, returning a Suggestion object with a piece enumerator and location
  5. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM