Java_上机4

任务1 完善“卡牌操作”类的功能

任务1文件下载

效果图

任务1效果图

源代码如下:

MainProcess.class

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
public class MainProcess {
public static void main(String[] args) {
int cardNum = 1; //定义牌的张数
CardOperator co = new CardOperator(cardNum);
System.out.println("卡牌初始化:");
co.showCards();

co.shuffleCards();
System.out.println("卡牌洗牌后:");
co.showCards();

co.dealCards();
System.out.println("发牌后:");
co.showDealCards();

co.rankCardsDDZType();
System.out.println("卡牌按斗地主模式理牌后:");
co.showDealCards();

co.rankCardsTLJType();
System.out.println("卡牌按拖拉机模式理牌后:");
co.showDealCards();

co.rankCards();
System.out.println("所有卡牌理牌后:");
co.showCards();

}
}

CardOperator.class

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
public class CardOperator {
private int num_per_cards = 52; //每副卡牌的数量
private int cards_num; //一共几副牌
private static int [][] cards; //卡牌组数,例如三副牌,就是3*52的二维矩阵
private static int deal_cards_num = 26; //指定发牌的数量
private static int [] deal_cards = new int[deal_cards_num]; //用以存储发出的牌

public CardOperator(int user_input_cards_num){
//接受几副牌的信息
cards_num = user_input_cards_num;
initialCards();
}

public void initialCards(){
//每副牌都按照黑桃红桃、方片、草花,A-K的顺序初始化
//系统认定先是13张黑桃,再13张红桃A-K,再13张方片A-K,最后13张草花A-K
cards = new int[cards_num][num_per_cards];
for (int i = 0; i < cards_num; i++) {
for (int j = 0; j < num_per_cards; j++) {
cards[i][j] = j+i*num_per_cards;
}
}
}
public void showCards(){
//依次显示每副牌的牌面
String card_type; //牌型: 花色
String card_value; //牌值
for (int i = 0; i < cards_num; i++) {
System.out.println("第"+(i+1)+"副牌:");
for (int j = 0; j < num_per_cards; j++) {
int value= (cards[(j+i*num_per_cards)/52][(j+i*num_per_cards)%52]+1) % 13;
int type = (cards[(j+i*num_per_cards)/52][(j+i*num_per_cards)%52]%52)/13;
card_type = getCardType(type);
card_value = getCardValue(value);
System.out.print(card_type+card_value+",");
if((j+1) % 13 == 0){
System.out.println();
}
}
System.out.println();
}
}

private static String getCardType(int type){
String card_type = null;
switch (type){
case 0:
card_type = "♠";
break;
case 1:
card_type = "♥";
break;
case 2:
card_type = "♦";
break;
case 3:
card_type = "♣";
break;
}
return card_type;
}

private static String getCardValue(int value){
String card_value;
switch (value){
case 1:
card_value = "A";
break;
case 2:
card_value = "2";
break;
case 3:
card_value = "3";
break;
case 4:
card_value = "4";
break;
case 5:
card_value = "5";
break;
case 6:
card_value = "6";
break;
case 7:
card_value = "7";
break;
case 8:
card_value = "8";
break;
case 9:
card_value = "9";
break;
case 10:
card_value = "10";
break;
case 11:
card_value = "Jack";
break;
case 12:
card_value = "Queen";
break;
case 0:
card_value = "King";
break;
default:
card_value = "wrong";
}
return card_value;
}

public void shuffleCards(){
int shuffle_num = 100; //打乱100次
int first_index,second_index;//用于交换位置的两个元素的下标
int temp; //用于交换时候的临时变量
for (int i = 0; i < cards_num; i++) {
for (int j = 0; j < shuffle_num; j++) {
first_index = (int) (Math.random()*(num_per_cards-1));
second_index = (int) (Math.random()*(num_per_cards-1));
temp = cards[i][first_index];
cards[i][first_index] = cards[i][second_index];
cards[i][second_index] = temp;
}
}

}

public void rankCards(){
for (int i = 0; i < cards_num; i++) {
quick_sort(cards[i],0, num_per_cards-1);
}
}

private void quick_sort(int [] array, int l, int r){
int head = l;
int tail = r;
while (head < tail) {
while (head < tail) {
if (array[l] <= array[tail]) {
tail--;
} else {
break;
}
}
while (head < tail) {
if (array[l] >= array[head]) {
head++;
} else {
break;
}
}
if (head < tail) {
int temp;
temp = array[head];
array[head] = array[tail];
array[tail] = temp;
} else if (head == tail) {
int temp;
temp = array[head];
array[head] = array[l];
array[l] = temp;
quick_sort(array, l, head - 1);
quick_sort(array, head + 1, r);
break;

} else {
System.out.println("发生错误");
}
}
}

public void showDealCards() {
//依次显示每副牌的牌面
String card_type; //牌型: 花色
String card_value; //牌值
for (int i = 0; i < cards_num; i++) {
System.out.println("第"+(i+1)+"副牌:");
for (int j = 0; j < deal_cards_num; j++) {
int value = (deal_cards[(j+i*num_per_cards)%52]+1) % 13;
int type = (deal_cards[(j+i*num_per_cards)%52]%52/13);
card_type = getCardType(type);
card_value = getCardValue(value);
System.out.print(card_type+card_value+",");
if((j+1) % 13 == 0){
System.out.println();
}
}
System.out.println();
}
}

public void dealCards() {
for (int i = 0; i < deal_cards_num; i++) {
deal_cards[i] = cards[0][i];
}
}

private static int getTrueValueDDZ(int cardNum){ //对于特殊的A 2 和 k 做特殊处理
if(cardNum == 0){
return 2;
}else if(cardNum == 1){
return 1;
}else if(cardNum == 2){
return 0;
}else {
return 15-cardNum;
}
}

private static int compareDDZ(int cardA, int cardB){ //这里的cardA和cardB表示的是卡牌本身的值
int compare;
compare = getTrueValueDDZ((cardA+1) % 13)-getTrueValueDDZ((cardB+1) % 13);
return compare;
}

private static int compareTLJ(int cardA, int cardB){ //这里的cardA和cardB表示的是卡牌本身的值
int compare;
compare = (cardA%52/13) - (cardB%52/13);
return compare;
}

private void quick_sort_DDZ(int [] array, int l, int r){
int head = l;
int tail = r;
while (head < tail) {
while (head < tail) {
if (compareDDZ(array[l],array[tail]) <= 0) {
tail--;
} else {
break;
}
}
while (head < tail) {
if (compareDDZ(array[l],array[head]) >= 0) {
head++;
} else {
break;
}
}
if (head < tail) {
int temp;
temp = array[head];
array[head] = array[tail];
array[tail] = temp;
} else if (head == tail) {
int temp;
temp = array[head];
array[head] = array[l];
array[l] = temp;
quick_sort_DDZ(array, l, head - 1);
quick_sort_DDZ(array, head + 1, r);
break;

} else {
System.out.println("发生错误");
}
}
}

private void quick_sort_TLJ(int[] array, int l, int r) {
int head = l;
int tail = r;
while (head < tail) {
while (head < tail) {
if (compareTLJ(array[l],array[tail]) < 0 || (compareTLJ(array[l],array[tail]) == 0 && compareDDZ(array[l],array[tail]) <= 0)) {
tail--;
} else {
break;
}
}
while (head < tail) {
if (compareTLJ(array[l],array[head]) > 0 || (compareTLJ(array[l],array[head]) == 0 && compareDDZ(array[l],array[head]) >= 0)) {
head++;
} else {
break;
}
}
if (head < tail) {
int temp;
temp = array[head];
array[head] = array[tail];
array[tail] = temp;
} else if (head == tail) {
int temp;
temp = array[head];
array[head] = array[l];
array[l] = temp;
quick_sort_TLJ(array, l, head - 1);
quick_sort_TLJ(array, head + 1, r);
break;

} else {
System.out.println("发生错误");
}
}
}

public void rankCardsDDZType() {
//按照斗地主的方式实现卡牌的排序
quick_sort_DDZ(deal_cards,0,deal_cards.length-1);
}

public void rankCardsTLJType() {
quick_sort_TLJ(deal_cards,0,deal_cards.length-1);
}




}

