java球员入场代码的简单介绍

求一个JAVA游戏代码!!!急!!!

俄罗斯方块——java源代码提供

创新互联公司基于分布式IDC数据中心构建的平台为众多户提供成都移动机房托管 四川大带宽租用 成都机柜租用 成都服务器租用。

import java.awt.*;

import java.awt.event.*;

//俄罗斯方块类

public class ERS_Block extends Frame{

public static boolean isPlay=false;

public static int level=1,score=0;

public static TextField scoreField,levelField;

public static MyTimer timer;

GameCanvas gameScr;

public static void main(String[] argus){

ERS_Block ers = new ERS_Block("俄罗斯方块游戏 V1.0 Author:Vincent");

WindowListener win_listener = new WinListener();

ers.addWindowListener(win_listener);

}

//俄罗斯方块类的构造方法

ERS_Block(String title){

super(title);

setSize(600,480);

setLayout(new GridLayout(1,2));

gameScr = new GameCanvas();

gameScr.addKeyListener(gameScr);

timer = new MyTimer(gameScr);

timer.setDaemon(true);

timer.start();

timer.suspend();

add(gameScr);

Panel rightScr = new Panel();

rightScr.setLayout(new GridLayout(2,1,0,30));

rightScr.setSize(120,500);

add(rightScr);

//右边信息窗体的布局

MyPanel infoScr = new MyPanel();

infoScr.setLayout(new GridLayout(4,1,0,5));

infoScr.setSize(120,300);

rightScr.add(infoScr);

//定义标签和初始值

Label scorep = new Label("分数:",Label.LEFT);

Label levelp = new Label("级数:",Label.LEFT);

scoreField = new TextField(8);

levelField = new TextField(8);

scoreField.setEditable(false);

levelField.setEditable(false);

infoScr.add(scorep);

infoScr.add(scoreField);

infoScr.add(levelp);

infoScr.add(levelField);

scorep.setSize(new Dimension(20,60));

scoreField.setSize(new Dimension(20,60));

levelp.setSize(new Dimension(20,60));

levelField.setSize(new Dimension(20,60));

scoreField.setText("0");

levelField.setText("1");

//右边控制按钮窗体的布局

MyPanel controlScr = new MyPanel();

controlScr.setLayout(new GridLayout(5,1,0,5));

rightScr.add(controlScr);

//定义按钮play

Button play_b = new Button("开始游戏");

play_b.setSize(new Dimension(50,200));

play_b.addActionListener(new Command(Command.button_play,gameScr));

//定义按钮Level UP

Button level_up_b = new Button("提高级数");

level_up_b.setSize(new Dimension(50,200));

level_up_b.addActionListener(new Command(Command.button_levelup,gameScr));

//定义按钮Level Down

Button level_down_b =new Button("降低级数");

level_down_b.setSize(new Dimension(50,200));

level_down_b.addActionListener(new Command(Command.button_leveldown,gameScr));

//定义按钮Level Pause

Button pause_b =new Button("游戏暂停");

pause_b.setSize(new Dimension(50,200));

pause_b.addActionListener(new Command(Command.button_pause,gameScr));

//定义按钮Quit

Button quit_b = new Button("退出游戏");

quit_b.setSize(new Dimension(50,200));

quit_b.addActionListener(new Command(Command.button_quit,gameScr));

controlScr.add(play_b);

controlScr.add(level_up_b);

controlScr.add(level_down_b);

controlScr.add(pause_b);

controlScr.add(quit_b);

setVisible(true);

gameScr.requestFocus();

}

}

//重写MyPanel类,使Panel的四周留空间

class MyPanel extends Panel{

public Insets getInsets(){

return new Insets(30,50,30,50);

}

}

//游戏画布类

