Java中的一维战舰游戏
我是一名大学生,也是一名Java开发新手。我得到了这个任务,我需要用字符串模拟一个一维战舰游戏。给我的规则如下:Java中的一维战舰游戏,java,string,Java,String,我是一名大学生,也是一名Java开发新手。我得到了这个任务,我需要用字符串模拟一个一维战舰游戏。给我的规则如下: 木板的长度必须为10,高度必须为1 计算机把一条长一艘船和一条长两艘船放在船上 用户必须在10个可能的位置之一重复射击 电脑会对每一次射击做出“水”,“命中”,“沉没”,“已经命中”的反应 当所有船只都沉没时,游戏结束时,电脑会告诉你需要多少次射击 到目前为止,我想到的是: 定义一个方法String buildBoard(),该方法返回一个由7个字符“water”组成的随机字符串;2
public class UnidimensionalBattleship {
/* x - miss (no ships here)
* . - sea (available for firing)
* s - enemy ship (available for firing)
* c - enemy ship (2 spaces, available for firing) */
public static String map() {
String sea = new String("..........");
int pos;
Random rnd = new Random();
pos = rnd.nextInt(sea.length());
sea = sea.substring(0, pos) + "s" + sea.substring(pos + 1);
int shipsSet = 0;
do {
if (sea.substring(pos).equals(".") && sea.substring(pos+1).equals(".")) { //pos available
sea.substring(pos).equals("cc");
shipsSet++;
}
}while(shipsSet < 2);
return sea;
}
public static String manageShot(String sea, int p) {
char outcome = sea.charAt(p);
switch(outcome) {
case '.': System.out.println("Miss");
sea=sea.substring(p, p) + "x" + sea.substring(p+1);
break;
case 's': System.out.println("Sunk!");
sea=sea.substring(p, p) + "x" + sea.substring(p+1);
break;
case 'c': System.out.println("Hit!");
sea=sea.substring(p, p) + "x" + sea.substring(p+1);
break;
case 'x': System.out.println("Already Hit");
break;
}
return sea;
}
public static boolean endGame(String sea) {
int i; //counter
i=0;
for (i=0; i<sea.length(); i++) {
if (sea.charAt(i)=='.'|| sea.charAt(i)=='x')
i++;
}
return true;
}
公共级一维货船{
/*x-未命中(此处无船舶)
*-海上(可用于射击)
*s-敌舰(可供射击)
*c-敌舰(2个空间,可供射击)*/
公共静态字符串映射(){
字符串sea=新字符串(“……”);
int pos;
随机rnd=新随机();
pos=rnd.nextInt(sea.length());
sea=sea.子字符串(0,位置)+“s”+sea.子字符串(位置+1);
int-shipsSet=0;
做{
if(sea.substring(pos).equals(“.”)和&sea.substring(pos+1).equals(“.”){//pos
sea子串(位置)等于(“cc”);
shipsSet++;
}
}while(shipsSet<2);
归海;
}
公共静态字符串manageShot(字符串sea,int p){
char结果=sea.charAt(p);
转换(结果){
案例“.”:System.out.println(“Miss”);
sea=sea.substring(p,p)+“x”+sea.substring(p+1);
打破
案例:System.out.println(“沉没!”);
sea=sea.substring(p,p)+“x”+sea.substring(p+1);
打破
案例“c”:System.out.println(“命中!”);
sea=sea.substring(p,p)+“x”+sea.substring(p+1);
打破
案例“x”:System.out.println(“已命中”);
打破
}
归海;
}
公共静态布尔结束游戏(字符串sea){
int i;//计数器
i=0;
对于(i=0;i我想我已经得到了第一部分——您可以使用Apache Commons中的RandomStringUtils。。。
如您所见,它需要一个整数和一个字符串-整数是字符串的长度,字符串是它将使用的字符组
我会在网上浏览一下,看看我是否能帮你把这两个角色保持在一起
我的问题是,我完全不知道如何生成一个随机字符串,特别是当我必须让两个字符相邻时。有人能告诉我怎么做吗
由于您需要在字符串中执行此操作,一些使用字符串函数的简单字符串操作将起作用。为了确定它是“命中”、“沉没”、“未命中”还是已经命中,您可以使用一组预定义的字母来表示其中的每一个字母
例如:
x - miss (no ships here)
. - sea (available for firing)
s - enemy ship (available for firing)
c - enemy ship (2 spaces, available for firing)
String map = String.valueOf(sea);
int attackPos = scn.nextInt();
char outcome = map.charAt(attackPos);
switch(outcome){
case '.': System.out.println("Miss");
//use substring to mark this spot as "X"
break;
case 's': System.out.println("Sunk!"); //ship 's' only takes 1 space
//use substring to mark this spot as "X"
break;
case 'c': System.out.println("Hit!"); //ship 'c' takes 2 spaces
//use substring to mark this spot as "X"
break;
case 'x': System.out.println("Already Hit");
break;
}
要生成随机位置船舶的字符串,有多种方法。您可以使用StringBuilder并连接字符串,或者只需使用字符数组并在定位船舶后将其转换为字符串:
char[] sea = new char[10];
for(int x=0; x<sea.length; x++)
sea[x] = '.'; //set everything as sea first
Random rnd = new Random();
sea[rnd.nextInt(sea.length)] = 's'; //sea ship at random position
生成结果:
x - miss (no ships here)
. - sea (available for firing)
s - enemy ship (available for firing)
c - enemy ship (2 spaces, available for firing)
String map = String.valueOf(sea);
int attackPos = scn.nextInt();
char outcome = map.charAt(attackPos);
switch(outcome){
case '.': System.out.println("Miss");
//use substring to mark this spot as "X"
break;
case 's': System.out.println("Sunk!"); //ship 's' only takes 1 space
//use substring to mark this spot as "X"
break;
case 'c': System.out.println("Hit!"); //ship 'c' takes 2 spaces
//use substring to mark this spot as "X"
break;
case 'x': System.out.println("Already Hit");
break;
}
以上将给你一个很好的想法,在哪里开始和如何实现整个程序。如果你不允许使用char数组,简单地使用StringBuilder。如果它不被允许,只要使用子串来生成一个带有随机位置的船的地图。I don’’我不知道如何准确地编码为什么你可以使用原子弹,却要用毛巾杀死苍蝇bomb@Dici我手头没有原子弹:)^^对不起,我的意思是,你通常不希望学生在一个小项目中使用Apache或任何外部库。你的飞船可能会重叠,我通常不喜欢在不满足随机条件的情况下继续循环。我可能错了,但这听起来对很多飞船来说很昂贵,因为碰撞越来越像坦克为了回答这个问题,我正在努力理解它是如何工作的。如果我做对了,我必须为第二艘船(c)定义一个Random rnd2=new Random()太好了。但是我怎样才能把它带到某个地方呢?迪奇,我不需要担心船的数量太多,我只需要two@Dici如果船只相撞,它将生成一个新的位置,因此最终将根据需要生成n艘船只。有很多方法可以做到这一点。更有效的方法对于OP来说太多了。当然,我们可以简单地生成一个数组like“sss……”和洗牌