这是我目前用于tic tac toe的代码,它运行良好,我修复了如何保持得分,但是,我觉得代码不好看和干净。有没有办法让代码更清晰。
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);
}
}
}
}
这有效,但有更有效的方法来解决它吗?
答案 0 :(得分:2)
为了获得游戏的赢家和输家,并给出正确的分数,你必须检查游戏板的状态。解析文本有点复杂,需要大量不必要的工作,所以我建议你以matrix
的方式代表董事会。
示例:强>
char[][] gameBoard = new char[3][3];
这样,当您通过扫描仪接收输入时,就像您的代码一样,您将输入您想要的字符(&#39; O&#39; X&#39;)到gameBoard变量像这样:
// 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'
每个玩家每转一圈后,你会调用以下函数(自己完成代码,查找 java矩阵迭代):
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.
}
答案 1 :(得分:1)
您的程序似乎无法检测到玩家何时获胜。 实现之后,您所要做的就是以整数跟踪每个玩家的得分。当你发现任何一个玩家赢了,增加对应于他的分数的整数。 :)
答案 2 :(得分: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);
}
}
}
这是最有效的代码。我真的可以创造。我相信有更简单的方法可以做到这一点;但是,我认为这仍然非常简洁,几乎没有重复的代码。