伙计们,我已经从头开始完成了TicTacToe游戏的代码,如果任何一个玩家在一条线上水平或垂直或对角地获得相同的值,它就可以正常工作。但是,如果比赛是关系如下:
X O X
O X X
O X O
Player #2 (O) enter the row and column numbers:
尽管我已经在count
设置了限制,但代码仍会询问用户输入。用户必须输入总共9
个输入字符,因为TicTacToe中有9
个框。每当用户输入row
和col
时,count
增加1
个增量。我的while-loop
看起来像这样:
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;
}
}
答案 0 :(得分:2)
你的问题源于你给每个玩家都有机会的事实。您需要在循环中间添加另一个检查。在Tic Tac Toe,并不是每个玩家都获得相同的转弯,所以你在每个人都去之后检查,而不是在游戏完成时。
你需要另一张支票(在我的代码中是第67行):
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;
}
}
作为旁注,我通过使用调试器并单步执行程序来发现此错误。