Java 在方法中更改二维数组中的变量
基本上,我试图用Java制作一个tic-tac-toe游戏,在ChoiceInput方法中,grid[row][column]似乎不想在这个方法中更改值。我不知道为什么它不起作用Java 在方法中更改二维数组中的变量,java,string,methods,tic-tac-toe,jagged-arrays,Java,String,Methods,Tic Tac Toe,Jagged Arrays,基本上,我试图用Java制作一个tic-tac-toe游戏,在ChoiceInput方法中,grid[row][column]似乎不想在这个方法中更改值。我不知道为什么它不起作用 import java.util.*; public class TicTacToe { public static void main (String args[]){ String [][] gameGrid = new String [3][3]; Scanner scan = new Scan
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;
}
}
}
}
}
}
import java.util.*;
公共类Tictatcoe
{
公共静态void main(字符串参数[]){
字符串[][]gameGrid=新字符串[3][3];
扫描仪扫描=新扫描仪(System.in);
Random rand=新的Random();
int counter=0;//检查游戏是否在开始
布尔检查=真;
布尔player=true;//决定轮到谁。player 1=true。player 2=false。
布尔选择=false;//O为false,X为true
String pick=new String(“”;//选择演奏者的乐曲
字符串输入=新字符串(“”;//播放器1和播放器2的位置/指令/完成选择
字符串片段1=新字符串(“”);
字符串片段2=新字符串(“”);
睡眠(500);
System.out.println(“欢迎来到Tic-Tac-Toe!**Java版**”);
睡眠(1000);
//玩家X或O开始
System.out.println(“\n第1层,您想成为X还是O?”);
pick=scan.nextLine();
做{
if(拾取相等信号案例(“x”)){
选择=正确;
piece1=“X”;
分段2=“O”;
睡眠(750);
类型字符串(30,“\n第1层,您是Xs”);
睡眠(500);
键入字符串(30,“玩家2,你就是操作系统”);
检查=正确;
}
else if(选择相等信号案例(“o”)){
选择=错误;
1=“O”;
piece2=“X”;
睡眠(500);
typedString(30,“\n第1层,您是操作系统”);
睡眠(500);
键入字符串(30,“玩家2,你是Xs”);
检查=正确;
}
否则{
睡眠(500);
System.out.println(“\n这不是有效答案。”);
睡眠(500);
键入字符串(25,“请再次选择:X还是O?”);
检查=错误;
}
睡眠(750);
System.out.println(“\n游戏现在将开始…”);
}while(check==false);
//玩家X或O结束
睡眠(2000年);
类型字符串(25,“\n正在创建新的Tic Tac Toe网格…”);
blankGrid(gameGrid);
睡眠(1000);
键入字符串(25,“查找游戏说明…”);
睡眠(1000);
//比赛开始
而(!input.equalsIgnoreCase(“完成”)){
如果(计数器==0){
System.out.println(“\n名称已启动!”);
睡眠(1000);
类型字符串(50),\n有关如何播放类型帮助的说明。\n“;
}
//帮助
else if(input.equalsIgnoreCase(“help”)){
游戏说明();
输入=”;
如果(球员){
player=false;
}
否则{
玩家=真;
}
}
否则{
//球员1开始
如果(球员){
睡眠(750);
System.out.println(“轮到玩家1了。选择一个放置“+piece1+”的位置”);
chooseGrid(gameGrid);
输入=scan.nextLine();
ChoiceInput(游戏网格、输入、玩家、片段1、片段2);
睡眠(750);
player=false;
/*睡眠(500);
System.out.print(“这不是有效答案!\n”);
睡眠(500);
System.out.println(“记住,如果你不知道该怎么做,你总是可以键入‘help’”);
睡眠(750);
输入=”;
*/
}
//球员2开始
否则如果(!player){
睡眠(750);
System.out.println(“轮到玩家2了。选择一个放置“+piece2+”的位置”);
chooseGrid(gameGrid);
输入=scan.nextLine();
ChoiceInput(游戏网格、输入、玩家、片段1、片段2);
睡眠(750);
玩家=真;
/*
睡眠(500);
System.out.print(“这不是有效答案!\n”);
睡眠(500);
System.out.println(“记住,如果你不知道该怎么做,你总是可以键入‘help’”);
睡眠(750);
输入=”*/
}
显示网格(gameGrid);
}
睡眠(1000);
计数器++;
}
}
//游戏结束
/*
*blankGrid:创建一个空白的Tic-Tac-Toe数组模板。
*/
公共静态空白网格(字符串[][]网格){
睡眠(500);
对于(int row=0;row)请发布您尝试过的内容以及从中获得的信息。如果您将if(player)放入
循环内的条件您可以删除大量重复的代码将文本移到代码上方。这让读者对随后的代码有一些关注。您是否尝试调试您的程序?请发布相关方法,而不是整个程序。如果(!player)是一段完全冗余的代码,请发布其他方法。