gpt4 book ai didi

java - 当我运行我的程序时没有任何显示

转载 作者:行者123 更新时间:2023-12-01 09:07:43 25 4
gpt4 key购买 nike

我只是 Java oop 的初学者,我真的需要帮助,我想制作一个 Tic-Tac-Toe 游戏。没有错误,但它没有向我显示任何内容。控制台工作正常,因为我尝试了其他方法,但我真的不知道问题出在哪里。如果我的英语有任何错误,我很抱歉,这不是我的母语。

谢谢。

package tictacto;

public class Jeu {

public static void main(String[] args) {
Board grille = new Board();
Joueur player = new Joueur("cc");
boolean test;
//char c;
int i = 0;
int[] tab;

test = grille.checkBoard();
System.out.println(test);
player.choixSigne();
//c = player.getSigne();
while ((test == false) && (i < 9)) {
grille.afficheBoard();
player.seDeplacer();
tab = player.getMove();
grille.InsererMouvement(tab);
i++;
test = grille.checkBoard();
}

}

}

...

package tictacto;

import java.util.Scanner;

public class Joueur {
// attributes

protected final String pseudo;
protected int score;
private char signe;
private int[] move = new int[2];
// méthodes
Scanner sc = new Scanner(System.in);

Joueur(String psd) {
this.signe = ' ';
this.pseudo = psd;
this.score = 0;
this.move[0] = 0;
this.move[1] = 0;
}

public void choixSigne() {
String s = new String();
boolean test = false;
while (test == false) {
System.out.println("veuillez choisir votre signe");
String sgne = sc.nextLine();
char c = sgne.charAt(0);
s = c + "";
if (s.equals('X') || s.equals('O')) {
test = true;
System.out.println("Le Choix de signe est effectué avec succes ");
signe = s.charAt(0);
} else {
test = false;
System.out.println("Veuillez refaire votre choix");
}
}

}

public char getSigne() {
return signe;
}

public int[] getMove() {
return move;
}

public void seDeplacer() {
System.out.println("Veuillez choisir la ligne");
int a = sc.nextInt();
move[0] = a;
System.out.println("Veuillez choisir la colonne");
int b = sc.nextInt();
move[1] = b;

}

}

..

package tictacto;

class Board {

public char[][] Board = new char[3][3];
protected Joueur J;

Board() {
for (int k = 0; k <= 2; k++) {
for (int j = 0; j <= 2; j++) {
Board[k][j] = ' ';
}
}

}

public void afficheBoard() {

System.out.println("\t The Board");
System.out.print("\n");
for (int i = 0; i <= 2; i++) {

System.out.print("\t |");
System.out.print(Board[0][i]);
System.out.print("|");
System.out.print(Board[1][i]);
System.out.print("|");
System.out.print(Board[2][i]);
System.out.print("|");
System.out.print("\n");

}
}

// Ajouter un mouvement à la grille
public boolean InsererMouvement(int[] tab) {
if (Board[tab[0]][tab[1]] == ' ') {
Board[tab[0]][tab[1]] = J.getSigne();
System.out.println("Next move");
return (true);
} else {
System.out.println("Try again");
}
return false;
}

public char[][] getBoard() {
return Board;
}

// Check Board
public boolean checkBoard() {
boolean a = false;
boolean b = false;
// vérifier les lignes
while (a == false) {
for (int i = 0; i <= 2; i++) {
if ((Board[i][0] == 'X') && (Board[i][1] == 'X') && (Board[i][2] == 'X') || (Board[i][0] == 'O') && (Board[i][1] == 'O') && (Board[i][2] == 'O')
|| ((Board[0][i] == 'X') && (Board[1][i] == 'X') && (Board[2][i] == 'X')
|| (Board[0][i] == 'O') && (Board[1][i] == 'O') && (Board[2][i] == 'O'))) {
a = true;
} else {
a = false;
}
}
}
// vérifier les diagonales

if ((Board[0][0] == 'X') && (Board[1][1] == 'X') && (Board[2][2] == 'X')
|| (Board[0][0] == 'O') && (Board[1][1] == 'O') && (Board[2][2] == 'O')
|| (Board[2][0] == 'X') && (Board[1][1] == 'X') && (Board[0][2] == 'X')
|| (Board[2][0] == 'O') && (Board[1][1] == 'O') && (Board[0][2] == 'O')) {
b = true;
} else {
b = false;
}
if (a || b) {
return (true);
} else {
return (false);
}
}

}

