篱笆外小雨


  • 首页

  • 分类

  • 关于

  • 归档

  • 标签

简单选择排序与堆排序——Java语言描述

发表于 2017-09-07 | 分类于 排序算法 | 阅读次数

选择排序的主要思想是每一趟从待排序列中选取一个关键字值最小的记录,也即第一趟从n个记录中选取关键字值最小的记录,在第二趟中,从剩下的n-1个记录中选取关键字值最小的记录,直到整个序列的记录都选完为止。

程序代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.util.Arrays;

public class SelectionSort {

/**
* 直接选择排序:又称简单选择排序。在第一趟中,从n个记录中找出关键字值最小的记录与第一个记录交换;在第二趟中,
* 从第二个记录开始的n-1个记录中在选出关键字值最小的记录与第二个记录交换;以此
* 类推,在第i趟中,从第i个记录开始的n-i+1个记录中选出关键字值最小的记录与第i个
* 记录交换,直到整个序列按关键字值有序
* 空间复杂度为O(1)
* 时间复杂度为O(n^2)
* 算法稳定性:简单选择排序是一种不稳定的排序算法
* */
/**
* 主要步骤:
* 1.设置i的初始值为0;
* 2.当i<n-1时,重复下列步骤;
* 2.1.在无序子序列{s[i+1],...s[n-1]}中选出一个关键字值最小的记录s[min];
* 2.2.若s[min]不是s[i],(min != i),则交换s[i]和s[min]的位置,否则不进行任何交换;
* 2.3.将i的值加1.
* */

public static void StraightSelectionSort(int straight[]) {
int temp;
int i,j;
for(i=0;i<straight.length-1;i++){
int min = i; //设第i条记录的关键字值最小
for(j=i+1;j<straight.length;j++){ //在子序列中选择关键字值最小的记录
if(straight[j]<straight[min]){
min=j; //记住关键字值最小的下标
}
}
if(min != i){ //将本趟关键字值最小的记录与第i条记录交换
temp=straight[i];
straight[i]=straight[min];
straight[min]=temp;
}
// else{
// continue;
// }
System.out.println("第"+(i+1)+"趟: "+Arrays.toString(straight));
}
}

/**
* 堆排序:对一组待排序记录的关键字,首先按堆的定义排成一个序列(即建立初始堆,大根堆或小根堆),
* 从而可以输出堆顶的最大关键字(对于大根堆而言)然后将剩余的关键字在调整成新堆,便得到次大的关键字,
* 如此反复,直到全部关键字排成有序序列为止。
* 空间复杂度为O(1)
* 时间复杂度为O(nlogn)
* 算法稳定性:堆排序算法是一种不稳定的排序算法
* */
/**
* 调整堆主要步骤:待调整成堆的完全二叉树存放在h[low...high]
* 1.设置初始值temp=h[low]
* 2.当i<=high时重复下列操作
* 2.1.若i<high且h[i]>h[i+1],则i++;
* 2.2.若temp>h[i]则h[low]=h[i],low=i;
* 3.h[low]=temp.
*
* */

/** 筛选法将一个数组中的元素调整成大根堆 */
public static void HeapAdjust(int heap[],int low,int high) {
int temp=heap[low];
int i;
for(i=2*low+1;i<=high;i=i*2+1){ //沿值较大的孩子结点向下筛选
if(i<high && heap[i]<heap[i+1]){ //i是值较大的元素的下标
i++;
}
if(!(temp<heap[i])){
break;
}
heap[low]=heap[i];
low=i; //用low记录待插入元素的下标
}
heap[low]=temp;
System.out.println(Arrays.toString(heap));
}
/** 筛选法将一个数组中的元素调整成小根堆 */
public static void HeapAdjust2(int heap[],int low,int high) {
int temp=heap[low];
int i;
for(i=2*low+1;i<=high;i=i*2+1){ //沿值较小的孩子结点向下筛选
if(i<high && heap[i]>heap[i+1]){ //i是值较小的元素的下标
i++;
}
if(temp>heap[i]){
heap[low]=heap[i];
low=i; //用low记录待插入元素的下标
}
else{
i=high+2;
}
}
heap[low]=temp;
System.out.println(Arrays.toString(heap));
}

/**
* 堆排序主要步骤:
* 1.将待排序记录{h[0],h[1],...,h[n-1]}建成一个完全二叉树;
* 2.将下标为[n/2]-1的记录作为开始调整的子树的根节点
* 3.找出此结点的两个孩子结点中的关键字值较小者,将其与父结点比较,若父结点的关键字值大则交换
* 然后以交换后的子结点作为新的父结点,重复此步骤,直到没有子结点为止。
* 4.以步骤3中原来的父结点所在的位置向前推进一个位置,作为新的调整的子树的根结点。继续重复步骤3直到调整到树根
* 5.堆建成后,将树根与二叉树的最后一个结点交换后,再将最后一个结点输出(即输出的是原本的树根);
* 然后比较根结点的两个子结点,若做子节点的关键字较小,则调整左子树;反之,调整右子树。使它成堆;
* 6.重复步骤5直到二叉树仅剩一个结点为止。
* */

/** 堆排序算法 **/
public static void HeapSort(int heap[]) {
int len=heap.length;
int temp,i;
for(i=len/2-1; i>=0; i--){ //创建堆
HeapAdjust(heap, i, len-1);
}
for(i=len-1; i>0; i--){ //将堆顶元素heap[0]与序列的最后元素heap[i]交换
temp = heap[0];
heap[0] = heap[i];
heap[i] = temp;
HeapAdjust(heap, 0, i-1); //待排元素减1,剩余元素重新调整为大根堆
}

}
public static void main(String[] args) {
// TODO Auto-generated method stub
int straight[]={35,57,48,5,29,87,17,35,66,92};
int heap[]={33,25,46,13,58,95,18,63,78,80};
StraightSelectionSort(straight);
System.out.println("--------------------分割线--------------------");
HeapSort(heap);
}

}

