2017-02-10 4 views
-1

이것은 tic tac 발가락에 대해 현재 가지고있는 코드이며 잘 작동하며 점수를 유지하는 방법을 수정했지만 코드가 좋고 깨끗하지는 않습니다. 코드를 더 깨끗하게 만들 수있는 방법이 있습니까?Tic Tac Toe Score

import java.util.Scanner; 
import java.io.*; 
import java.lang.String; 


     public class TicTacToe{ 
      public static int a; 
      public static int b; 
      public static int c; 
      public static int d; 
      public static int e; 
      public static int f; 
      public static int g; 
      public static int h; 
      public static int i; 
        public static boolean Winner(){ 
          if(a == 1 && b == 1 && c == 1){ 
            return true;  
            } 
           else if(d == 1 && e == 1 && f == 1){ 
            return true;  
            } 
           else if(g == 1 && h == 1 && i == 1){ 
            return true;  
            } 
           else if(a == 1 && d == 1 && g == 1){ 
            return true;  
            } 
           else if(b == 1 && e == 1 && h == 1){ 
            return true;  
            } 
           else if(c == 1 && f == 1 && i == 1){ 
            return true;  
            } 
           else if(a == 1 && e == 1 && i == 1){ 
            return true;  
            } 
           else if(c == 1 && e == 1 && g == 1){ 
            return true;  
            } 
           else if(a == 2 && b == 2 && c == 2){ 
            return true;  
            } 
           else if(d == 2 && e == 2 && f == 2){ 
            return true;  
            } 
           else if(g == 2 && h == 2 && i == 2){ 
            return true; 
            }  
           else if(a == 2 && d == 2 && g == 2){ 
            return true; 
            }  
           else if(b == 2 && e == 2 && h == 2){ 
            return true; 
            }  
           else if(c == 2 && f == 2 && i == 2){ 
            return true; 
            }  
           else if(a == 2 && e == 2 && i == 2){ 
            return true;  
            } 
           else if(c == 2 && e == 2 && g == 2){ 
            return true; 
            }  
            return false; 
            } 
       public static void main (String args[]){ 
         String line1 = "1 | 2 | 3"; 
         String line2 = "-------"; 
         String line3 = "4 | 5 | 6"; 
         String line4 = "-------"; 
         String line5 = "7 | 8 | 9"; 
         String line6 = "-------"; 
           Scanner tic = new Scanner (System.in); 
           Scanner tac = new Scanner (System.in); 

            System.out.println("Welcome to a game of Tic Tac Toe"); 
            System.out.println("The game for those who aren't good at anything else."); 
            System.out.println (line1); 
            System.out.println (line2); 
            System.out.println (line3); 
            System.out.println (line4); 
            System.out.println (line5); 
            System.out.println (line6); 


        for (int i=0; i < 7; i++) 
         { 

         System.out.println("First player please choose a number. "); 
         int number = tic.nextInt(); 

         if (number == 1){ 
         line1 = line1.replace ("1","O"); 
         a = 1; 
         } 
         if (number == 2){ 
         line1 = line1.replace ("2","O"); 
         b = 1; 
         } 
         if (number == 3){ 
         line1 = line1.replace ("3","O"); 
         c = 1; 
         } 
         if (number == 4){ 
         line3 = line3.replace ("4","O"); 
         d = 1; 
         } 
         if (number == 5){ 
         line3 = line3.replace ("5","O"); 
         e = 1; 
         } 
         if (number == 6){ 
         line3 = line3.replace ("6","O"); 
         f = 1; 
         } 
         if (number == 7){ 
         line5 = line5.replace ("7","O"); 
         g = 1; 
         } 
         if (number == 8){ 
         line5 = line5.replace ("8","O"); 
         h = 1; 
         } 
         if (number == 9){ 
         line5 = line5.replace ("9","O"); 
         i = 1; 
         } 


          System.out.println(line1); 
          System.out.println(line2); 
          System.out.println(line3); 
          System.out.println(line4); 
          System.out.println(line5); 
            if(Winner()){ 
            System.out.println("First Player wins."); 
            System.exit(1); 
             } 
          if(a != 0 && b != 0 && c != 0 && d != 0 && e != 0 && f != 0 && g != 0 && h != 0 && i != 0){ 
            System.exit(1); 
             } 
         System.out.println("Second player please choose a number. "); 
         int number2 = tac.nextInt(); 

         if (number2 == 1){ 
         line1 = line1.replace ("1","X"); 
         a = 2; 
         } 
         if (number2 == 2){ 
         line1 = line1.replace ("2","X"); 
         b = 2; 
         } 
         if (number2 == 3){ 
         line1 = line1.replace ("3","X"); 
         c = 2; 
         } 
         if (number2 == 4){ 
         line3 = line3.replace ("4","X"); 
         d = 2; 
         } 
         if (number2 == 5){ 
         line3 = line3.replace ("5","X"); 
         e = 2; 
         } 
         if (number2 == 6){ 
         line3 = line3.replace ("6","X"); 
         f = 2; 
         } 
         if (number2 == 7){ 
         line5 = line5.replace ("7","X"); 
         g = 2; 
         } 
         if (number2 == 8){ 
         line5 = line5.replace ("8","X"); 
         h = 2; 
         } 
         if (number2 == 9){ 
         line5 = line5.replace ("9","X"); 
         i = 2; 
         } 
           System.out.println(line1); 
           System.out.println(line2); 
           System.out.println(line3); 
           System.out.println(line4); 
           System.out.println(line5); 
           System.out.println(line6); 
           if(Winner()){ 
            System.out.println("Second Player wins."); 
            System.exit(1); 
             } 
          if(a != 0 && b != 0 && c != 0 && d != 0 && e != 0 && f != 0 && g != 0 && h != 0 && i != 0){ 
            System.exit(1); 
     } 
    } 
    } 
    } 

