好吧所以我必须制作一个随机的tictactoe检查器,显示x胜出,o胜,以及何时出现平局。所以我遇到的问题是它不会显示关系,有时会说x或o赢了,否则就赢了。我不知道在我的代码中要改变什么,因为在我进行对角线检查之前,它会打印出关系。这是整个代码,但我很确定问题是来自检查板部分,使xWin和oWin出现真实我无法找到它在那做什么。
package test;
import java.util.Scanner;
import java.util.Random;
public class TicTacToe {
public static void main(String[] args) {
System.out.println("Welcome to random Tic Tac Toe Checker. Let's see our randomly generated board.");
int dimension = 3;
char[][] board = new char[dimension][dimension];
Random r = new Random();
for (int i = 0; i < 3; i++) // filling board
{
for (int j = 0; j < 3; j++) {
int choice = r.nextInt(2);
if (choice == 0) {
board[i][j] = 'X';
} else if (choice == 1) {
board[i][j] = 'O';
}
}
}
for (int i = 0; i < 3; i++) // filling board
{
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
boolean xWin = false;// checking board, order horizontal,vertical,left
// and right diagonal
boolean oWin = false;
for (int i = 0; i < 3; i++) {
boolean lineWin = true;
for (int j = 0; j < 3; j++) {
if (board[i][j] != board[i][0]) {
lineWin = false;
}
}
if (lineWin == true) {
if (board[i][0] == 'X') {
xWin = false;
}
if (board[i][0] == 'O') {
oWin = false;
}
}
}
for (int j = 0; j < 3; j++) {
boolean lineWin = true;
for (int i = 0; i < 3; i++) {
if (board[i][j] != board[0][j]) {
lineWin = true;
}
}
if (lineWin == true) {
if (board[0][j] == 'X') {
xWin = true;
}
if (board[0][j] == 'O') {
oWin = true;
}
}
}
boolean lineWin = true;
for (int i = 0; i < 3; i++) {
if (board[0][0] != board[i][i]) {
lineWin = false;
}
if (lineWin == true) {
if (board[0][0] == 'X') {
xWin = true;
}
if (board[0][0] == 'O') {
oWin = true;
}
}
}
lineWin = true;
for (int i = 0; i < 3; i++) {
if (board[0][0] != board[i][2 - i]) {
lineWin = false;
}
if (lineWin == true) {
if (board[0][0] == 'X') {
xWin = true;
}
if (board[0][0] == 'O') {
oWin = true;
}
}
}
if (xWin == false && oWin == false)// printing winners
{
System.out.println("CAT!It's a tie no one wins");
}
if (xWin == true) {
System.out.println("X wins!");
}
if (oWin == true) {
System.out.println("O wins!");
}
}
}
答案 0 :(得分:2)
我修正了布尔错误以及对角线逻辑:
import java.util.Random;
public class TicTacToe {
private static final int DIMENSION = 3;
public static void main(String[] args) {
System.out.println("Welcome to random Tic Tac Toe Checker. Let's see our randomly generated board.");
char[][] board = new char[DIMENSION][DIMENSION];
final Random r = new Random();
for (int i = 0; i < DIMENSION; i++) {
for (int j = 0; j < DIMENSION; j++) {
int choice = r.nextInt(2);
if (choice == 0) {
board[i][j] = 'X';
} else if (choice == 1) {
board[i][j] = 'O';
}
}
}
for (int i = 0; i < DIMENSION; i++) {
for (int j = 0; j < DIMENSION; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
boolean xWin = false;
boolean oWin = false;
for (int i = 0; i < DIMENSION; i++) {
boolean lineWin = true;
for (int j = 0; j < DIMENSION; j++) {
if (board[i][j] != board[i][0]) {
lineWin = false;
}
}
if (lineWin) {
if (board[i][0] == 'X') {
xWin = true;
}
if (board[i][0] == 'O') {
oWin = true;
}
}
}
for (int j = 0; j < DIMENSION; j++) {
boolean lineWin = true;
for (int i = 0; i < DIMENSION; i++) {
if (board[i][j] != board[0][j]) {
lineWin = false;
}
}
if (lineWin) {
if (board[0][j] == 'X') {
xWin = true;
}
if (board[0][j] == 'O') {
oWin = true;
}
}
}
boolean lineWin = true;
for (int i = 0; i < DIMENSION; i++) {
if (board[0][0] != board[i][i]) {
lineWin = false;
}
}
if (lineWin) {//this check should not be in the loop
if (board[0][0] == 'X') {
xWin = true;
}
if (board[0][0] == 'O') {
oWin = true;
}
}
lineWin = true;
for (int i = 0; i < DIMENSION; i++) {
if (board[DIMENSION - 1][0] != board[i][DIMENSION - 1 - i]) {
lineWin = false;
}
}
if (lineWin) {//this check should not be in the loop
if (board[DIMENSION - 1][0] == 'X') {
xWin = true;
}
if (board[DIMENSION - 1][0] == 'O') {
oWin = true;
}
}
if (xWin == true && oWin == true) {//printing winners
System.out.println("Both players won!");
}
if (xWin == false && oWin == false) {
System.out.println("CAT!It's a tie no one wins");
}
if (xWin == true) {
System.out.println("X wins!");
}
if (oWin == true) {
System.out.println("O wins!");
}
}
}
注意:您可以增加DIMENSION以获得笑声。
答案 1 :(得分:0)
在第一个区块
if(lineWin == true)
{
if(board[i][0] == 'X')
{
xWin = false;
}
if(board[i][0] == 'O')
{
oWin = false;
}
}
应更改如下。
if(lineWin == true)
{
if(board[i][0] == 'X')
{
xWin = true;
}
if(board[i][0] == 'O')
{
oWin = true;
}
}
此外,在第二个块中,部分
if(board[i][j] != board[0][j])
{
lineWin = true;
}
应更改如下。
if(board[i][j] != board[0][j])
{
lineWin = false;
}
答案 2 :(得分:0)
我认为此文件中的主要问题出现在中间位置,特别是第34-66行,您可以对相同字符的水平和垂直行进行初始检查。
for (int i = 0; i < 3; i++) {
boolean lineWin = true;
for (int j = 0; j < 3; j++) {
if (board[i][j] != board[i][0]) {
lineWin = false;
}
}
if (lineWin == true) {
if (board[i][0] == 'X') {
xWin = false; //THESE LINES SHOULD BE CHANGING THE WIN CONDITION TO TRUE
}
if (board[i][0] == 'O') {
oWin = false; //LISTED ABOVE
}
}
}
for (int j = 0; j < 3; j++) {
boolean lineWin = true;
for (int i = 0; i < 3; i++) {
if (board[i][j] != board[0][j]) {
lineWin = true; //THIS LINE SHOULD BE FALSE
}
}
if (lineWin == true) {
if (board[0][j] == 'X') {
xWin = true;
}
if (board[0][j] == 'O') {
oWin = true;
}
}
我列出了一些修改代码并使其按照您希望的方式运行的更改。看起来在错误的时间将布尔值声明为true
或false
时会出现一些错误,因此应该可以解决问题。