Skip to content

Commit

Permalink
add huluwa3
Browse files Browse the repository at this point in the history
  • Loading branch information
wcr7966 committed Jan 21, 2018
1 parent 458210a commit 6852e0b
Show file tree
Hide file tree
Showing 18 changed files with 624 additions and 0 deletions.
Binary file modified 20171010/.DS_Store
Binary file not shown.
18 changes: 18 additions & 0 deletions 20171010/王琛然-151220104/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class BubbleSort implements Sort {

@Override
public void sort(Queue queue) {
Creature creature;
Position[] positions = queue.getPositions();

for (int i = 0; i < positions.length - 1; i++) {
for (int j = 0; j < positions.length - 1 - i; j++) {
if (((Comparable) (positions[j].getHolder())).biggerThan((Comparable) (positions[j + 1].getHolder()))) {
creature = positions[j].getHolder();
positions[j + 1].getHolder().setPosition(positions[j]);
creature.setPosition(positions[j + 1]);
}
}
}
}
}
4 changes: 4 additions & 0 deletions 20171010/王琛然-151220104/Comparable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public interface Comparable {

public boolean biggerThan(Comparable another);
}
13 changes: 13 additions & 0 deletions 20171010/王琛然-151220104/Creature.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public interface Creature {

public void report();

public void reportName();


public void setPosition(Position position);


public Position getPosition();
}

5 changes: 5 additions & 0 deletions 20171010/王琛然-151220104/Formation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import java.util.ArrayList;

public interface Formation {
public void excuete(ArrayList<? extends Creature> creatures, Creature c);
}
45 changes: 45 additions & 0 deletions 20171010/王琛然-151220104/Grandpa.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
public class Grandpa implements Creature{
private String name;
private Position position;

Grandpa(){
this.name = "爷爷";
this.position = null;
}


Grandpa(int x, int y){
this.name = "爷爷";
this.position = new Position(x, y);
}

@Override
public Position getPosition() {
return position;
}


@Override
public void setPosition(Position position) {
this.position = position;
position.setHolder(this);
}


@Override
public void report() {
System.out.println(this.toString());
}


@Override
public void reportName(){
System.out.print(name);
}


@Override
public String toString(){
return "爷爷" + "at[" + this.position.getX() + "][" + this.position.getY() + "]";
}
}
40 changes: 40 additions & 0 deletions 20171010/王琛然-151220104/HEYI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import javafx.geometry.Pos;

import javax.management.ListenerNotFoundException;
import java.util.ArrayList;

public class HEYI implements Formation {
private ArrayList<? extends Creature> creatures;

@Override
public void excuete(ArrayList<? extends Creature> creatures, Creature c){
int num = 7;
try{
this.creatures = creatures;
}catch(NullPointerException e) {
System.out.println("ArrayList 空指针异常");
e.printStackTrace();
}
Position p = new Position(num/2 + 5, 15);
this.creatures.get(0).setPosition(p);
int j = 1;
for(int i = 1; i < num; i++){
if(i % 2 == 1){
p = new Position(num/2 - j + 5, j + 15);
this.creatures.get(i).setPosition(p);
}
else{
p = new Position(num/2 + j + 5, j + 15);
this.creatures.get(i).setPosition(p);
j++;
}
}
try {
p = new Position(num / 2 + 5, 18);
c.setPosition(p);
}catch(NullPointerException e) {
System.out.println("Creature 空指针异常");
e.printStackTrace();
}
}
}
75 changes: 75 additions & 0 deletions 20171010/王琛然-151220104/Hulu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
enum Color {
, , , 绿, , ,
}

enum Rank {
老大, 老二, 老三, 老四, 老五, 老六, 老七
}

