Java 提克托游戏。工作不正常
各位,我已经从零开始完成了Tictatcoe游戏的代码,如果任何玩家在水平、垂直或对角的一行中获得相同的值,那么它就可以正常工作。但如果比赛是平局,如以下所示:Java 提克托游戏。工作不正常,java,Java,各位,我已经从零开始完成了Tictatcoe游戏的代码,如果任何玩家在水平、垂直或对角的一行中获得相同的值,那么它就可以正常工作。但如果比赛是平局,如以下所示: X O X O X X O X O Player #2 (O) enter the row and column numbers: 尽管我在count中设置了限制,但代码仍会要求用户输入。用户必须总共输入9个字符,因为TictoE中有9个框。每次用户输入行和列编号时,计数递增1。我的while循环如下所示: while(!(
X O X
O X X
O X O
Player #2 (O) enter the row and column numbers:
尽管我在count
中设置了限制,但代码仍会要求用户输入。用户必须总共输入9
个字符,因为TictoE中有9
个框。每次用户输入行
和列
编号时,计数
递增1
。我的while循环
如下所示:
while(!(count > 9)){
.....................
}
我的while
循环有什么问题吗
import java.util.Scanner;
import java.util.*;
public class TicTacToe {
public static final char[][] theBoard = new char[4][4];
static Scanner kbd = new Scanner(System.in);
public static void main(String[] args){
System.out.println("Hello! Welcome to TicTacToe!");
System.out.println("Player #1 is X! \nPlayer #2 is Y!");
System.out.println();
getBoard();
System.out.println();
playGame();
}
public static void getBoard(){
for(int i = 1; i < theBoard.length; i++){
for(int j = 1; j < theBoard[i].length; j++){
theBoard[i][j] = '_';
System.out.print(theBoard[i][j] + " ");
}
System.out.println();
}
}
public static void playGame(){
int row = 0;
int col = 0;
boolean check = false;
int count = 0;
while(!(count > 9)){
System.out.println();
System.out.println("Player #1 (X) enter the row and column numbers: ");
row = kbd.nextInt();
col = kbd.nextInt();
System.out.println();
count++;
if (row < 0 || col < 0 || row > theBoard.length
|| col > theBoard.length) {
throw new RuntimeException("The numbers should be in range 1-3");
}
else if (theBoard[row][col] != '_') {
throw new RuntimeException("The space already filled!");
}
else {
theBoard[row][col] = 'X';
for (int i = 1; i < theBoard.length; i++) {
for (int j = 1; j < theBoard.length; j++) {
System.out.print(theBoard[i][j] + " ");
}
System.out.println();
}
}
check = checkIfWin();
if(check == true){
break;
}
System.out.println();
System.out.println("Player #2 (O) enter the row and column numbers: ");
row = kbd.nextInt();
col = kbd.nextInt();
System.out.println();
count++;
if (row < 0 || col < 0 || row > theBoard.length
|| col > theBoard.length) {
throw new RuntimeException("The numbers should be in range 1-3");
}
else if (theBoard[row][col] != '_') {
throw new RuntimeException("The space already filled!");
}
else {
theBoard[row][col] = 'O';
for (int i = 1; i < theBoard.length; i++) {
for (int j = 1; j < theBoard.length; j++) {
System.out.print(theBoard[i][j] + " ");
}
System.out.println();
}
}
check = checkIfWin();
if(check == true){
break;
}
}
System.out.println("The game is tie!");
}
private static boolean checkIfWin(){
boolean result = false;
int countX = 0;
int countO = 0;
//Check 1st line
for(int i = 1; i < theBoard.length; i++){
if(theBoard[1][i] == 'X'){
countX++;
}
else if(theBoard[1][i] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 2nd line
for(int i = 1; i < theBoard.length; i++){
if(theBoard[2][i] == 'X'){
countX++;
}
else if(theBoard[2][i] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 3d line
for(int i = 1; i < theBoard.length; i++){
if(theBoard[3][i] == 'X'){
countX++;
}
else if(theBoard[3][i] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 1s column
for(int i = 1; i < theBoard.length; i++){
if(theBoard[i][1] == 'X'){
countX++;
}
else if(theBoard[i][1] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 2nd column
for(int i = 1; i < theBoard.length; i++){
if(theBoard[i][2] == 'X'){
countX++;
}
else if(theBoard[i][2] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 3d column
for(int i = 1; i < theBoard.length; i++){
if(theBoard[i][3] == 'X'){
countX++;
}
else if(theBoard[i][3] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
//Check first diagonal
if(theBoard[1][1] == 'X' && theBoard[2][2] == 'X' && theBoard[3][3] == 'X'){
result = true;
System.out.println("The Player #1 (X) Wins!");
}
else if(theBoard[1][1] == 'O' && theBoard[2][2] == 'O' && theBoard[3][3] == 'O'){
result = true;
System.out.println("The Player #2 (O) Wins!");
}
// Check first diagonal
if (theBoard[1][3] == 'X' && theBoard[2][2] == 'X'&& theBoard[3][1] == 'X'){
result = true;
System.out.println("The Player #1 (X) Wins!");
} else if (theBoard[1][3] == 'O' && theBoard[2][2] == 'O'&& theBoard[3][1] == 'O') {
result = true;
System.out.println("The Player #2 (O) Wins!");
}
return result;
}
}
import java.util.Scanner;
导入java.util.*;
公共类Tictatcoe{
公共静态最终字符[][]董事会=新字符[4][4];
静态扫描仪kbd=新扫描仪(System.in);
公共静态void main(字符串[]args){
System.out.println(“你好!欢迎来到Tictatcoe!”);
System.out.println(“玩家1是X!\n玩家2是Y!”);
System.out.println();
getBoard();
System.out.println();
游戏();
}
公共静态无效getBoard(){
对于(int i=1;i9)){
System.out.println();
System.out.println(“播放器#1(X)输入行和列编号:”;
row=kbd.nextInt();
col=kbd.nextInt();
System.out.println();
计数++;
如果(行<0 | |列<0 | |行>线路板长度
||col>theBoard.长度){
抛出新的RuntimeException(“数字应该在1-3范围内”);
}
否则如果(董事会[行][col]!=''.'){
抛出新的RuntimeException(“空间已满!”);
}
否则{
董事会[行][col]=“X”;
对于(int i=1;i线路板长度
||col>theBoard.长度){
抛出新的RuntimeException(“数字应该在1-3范围内”);
}
否则如果(董事会[行][col]!=''.'){
抛出新的RuntimeException(“空间已满!”);
}
否则{
董事会[行][col]=“O”;
对于(int i=1;i check = checkIfWin();
if((check == true) || (count >= 8)){
break;
}
package tictactoe;
import java.util.Scanner;
import java.util.*;
public class TicTacToe {
public static final char[][] theBoard = new char[4][4];
static Scanner kbd = new Scanner(System.in);
public static void main(String[] args){
System.out.println("Hello! Welcome to TicTacToe!");
System.out.println("Player #1 is X! \nPlayer #2 is Y!");
System.out.println();
getBoard();
System.out.println();
playGame();
}
public static void getBoard(){
for(int i = 1; i < theBoard.length; i++){
for(int j = 1; j < theBoard[i].length; j++){
theBoard[i][j] = '_';
System.out.print(theBoard[i][j] + " ");
}
System.out.println();
}
}
public static void playGame(){
int row = 0;
int col = 0;
boolean check = false;
int count = 0;
//change this
while(count <= 8){
count++;
System.out.println();
System.out.println("Player #1 (X) enter the row and column numbers: ");
row = kbd.nextInt();
col = kbd.nextInt();
System.out.println();
if (row < 0 || col < 0 || row > theBoard.length
|| col > theBoard.length) {
throw new RuntimeException("The numbers should be in range 1-3");
}
else if (theBoard[row][col] != '_') {
throw new RuntimeException("The space already filled!");
}
else {
theBoard[row][col] = 'X';
for (int i = 1; i < theBoard.length; i++) {
for (int j = 1; j < theBoard.length; j++) {
System.out.print(theBoard[i][j] + " ");
}
System.out.println();
}
}
check = checkIfWin();
//change this
if((check == true) || (count >= 8)){
break;
}
System.out.println();
System.out.println("Player #2 (O) enter the row and column numbers: ");
row = kbd.nextInt();
col = kbd.nextInt();
System.out.println();
count++;
if (row < 0 || col < 0 || row > theBoard.length
|| col > theBoard.length) {
throw new RuntimeException("The numbers should be in range 1-3");
}
else if (theBoard[row][col] != '_') {
throw new RuntimeException("The space already filled!");
}
else {
theBoard[row][col] = 'O';
for (int i = 1; i < theBoard.length; i++) {
for (int j = 1; j < theBoard.length; j++) {
System.out.print(theBoard[i][j] + " ");
}
System.out.println();
}
}
check = checkIfWin();
if(check == true){
break;
}
}
System.out.println("The game is tie!");
}
private static boolean checkIfWin(){
boolean result = false;
int countX = 0;
int countO = 0;
//Check 1st line
for(int i = 1; i < theBoard.length; i++){
if(theBoard[1][i] == 'X'){
countX++;
}
else if(theBoard[1][i] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 2nd line
for(int i = 1; i < theBoard.length; i++){
if(theBoard[2][i] == 'X'){
countX++;
}
else if(theBoard[2][i] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 3d line
for(int i = 1; i < theBoard.length; i++){
if(theBoard[3][i] == 'X'){
countX++;
}
else if(theBoard[3][i] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 1s column
for(int i = 1; i < theBoard.length; i++){
if(theBoard[i][1] == 'X'){
countX++;
}
else if(theBoard[i][1] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 2nd column
for(int i = 1; i < theBoard.length; i++){
if(theBoard[i][2] == 'X'){
countX++;
}
else if(theBoard[i][2] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 3d column
for(int i = 1; i < theBoard.length; i++){
if(theBoard[i][3] == 'X'){
countX++;
}
else if(theBoard[i][3] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
//Check first diagonal
if(theBoard[1][1] == 'X' && theBoard[2][2] == 'X' && theBoard[3][3] == 'X'){
result = true;
System.out.println("The Player #1 (X) Wins!");
}
else if(theBoard[1][1] == 'O' && theBoard[2][2] == 'O' && theBoard[3][3] == 'O'){
result = true;
System.out.println("The Player #2 (O) Wins!");
}
// Check first diagonal
if (theBoard[1][3] == 'X' && theBoard[2][2] == 'X'&& theBoard[3][1] == 'X'){
result = true;
System.out.println("The Player #1 (X) Wins!");
} else if (theBoard[1][3] == 'O' && theBoard[2][2] == 'O'&& theBoard[3][1] == 'O') {
result = true;
System.out.println("The Player #2 (O) Wins!");
}
return result;
}
}