日期:2014-05-20 浏览次数:20777 次
import java.io.*;
import java.util.*;
public class GameHelper {
private static final String alphabet="abcdefg";
private int gridLength=7;
private int gridSize=49; //49的网格 7*7
private int[] grid=new int[gridSize]; //定义一个49个元素的数组
private int comCount=0;
public String getUserInput(String prompt){
//输入函数,返回,带提示功能,返回用户在键盘上输入
String inputLine=null; //保存返回值
System.out.print(prompt+" "); //通过prompt参数,提示信息
try{
BufferedReader is=new BufferedReader(new InputStreamReader(System.in));
//标准输入
inputLine=is.readLine();
//将参数传递给inputline变量
if(inputLine.length()==0){
return null; //输入数据的长度为0,则返回空值
}
}
catch(IOException e){
System.out.println("IOException: "+e); //错误处理
}
return inputLine.toLowerCase(); //转换为小写,并返回
}
public ArrayList<String> placeDotCom(int comSize){
//函数返回值为字符串的数组,参数为Int数据表示大小
ArrayList<String> alphaCells=new ArrayList<String>(); //定义一个空的数组
String[] alphacoords =new String[comSize]; //字母的坐标
String temp=null; //空字符串
int[] coords=new int[comSize]; //定义Int坐标
int attempts=0; //定义一个数字
boolean success=false; //判断
int location=0; //
comCount++; //计数器
int incr=1; //增长值为 1
if(1==(comCount%2)){
incr=gridLength; //如果comCount为奇数,则incr为gridLength???
}
while(!success&attempts++<200){
location=(int)(Math.random()*gridSize);
int x=0;
success=true;
while(success&&x<comSize){
if(grid[location]==0){
coords[x++]=location;
location+=incr;
if(location>=gridSize){
success=false;
}
if(x>0&&(location%gridLength==0)){
success=false;
}
}else{
success=false;
}
}
}
int x=0;
int row=0;
int column=0;
while(x<comSize){
grid[coords[x]]=1;
row=(int)(coords[x]/gridLength);
column=coords[x]%gridLength;
temp=String.valueOf(alphabet.charAt(column));
alphaCells.add(temp.concat(Integer.toString(row)));
x++;
}
return alphaCells;
}
}