class GameCanvas extends Canvas implements KeyListener{

final int unitSize = 30; //小方块边长

int rowNum; //正方格的行数

int columnNum; //正方格的列数

int maxAllowRowNum; //允许有多少行未削

int blockInitRow; //新出现块的起始行坐标

int blockInitCol; //新出现块的起始列坐标

int [][] scrArr; //屏幕数组

Block b; //对方快的引用

//画布类的构造方法

GameCanvas(){

rowNum = 15;

columnNum = 10;

maxAllowRowNum = rowNum - 2;

b = new Block(this);

blockInitRow = rowNum - 1;

blockInitCol = columnNum/2 - 2;

scrArr = new int [32][32];

}

//初始化屏幕,并将屏幕数组清零的方法

void initScr(){

for(int i=0;irowNum;i++)

for (int j=0; jcolumnNum;j++)

scrArr[j]=0;

b.reset();

repaint();

}

//重新刷新画布方法

public void paint(Graphics g){

for(int i = 0; i rowNum; i++)

for(int j = 0; j columnNum; j++)

drawUnit(i,j,scrArr[j]);

}

//画方块的方法

public void drawUnit(int row,int col,int type){

scrArr[row][col] = type;

Graphics g = getGraphics();

tch(type){ //表示画方快的方法

case 0: g.setColor(Color.black);break; //以背景为颜色画

case 1: g.setColor(Color.blue);break; //画正在下落的方块

case 2: g.setColor(Color.magenta);break; //画已经落下的方法

}

g.fill3DRect(col*unitSize,getSize().height-(row+1)*unitSize,unitSize,unitSize,true);

g.dispose();

}

public Block getBlock(){

return b; //返回block实例的引用

}

//返回屏幕数组中(row,col)位置的属性值

public int getScrArrXY(int row,int col){

if (row 0 || row = rowNum || col 0 || col = columnNum)

return(-1);

else

return(scrArr[row][col]);

}

//返回新块的初始行坐标方法

public int getInitRow(){

return(blockInitRow); //返回新块的初始行坐标

}

//返回新块的初始列坐标方法

public int getInitCol(){

return(blockInitCol); //返回新块的初始列坐标

}

//满行删除方法

void deleteFullLine(){

int full_line_num = 0;

int k = 0;

for (int i=0;irowNum;i++){

boolean isfull = true;

L1:for(int j=0;jcolumnNum;j++)

if(scrArr[j] == 0){

k++;

isfull = false;

break L1;

}

if(isfull) full_line_num++;

if(k!=0 k-1!=i !isfull)

for(int j = 0; j columnNum; j++){

if (scrArr[j] == 0)

drawUnit(k-1,j,0);

else

drawUnit(k-1,j,2);

scrArr[k-1][j] = scrArr[j];

}

}

for(int i = k-1 ;i rowNum; i++){

for(int j = 0; j columnNum; j++){

drawUnit(i,j,0);

scrArr[j]=0;

}

}

ERS_Block.score += full_line_num;

ERS_Block.scoreField.setText(""+ERS_Block.score);

}

//判断游戏是否结束方法

boolean isGameEnd(){

for (int col = 0 ; col columnNum; col ++){

if(scrArr[maxAllowRowNum][col] !=0)

return true;

}

return false;

}

public void keyTyped(KeyEvent e){

}

public void keyReleased(KeyEvent e){

}

//处理键盘输入的方法

public void keyPressed(KeyEvent e){

if(!ERS_Block.isPlay)

return;

tch(e.getKeyCode()){

case KeyEvent.VK_DOWN:b.fallDown();break;

case KeyEvent.VK_LEFT:b.leftMove();break;

case KeyEvent.VK_RIGHT:b.rightMove();break;

case KeyEvent.VK_SPACE:b.leftTurn();break;

}

}

}

//处理控制类

