Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Snowleopard_wu
Java
提交
d63f470e
J
Java
项目概览
Snowleopard_wu
/
Java
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
Java
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
d63f470e
编写于
11月 22, 2020
作者:
G
github-actions
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Formatted with Google Java Formatter
上级
52ab5fbc
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
211 addition
and
206 deletion
+211
-206
Others/RestrictedTowerOfHanoi/Main/Hanoi.java
Others/RestrictedTowerOfHanoi/Main/Hanoi.java
+211
-206
未找到文件。
Others/RestrictedTowerOfHanoi/Main/Hanoi.java
浏览文件 @
d63f470e
import
java.awt.*
;
import
java.awt.event.*
;
import
java.util.*
;
import
javax.swing.*
;
import
javax.swing.Timer
;
import
java.awt.*
;
import
java.awt.event.*
;
public
class
Hanoi
extends
JFrame
{
public
static
int
ONE_SECOND
=
1000
;
int
number_of_disks
=
0
;
int
game_counter
=
0
;
int
i
=
0
;
/* GUI COMPONENTS */
public
JButton
move_button
=
new
JButton
();
public
JButton
exit_button
=
new
JButton
();
public
JButton
replay_button
=
new
JButton
();
public
JButton
auto_button
=
new
JButton
();
/* BACKEND COMPONENTS */
public
ArrayList
<
String
>
movements
=
new
ArrayList
<
String
>();
public
StringBuilder
stringBuilder
=
new
StringBuilder
();
public
ArrayList
<
Integer
>
Stack1
=
new
ArrayList
<
Integer
>();
public
ArrayList
<
Integer
>
Stack2
=
new
ArrayList
<
Integer
>();
public
ArrayList
<
Integer
>
Stack3
=
new
ArrayList
<
Integer
>();
public
void
updateStacks
()
{
if
(
game_counter
!=
movements
.
size
())
{
String
temp
=
movements
.
get
(
game_counter
);
System
.
out
.
println
(
temp
);
if
(
temp
.
charAt
(
1
)==
'A'
)
{
if
(
temp
.
charAt
(
2
)==
'B'
)
{
int
x
=
Stack1
.
get
(
Stack1
.
size
()-
1
);
Stack1
.
remove
(
Stack1
.
size
()-
1
);
Stack2
.
add
(
x
);
}
}
if
(
temp
.
charAt
(
1
)==
'C'
)
{
if
(
temp
.
charAt
(
2
)==
'B'
)
{
int
x
=
Stack3
.
get
(
Stack3
.
size
()-
1
);
Stack3
.
remove
(
Stack3
.
size
()-
1
);
Stack2
.
add
(
x
);
}
}
if
(
temp
.
charAt
(
1
)==
'B'
)
{
if
(
temp
.
charAt
(
2
)==
'C'
)
{
int
x
=
Stack2
.
get
(
Stack2
.
size
()-
1
);
Stack2
.
remove
(
Stack2
.
size
()-
1
);
Stack3
.
add
(
x
);
}
else
if
(
temp
.
charAt
(
2
)==
'A'
)
{
int
x
=
Stack2
.
get
(
Stack2
.
size
()-
1
);
Stack2
.
remove
(
Stack2
.
size
()-
1
);
Stack1
.
add
(
x
);
}
}
revalidate
();
repaint
();
game_counter
++;
public
class
Hanoi
extends
JFrame
{
public
static
int
ONE_SECOND
=
1000
;
int
number_of_disks
=
0
;
int
game_counter
=
0
;
int
i
=
0
;
/* GUI COMPONENTS */
public
JButton
move_button
=
new
JButton
();
public
JButton
exit_button
=
new
JButton
();
public
JButton
replay_button
=
new
JButton
();
public
JButton
auto_button
=
new
JButton
();
/* BACKEND COMPONENTS */
public
ArrayList
<
String
>
movements
=
new
ArrayList
<
String
>();
public
StringBuilder
stringBuilder
=
new
StringBuilder
();
public
ArrayList
<
Integer
>
Stack1
=
new
ArrayList
<
Integer
>();
public
ArrayList
<
Integer
>
Stack2
=
new
ArrayList
<
Integer
>();
public
ArrayList
<
Integer
>
Stack3
=
new
ArrayList
<
Integer
>();
public
void
updateStacks
()
{
if
(
game_counter
!=
movements
.
size
())
{
String
temp
=
movements
.
get
(
game_counter
);
System
.
out
.
println
(
temp
);
if
(
temp
.
charAt
(
1
)
==
'A'
)
{
if
(
temp
.
charAt
(
2
)
==
'B'
)
{
int
x
=
Stack1
.
get
(
Stack1
.
size
()
-
1
);
Stack1
.
remove
(
Stack1
.
size
()
-
1
);
Stack2
.
add
(
x
);
}
}
public
void
paint
(
Graphics
canvas
)
{
super
.
paint
(
canvas
);
//Drawing pedestels
for
(
int
i
=
0
;
i
<
3
;
i
++)
{
canvas
.
drawRect
(
30
+
i
*
230
,
670
,
200
,
20
);
canvas
.
setColor
(
new
Color
(
76
,
174
,
227
));
//Blue Accent
canvas
.
fillRect
(
30
+
i
*
230
,
670
,
200
,
20
);
canvas
.
fillRect
(
130
+
i
*
230
-
2
,
670
-
170
,
4
,
170
);
canvas
.
setColor
(
new
Color
(
150
,
0
,
0
));
//Arseny
canvas
.
fillRect
(
130
+
i
*
230
-
2
,
670
-
170
,
4
,
170
);
}
if
(
temp
.
charAt
(
1
)
==
'C'
)
{
if
(
temp
.
charAt
(
2
)
==
'B'
)
{
int
x
=
Stack3
.
get
(
Stack3
.
size
()
-
1
);
Stack3
.
remove
(
Stack3
.
size
()
-
1
);
Stack2
.
add
(
x
);
}
//Disks in stack1
for
(
int
i
=
1
;
i
<=
Stack1
.
size
();
i
++)
{
canvas
.
drawRect
(
130
-
Stack1
.
get
(
i
-
1
)*
10
,
670
-
i
*
12
,
Stack1
.
get
(
i
-
1
)*
20
,
10
);
canvas
.
setColor
(
new
Color
(
64
,
26
,
0
));
//Brown Wolfers
canvas
.
fillRect
(
130
-
Stack1
.
get
(
i
-
1
)*
10
,
670
-
i
*
12
,
Stack1
.
get
(
i
-
1
)*
20
,
10
);
}
//Disks in stack2
for
(
int
i
=
1
;
i
<=
Stack2
.
size
();
i
++)
{
canvas
.
drawRect
(
360
-
Stack2
.
get
(
i
-
1
)*
10
,
670
-
i
*
12
,
Stack2
.
get
(
i
-
1
)*
20
,
10
);
canvas
.
setColor
(
new
Color
(
64
,
26
,
0
));
//Brown Wolfers
canvas
.
fillRect
(
360
-
Stack2
.
get
(
i
-
1
)*
10
,
670
-
i
*
12
,
Stack2
.
get
(
i
-
1
)*
20
,
10
);
}
//Disks in stack3
for
(
int
i
=
1
;
i
<=
Stack3
.
size
();
i
++)
{
canvas
.
drawRect
(
590
-
Stack3
.
get
(
i
-
1
)*
10
,
670
-
i
*
12
,
Stack3
.
get
(
i
-
1
)*
20
,
10
);
canvas
.
setColor
(
new
Color
(
64
,
26
,
0
));
//Brown Wolfers
canvas
.
fillRect
(
590
-
Stack3
.
get
(
i
-
1
)*
10
,
670
-
i
*
12
,
Stack3
.
get
(
i
-
1
)*
20
,
10
);
}
if
(
temp
.
charAt
(
1
)
==
'B'
)
{
if
(
temp
.
charAt
(
2
)
==
'C'
)
{
int
x
=
Stack2
.
get
(
Stack2
.
size
()
-
1
);
Stack2
.
remove
(
Stack2
.
size
()
-
1
);
Stack3
.
add
(
x
);
}
else
if
(
temp
.
charAt
(
2
)
==
'A'
)
{
int
x
=
Stack2
.
get
(
Stack2
.
size
()
-
1
);
Stack2
.
remove
(
Stack2
.
size
()
-
1
);
Stack1
.
add
(
x
);
}
}
revalidate
();
repaint
();
game_counter
++;
}
}
// Function to initialize the widget properties and the frame.
public
void
initialize
()
{
public
void
paint
(
Graphics
canvas
)
{
super
.
paint
(
canvas
);
move_button
.
setIcon
(
new
ImageIcon
(
"../Resources/rsz_move.png"
));
move_button
.
setBounds
(
130
,
0
,
50
,
50
);
// Drawing pedestels
for
(
int
i
=
0
;
i
<
3
;
i
++)
{
canvas
.
drawRect
(
30
+
i
*
230
,
670
,
200
,
20
);
canvas
.
setColor
(
new
Color
(
76
,
174
,
227
));
// Blue Accent
canvas
.
fillRect
(
30
+
i
*
230
,
670
,
200
,
20
);
auto_button
.
setIcon
(
new
ImageIcon
(
"../Resources/rsz_loop.png"
));
auto_button
.
setBounds
(
260
,
0
,
50
,
50
);
replay_button
.
setIcon
(
new
ImageIcon
(
"../Resources/rsz_replay.jpg"
));
replay_button
.
setBounds
(
390
,
0
,
50
,
50
);
canvas
.
fillRect
(
130
+
i
*
230
-
2
,
670
-
170
,
4
,
170
);
canvas
.
setColor
(
new
Color
(
150
,
0
,
0
));
// Arseny
canvas
.
fillRect
(
130
+
i
*
230
-
2
,
670
-
170
,
4
,
170
);
}
exit_button
.
setIcon
(
new
ImageIcon
(
"../Resources/rsz_exit.png"
));
exit_button
.
setBounds
(
520
,
0
,
50
,
50
);
// Disks in stack1
for
(
int
i
=
1
;
i
<=
Stack1
.
size
();
i
++)
{
canvas
.
drawRect
(
130
-
Stack1
.
get
(
i
-
1
)
*
10
,
670
-
i
*
12
,
Stack1
.
get
(
i
-
1
)
*
20
,
10
);
canvas
.
setColor
(
new
Color
(
64
,
26
,
0
));
// Brown Wolfers
canvas
.
fillRect
(
130
-
Stack1
.
get
(
i
-
1
)
*
10
,
670
-
i
*
12
,
Stack1
.
get
(
i
-
1
)
*
20
,
10
);
}
add
(
move_button
);
add
(
exit_button
);
add
(
replay_button
);
add
(
auto_button
);
// Disks in stack2
for
(
int
i
=
1
;
i
<=
Stack2
.
size
();
i
++)
{
canvas
.
drawRect
(
360
-
Stack2
.
get
(
i
-
1
)
*
10
,
670
-
i
*
12
,
Stack2
.
get
(
i
-
1
)
*
20
,
10
);
canvas
.
setColor
(
new
Color
(
64
,
26
,
0
));
// Brown Wolfers
canvas
.
fillRect
(
360
-
Stack2
.
get
(
i
-
1
)
*
10
,
670
-
i
*
12
,
Stack2
.
get
(
i
-
1
)
*
20
,
10
);
}
setLayout
(
null
);
setSize
(
720
,
720
);
setVisible
(
true
);
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
// Disks in stack3
for
(
int
i
=
1
;
i
<=
Stack3
.
size
();
i
++)
{
canvas
.
drawRect
(
590
-
Stack3
.
get
(
i
-
1
)
*
10
,
670
-
i
*
12
,
Stack3
.
get
(
i
-
1
)
*
20
,
10
);
canvas
.
setColor
(
new
Color
(
64
,
26
,
0
));
// Brown Wolfers
canvas
.
fillRect
(
590
-
Stack3
.
get
(
i
-
1
)
*
10
,
670
-
i
*
12
,
Stack3
.
get
(
i
-
1
)
*
20
,
10
);
}
// Main cnstructor.
Hanoi
()
{
super
(
"restricted tower of hanoi"
);
initialize
();
//MOVE BUTTON ACTION LISTENER
move_button
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
updateStacks
();
}
}
// Function to initialize the widget properties and the frame.
public
void
initialize
()
{
move_button
.
setIcon
(
new
ImageIcon
(
"../Resources/rsz_move.png"
));
move_button
.
setBounds
(
130
,
0
,
50
,
50
);
auto_button
.
setIcon
(
new
ImageIcon
(
"../Resources/rsz_loop.png"
));
auto_button
.
setBounds
(
260
,
0
,
50
,
50
);
replay_button
.
setIcon
(
new
ImageIcon
(
"../Resources/rsz_replay.jpg"
));
replay_button
.
setBounds
(
390
,
0
,
50
,
50
);
exit_button
.
setIcon
(
new
ImageIcon
(
"../Resources/rsz_exit.png"
));
exit_button
.
setBounds
(
520
,
0
,
50
,
50
);
add
(
move_button
);
add
(
exit_button
);
add
(
replay_button
);
add
(
auto_button
);
setLayout
(
null
);
setSize
(
720
,
720
);
setVisible
(
true
);
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
}
// Main cnstructor.
Hanoi
()
{
super
(
"restricted tower of hanoi"
);
initialize
();
// MOVE BUTTON ACTION LISTENER
move_button
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
updateStacks
();
}
});
//EXIT BUTTON ACTION LISTENER
exit_button
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
System
.
exit
(
0
);
}
// EXIT BUTTON ACTION LISTENER
exit_button
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
System
.
exit
(
0
);
}
});
//REPLAY BUTTON ACTION LISTENER
replay_button
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
startGame
();
repaint
();
}
// REPLAY BUTTON ACTION LISTENER
replay_button
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
startGame
();
repaint
();
}
});
//
AUTOMATIC PLAY BUTTON ACTION LISTENER
auto_button
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
timer
.
start
();
if
(
game_counter
==
movements
.
size
())
{
timer
.
stop
();
}
//
AUTOMATIC PLAY BUTTON ACTION LISTENER
auto_button
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
timer
.
start
();
if
(
game_counter
==
movements
.
size
())
{
timer
.
stop
();
}
}
});
}
Timer
timer
=
new
Timer
(
ONE_SECOND
,
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
e
)
{
updateStacks
();
}
});
}
public
void
startGame
()
{
Timer
timer
=
new
Timer
(
ONE_SECOND
,
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
e
)
{
updateStacks
();
}
});
System
.
out
.
println
(
"New Game Started"
);
timer
.
stop
();
Stack1
=
new
ArrayList
<
Integer
>();
Stack2
=
new
ArrayList
<
Integer
>();
Stack3
=
new
ArrayList
<
Integer
>();
public
void
startGame
()
{
movements
=
new
ArrayList
<
String
>(
);
game_counter
=
0
;
System
.
out
.
println
(
"New Game Started"
);
timer
.
stop
()
;
for
(
int
i
=
0
;
i
<
number_of_disks
;
i
++)
{
Stack1
.
add
(
number_of_disks
-
i
);
}
Stack1
=
new
ArrayList
<
Integer
>();
Stack2
=
new
ArrayList
<
Integer
>(
);
Stack3
=
new
ArrayList
<
Integer
>();
towerOfHanoi
(
number_of_disks
,
'A'
,
'C'
,
'B'
);
}
movements
=
new
ArrayList
<
String
>(
);
game_counter
=
0
;
public
static
void
main
(
String
args
[])
{
Hanoi
tower
=
new
Hanoi
();
int
number
=
Integer
.
parseInt
(
args
[
0
]);
tower
.
number_of_disks
=
number
;
tower
.
startGame
();
/*for(int i=0;i<tower.movements.size();i++) {
System.out.println(tower.movements.get(i));
//System.out.println(tower.Stack1.get(i));
}*/
for
(
int
i
=
0
;
i
<
number_of_disks
;
i
++)
{
Stack1
.
add
(
number_of_disks
-
i
);
}
//Recursive function to formulate restricted tower of hanoi.
public
void
towerOfHanoi
(
int
n
,
char
from_rod
,
char
to_rod
,
char
aux_rod
)
{
if
(
n
==
1
)
{
stringBuilder
.
setLength
(
0
);
stringBuilder
.
append
(
Integer
.
toString
(
n
));
stringBuilder
.
append
(
from_rod
);
stringBuilder
.
append
(
aux_rod
);
movements
.
add
(
stringBuilder
.
toString
());
//System.out.println("Move disk 1 from rod " + from_rod + " to rod " + aux_rod);
stringBuilder
.
setLength
(
0
);
stringBuilder
.
append
(
Integer
.
toString
(
n
));
stringBuilder
.
append
(
aux_rod
);
stringBuilder
.
append
(
to_rod
);
movements
.
add
(
stringBuilder
.
toString
());
//System.out.println("Move disk 1 from rod " + aux_rod + " to rod " + to_rod);
return
;
}
towerOfHanoi
(
n
-
1
,
from_rod
,
to_rod
,
aux_rod
);
stringBuilder
.
setLength
(
0
);
stringBuilder
.
append
(
Integer
.
toString
(
n
));
stringBuilder
.
append
(
from_rod
);
stringBuilder
.
append
(
aux_rod
);
movements
.
add
(
stringBuilder
.
toString
());
//System.out.println("Move disk " + n + " from rod " + from_rod + " to rod " + aux_rod);
towerOfHanoi
(
n
-
1
,
to_rod
,
from_rod
,
aux_rod
);
stringBuilder
.
setLength
(
0
);
stringBuilder
.
append
(
Integer
.
toString
(
n
));
stringBuilder
.
append
(
aux_rod
);
stringBuilder
.
append
(
to_rod
);
movements
.
add
(
stringBuilder
.
toString
());
//System.out.println("Move disk " + n + " from rod " + aux_rod + " to rod " + to_rod);
towerOfHanoi
(
n
-
1
,
from_rod
,
to_rod
,
aux_rod
);
towerOfHanoi
(
number_of_disks
,
'A'
,
'C'
,
'B'
);
}
public
static
void
main
(
String
args
[])
{
Hanoi
tower
=
new
Hanoi
();
int
number
=
Integer
.
parseInt
(
args
[
0
]);
tower
.
number_of_disks
=
number
;
tower
.
startGame
();
/*for(int i=0;i<tower.movements.size();i++) {
System.out.println(tower.movements.get(i));
//System.out.println(tower.Stack1.get(i));
}*/
}
// Recursive function to formulate restricted tower of hanoi.
public
void
towerOfHanoi
(
int
n
,
char
from_rod
,
char
to_rod
,
char
aux_rod
)
{
if
(
n
==
1
)
{
stringBuilder
.
setLength
(
0
);
stringBuilder
.
append
(
Integer
.
toString
(
n
));
stringBuilder
.
append
(
from_rod
);
stringBuilder
.
append
(
aux_rod
);
movements
.
add
(
stringBuilder
.
toString
());
// System.out.println("Move disk 1 from rod " + from_rod + " to rod " + aux_rod);
stringBuilder
.
setLength
(
0
);
stringBuilder
.
append
(
Integer
.
toString
(
n
));
stringBuilder
.
append
(
aux_rod
);
stringBuilder
.
append
(
to_rod
);
movements
.
add
(
stringBuilder
.
toString
());
// System.out.println("Move disk 1 from rod " + aux_rod + " to rod " + to_rod);
return
;
}
towerOfHanoi
(
n
-
1
,
from_rod
,
to_rod
,
aux_rod
);
stringBuilder
.
setLength
(
0
);
stringBuilder
.
append
(
Integer
.
toString
(
n
));
stringBuilder
.
append
(
from_rod
);
stringBuilder
.
append
(
aux_rod
);
movements
.
add
(
stringBuilder
.
toString
());
// System.out.println("Move disk " + n + " from rod " + from_rod + " to rod " + aux_rod);
towerOfHanoi
(
n
-
1
,
to_rod
,
from_rod
,
aux_rod
);
stringBuilder
.
setLength
(
0
);
stringBuilder
.
append
(
Integer
.
toString
(
n
));
stringBuilder
.
append
(
aux_rod
);
stringBuilder
.
append
(
to_rod
);
movements
.
add
(
stringBuilder
.
toString
());
// System.out.println("Move disk " + n + " from rod " + aux_rod + " to rod " + to_rod);
towerOfHanoi
(
n
-
1
,
from_rod
,
to_rod
,
aux_rod
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录