测试结果(大根堆)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
第1趟: [5, 57, 48, 35, 29, 87, 17, 35, 66, 92]
第2趟: [5, 17, 48, 35, 29, 87, 57, 35, 66, 92]
第3趟: [5, 17, 29, 35, 48, 87, 57, 35, 66, 92]
第4趟: [5, 17, 29, 35, 48, 87, 57, 35, 66, 92]
第5趟: [5, 17, 29, 35, 35, 87, 57, 48, 66, 92]
第6趟: [5, 17, 29, 35, 35, 48, 57, 87, 66, 92]
第7趟: [5, 17, 29, 35, 35, 48, 57, 87, 66, 92]
第8趟: [5, 17, 29, 35, 35, 48, 57, 66, 87, 92]
第9趟: [5, 17, 29, 35, 35, 48, 57, 66, 87, 92]
--------------------分割线--------------------
[33, 25, 46, 13, 80, 95, 18, 63, 78, 58]
[33, 25, 46, 78, 80, 95, 18, 63, 13, 58]
[33, 25, 95, 78, 80, 46, 18, 63, 13, 58]
[33, 80, 95, 78, 58, 46, 18, 63, 13, 25]
[95, 80, 46, 78, 58, 33, 18, 63, 13, 25]
[80, 78, 46, 63, 58, 33, 18, 25, 13, 95]
[78, 63, 46, 25, 58, 33, 18, 13, 80, 95]
[63, 58, 46, 25, 13, 33, 18, 78, 80, 95]
[58, 25, 46, 18, 13, 33, 63, 78, 80, 95]
[46, 25, 33, 18, 13, 58, 63, 78, 80, 95]
[33, 25, 13, 18, 46, 58, 63, 78, 80, 95]
[25, 18, 13, 33, 46, 58, 63, 78, 80, 95]
[18, 13, 25, 33, 46, 58, 63, 78, 80, 95]
[13, 18, 25, 33, 46, 58, 63, 78, 80, 95]

冒泡排序与快速排序——Java语言描述

发表于 2017-09-06 | 分类于 排序算法 | 阅读次数

冒泡排序与快速排序都应用了交换排序基本思想。所谓交换排序就是两两比较待排序记录的关键字,若两个记录的次序相反则交换这两个记录,直到没有反序的记录为止。

程序代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.util.Arrays;

public class ExchangeSort {

/**
* 冒泡排序:将待排序的数组看作从上到下排列,把关键字值较小的记录看作“较轻的”,关键字值较大的看作“较重的”,
* 较小的关键字值看记录好像水中的气泡一样,向上“浮”;较大关键字值的记录就如水中的石头向下沉,当所有
* 的气泡都浮到了相应的位置,并且所有的石块都沉到了水底,排序就结束了。
* 空间复杂度为O(1)
* 时间复杂度为O(n^2)
* 算法稳定性:冒泡排序是一种稳定的排序算法。
* */
/**
* 主要步骤归纳:
* 1.设置初值i=1;
* 2.在无序序列{a[0],a[1],...a[n-1]}中,从头至尾依次比较相邻的两个记录
* a[j]与a[j=1](0<=j<=n-i-1),若a[j].key>a[j+1].key,则交换位置。
* 3.i=i+1;
* 4.重复步骤2,3,直到在步骤2中未发生记录交换成i=n-1为止。
*
* */
public static void BubbleSort(int bubble[]) {
int temp;
boolean flag=true; //记录是否发生交换
int i,j;
for(i=1;i<bubble.length && flag;i++){ //有交换时再进行下一趟,最多n-1趟
flag=false;
for(j=0;j<bubble.length-i;j++){
if(bubble[j]>bubble[j+1]){ //逆序时,相互交换位置
temp=bubble[j];
bubble[j]=bubble[j+1];
bubble[j+1]=temp;
flag=true;
}
}
System.out.println("第"+i+"趟:"+Arrays.toString(bubble));
}
}

/**
* 快速排序:通过一趟排序将要排序的记录分隔成独立的两个部分,其中一部分的所有记录的关键字值都比
* 另一部分的所有记录关键字值小,然后再按此方法对这两部分记录分别进行快速排序,整个记录
* 过程可以递归进行,以此达到整个记录序列变成有序
* 空间复杂度为O(logn)
* 时间复杂度:最坏为O(n^2),平均为O(nlogn)
* 算法稳定性:快速排序是一种不稳定的排序算法
* */
/**
* 主要步骤:
* 1.设置两个变量i,j,初始值分别为low和high,分别表示待排序序列的起始下标和终止下标;
* 2.将第i个记录暂存在变量temp中,及temp=Q[i];
* 3.从小标为j的位置开始由后向前以此搜索,当找到第一个比temp的关键字小的记录时,则将该
* 记录向前移动到下标为i的位置上,然后i=i+1;
* 4.从下标为i的位置开始由前向后依次搜索,当找到第一个比temp的关键字值大的记录时,则将
* 该记录向后移动到下标为j的位置上,然后j=j-1;
* 5.重复2,3,4步骤,直到i==j;
* 6.Q[i]=temp.
*
* */

//一趟快速排序算法
public static int Quick(int quick[],int i,int j) {
int temp=quick[i];
while(i<j){ //从表的两端交替向中间扫描
while(i<j && temp<=quick[j]){
j--;
}
if(i<j){
quick[i]=quick[j]; //将比quick[i]的关键字值小的记录向前移动
i++;
}
while(i<j && temp>quick[i]){
i++;
}
if(i<j){
quick[j]=quick[i]; //将比quick[i]的关键字值大的记录向后移动
j--;
}
}
quick[i]=temp;
System.out.println(Arrays.toString(quick));
return i;
}
//递归形式的快速排序算法
public static void QuickSort(int quick[],int low,int high){
if(low<high){
int i=Quick(quick, low, high) ; //一趟排序将序列分为两部分
QuickSort(quick,low,i-1); //低子表递归排序
QuickSort(quick, i+1, high); //高子表递归排序
}
}


public static void main(String[] args) {
// TODO Auto-generated method stub
int bubble[]={52,24,76,9,50,64,17,39,47,15};
int quick[]={52,39,67,95,70,8,25,52,45,19};
BubbleSort(bubble);
System.out.println("---------------------分割线--------------------");
QuickSort(quick, 0, quick.length-1);
}
}