任务2 完成一个支持各种数组操作的类

任务2文件下载

效果图

任务2效果图

源代码如下

MainProcess.class

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
public class MainProcess {
public static void main(String[] args) {
int size = 10;
int [] array = new int[size];
for (int i = 0; i < array.length; i++) {
array[i] = (int)(Math.random()*20);
}

AdvancedArray aa = new AdvancedArray();

//设置数组
System.out.println("原始数组:");
aa.setArray(array);
aa.printArray();

//排序数组
System.out.println("排序后数组:");
aa.quickSort(0,aa.myList.length-1);
aa.printArray();

//二分法查找数组元素key
int key = (int)(Math.random()*20);
System.out.printf("元素%d的位置是: %d%n",key, aa.binarySearch(key));

//找最大最小值
System.out.printf("最大数: %d%n", aa.getMaxValue());
System.out.printf("最小数: %d%n", aa.getMinValue());

//混排数组
aa.shuffle(100);
System.out.println("混排后的数组:");
aa.printArray();

//移动数组
int offset = (int)(Math.random()*aa.myList.length);
String direction = "left";
aa.move(direction, offset);
System.out.printf("向%s移动%d位后的数组:%n", direction,offset);
aa.printArray();

offset = (int)(Math.random()*aa.myList.length);
direction = "right";
aa.move(direction, offset);
System.out.printf("向%s移动%d位后的数组:%n", direction,offset);
aa.printArray();

//插入数据
key = (int)(Math.random()*20);
int index = (int)(Math.random()*aa.myList.length);
aa.insert(index,key);
System.out.printf("在下标%d位置插入数据%d后的数组:%n", index, key);
aa.printArray();

}
}