class Command implements ActionListener{

static final int button_play = 1; //给按钮分配编号

static final int button_levelup = 2;

static final int button_leveldown = 3;

static final int button_quit = 4;

static final int button_pause = 5;

static boolean pause_resume = true;

int curButton; //当前按钮

GameCanvas scr;

//控制按钮类的构造方法

Command(int button,GameCanvas scr){

curButton = button;

this.scr=scr;

}

//按钮执行方法

public void actionPerformed (ActionEvent e){

tch(curButton){

case button_play:if(!ERS_Block.isPlay){

scr.initScr();

ERS_Block.isPlay = true;

ERS_Block.score = 0;

ERS_Block.scoreField.setText("0");

ERS_Block.timer.resume();

}

scr.requestFocus();

break;

case button_levelup:if(ERS_Block.level 10){

ERS_Block.level++;

ERS_Block.levelField.setText(""+ERS_Block.level);

ERS_Block.score = 0;

ERS_Block.scoreField.setText(""+ERS_Block.score);

}

scr.requestFocus();

break;

case button_leveldown:if(ERS_Block.level 1){

ERS_Block.level--;

ERS_Block.levelField.setText(""+ERS_Block.level);

ERS_Block.score = 0;

ERS_Block.scoreField.setText(""+ERS_Block.score);

}

scr.requestFocus();

break;

case button_pause:if(pause_resume){

ERS_Block.timer.suspend();

pause_resume = false;

}else{

ERS_Block.timer.resume();

pause_resume = true;

}

scr.requestFocus();

break;

case button_quit:System.exit(0);

}

}

}

//方块类

class Block {

static int[][] pattern = {

{0x0f00,0x4444,0x0f00,0x4444},//用十六进至表示,本行表示长条四种状态

{0x04e0,0x0464,0x00e4,0x04c4},

{0x4620,0x6c00,0x4620,0x6c00},

{0x2640,0xc600,0x2640,0xc600},

{0x6220,0x1700,0x2230,0x0740},

{0x6440,0x0e20,0x44c0,0x8e00},

{0x0660,0x0660,0x0660,0x0660}

};

int blockType; //块的模式号(0-6)

int turnState; //块的翻转状态(0-3)

int blockState; //快的下落状态

int row,col; //块在画布上的坐标

GameCanvas scr;

//块类的构造方法

Block(GameCanvas scr){

this.scr = scr;

blockType = (int)(Math.random() * 1000)%7;

turnState = (int)(Math.random() * 1000)%4;

blockState = 1;

row = scr.getInitRow();

col = scr.getInitCol();

}

//重新初始化块,并显示新块

public void reset(){

blockType = (int)(Math.random() * 1000)%7;

turnState = (int)(Math.random() * 1000)%4;

blockState = 1;

row = scr.getInitRow();

col = scr.getInitCol();

dispBlock(1);

}

//实现“块”翻转的方法

public void leftTurn(){

if(assertValid(blockType,(turnState + 1)%4,row,col)){

dispBlock(0);

turnState = (turnState + 1)%4;

dispBlock(1);

}

}

//实现“块”的左移的方法

public void leftMove(){

if(assertValid(blockType,turnState,row,col-1)){

dispBlock(0);

col--;

dispBlock(1);

}

}

//实现块的右移

public void rightMove(){

if(assertValid(blockType,turnState,row,col+1)){

dispBlock(0);

col++;

dispBlock(1);

}

}

//实现块落下的操作的方法

public boolean fallDown(){

if(blockState == 2)

return(false);

if(assertValid(blockType,turnState,row-1,col)){

dispBlock(0);

row--;

dispBlock(1);

return(true);

}else{

blockState = 2;

dispBlock(2);

return(false);

}

}

//判断是否正确的方法

boolean assertValid(int t,int s,int row,int col){

int k = 0x8000;

for(int i = 0; i 4; i++){

for(int j = 0; j 4; j++){

if((int)(pattern[t][s]k) != 0){

int temp = scr.getScrArrXY(row-i,col+j);

if (temp0||temp==2)

return false;

}

k = k 1;

}

}

return true;

}

//同步显示的方法

public synchronized void dispBlock(int s){

int k = 0x8000;

for (int i = 0; i 4; i++){

for(int j = 0; j 4; j++){

if(((int)pattern[blockType][turnState]k) != 0){

scr.drawUnit(row-i,col+j,s);

}

k=k1;

}

}

}

}