最佳答案

你的程序中似乎有很多错误。准确定位它们是一项艰巨的工作。因此,我建议您一定要注意我为解决问题所做的更改。您也可以对其进行改进,以使您的程序变得更好。

package tictacto;

public class Jeu {

public static void main(String[] args) {
Board grille = new Board();
Joueur player = new Joueur("cc");
boolean test = false;
//char c;
int i = 0;
int[] tab;

//test = grille.checkBoard();
System.out.println(test);
player.choixSigne();
//c = player.getSigne();
while ((test == false) && (i < 9)) {
grille.afficheBoard();
player.seDeplacer();
tab = player.getMove();
grille.InsererMouvement(tab,player);
i++;
test = grille.checkBoard();
}

}

}

...

package tictacto;

class Board {

public char[][] Board = new char[3][3];
//protected Joueur J = new Joueur("cc");

Board() {
for (int k = 0; k <= 2; k++) {
for (int j = 0; j <= 2; j++) {
Board[k][j] = ' ';
}
}

}

public void afficheBoard() {

System.out.println("\t The Board");
System.out.print("\n");
for (int i = 0; i <= 2; i++) {

System.out.print("\t |");
System.out.print(Board[0][i]);
System.out.print("|");
System.out.print(Board[1][i]);
System.out.print("|");
System.out.print(Board[2][i]);
System.out.print("|");
System.out.print("\n");

}
}

// Ajouter un mouvement à la grille
public boolean InsererMouvement(int[] tab,Joueur J) {
if (Board[tab[0]][tab[1]] == ' ') {
Board[tab[0]][tab[1]] = J.getSigne();

System.out.println("Next move");
return true;
} else {
System.out.println("Try again");
}
return false;
}

public char[][] getBoard() {
return Board;
}

// Check Board
public boolean checkBoard() {
boolean a = false;
boolean b = false;
// vérifier les lignes
for (int i = 0; i <= 2; i++) {
if ((Board[i][0] == 'X') && (Board[i][1] == 'X') && (Board[i][2] == 'X') || (Board[i][0] == 'O') && (Board[i][1] == 'O') && (Board[i][2] == 'O')
|| ((Board[0][i] == 'X') && (Board[1][i] == 'X') && (Board[2][i] == 'X')
|| (Board[0][i] == 'O') && (Board[1][i] == 'O') && (Board[2][i] == 'O'))) {

a = true;
} else {
a = false;
}
}

// vérifier les diagonales
if ((Board[0][0] == 'X') && (Board[1][1] == 'X') && (Board[2][2] == 'X')
|| (Board[0][0] == 'O') && (Board[1][1] == 'O') && (Board[2][2] == 'O')
|| (Board[2][0] == 'X') && (Board[1][1] == 'X') && (Board[0][2] == 'X')
|| (Board[2][0] == 'O') && (Board[1][1] == 'O') && (Board[0][2] == 'O')) {

b = true;
} else {
b = false;
}

if (a || b) {
return (true);
} else {
return (false);
}
}

}

...

package tictacto;

import java.util.Scanner;

public class Joueur {
// attributes

protected final String pseudo;
protected int score;
private char signe;
private int[] move = new int[2];
// méthodes
Scanner sc = new Scanner(System.in);

Joueur(String psd) {
this.signe = ' ';
this.pseudo = psd;
this.score = 0;
this.move[0] = 0;
this.move[1] = 0;
}

public void choixSigne() {
String s = new String();
boolean test = false;
while (test == false) {
System.out.println("veuillez choisir votre signe");
String sgne = sc.nextLine();
char c = sgne.charAt(0);
s = c + "";
if (s.equals("X") || s.equals("O")) {
test = true;
System.out.println("Le Choix de signe est effectué avec succes ");
signe = s.charAt(0);
} else {
test = false;
System.out.println("Veuillez refaire votre choix");
}
}

}

public char getSigne() {
return signe;
}

public int[] getMove() {
return move;
}

public void seDeplacer() {
System.out.println("Veuillez choisir la ligne");
int a = sc.nextInt();
move[0] = a;
System.out.println("Veuillez choisir la colonne");
int b = sc.nextInt();
move[1] = b;

}

}

关于java - 当我运行我的程序时没有任何显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41130210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com