BasicArray.class

1
2
3
4
public abstract class BasicArray {
int[] myList;
abstract public void setArray(int[] array);
}

BasicArrayOperation.class

1
2
3
4
5
6
7
public interface BasicArrayOperation{
int getMaxValue();
int getMinValue();
void insert(int index, int key);
void move(String direction, int offset);
void printArray();
}

AdvancedArrayOperation.class

1
2
3
4
5
public interface AdvancedArrayOperation {
int binarySearch(int key);
void quickSort(int left, int right);
void shuffle(int num);
}

AdvancedArray.class

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
import java.util.Objects;

public class AdvancedArray extends BasicArray implements BasicArrayOperation, AdvancedArrayOperation{

@Override
public void setArray(int[] array) {
myList = array;
}

public int getMaxValue() {
int maxValue = 0;
for (int i = 0; i < myList.length; i++) {
if (maxValue < myList[i]){
maxValue = myList[i];
}
}
return maxValue;
}

@Override
public int getMinValue() {
int maxValue = myList[0];
for (int i = 0; i < myList.length; i++) {
if (maxValue > myList[i]){
maxValue = myList[i];
}
}
return maxValue;
}

@Override
public void insert(int index, int key) {
myList[index] = key;
}

@Override
public void move(String direction, int offset) {
int [] array = new int[10];
System.arraycopy(myList, 0, array, 0, myList.length);
if (Objects.equals(direction, "left")){
for (int i = 0; i < myList.length; i++) {

myList[i] = array[(i + offset) % myList.length];
}
}else if(Objects.equals(direction, "right")){
for (int i = 0; i < myList.length; i++) {
myList[i] = array[(i+ myList.length - offset) % myList.length];
}
}
}

@Override
public void printArray() {
for (int i = 0; i < myList.length; i++) {
System.out.print(myList[i]+" ");
}
System.out.println();
}

@Override
public int binarySearch(int key) {
for (int i = 0; i < myList.length; i++) {
if (myList[i] == key){
return i;
}
}
return -1;
}

@Override
public void quickSort(int left, int right) {
int head = left;
int tail = right;
while (head < tail) {
while (head < tail) {
if (myList[left] <= myList[tail]) {
tail--;
} else {
break;
}
}
while (head < tail) {
if (myList[left] >= myList[head]) {
head++;
} else {
break;
}
}
if (head < tail) {
int temp;
temp = myList[head];
myList[head] = myList[tail];
myList[tail] = temp;
} else if (head == tail) {
int temp;
temp = myList[head];
myList[head] = myList[left];
myList[left] = temp;
quickSort(left, head - 1);
quickSort(head + 1, right);
break;

} else {
System.out.println("发生错误");
}
}
}

@Override
public void shuffle(int num) {
for (int i = 0; i < num; i++) {
int first = (int)(Math.random()*10);
int second = (int)(Math.random()*10);
int temp = myList[first];
myList[first] = myList[second];
myList[second] = temp;
}
}
}