测试结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
第1趟:[24, 52, 9, 50, 64, 17, 39, 47, 15, 76]
第2趟:[24, 9, 50, 52, 17, 39, 47, 15, 64, 76]
第3趟:[9, 24, 50, 17, 39, 47, 15, 52, 64, 76]
第4趟:[9, 24, 17, 39, 47, 15, 50, 52, 64, 76]
第5趟:[9, 17, 24, 39, 15, 47, 50, 52, 64, 76]
第6趟:[9, 17, 24, 15, 39, 47, 50, 52, 64, 76]
第7趟:[9, 17, 15, 24, 39, 47, 50, 52, 64, 76]
第8趟:[9, 15, 17, 24, 39, 47, 50, 52, 64, 76]
第9趟:[9, 15, 17, 24, 39, 47, 50, 52, 64, 76]
---------------------分割线--------------------
[19, 39, 45, 25, 8, 52, 70, 52, 95, 67]
[8, 19, 45, 25, 39, 52, 70, 52, 95, 67]
[8, 19, 39, 25, 45, 52, 70, 52, 95, 67]
[8, 19, 25, 39, 45, 52, 70, 52, 95, 67]
[8, 19, 25, 39, 45, 52, 67, 52, 70, 95]
[8, 19, 25, 39, 45, 52, 52, 67, 70, 95]

直接插入排序与希尔排序——Java语言描述

发表于 2017-09-06 | 分类于 排序算法 | 阅读次数

之前有被问到有没有想过用Java描述排序算法,很不好意思的回答了没有。因为学数据结构时用的是C。刚好最近在复习,就准备试着用Java写出来。

插入排序

介绍两种插入排序方法:直接插入排序和希尔排序

程序代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.Arrays;

/*
* 插入排序:直接插入排序,希尔排序
*
* */

