2013-08-28 3 views
2

나는 SOS라는 게임을 만들고있다. 3x3 보드 게임이며 Tic-Tac-Toe와 같은 개념이지만이 게임에서 플레이어는 X 또는 O로 플레이할지 여부를 선택할 수 없습니다.이 게임의 유일한 규칙은 "SOS"를 형성하는 것입니다.SOS 게임 : Tic-Tac-Toe의 개념

모든 위치가 채워지면 프로그램을 종료해야하며 "SOS"를 형성 한 플레이어에게 "SOS"가 형성됩니다.

내 문제는 득점에 관한 것입니다. 첫 번째 행의 SOS를 (- - -)으로 입력 한 후 두 번째 행의 첫 번째 열에 "O"를 입력하려고 시도했는데 플레이어 2가 증가합니다. 그것은 내 프로그램에서 두 번째 else를 만족시키지 않았기 때문에 증가하지 않아야합니다. 왜 그런가? 버그는 코드의이 섹션에서 발생하는

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 

    } 
    } 
    } 
+0

첫 번째 조건에는 두 개가 있습니다! 아마 틀렸어 ... !! (ar [0] [0] .equalsIgnoreCase ("") –

+1

링크 할 수있는 게임에 대한 자세한 설명이 있습니까? – thegrinner

+0

당신의 코드에 이중 부정이 있습니다 : !! (ar [0] [0]. equalsIgnoreCase (""))'... 고치려고 시도하십시오 –

답변

2

: 첫번째 선수가 제대로이 위치에 SOS를 입력하면

if((ar[0]+ar[1]+ar[2]).equals("SOS")) 

,이 사항이 항상있을 것입니다 여기에

내 코드입니다 참된. 어느 모든 회전에서 실행/다른 경우가 중첩 된 것을 의미한다 : 4 턴 동안

if(j%2 == 0) 
    { 
     player1++; 
    } 
    else if(j % 2 != 0) 
    { 
     player2++; 
    } 

는, j는 3과 동일합니다 (! J % 2 = 0) player2를 증가하는 사실이다.

프로그램을 계속 실행하면 initial if가 항상 true이기 때문에 player1과 player2가 매회마다 계속 증가하는 것을 볼 수 있습니다.

+0

아, 그래요. 내가 고칠 수 있을까? –