![](/img/trans.png)
[英]DiceRoll program using arrays, how can I accommodate multiple players?
[英]How can I end my program when one of players win?
我在這里有一個簡單的戰艦游戲,其中 2 名玩家為他們的船輸入繩索,然后他們互相射擊。 當其中一個成功命中 5 次時,我該如何停止程序? Siatka A 是玩家 A 放置他的船只的地方,而 siatka B 是玩家 B 放置他的船只的地方。
public static int shotA(boolean enemy[][], boolean mine[][]){
Scanner shot = new Scanner(System.in);
int x = Integer.parseInt(shot.nextLine());
int y = Integer.parseInt(shot.nextLine());
if (enemy[x][y]==true){
System.out.println("Trafiony");
System.out.println("Jeszcze raz strzelasz");
shotA(enemy, mine);
}else{
System.out.println("Pudło");
System.out.println("Teraz gracz B");
shotB(mine, enemy);
}
return ;
}
public static int shotB(boolean enemy[][], boolean mine[][]){
Scanner shot = new Scanner(System.in);
int x = Integer.parseInt(shot.nextLine());
int y = Integer.parseInt(shot.nextLine());
if (enemy[x][y]==true){
System.out.println("Trafiony");
System.out.println("Jeszcze raz strzelasz");
shotB(enemy, mine);
}else{
System.out.println("Pudło");
System.out.println("Teraz gracz A");
shotB(mine, enemy);
}
return ;
public static void main(String[] args) {
boolean[][] siatkaA = new boolean[10][10];
boolean[][] siatkaB = new boolean[10][10];
for (int i = 0; i < siatkaA.length; i++) {
for (int j = 0; j < siatkaA[0].length; j++) {
siatkaA[i][j]=false;
siatkaB[i][j]=false;
}
}
Scanner wsp = new Scanner(System.in);
System.out.println("Podaj wspołrzędne 5 statków graczu A(wartości 0-9, pisane po przecinku");
String statkiA=wsp.nextLine();
System.out.println("Podaj wspołrzędne 5 statków graczu B(wartości 0-9, pisane po przecinku");
String statkiB=wsp.nextLine();
String[] wspA= statkiA.split(",");
String[] wspB= statkiB.split(",");
int first=0;
int second=0;
for (int i = 0; i < wspA.length; i+=2) {
first=Integer.parseInt(wspA[i]);
second=Integer.parseInt(wspA[i+1]);
siatkaA[first][second]=true;
}
for (int i = 0; i < wspB.length; i+=2) {
first=Integer.parseInt(wspB[i]);
second=Integer.parseInt(wspB[i+1]);
siatkaB[first][second]=true;
}
System.out.println("Zaczyna gracz A:");
shotA(siatkaB, siatkaA);
我正在做這個簡單的培訓代碼,但我被困在那部分並尋求一些幫助和建議。
一種簡單的方法是添加兩個計數器變量,每個玩家(玩家 A 和玩家 B)一個,因此當射門成功時,將一個計數器變量添加到計數器變量。 當計數器變量等於 5 時,您可以將 boolean 變量設置為 true 並編寫一個 if 語句,例如 boolean 標志是否為 true 然后結束游戲。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.