public class StraightInsertion {
/*
* 直接插入排序:每趟将一条待排序的记录,按其关键字值的大小插入到前面的记录序列中的合适位置,直到全部插入完成为止。
* 时间复杂度为O(n^2)
* 空间复杂度为O(1)
* 算法稳定性:直接插入排序是一种稳定的排序算法
* */
public static void InsertSort(int insert[]){
int temp;
int i,j;
for(i=1;i<insert.length;i++){
temp = insert[i];
for(j=i-1;j>=0 && temp<insert[j];j--){
insert[j+1]=insert[j];
}
insert[j+1]=temp;
System.out.println(Arrays.toString(insert));
}
}

/**
* 希尔排序:又称缩小增量排序。先取一个小于n的整数d(称为增量),然后把排序表中的n个记录分为d个子表,
* 从下标为0的记录开始,间隔为d的记录组成一个子表,在各个子表内进行直接插入排序。在一趟之后,
* 间隔为d的记录组成的子表已有序,随着有序性的改善,逐步减小增量d,重复进行上述操作,直到d=1。
*空间复杂度为O(1)
*算法稳定性:希尔排序是一种不稳定的排序算法
* */

public static void ShellSort(int shell[]) {
int temp;
int i,j,d;
for(d=shell.length/2;d>0;d /=2){
for(i=d;i<shell.length;i++){
temp=shell[i];
for(j=i-d;j>=0 && temp<shell[j];j -=d){
shell[j+d]=shell[j];
}
shell[j+d]=temp;
System.out.println(Arrays.toString(shell));
}
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
int insert[]={34,21,67,23,45,56,8,66,70,36};
int shell[]={67,52,42,98,35,7,26,39,18,84};
InsertSort(insert);
ShellSort(shell);
}

}
测试结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

[21, 34, 67, 23, 45, 56, 8, 66, 70, 36]
[21, 34, 67, 23, 45, 56, 8, 66, 70, 36]
[21, 23, 34, 67, 45, 56, 8, 66, 70, 36]
[21, 23, 34, 45, 67, 56, 8, 66, 70, 36]
[21, 23, 34, 45, 56, 67, 8, 66, 70, 36]
[8, 21, 23, 34, 45, 56, 67, 66, 70, 36]
[8, 21, 23, 34, 45, 56, 66, 67, 70, 36]
[8, 21, 23, 34, 45, 56, 66, 67, 70, 36]
[8, 21, 23, 34, 36, 45, 56, 66, 67, 70]
[7, 52, 42, 98, 35, 67, 26, 39, 18, 84]
[7, 26, 42, 98, 35, 67, 52, 39, 18, 84]
[7, 26, 39, 98, 35, 67, 52, 42, 18, 84]
[7, 26, 39, 18, 35, 67, 52, 42, 98, 84]
[7, 26, 39, 18, 35, 67, 52, 42, 98, 84]
[7, 26, 39, 18, 35, 67, 52, 42, 98, 84]
[7, 18, 39, 26, 35, 67, 52, 42, 98, 84]
[7, 18, 35, 26, 39, 67, 52, 42, 98, 84]
[7, 18, 35, 26, 39, 67, 52, 42, 98, 84]
[7, 18, 35, 26, 39, 67, 52, 42, 98, 84]
[7, 18, 35, 26, 39, 42, 52, 67, 98, 84]
[7, 18, 35, 26, 39, 42, 52, 67, 98, 84]
[7, 18, 35, 26, 39, 42, 52, 67, 98, 84]
[7, 18, 35, 26, 39, 42, 52, 67, 98, 84]
[7, 18, 35, 26, 39, 42, 52, 67, 98, 84]
[7, 18, 26, 35, 39, 42, 52, 67, 98, 84]
[7, 18, 26, 35, 39, 42, 52, 67, 98, 84]
[7, 18, 26, 35, 39, 42, 52, 67, 98, 84]
[7, 18, 26, 35, 39, 42, 52, 67, 98, 84]
[7, 18, 26, 35, 39, 42, 52, 67, 98, 84]
[7, 18, 26, 35, 39, 42, 52, 67, 98, 84]
[7, 18, 26, 35, 39, 42, 52, 67, 84, 98]

CSS样式中字体属性

发表于 2017-07-12 | 分类于 HTML+CSS | 阅读次数

关于CSS样式中字体设置

1
2
3
4
5
font-family 字体样式
font-style 字体风格 (normal - 正常;italic - 斜体;oblique - 倾斜)
font-weight 字体加粗
font-size 字体大小
color 字体颜色

ul li标签

发表于 2017-07-12 | 分类于 HTML+CSS | 阅读次数

定义与用法

    标签定义无序列表

  • 标签定义列表项目
    1. 标签定义有序列表
      1
      2
      3
      4
      5
      6
      <ul>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      </ul>

    1
    2
    3
    4
    5
    6
    <ol>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    </ol>

    CSS样式

    1 去掉ul的样式,即li前面的点或其他

    1
    2
    3
    ul{list-style: none;}
    or
    li{list-style-type: none;}

    2 li元素横向排列

    1
    li{float:left;}

    2 li自动换行

    1
    li{white-space:nowrap;}

    3 li横向显示不换行

    1
    display:inline-block;

    Oracle数据库:数据的导入

    发表于 2017-07-11 | 分类于 DataBase | 阅读次数

    向Oracle数据库中导入 .dmp 数据文件

    在命令提示符下输入

    imp 登录名/密码@数据库名 file=路径 table=(表名)

    标签

    发表于 2017-07-07 | 分类于 CSS | 阅读次数

    关于超链接标签:a 标签的使用

    定义和用法

    标签定义超链接,用于链接到另一张页面

    1
    2

    <a href="https://fencerain.github.io"></a>

    href 指示链接的目标

    CSS样式

    1 去掉下划线

    1
    a{text-decoration:none;}

    text-decoration参数:
    none :  无装饰
    blink :  闪烁
    underline :  下划线
    line-through :  贯穿线
    overline :  上划线

    2 设置背景色

