Java Tic Tac Toe,打印3块板
我在Tic-Tac-Toe Java程序的代码中遇到了挫折。每次我运行它时,游戏板会打印3次,第二次打印o,X都会填写一行,表示玩家X获胜。我一直在想办法,但还是找不到问题所在。我想问的是,如果代码打印出的内容超过了需要的数量,并且停止填充行,那么主要的问题是什么 我希望我的格式正确Java Tic Tac Toe,打印3块板,java,tic-tac-toe,Java,Tic Tac Toe,我在Tic-Tac-Toe Java程序的代码中遇到了挫折。每次我运行它时,游戏板会打印3次,第二次打印o,X都会填写一行,表示玩家X获胜。我一直在想办法,但还是找不到问题所在。我想问的是,如果代码打印出的内容超过了需要的数量,并且停止填充行,那么主要的问题是什么 我希望我的格式正确 import java.util.Scanner; public class TicTacToeWork { //Variable declaration. public static char
import java.util.Scanner;
public class TicTacToeWork {
//Variable declaration.
public static char[][] GameBoard = new char[3][3];
public static Scanner keyboard = new Scanner(System.in);
public static int column, row;
public static char PlayerTurn = 'X';
public static void main(String args[]) {
for (int i = 0; i > 3; i++) {
for (int j = 0; j < 3; j++) {
GameBoard[i][j] = '_';
}
}
System.out.println("Enter coordinates of row then column to choose your space.");
Playing();
}
public static void Playing() {
boolean PlayerPlaying = true;
while (PlayerPlaying) {
boolean playing = true;
while (playing) {
row = keyboard.nextInt() - 1;
column = keyboard.nextInt() - 1;
GameBoard[row][column] = PlayerTurn;
if (EndGame(row, column)) {
playing = false;
System.out.println("Game over player " + PlayerTurn + " wins!");
}
BoardPrint();
if (PlayerTurn == 'X') {
PlayerTurn = 'O';
} else {
PlayerTurn = 'X';
}
}
}
}
public static void BoardPrint() {
//Print out the game board.
for (int i = 0; i < GameBoard.length; i++) {
for (int n = 0; n < 3; n++) {
System.out.println();
for (int j = 0; j < 3; j++) {
if (j == 0) {
System.out.print("| ");
}
System.out.print(GameBoard[i][j] + " | ");
}
}
System.out.println();
}
System.out.println();
}
public static boolean EndGame(int RowMove, int ColumnMove) {
//Deciding factors on who wins and ties.
if (GameBoard[0][ColumnMove] == GameBoard[1][ColumnMove]
&& GameBoard[1][ColumnMove] == GameBoard[2][ColumnMove]) {
return true;
}
if (GameBoard[RowMove][0] == GameBoard[1][RowMove]
&& GameBoard[RowMove][0] == GameBoard[RowMove][2]) {
return true;
}
if (GameBoard[0][0] == GameBoard[1][1] && GameBoard[0][0] == GameBoard[2][2]
&& GameBoard[1][1] != '_') {
return true;
}
if (GameBoard[0][2] == GameBoard[1][1] && GameBoard[0][2] == GameBoard[2][0]
&& GameBoard[1][1] != '_') {
return true;
} else {
return false;
}
}
}
import java.util.Scanner;
公开课{
//变量声明。
公共静态字符[][]游戏板=新字符[3][3];
公共静态扫描仪键盘=新扫描仪(System.in);
公共静态int列,行;
publicstaticcharplayerturn='X';
公共静态void main(字符串参数[]){
对于(int i=0;i>3;i++){
对于(int j=0;j<3;j++){
游戏板[i][j]='";
}
}
System.out.println(“输入行坐标,然后输入列坐标以选择您的空间。”);
玩();
}
公共静态无效播放(){
布尔PlayerPlaying=true;
while(PlayerPlaying){
布尔播放=真;
玩的时候{
行=键盘.nextInt()-1;
column=keyboard.nextInt()-1;
GameBoard[行][列]=玩家翻转;
if(结束游戏(行、列)){
玩=假;
System.out.println(“游戏结束玩家”+PlayerTurn+wins!”);
}
BoardPrint();
如果(PlayerTurn='X'){
PlayerTurn='O';
}否则{
PlayerTurn='X';
}
}
}
}
公共静态无效BoardPrint(){
//打印出游戏板。
对于(int i=0;i
您的BoardPrint方法完全错误。你必须这样做
public static void BoardPrint() {
System.out.println("-------------");
for (int i = 0; i < 3; i++) {
System.out.print("| ");
for (int j = 0; j < 3; j++) {
System.out.print(GameBoard[i][j] + " | ");
}
System.out.println();
System.out.println("-------------");
}
publicstaticvoidboardprint(){
System.out.println(“--------------”;
对于(int i=0;i<3;i++){
系统输出打印(“|”);
对于(int j=0;j<3;j++){
System.out.print(GameBoard[i][j]+“|”);
}
System.out.println();
System.out.println(“--------------”;
}
您还必须重新检查以获得胜利
一点提示。您没有正确使用布尔方法。这一行
if(EndGame(row,column))
表示如果EndGame为true,则停止游戏。您设置方法的方式没有正确执行检查。您必须说“if not true”-因此它看起来像if(!EndGame(row,column))
还有一些其他错误,但这将为您完成项目提供一个良好的开端。您的问题是什么?我们不是来调试您的代码的。请阅读帮助: