如何修復我的tic tac toe遊戲?下面的代碼是我所知道的。它運行,但沒有使用Eclipse霓虹燈Java eclipse tic tac toe遊戲
import java.util.Scanner;
public class TicTacToeGame {
// Declare variables
public char[][] board; //the game board
public boolean xTurn; //if true X's turn, if false O's turn
public Scanner keyboard; //reads user input
public TicTacToeGame() {
xTurn = true;
keyboard = new Scanner(System.in);
//create the board
board = new char[3][3];
//initialize the board
for(int r = 0; r < 3; r++) {
for(int c = 0; c < 3; c++)
board[r][c] = ' ';
}
}
public void displayRow(int row) {
System.out.println(" " + board[row][0] + " | " + board[row][1] + " | " + board[row][2]);
}
//the rest of the game board
public void displayBoard() {
displayRow(0);
System.out.println("-----------");
displayRow(1);
System.out.println("-----------");
displayRow(2);
System.out.println("Which row, column would you like to move to? Enter two numbers between 0-2 separated by a space to indicate position.");
}
public boolean getMove() {
boolean invalid = true;
int row = 0, column = 0;
//user input
while(invalid) {
row = keyboard.nextInt();
column = keyboard.nextInt();
//check for valid spot
if(row >= 0 && row <= 2 && column >= 0 && column <= 2) {
//check that the position is not taken
if(board[row][column] != ' ')
System.out.println("That position is already taken");
else
invalid = false;
}
}
//fill in the game board with the player mark
if(xTurn)
board[row][column] = 'X';
else
board[row][column] = 'O';
return winner(row,column);
}
//check for win
public boolean winner(int lastR, int lastC) {
boolean winner = false; // no winner
char symbol = board[lastR][lastC]; //last made mark
//check left-right
int numFound = 0;
for(int c = 0; c < 3; c++) {
if(board[lastR][c] == symbol)
numFound++;
}
if(numFound == 3)
winner = true;
//check up-down
numFound = 0;
for(int r = 0; r < 3; r++) {
if(board[r][lastC] == symbol)
numFound++;
}
if(numFound == 3)
winner = true;
//check both diagonals
numFound = 0;
for(int i = 0; i < 3; i++) {
if(board[i][i] == symbol)
numFound++;
}
if(numFound == 3)
winner = true;
numFound = 0;
for(int i = 0; i < 3; i++) {
if(board[i][2-i] == symbol)
numFound++;
}
if(numFound == 3)
winner = true;
return winner;
}
public boolean boardFull() {
//how many spots that are taken by each player
int numSpotsFilled = 0;
for(int r = 0; r < 3; r++) {
for(int c = 0; c < 3; c++) {
if(board[r][c] == 'X' || board[r][c] == 'O')
numSpotsFilled++;
}
}
return numSpotsFilled == 9;
}
//start game.
public void play() {
while(true) {
displayBoard();
if(xTurn)
System.out.println("X's Turn!");
else
System.out.println("O's Turn!");
int choice = keyboard.nextInt();
if(choice == 1) {
if(getMove()) {
// winner!
displayBoard(); //display winner board
if(xTurn)
System.out.println("X Wins!");
else
System.out.println("O Wins!");
System.exit(0);
}
else if(boardFull()) {
//tie
displayBoard(); //display tie board
System.out.println("Tie!");
System.exit(0);
}
else {
//no winner
xTurn = !xTurn; //switch players
}
}
}
}
public static void main(String[] args){
TicTacToeGame game = new TicTacToeGame();
game.play();
}
}
請參閱:[爲什麼「某人可以幫我解答問題?」)(https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual - 請說明什麼是不工作的,預期的輸出和實際輸出 – Frakcool
隨意擴展*「它運行但不正確」* – shash678
當它第一次運行板是在那裏,但你必須把col列 –