我正在創建一個名爲SOS的遊戲。這是一款3x3棋盤遊戲,與Tic-Tac-Toe有相同的概念,但在這個遊戲中,玩家不能選擇是否玩X或O,這個遊戲中唯一的規則就是形成「SOS」。SOS遊戲:Tic-Tac-Toe的概念
我們的計劃應該在所有職位被填補後終止,並且每個組成「SOS」將被添加到組成「SOS」的玩家。
我的問題是關於打分。在第一行輸入SOS後,在(- - -)
之後,我嘗試在第二行第一列輸入「O」,並且播放器2將增加。它不應該增加,因爲如果在我的程序中它不符合第二個。爲什麼會發生?
這裏是我的代碼:
import java.util.Scanner;
public class SOS
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String ar[] = {"-","-","-","-","-","-","-","-","-"};
int player1 = 0;
int player2 = 0;
int index = 0;
for(int l = 0; l<3; l++)
{
for(int j = 0; j<3; j++)
{
System.out.print(ar[j]);
}
System.out.print("\n");
}
for(int j = 0;j < 9;j++)
{
//Input position and letter
System.out.println("Enter the position number: ");
index = input.nextInt();
input.nextLine();
System.out.println("Enter (S or O): ");
ar[index - 1] = input.nextLine();
//Output for the game
for(int l = 0; l<9; l++)
{
System.out.print(ar[l]);
if(l == 2)
{
System.out.println();
}
else if(l == 5)
{
System.out.println();
}
else if(l == 8)
{
System.out.println();
}
}
//condition
if((ar[0]+ar[1]+ar[2]).equals("SOS"))
{
if(j%2 == 0)
{
player1++;
System.out.println("Player 1: "+player1+" Player 2: "+player2);
}
else if(j % 2 != 0)
{
player2++;
System.out.println("Player 1: "+player1+" Player 2: "+player2);
}
}
else if((ar[3]+ar[4]+ar[5]).equals("SOS"))
{
if(j%2 == 0)
{
player1++;
System.out.println("Player 1: "+player1+" Player 2: "+player2);
}
else if(j % 2 != 0)
{
player2++;
System.out.println("Player 1: "+player1+" Player 2: "+player2);
}
}
else
{
System.out.println("Player 1: "+player1+" Player 2: "+player2);
}
//end of condition
}
}
}
第一種情況你有兩個! 可能這是錯誤的...... !!(ar [0] [0] .equalsIgnoreCase(「」) –
你有更深的解釋,你可以鏈接到遊戲(也許是一個或兩個例子)?我不明白這裏的約束條件 – thegrinner
你的代碼有雙重否定:'!!(ar [0] [0] .equalsIgnoreCase(「」))'...嘗試修復 –