이것은 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);
}
}
}
}
이 방법이 효과적이지만 더 효율적인 방법이 있습니까?
"그냥 작동하지 않는 것"이라는게 무슨 뜻입니까? 점수를 추적 할 변수가 보이지 않습니다. – Carcigenicate
먼저 우승자를 탐지하는 알고리즘을 구현하면 안됩니까? 현재 실종 상태입니다. 그리고 코드 중복 방지에 대한 약간의 연구를해야합니다. 비록 당신이 새 것이라하더라도 그것에 대해 신경 써야합니다. – Tom
코드 포맷은 끔찍합니다. 인간의 가독성을 위해 서식을 지정하십시오 (들여 쓰기 코드). --- 반복되는 코드가 너무 많습니다. * 방법 *에 대해 아직 배웠습니까? --- [** ** 최소, 완전하고 검증 가능한 예제를 만드는 방법] (http://stackoverflow.com/help/mcve)을 읽어보십시오. – Andreas