//定时线程

class MyTimer extends Thread{

GameCanvas scr;

public MyTimer(GameCanvas scr){

this.scr = scr;

}

public void run(){

while(true){

try{

sleep((10-ERS_Block.level + 1)*100);

}

catch(InterruptedException e){}

if(!scr.getBlock().fallDown()){

scr.deleteFullLine();

if(scr.isGameEnd()){

ERS_Block.isPlay = false;

suspend();

}else

scr.getBlock().reset();

}

}

}

class WinListener extends WindowAdapter{

public void windowClosing (WindowEvent l){

System.exit(0);

}

}

求java代码

/*

我现在有一个String类型的数组,里面存的都是数字(例:String[] aa={01,02,03...}),这个是从数据库取得,没法改

现在我想用for循环把把数组里的每一个数据都加上一个数字

*/

public class Test {

public static void main( String[] args ) {

String[] aa={"01","02","03"};

int inc=100;//假设加100

System.out.println("before add:");

for(int i=0;iaa.length;i++){

System.out.println(aa[i]);

}

String temp=null;

int mid=0;

for(int i=0;iaa.length;i++){

temp=aa[i];

try{

mid=Integer.parseInt(temp);

}catch(Exception e){

System.out.println(aa[i]+"不是数字");

e.printStackTrace();

System.exit(-1);

}

mid=mid+inc;

aa[i]=String.valueOf(mid);

}

System.out.println("after add "+inc+" :");

for(int i=0;iaa.length;i++){

System.out.println(aa[i]);

}

}

}

高分求java代码 (认真回答还有悬赏)

按你现在说的这个内容,我觉得三个类就能表现出来,我写了一下,你看看能不能满足你的要求:

/**

* 运动员信息

* @author songfeng163

*/

public class PlayerState {

private String lx; //运动类型

private String xing; // 姓

private String ming; // 名

public String getLx() {

return lx;

}

public void setLx(String lx) {

this.lx = lx;

}

public String getXing() {

return xing;

}

public void setXing(String xing) {

this.xing = xing;

}

public String getMing() {

return ming;

}

public void setMing(String ming) {

this.ming = ming;

}

//返回姓名信息

@Override

public String toString() {

return xing+ming;

}

}

/**

* 运动员得分信息

* @author songfeng163

*/

public class SoccerPlayerStats {

private PlayerState player; //运动员信息

private double spointsScored; //点球得分

private double assists; //助攻得分

private double penaltyKickRate; //罚球得分概率

public PlayerState getPlayer() {

return player;

}

public void setPlayer(PlayerState player) {

this.player = player;

}

public double getSpointsScored() {

return spointsScored;

}

public void setSpointsScored(double spointsScored) {

this.spointsScored = spointsScored;

}

public double getAssists() {

return assists;

}

public void setAssists(double assists) {

this.assists = assists;

}

public double getPenaltyKickRate() {

return penaltyKickRate;

}

public void setPenaltyKickRate(double penaltyKickRate) {

this.penaltyKickRate = penaltyKickRate;

}

//返回分数信息

@Override

public String toString() {

return "** PLAYER ADDED **"+ player.toString()+"; Points:"+ spointsScored + "; Assists: "+ assists +"; Penalty Kick %:" + penaltyKickRate;

}

}

这里是主类:

import java.util.Scanner;

import java.util.List;

import java.util.ArrayList;

/**

* 主类

* @author songfeng163

*/

public class MyMain {

//添加方法

public ListSoccerPlayerStats add(ListSoccerPlayerStats list){

Scanner in=new Scanner(System.in);

PlayerState player=new PlayerState();

SoccerPlayerStats score=new SoccerPlayerStats();

System.out.println("请输入运动类型:足球,高尔夫,田径:");

player.setLx(in.next());

System.out.println("请输入姓:");

player.setXing(in.next());

System.out.println("请输入名:");

player.setMing(in.next());

System.out.println("请输入该运动员的点球得分:");

score.setSpointsScored(in.nextDouble());

System.out.println("请输入该运动员的助攻得分:");

score.setAssists(in.nextDouble());

System.out.println("请输入该运动员的罚球得分概率:");

score.setPenaltyKickRate(in.nextDouble());

score.setPlayer(player);

System.out.println(score); //显示新添加的人员

list.add(score);

return list;

}

//删除方法

public ListSoccerPlayerStats del(ListSoccerPlayerStats list){

Scanner in=new Scanner(System.in);

PlayerState player=new PlayerState();

System.out.println("请输入要删除运动员的姓:");

player.setXing(in.next());

System.out.println("请输入要删除运动员的名:");

player.setMing(in.next());

for(SoccerPlayerStats score:list){

if(score.getPlayer().getXing().equals(player.getXing())

score.getPlayer().getMing().equals(player.getMing())){

list.remove(score);

break;

}

}

return list;

}

//显示方法

public void show(ListSoccerPlayerStats list){

System.out.println("当前共有:"+list.size()+"条记录,内容如下:");

for(SoccerPlayerStats score:list){

System.out.println(score);

}

}

//保存方法

public ListSoccerPlayerStats save(ListSoccerPlayerStats list){

System.out.println("请输入要保存的文件名:");

Scanner in=new Scanner(System.in);

String fileName=in.next();

try{

BufferedWriter bw = new BufferedWriter(new FileWriter("C:/"+fileName));

for(SoccerPlayerStats score:list){

bw.write(score.toString());

bw.newLine();

}

bw.flush();

bw.close();

}catch(Exception ex){

ex.printStackTrace();

}

System.out.println("** 1 RECORD SAVED TO "+fileName+" **");

return list;

}

public static void main(String[] args) {

MyMain myMain=new MyMain();

Scanner in=new Scanner(System.in);

//如果像你说的是数组的话,数据没有办法动态变化,所以我用了list

ListSoccerPlayerStats list=new ArrayListSoccerPlayerStats();

String cz="QUIT"; //默认的操作为退出

System.out.println("下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?");

String czin=in.next(); //输入数据

cz=czin.toUpperCase(); //转换成大写

while(!cz.equals("QUIT")){ //不是退出

if(cz.equals("ADD")){ //添加数据

list = myMain.add(list);

}else if(cz.equals("REMOVE")){ //删除数据

list = myMain.del(list);

}else if(cz.equals("SHOW")){ //显示数据

myMain.show(list);

}else if(cz.equals("SAVE")){ //保存数据

myMain.save(list);

}

System.out.println("下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?");

czin=in.next(); //输入数据

cz=czin.toUpperCase(); //转换成大写

}

System.out.println("** GOODBYE! **");

}

}

运行结果如下:

下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?

add

请输入运动类型:足球,高尔夫,田径:

足球

请输入姓:

请输入名:

请输入该运动员的点球得分:

4

请输入该运动员的助攻得分:

2

请输入该运动员的罚球得分概率:

0.25

** PLAYER ADDED **王明; Points:4.0; Assists: 2.0; Penalty Kick %:0.25

下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?

add

请输入运动类型:足球,高尔夫,田径:

高尔夫

请输入姓:

请输入名:

请输入该运动员的点球得分:

2

请输入该运动员的助攻得分:

2

请输入该运动员的罚球得分概率:

0.3

** PLAYER ADDED **李三; Points:2.0; Assists: 2.0; Penalty Kick %:0.3

下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?

show

当前共有:2条记录,内容如下:

** PLAYER ADDED **王明; Points:4.0; Assists: 2.0; Penalty Kick %:0.25

** PLAYER ADDED **李三; Points:2.0; Assists: 2.0; Penalty Kick %:0.3

下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?

save

请输入要保存的文件名:

Playerdata.txt

下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?

remove

请输入要删除运动员的姓:

请输入要删除运动员的名:

下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?

show

当前共有:1条记录,内容如下:

** PLAYER ADDED **王明; Points:4.0; Assists: 2.0; Penalty Kick %:0.25

下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?

quit

** GOODBYE! **

求一Java程序代码!!

public class Matriculate {

private int score=400;

public int isMatriculate(Student stu){

if (stu.getIntgretResult()=score||(stu.getIntgretResult()=300stu.getSports()=90)) {

return 1;

}else {

return 0;

}

}

public static void main(String[] args) throws Exception {

Student stu1=new Student("zhangsan",1);

stu1.setIntgretResult(400);

Student stu2=new Student("lisi",2);

stu2.setSports(85);

stu2.setIntgretResult(356);

Matriculate m=new Matriculate();

int c1=m.isMatriculate(stu1);

if (c10) {

System.out.println("id:"+stu1.getId()+"\tname:"+stu1.getName()

+"\tIntgretResult:"+stu1.getIntgretResult()+"\tsports"+stu1.getSports());

System.out.println("被录取");

}

int c2=m.isMatriculate(stu2);

if (c20) {

System.out.println("id:"+stu2.getId()+"\tname:"+stu2.getName()

+"\tIntgretResult:"+stu2.getIntgretResult()+"\tsports"+stu2.getSports());

System.out.println("被录取");

}

}

}

class School{

public School(){}

public School(String schoolname, double scoreLine) {

this.schoolname = schoolname;

this.scoreLine = scoreLine;

}

private String schoolname;

private double scoreLine;

public String getSchoolname() {

return schoolname;

}

public double getScoreLine() {

return scoreLine;

}

}

class Student{

public Student(){}

public Student(String name, int id) {

this.name = name;

this.id = id;

}

private String name;

private int id;

private double intgretResult;

private int sports;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public double getIntgretResult() {

return intgretResult;

}

public void setIntgretResult(double intgretResult) {

this.intgretResult = intgretResult;

}

public int getSports() {

return sports;

}

public void setSports(int sports) {

this.sports = sports;

}

}

java代码

public float calSalary(){

return this.salary+this.salary*this.inc;

}

这个方法返回一个float型,是你再Employee e = new Employee("e001","AS",5000.0f,0.1f);

赋的值,把他们的值计算,然后返回,你调用后就得到

求助:循环赛—java源代码

以前的!

====代码====

package com;

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.List;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JList;

import javax.swing.JPanel;

import javax.swing.JTextField;

/**

* 网球赛

* @author Cris

* @ClassName: Match

* @Version

* @ModifiedBy

* @Copyright @ 2010 HL Technology Inc.

* @date 2011-5-24 上午11:29:39

* @description

*/

public class Match implements ActionListener{

private JFrame f;

private JTextField tf;

private JPanel container;

private JList myList ;

private JLabel message;

private JButton b;

/**

* @author Cris

* @title: main

* @date 2011-5-23 上午11:07:27

* @param args void

*/

public static void main(String[] args) {

Match t = new Match();

t.go();

}

public void go(){

f = new JFrame("比赛");

f.setLocation(100, 200);

f.setSize(600,500);

container = new JPanel(new BorderLayout());

f.setContentPane(container);

JPanel title = new JPanel(new FlowLayout());

tf = new JTextField(10);

title.add(tf);

b = new JButton("确定");

b.addActionListener(this);

title.add(b);

message = new JLabel();

title.add(message);

container.add(title,BorderLayout.NORTH);

myList = new JList();

container.add(myList,BorderLayout.CENTER);

f.setVisible(true);

}

/**

* 显示比赛结果

* @author Cris

* @title: show

* @date 2011-5-24 上午11:31:05

* @param n void

*/

private void show(int n){

Cris c = new Cris(n);

ListListString[] result = c.getResult();

String[] str = new String[result.size()];

for (int i = 0; i result.size(); i++) {

StringBuilder sb = new StringBuilder();

sb.append("第").append(i+1).append("天: ");

ListString[] matchList = result.get(i);

for(String[] match : matchList){

sb.append("[").append(match[0]).append("-").append(match[1]).append("] ");

}

str[i] = sb.toString();

}

myList.setListData(str);

message.setText("");

f.setVisible(true);

}

/**

* 清空数据

* @author Cris

* @title: clear

* @date 2011-5-24 上午11:30:36 void

*/

private void clear(){

String[] str = {""};

myList.setListData(str);

message.setText("");

}

public void actionPerformed(ActionEvent e) {

clear();

String s = tf.getText();

if (s == null || "".equals(s = s.trim()) ) {

message.setText("请输入一个偶数");

}

try {

int n = Integer.parseInt(s);

if (n = 0 || n % 2 != 0) {

message.setText("请输入一个偶数");

}else{

show(n);

}

} catch (Exception ex) {

message.setText("请输入一个偶数");

}

f.setVisible(true);

}

}

第二个类:

package com;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.LinkedList;

import java.util.List;

import java.util.Map;

import java.util.Random;

public class Cris {

/**选手数量*/

private int n ;

/**各个运动员还没进行的比赛 key:球员名称,value:需进行的比赛集合*/

private MapString, ListString[] playerMacth ;

/**球员人名*/

private String[] players ;

/**最终每天的比赛分布集合*/

private ListListString[] result;

/**所有可选的比赛*/

private ListListString[] allMatchData[];

/**每天的比赛*/

ListString[] dayMatch ;

/**一个随机对象*/

private Random r;

/**当前运算到的天数*/

private int nowDay;

public Cris(int n){

this.n = n;

playerMacth = new HashMapString, ListString[]();;

result = new ArrayListListString[]();

allMatchData = new LinkedList[n-1];

r = new Random();

init();

go();

}

/**

* 初始化球员

* @author Cris

* @title: init

* @date 2011-5-23 下午04:22:32 void

*/

private void init(){

//定义球员人名,以数字代表,从1开始

players = new String[n];

for (int i = 0; i players.length; i++) {

//定义球员的名字

players[i] = String.valueOf(i+1);

//存放每个球员要进行的比赛的集合

ListString[] matchList = new LinkedListString[]();

//球员赛事集合

playerMacth.put(players[i], matchList);

//球员集合,不变

// playerListConst.add(players[i]);

}

//定义每个人的比赛

for (int i = 0; i players.length -1; i++) {

for (int j = i+1; j players.length ; j++) {

String[] match = {players[i],players[j]};//定义一个比赛

//把此赛事添加到相应球员需比赛集合中

playerMacth.get(players[i]).add(match);

playerMacth.get(players[j]).add(match);

}

}

}

/**

* 随机获得一个当前天数可进行的比赛组合,并把此比赛组合从总集合中移除

* 如果当前天数无比赛,则先获得比赛,再随机

* @author Cris

* @title: getNowDayMatch

* @date 2011-5-24 上午09:48:23

* @return ListString[]

*/

private ListString[] getNowDayMatch(){

if (allMatchData[nowDay] == null) {

setAllMatchNowDay();

}

if (! allMatchData[nowDay].isEmpty()) {

int index = r.nextInt(allMatchData[nowDay].size());

ListString[] list = allMatchData[nowDay].get(index);

allMatchData[nowDay].remove(index);

return list;

}else {

return null;

}

}

/**

* 取消前一天的比赛

* @author Cris

* @title: cancelYesterday

* @date 2011-5-24 上午09:56:12 void

*/

private void cancelYesterday(){

//清空当前天数的可选比赛组合

allMatchData[nowDay] = null ;

//时间恢复到前一天

nowDay--;

//获得前一天的比赛

ListString[] matchList = result.get(nowDay);

//取消前一天的比赛

result.remove(nowDay);

//恢复每个运动员未进行的比赛

for(String[] match : matchList){

playerMacth.get(match[0]).add(match);

playerMacth.get(match[1]).add(match);

}

}

public void go(){

//循环比赛天数

for ( nowDay = 0; nowDay n -1; ) {

//1.随机一个比赛

ListString[] matchList = getNowDayMatch();

//无比赛组合 ,返回上一天 取消上一天的比赛,时间回退到前一天,跳过此次循环

if (matchList == null) {

cancelYesterday();

continue;

}

//3遍历比赛 去除运动员 去除运动员的未进行比赛

for(String[] match : matchList){

playerMacth.get(match[0]).remove(match);

playerMacth.get(match[1]).remove(match);

}

result.add(matchList);

nowDay++;

}

}

/**

* 从1号队员开始,获得当前天数所有可组合的赛事

* @author Cris

* @title: setAllMatchNowDay

* @date 2011-5-24 上午09:42:51 void

*/

private void setAllMatchNowDay(){

allMatchData[nowDay] = new LinkedListListString[]();

int n = 1;//1号运动员

//此运动员未进行的比赛

ListString[] matchList = playerMacth.get(String.valueOf(n));

for(String[] match : matchList){

ListString[] selectedList = new ArrayListString[]();

selectedList.add(match);

doDetAllMatchNowDay(n+1, selectedList);

}

}

/**

* 今天比赛的所有组合

* @author Cris

* @title: doDetAllMatchNowDay

* @date 2011-5-24 上午09:46:05

* @param playerNum

* @param selectedList void

*/

private void doDetAllMatchNowDay(int playerNum,ListString[] selectedList){

//运动员名称

String player = String.valueOf(playerNum);

boolean flag = true;

//判断此运动员是不是已被选过

for(String[] match : selectedList){

if (match[0].equals(player) || match[1].equals(player)) {

flag = false;

break;

}

}

//已被选过

if ( flag == false) {

//最后一个人员

if (playerNum == n) {

allMatchData[nowDay].add(selectedList);

}else{

doDetAllMatchNowDay(playerNum+1, selectedList);

}

return;

}

//此运动员未进行的比赛

ListString[] matchList = playerMacth.get(player);

//此运动员可选择的比赛

ListString[] canDoList = new ArrayListString[]();

for(String[] match : matchList){

flag = true;

for(String[] selected : selectedList){

if(match[0].equals(player)){

if(match[1].equals(selected[0])){

flag = false;

}else if(match[1].equals(selected[1])){

flag = false;

}

}else{

if(match[0].equals(selected[0])){

flag = false;

}else if(match[0].equals(selected[1])){

flag = false;

}

}

}

if (flag) {

canDoList.add(match);

}

}

//遍历此运动员可选择的比赛

for(String[] match : canDoList){

ListString[] newList = copyArrayList(selectedList);

//记录当前比赛

newList.add(match);

//最后一个人员

if (playerNum == n) {

allMatchData[nowDay].add(newList);

}else{

doDetAllMatchNowDay(playerNum+1, newList);

}

}

}

/**

* 复制集合

* @author Cris

* @title: copyArrayList

* @date 2011-5-24 上午09:24:44

* @param list

* @return ListString[]

*/

private ListString[] copyArrayList(ListString[] list){

ListString[] newList = new ArrayListString[]();

for(String[] s : list){

newList.add(s);

}

return newList;

}

public ListListString[] getResult() {

return result;

}

}


当前文章:java球员入场代码的简单介绍
链接地址:http://cdiso.cn/article/hhjiph.html

其他资讯