import javax.swing.*;
public class TowersOfHanoi {
public static String showMoves(int n, char startPeg, char destPeg, char tempPeg) {
if(n==1) {
return "Move disk 1 from peg " + startPeg + " to peg " + destPeg + "\n";
}
else {
return showMoves(n-1,startPeg, tempPeg, destPeg)
+ "Mover disk " + n + " from peg " + startPeg
+ " to peg " + destPeg + "\n"
+ showMoves(n-1, tempPeg, destPeg, startPeg); // recursion
}
}
public static void main(String[] args) {
String nDisks = JOptionPane.showInputDialog("Enter number of disks"); // showInputDialog JOption으로 디스크 개수 값을 받는다.
String startPeg = JOptionPane.showInputDialog("Enter start peg (L, M, R)"); // 시작
String destPeg = JOptionPane.showInputDialog("Enter destination peg " // 도착
+ "(L,M,R), " + "but not " + startPeg); //
String tempPeg = JOptionPane.showInputDialog("Enter temporary peg " // 임시저장
+ "(L, M, R), "
+ "but not " + startPeg
+ " or " + destPeg);
String moves = showMoves(Integer.parseInt(nDisks),
startPeg.toUpperCase().charAt(0), // 한문자만 뽑기때문에 charAt(0) 사용
destPeg.toUpperCase().charAt(0),
tempPeg.toUpperCase().charAt(0));
JOptionPane.showMessageDialog(null, moves); // .showMessageDialog(null, 안에 내용, 제목)
}
}
필받았나
응 나 프로그래밍 정말 좋아하는데 다른 수업은 자거나 안하는데 프로그래밍 수업은 열심히 들음 ㅋㅋ 집에서도 과제는 안하고 요즘은 자바가 재밋어서 api에 있는거 하나씩 직접 만들어보는중 ㅋㅋㅋ
미친놈