    1
    a{background-color:#000000;}

    3 链接状态

    1
    2
    3
    4
    a:link - 未被访问
    a:visited - 已访问过
    a:hover - 鼠标悬浮
    a:active - 被点击

    Eclipse实用技巧:部分选项属性设置

    发表于 2017-06-24 | 分类于 Eclipse | 阅读次数

    简单介绍Eclipse的部分选项属性设置

    设置UTF-8编码格式

    window–>preference–>General–>Workspace–>Text file encoding–>Other–>UTF-8

    设置Eclipse字体大小和样式

    window–>preference–>General–>APPearance–>Colors and Fonts–>Basic–>Text Font–>Edit

    设置Web下JSP编码格式

    window–>preference–>Web–>JSP Files–>Encoding–>ISO 10646/Unicode(UTF-8)

    配置服务器

    window–>preference–>Server–>Runtime Environments–>Add–>Apache(版本自己选)–>next–>Browse(服务器路径)–>Finish

    设置Eclipse黑色主题

    window–>preference–>General–>APPearance–>Theme–>Dark

    设置显示行数

    window–>preference–>General–>Editors–>Text Editors–>Show line numbers

    设置代码自动补全

    window–>preference–>Java–>Editor–>Content Assist–>Auto activation triggers for Java:–>.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

    Ubuntu 14.04下安装Google Chrome浏览器

    发表于 2017-06-08 | 分类于 Linux | 阅读次数

    到谷歌官网下载Chrome的Deb安装包

    打开终端,进入放置安装包文件夹目录下

    1
    sudo dpkg -i google-chrome-stable_current_amd64.deb

    i后面是安装包的文件名

    出现错误:google-chrome-stable

    1
    sudo apt-get -f install

    Java 贪吃蛇

    发表于 2017-05-25 | 分类于 Java | 阅读次数

    刚考完试,趁着现在还有空,弄了一下贪吃蛇的游戏。本来想弄个HTML版的,奈何不会JS,就弄了个Java的。整个程序由三个类(MainInterface、Snake、SNode)构成。

    数据参数建议自己定义变量名,这样使用起来比较方便,也便于修改。如画板的宽度和高度,蛇与食物的大小。

    完成效果长这样

    MainInterface.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    	import javax.swing.*;
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;


    public class MainInterface extends JFrame implements KeyListener {
    Snake snake;
    static int rx;
    static int ry;
    static int score = 0;
    boolean start = false;
    public MainInterface() {
    super("贪吃蛇");
    snake=new Snake();
    //食物出现的位置
    rx = ((int)(Math.random() * (500 - 20) + 60))/10*10;
    ry = ((int)(Math.random() * (500 - 40) + 60))/10*10;
    this.setSize(600, 600);
    this.setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addKeyListener(this);//添加监听
    new Thread(new snackThread()).start();
    }

    public void paint(Graphics g)
    {
    super.paint(g); //没有会将面板刷掉
    g.setColor(Color.black);
    g.fillRect(50, 50, 500, 500); //刷新界面
    g.setColor(Color.white);
    g.drawRect(50, 50, 500, 500); //绘制边界
    g.setColor(Color.red);
    g.drawString("按方向键开始游戏",200,570);
    g.setColor(Color.red);
    g.drawString("Scores: " + score,440,570); //食物数量
    g.setColor(Color.red);
    g.fillRect(rx, ry, 10, 10); //食物
    g.setColor(Color.green); //蛇头
    g.fillRect(snake.snacknode.get(0).getX(), snake.snacknode.get(0).getY(), 10, 10);
    g.setColor(Color.white); //蛇身
    for(int i = 1; i < snake.snacknode.size(); i++) {
    g.fillRect(snake.snacknode.get(i).getX(), snake.snacknode.get(i).getY(), 10, 10);
    }
    }

    @Override
    public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    if(e.getKeyCode()==KeyEvent.VK_UP || e.getKeyCode()==KeyEvent.VK_DOWN || e.getKeyCode()==KeyEvent.VK_LEFT || e.getKeyCode()==KeyEvent.VK_RIGHT){
    start = true;
    }
    if(start){
    switch(e.getKeyCode()) {
    case KeyEvent.VK_UP:
    snake.changeDirection("Up");
    break;
    case KeyEvent.VK_DOWN:
    snake.changeDirection("Down");
    break;
    case KeyEvent.VK_LEFT:
    snake.changeDirection("Left");
    break;
    case KeyEvent.VK_RIGHT:
    snake.changeDirection("Right");
    break;
    }
    }

    }

    @Override
    public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

    }
    @Override
    public void update(Graphics arg0) {
    // TODO Auto-generated method stub
    super.update(arg0);
    }

