基本上我正在尝试用java创建一个tic tac toe游戏,而在choiceInputter方法中,似乎grid [row] [column]不想更改此方法中的值。请帮忙。我不知道为什么它不起作用。
编辑:我修好了。
import java.util.*;
public class TicTacToe
{
public static void main (String args[]){
String [][] gameGrid = new String [3][3];
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int counter = 0; //checks whether game is at beginning or not
boolean check = true;
boolean player = true; //decides who's turn it is. Player 1 = true. Player 2 = false.
boolean choice = false; //false for O, true for X
String pick = new String(" "); //selects the player's piece
String input = new String(" "); //placement/instruction/done choice for player 1 AND player 2
String piece1 = new String(" ");
String piece2 = new String(" ");
sleep(500);
System.out.println("Welcome to Tic-Tac-Toe! **Java Edition**");
sleep(1000);
//Player X or O start
System.out.println("\nPlayer 1, would you like to be X or O?");
pick = scan.nextLine();
do {
if(pick.equalsIgnoreCase("x")) {
choice = true;
piece1 = "X";
piece2 = "O";
sleep(750);
typedString(30,"\nPlayer 1, you are Xs.");
sleep(500);
typedString(30,"Player 2, you are Os.");
check = true;
}
else if(pick.equalsIgnoreCase("o")){
choice = false;
piece1 = "O";
piece2 = "X";
sleep(500);
typedString(30,"\nPlayer 1, you are Os.");
sleep(500);
typedString(30,"Player 2, you are Xs.");
check = true;
}
else {
sleep(500);
System.out.println("\nThat is not a valid answer.");
sleep(500);
typedString(25,"Please choose again: X or O?");
check = false;
}
sleep(750);
System.out.println("\nThe game will now begin...");
} while(check == false);
//Player X or O end
sleep(2000);
typedString(25,"\nCreating new Tic-Tac-Toe grid...");
blankGrid(gameGrid);
sleep(1000);
typedString(25,"Finding instructions to game...");
sleep(1000);
//Game start
while(!input.equalsIgnoreCase("done")){
if(counter == 0) {
System.out.println("\nGame has started!");
sleep(1000);
typedString(50, "\nFor instructions on how to play type help.\n");
}
//help
else if(input.equalsIgnoreCase("help")) {
gameInstructions();
input = " ";
if(player) {
player = false;
}
else {
player = true;
}
}
else{
//player 1's go
if(player) {
sleep(750);
System.out.println("Player 1's turn. Choose a place to put your " + piece1 + ".");
chooseGrid(gameGrid);
input = scan.nextLine();
choiceInputter(gameGrid, input, player, piece1, piece2);
sleep(750);
player = false;
/* sleep(500);
System.out.print("That is not a valid answer!\n");
sleep(500);
System.out.println("Remember you can always type 'help' if you don't know what to do.");
sleep(750);
input = " ";
*/
}
//player 2's go
else if(!player){
sleep(750);
System.out.println("Player 2's turn. Choose a place to put your " + piece2 + ".");
chooseGrid(gameGrid);
input = scan.nextLine();
choiceInputter(gameGrid, input, player, piece1, piece2);
sleep(750);
player = true;
/*
sleep(500);
System.out.print("That is not a valid answer!\n");
sleep(500);
System.out.println("Remember you can always type 'help' if you don't know what to do.");
sleep(750);
input = " ";*/
}
displayGrid(gameGrid);
}
sleep(1000);
counter++;
}
}
//Game end
/*
*blankGrid: creates a blank Tic-Tac-Toe array template.
*/
public static void blankGrid(String[][] grid) {
sleep(500);
for(int row = 0;row<grid.length;row++) {
for(int column = 0;column<grid[row].length;column++) {
grid[row][column] = " ";
}
}
}
/*
*displayGrid: Prints the current game grid at the end of the turn.
*/
public static void displayGrid(String[][] grid) {
sleep(500);
for(int row = 0;row<grid.length;row++) {
for(int column = 0;column<grid[row].length;column++) {
//not the last column and not X or O
if(column<grid[row].length-1) {
if((!grid[row][column].equalsIgnoreCase("X"))||(!grid[row][column].equalsIgnoreCase("O"))){
grid[row][column] = " ";
System.out.print(" " + grid[row][column] + " |");
}
else{
System.out.print(" " + grid[row][column] + " |");
}
}
else{
if(!grid[row][column].equalsIgnoreCase("X")||!grid[row][column].equalsIgnoreCase("O")){
grid[row][column] = " ";
System.out.print(" " + " " + " \n");
}
else{
System.out.print(" " + grid[row][column] + " \n");
}
}
}
if(row<grid.length-1){
System.out.println("-----------");
}
else{
}
}
}
/*
*chooseGrid: Prints the game grid with number locations for the Player to pick.
*/
public static void chooseGrid(String[][] grid) {
sleep(500);
int location = 1;
for(int row = 0;row<grid.length;row++) {
for(int column = 0;column<grid[row].length;column++) {
//not the last column and not X or O
if(column<grid[row].length-1) {
if(!grid[row][column].equalsIgnoreCase("X")||!grid[row][column].equalsIgnoreCase("O")){
grid[row][column] = Integer.toString(location);
System.out.print(" " + grid[row][column] + " |");
}
else{
System.out.print(" " + grid[row][column] + " |");
}
location++;
}
else{
if(!grid[row][column].equalsIgnoreCase("X")||!grid[row][column].equalsIgnoreCase("O")){
grid[row][column] = Integer.toString(location);
System.out.print(" " + grid[row][column] + " \n");
}
else{
System.out.print(" " + grid[row][column] + " \n");
}
location++;
}
}
if(row<grid.length-1){
System.out.println("-----------");
}
else{
}
}
}
/*
*gameInstructions: Prints the game instructions if the Player inputs help
*/
public static void gameInstructions() {
String instruct = new String("To pick the place you want your piece to go type the number at the location.\nUnfortunately, your actions can not be undone.\nThis game is multi-player.\nRemember its 'done' to end the game.");
sleep(500);
typedString(50,"\nInstructions: ");
sleep(750);
typedString(50,instruct);
sleep(750);
typedString(30," Have fun! :D\n");
}
/*
*sleep: makes the console wait depending on the amount of miliseconds chosen
*/
public static void sleep(long milis) {
try
{
Thread.sleep(milis);
}
catch(Exception ee) {}
}
/*
*typedString: Prints a string as if it was being typed live
*/
public static void typedString(long time, String sentence) {
for(int i = 0;i<sentence.length();i++){
System.out.print(sentence.charAt(i));
sleep(time);
}
System.out.println("");
}
/*
*choiceInputter: Inputs the player's choice into the grid/array
*/
public static void choiceInputter(String[][] grid, String choice,boolean player, String piece1, String piece2) {
if(player) {
for(int row = 0;row<grid.length;row++) {
for(int column = 0;column<grid[row].length;column++) {
if(grid[row][column].equalsIgnoreCase(choice)) {
System.out.println("I'm working");
grid[row][column] = piece1;
}
}
}
}
else {
for(int row = 0;row<grid.length;row++) {
for(int column = 0;column<grid[row].length;column++) {
if(grid[row][column].equalsIgnoreCase(choice)) {
System.out.println("I'm working");
grid[row][column] = piece2;
}
}
}
}
}
}