public class Hulu implements Creature, Comparable {
private Rank name;
private Color color;
private Position position;


public Hulu(Color color, Rank name) {
this.name = name;
this.color = color;
}

public Rank getName(){
return name;
}

public Color getColor() {
return color;
}

public void print_name() {
System.out.println(name);
}

public void print_color() {
System.out.println(color);
}



@Override
public Position getPosition() {
return position;
}

@Override
public void setPosition(Position position) {
this.position = position;
position.setHolder(this);
}

@Override
public boolean biggerThan(Comparable brother){

if (brother instanceof Hulu)
return this.getName().ordinal()> ((Hulu) brother).getName().ordinal();
else
return false;
}

@Override
public void report() {
System.out.println(this.toString());
}


@Override
public void reportName(){
System.out.print(name);
}


@Override
public String toString(){
return this.name.toString() + "(" + this.color.toString() + ")at[" + this.position.getX() + "][" + this.position.getY() + "]";
}

}
18 changes: 18 additions & 0 deletions 20171010/王琛然-151220104/InsertionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class InsertionSort implements Sort {

@Override
public void sort(Queue queue) {
Position[] positions = queue.getPositions();
Creature creature = null;
int j;
for (int i = 1; i < positions.length; i++) {
for (j = i; j > 0; j--) {
if (!((Comparable) (positions[j].getHolder())).biggerThan((Comparable) (positions[j - 1].getHolder()))) {
creature = positions[j].getHolder();
positions[j - 1].getHolder().setPosition(positions[j]);
creature.setPosition(positions[j - 1]);
}
}
}
}
}
37 changes: 37 additions & 0 deletions 20171010/王琛然-151220104/Minion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
public class Minion implements Creature {
private String name;
private Position position;

Minion(int index){
String s = String.valueOf(index);
this.name = "喽啰" + s;
}

@Override
public Position getPosition() {
return position;
}

@Override
public void setPosition(Position position) {
this.position = position;
position.setHolder(this);
}


@Override
public void report() {
System.out.println(this.toString());
}

@Override
public void reportName(){
System.out.print(name);
}


@Override
public String toString(){
return this.name + "at[" + this.position.getX() + "][" + this.position.getY() + "]";
}
}
38 changes: 38 additions & 0 deletions 20171010/王琛然-151220104/Position.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
public class Position {

private int x;
private int y;

private Creature holder;

public Creature getHolder() {
return holder;
}

public void setHolder(Creature holder) {
this.holder = holder;
}

public int getX() {
return x;
}

public int getY(){
return y;
}

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

public void setY(int y){
this.y = y;
}

public Position(int x, int y){
super();
this.x = x;
this.y = y;
this.holder = null;
}
}
73 changes: 73 additions & 0 deletions 20171010/王琛然-151220104/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.ArrayList;
import java.util.List;

public class Queue {
final int N = 20;

private Position[] positions;
private ArrayList<? extends Creature> creatures;

public Position[] getPositions() {
return positions;
}

public ArrayList<? extends Creature> getCreatures() {
return creatures;
}

public Queue(ArrayList<? extends Creature> list){
this.creatures = list;
this.positions = new Position[creatures.size()];
for(int i =0; i < creatures.size(); i++){
this.positions[i] = new Position(i + 5, 5);
this.creatures.get(i).setPosition(this.positions[i]);
}
new BubbleSort().sort(this);
}

public void rollCall(){
System.out.println("葫芦娃报告位置:");
for(Creature creature: this.creatures){
creature.report();
}
System.out.println("\n");
System.out.flush();

}

public void shuffle() {
Random rnd = ThreadLocalRandom.current();
for (int i = creatures.size() - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
Position position = creatures.get(index).getPosition();
creatures.get(index).setPosition(creatures.get(i).getPosition());
creatures.get(i).setPosition(position);
}
}

/*
public static void main(String[] args){
Hulu[] brothers = new Hulu[7];
for(int i = 0; i< brothers.length; i++){
brothers[i] = new Hulu(Color.values()[i], Rank.values()[i]);
}
Queue queue = new Queue(brothers);
queue.rollCall();
queue.shuffle();
queue.rollCall();
System.out.println("InsertionSort");
new InsertionSort().sort(queue);
queue.rollCall();
queue.shuffle();
queue.rollCall();
System.out.println("BubbleSort");
new BubbleSort().sort(queue);
queue.rollCall();
}
*/
}
Loading

0 comments on commit 6852e0b

Please sign in to comment.