    class snackThread implements Runnable
    {
    public void run() {
    while(true) {
    try {
    Thread.sleep(500);
    repaint();
    if(start) {
    snake.Move();
    }
    }
    catch(InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    new MainInterface();
    }
    }

    Snake.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    	import java.util.ArrayList;

    import javax.swing.JOptionPane;

    public class Snake {
    ArrayList<SNode> snacknode = new ArrayList<>(); // 蛇的集合
    private String direction; //蛇头移动方向
    private int length = 0; //蛇的长度
    private int headx; //蛇头X轴坐标
    private int heady; ////蛇头Y轴坐标

    /*
    * 初始化蛇
    * */
    public Snake() {
    direction = "Left";
    snacknode.add(new SNode(300,350));
    snacknode.add(new SNode(310,350));
    snacknode.add(new SNode(320,350));
    length = snacknode.size();
    }

    public String getDirection() {
    return direction;
    }

    public void setDirection(String direction) {
    this.direction = direction;
    }

    public int getLenght() {
    return length;
    }

    public void setLength(int length) {
    this.length = length;
    }

    boolean Dead(){
    for(int i = 1; i < snacknode.size(); i++) {
    //撞到自己
    if(snacknode.get(0).getX() == snacknode.get(i).getX() && snacknode.get(0).getY() == snacknode.get(i).getY())
    return true;
    }
    //撞到墙
    if(snacknode.get(0).getX() < 60 || snacknode.get(0).getX() > 530 || snacknode.get(0).getY() < 60 || snacknode.get(0).getY() > 520) {
    return true;
    }
    return false;
    }
    //蛇移动
    public void Move() {
    headx = snacknode.get(0).getX();
    heady = snacknode.get(0).getY();
    switch (direction) {
    case "Up":
    heady = heady - 10;
    break;
    case "Down":
    heady = heady + 10;
    break;
    case "Left":
    headx = headx - 10;
    break;
    case "Right":
    headx = headx + 10;
    break;
    default:
    break;
    }
    SNode newNode = new SNode();
    boolean eat = false;
    //吃掉食物
    if(Math.abs(headx - MainInterface.rx) < 10 && Math.abs(heady - MainInterface.ry) < 10) {
    eat = true;
    newNode = new SNode(snacknode.get(snacknode.size() - 1).getX(),snacknode.get(snacknode.size() - 1).getY());
    MainInterface.rx = ((int)(Math.random() * (500 - 20) + 60))/10*10;
    MainInterface.ry = ((int)(Math.random() * (500 - 40) + 60))/10*10;
    }
    for(int i = snacknode.size() - 1; i > 0; i--){
    snacknode.set(i, snacknode.get(i - 1));
    }
    snacknode.set(0,new SNode(headx,heady)); //将食物添加到蛇身
    if(Dead()) {
    JOptionPane.showMessageDialog(null, "Snake Dead!", "Message", JOptionPane.ERROR_MESSAGE);
    System.exit(1);
    }
    if(eat) {
    snacknode.add(newNode);
    MainInterface.score++;
    }
    }
    public void changeDirection(String dir) {
    direction = dir;
    }
    }

    SNode.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    	/*
    * 定义蛇身结点
    *
    * */

    public class SNode {
    private int x;
    private int y;
    public SNode() {}
    public SNode(int x ,int y){
    this.x = x;
    this.y = y;
    }

    public int getX() {
    return x;
    }

    public void setX(int x) {
    this.x=x;
    }

    public int getY() {
    return y;
    }

    public void setY(int y) {
    this.y=y;
    }
    }
    12345
    篱笆外小雨

    篱笆外小雨

    刚刚好^_^

    41 日志
    22 分类
    26 标签
    RSS
    © 2018 篱笆外小雨
    由 Hexo 强力驱动
    主题 - NexT.Mist