이 방법이 효과적이지만 더 효율적인 방법이 있습니까?

+1

"그냥 작동하지 않는 것"이라는게 무슨 뜻입니까? 점수를 추적 할 변수가 보이지 않습니다. – Carcigenicate

+1

먼저 우승자를 탐지하는 알고리즘을 구현하면 안됩니까? 현재 실종 상태입니다. 그리고 코드 중복 방지에 대한 약간의 연구를해야합니다. 비록 당신이 새 것이라하더라도 그것에 대해 신경 써야합니다. – Tom

+1

코드 포맷은 끔찍합니다. 인간의 가독성을 위해 서식을 지정하십시오 (들여 쓰기 코드). --- 반복되는 코드가 너무 많습니다. * 방법 *에 대해 아직 배웠습니까? --- [** ** 최소, 완전하고 검증 가능한 예제를 만드는 방법] (http://stackoverflow.com/help/mcve)을 읽어보십시오. – Andreas

답변

1

플레이어가 이기면 프로그램이 감지되지 않는 것 같습니다. 이것을 구현하면 각 플레이어의 점수를 정수로 추적하면됩니다. 그리고 어느 플레이어가 이겼다고 감지하면 자신의 점수에 해당하는 정수를 증가시킵니다. :)

+0

또한 모든 사각형에 X와 O가 채워진 후에도 게임이 끝나지 않습니다. –

+0

@DavidChoweller 아니요, 종료되지만 'for' 루프 카운터가 너무 높습니다 (의도 한 420 개의 말장난 없음 : P). – Tom

2

게임의 승자와 패자를 얻고 적절한 점수를 얻으려면 게임 보드의 상태를 확인해야합니다. 텍스트를 파싱하는 것은 약간 복잡하고 불필요한 작업이 많이 필요하므로 어떤 식 으로든 보드를 나타내는 것이 좋습니다 matrix.

예 :

char[][] gameBoard = new char[3][3]; 

이 방법, 당신은 스캐너를 통해 입력을받을 때 코드처럼, 당신이에 ('O'또는 'X')을 원하는 문자를 입력합니다 이 같은 게임 판 변수 :

// number is 1-9 according to your code 
int row = number/3; // Gives you 0 or 1 or 2 
int col = number%3; // Gives you 0 or 1 or 2 
gameBoard[row][col] = 'O'; // or 'X' 

각 플레이어의 각 턴 후, 다음과 같은 함수를 호출합니다 (코드를 직접 작성을 자바 매트릭스 반복를 조회) :

public boolean hasWinner(int playerNumber) 
{ 
    // playerNumber will be passed according to which player did the current turn. 
    // The function will try to search for the sequence that wins the game. 

} 
0
import javax.swing.*; 
import java.util.*; 
public class TicTacToe 
{ 
private String [] line = new String[5]; 
private Scanner input; 
private int counter; 

public TicTacToe() 
{ 
    counter = 0; 
    input = new Scanner(System.in); 
    line[0] = "1 | 2 | 3"; 
    line[1] = "_______"; 
    line[2] = "4 | 5 | 6"; 
    line[3] = "_______"; 
    line[4] = "7 | 8 | 9"; 
} 

public void rulesOfTheGame(){ 
    System.out.println("Tic Tac Toe"); 
    System.out.println(line); 
} 

public void replace(String str) 
{ 
    for (int j = 1; j < 10; j++) 
    { 
     System.out.println("Player choose a number."); 
     int number; 
     number = 0; 
     for (int i = 1; i < 10; i++) 
     { 
      number = input.nextInt(); 
      if ((number == i) && ((i == 1) || (i == 2) || (i == 3))){ 
       String num = "" + i + ""; 
       line[0] = line[0].replace(num,str); 
       counter++; 
      } 
      if ((number == i) && ((i == 4) || (i == 5) || (i == 6))){ 
       String num = "" + i + ""; 
       line[2] = line[2].replace(num,str); 
       counter++; 
      } 
      else 
      { 
       String num = "" + i + ""; 
       line[4] = line[4].replace(num,str); 
       counter++; 
      } 
      System.out.println(line); 
     } 
    } 
} 

public void winnerWinnerChickenDinner() 
{ 
    if ((counter >= 5) && (counter < 9)){ 
     String isItOver = JOptionPane.showInputDialog("Getting down to the wire here, is the game over yet! CAPITAL Y FOR YES OR CAPITAL N FOR NO", "Game Over"); 
     if (isItOver.equals("Y")) 
     { 
      System.out.println("Game Over"); 
     } 
     else 
     { 
      System.out.print(""); 
     } 
    } 
    else if (counter == 9){ 
     System.out.println("Game Over"); 
     System.exit(0); 
    } 
} 
} 

이 가장 효율적인 코드입니다. 나는 실제로 창조 할 수 있었다. 이 작업을 수행하는 더 쉬운 방법이 있다고 확신합니다. 그러나 이것은 반복 코드가 거의없는 매우 간결합니다.

+0

또한 새로운 클라이언트 클래스를 만들어 실제 인쇄 값을 가져와야합니다. – user7282043