`
ncs123
  • 浏览: 102964 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

丢手帕游戏Java实现

阅读更多
public class LinkedTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ChildLinked linker = new ChildLinked(9,2,2);
		linker.createLinked();
		linker.play();
	}
}

class Child{
	int no;  //编号
	Child nextChild;  //下一个节点
	public Child(int no){
		this.no = no;
	}
}

class ChildLinked{
	private int length;       //队列长度
	private int startNum;     //开始报数
	private int outNum;       //出列数
	Child firstChild = null;  //第一个结点
	Child temp = null;        //指针
	
	public ChildLinked(int length,int startNum,int outNum){
		this.length = length;
		this.startNum = startNum;
		this.outNum = outNum;
	}
	
	//生成圈
	public void createLinked(){
		Child utilChild = null;
		
		for(int i=1;i<=length;i++){
			if(i==1){
				firstChild = new Child(i);
				temp = firstChild;
			} else {
				utilChild = new Child(i);
				temp.nextChild = utilChild;
				temp = utilChild;
				
				if(i==length)
					utilChild.nextChild = firstChild;
			}
		}
	}
	
	//开始游戏
	public void play(){
		temp = firstChild;
		Child preChild = null;
		
		for(int i=1;i<startNum;i++){
			preChild = temp;
			temp = temp.nextChild;
		}
		
		while(length>0){
			for(int i=1;i<outNum;i++){
				preChild = temp;
				temp = temp.nextChild;
			}
			
			preChild.nextChild = temp.nextChild;
			System.out.println("child no="+temp.no+" is out");
			temp = temp.nextChild;
			length--;
		}
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics