JAVA 连接四种方法错误地递减行
JAVA Connect Four Method Incorrectly Decrements Row
我已经为 connectFour 编写了一个程序,它接受:
- 用户输入为 int(作为变量选择)
- count int(根据 returns char for R or Y 插入多维数组的方法自动计算的变量)
然后我编写了一个名为 dropChip 的方法,它接受如下选择和计数值:
public static void dropChip(int selection, int count)
{
--selection;//make selection a human number
if (grid[r][selection] == ' ')//while r(row) & selection is empty char
{
grid[r][selection] = userChip(count);//drop R or Y into grid
}
else if (grid[r][selection] != ' ')//while r selection has a letter value
{
int x = r--;//set x to value of decremented r
grid[x][selection] = userChip(count);//drop chip in next row of same column
}
}
这是我在终端中的输出示例:
| | | | | | |
| | | | | | |
| | | | | | |
| | | | |R| |
| | | |Y| | |
| | |R| | | |
Yellow player's turn.
Input a value between 1 and 6 to drop your chip: 3
| | | | | | |
| | | | | | |
| | |Y| | | |
| | | | |R| |
| | | |Y| | |
| | |R| | | |
问题:为什么第 2 列(人类为 3 个)中的 Y 和 R 没有直接堆叠在一起? r 的值应该是方法调用的局部值,而不是全局设置为负值,对吧?是什么赋予了?
在这里添加完整的程序以防有人想看得更深:
import java.util.*;
//create connectFour class
public class connectFour
{ //main method and public declarations
public static Scanner input = new Scanner(System.in);
public static int selection = 0;
//create variable for square multidimensional array usage
public static int y = 6;
//build new two dimensional array of 3 rows with 3 columns
public static char[][] grid = new char[y][y];
public static int r = grid.length - 1;//6 as human number
public static int c = grid[r].length - 1;//6 as human number
public static void main(String[] args)
{
System.out.println();
System.out.println("===============START PROGRAM===============\n");
//create game prompt
String prompt = "Welcome to the classic Connect Four game!\n\nThis program will start with the red player\nthen move to the yellow player.\n\nYou will chose a numerical value\nbetween 1 and 6 as the column to drop your chip.\n\nOnce you have dropped your chip, the program will scan\nthe columns and rows looking for a win.\nYou can win by having four chips stacked either\nhorizontally, vertically or diagonally.\n\nGood Luck!\n";
System.out.print(prompt);
//call the loadBlanks method with the variable of grid
loadBlanks(grid);
controller();
//check4Win(grid, selection);
System.out.println("===============END PROGRAM===============");
}
public static void controller()
{
//set maximum number of user attempts (36 in this case)
int maxAttempts = r * c;
//create an empty int
int count = 0;
//while the count value is less than maxAttempts
while (count < maxAttempts)
{
//determine which user turn it is sending count number
userTurn(count);
//print prompt for user disc
System.out.print("Input a value between 1 and 6 to drop your chip: ");
//store user value in selection
selection = input.nextInt();
System.out.println();
//send human number of selection to method that drops chip along with count value
dropChip(selection, count);
//print the connect four game
printValues(grid);
//increment value of count
count++;
}
}
public static void loadBlanks(char[][] grid)
{//while row is < total count of rows
for (int row = 0; row < grid.length; row++)
{//while column in row is < total count of columns
for (int column = 0; column < grid[row].length; column++)
{//fill grid with blank values
grid[row][column] = ' ';
}//end inner loop
}//end outer loop
}
public static void dropChip(int selection, int count)
{
--selection;//make selection a human number
int x = grid.length - 1;
if (grid[x][selection] == ' ')
{
grid[x][selection] = userChip(count);
}
else if (grid[x][selection] == 'R' || grid[x][selection] == 'Y')//(grid[x][selection] == 'R' || grid[r][selection] == 'Y')
{
grid[x-1][selection] = userChip(count);
}
}
//print all of the values in the array
public static void printValues(char[][] grid)
{//while row is < total count of rows
for (int row = 0; row < grid.length; row++)
{//while column in row is < total count of columns
for (int col = 0; col < grid[row].length; col++)
{//print inner loop bracket
System.out.print("|" + grid[row][col]);
}//end inner loop
System.out.println("|");//print outer loop bracket
}//end outer loop
}
//return the value of the user chip based on turn as char
public static char userChip(int count)
{
//set userColor to random value
char userColor = ' ';
//if the passed value of int x is evenly divisibly by 2, set char to R
if (count % 2 == 0)
{
userColor = 'R';
}//end if
//else if the int of x modulo 2 != 0, set char to Y
else if (count % 2 != 0)
{
userColor = 'Y';
}//end elseif
return userColor;//set value of char to userColor
}
//calculate user turn based on count value starting with red
public static void userTurn(int count)
{
String color = " ";
if (count % 2 == 0)
{
color = "Red";
}//end if
//else if the int of x modulo 2 != 0, set char to Y
else if (count % 2 != 0)
{
color = "Yellow";
}//end elseif
System.out.println();//whitespace for terminal
System.out.println(color + " player\'s turn.");//print user turn
}
好的,从字面上调试您的代码。我在你的 static void dropChip(int selection, int count)
方法声明中看到了这个 -
else if (grid[x][selection] == 'R' || grid[x][selection] == 'Y') {
grid[x - 1][selection] = userChip(count);
}
这就是避免重叠的原因。因为如果您在当前块中找到 'R'
或 'Y'
,您会将新字符放置在同一列(就在其上方)的网格的 x-1
行中。
如果您只想覆盖当前块中的现有值,您可以将 dropChip 方法更改为 -
public static void dropChip(int selection, int count) {
--selection;
int x = grid.length - 1;
grid[x][selection] = userChip(count);
}
此外,如果您改为使用
int x = r--;
然后是行数,每次到达此语句时都会减 1。导致向上块填充问题中共享的样本输出。
Edit - 附带说明,因为您在 controller
中只接受单一输入,所以我不接受查看您想要填充当前逻辑中倒数第二行以上的行(或更新逻辑中的最后一行。)
建议 - 尝试根据行和列进行输入,以便用所需的字符填充确切的块。
我昨天很晚才找到答案。通过使用 r 的全局值,只要 --r 选择的值等于 R 或 Y 字符,我的代码就会在全局范围内递减该值。为了解决这个问题,我使用了一个带有 x 的局部 for 循环,其中 x = grid.length-1 并在每次通过该方法时递减该值。我的 dropChip 方法的工作示例如下。感谢大家的comments/help!
public static void dropChip(int selection, int count)
{
--selection;//make selection a human number
for (int x = grid.length-1; x >= 0; --x)//set x to value of decremented r
{
if (grid[x][selection] == ' ')//while r(row) & selection is empty char
{
grid[x][selection] = userChip(count);//drop R or Y into grid
break;
}
//else grid[x][selection] = userChip(count);//drop chip in next row of same column
}
}
我已经为 connectFour 编写了一个程序,它接受:
- 用户输入为 int(作为变量选择)
- count int(根据 returns char for R or Y 插入多维数组的方法自动计算的变量)
然后我编写了一个名为 dropChip 的方法,它接受如下选择和计数值:
public static void dropChip(int selection, int count)
{
--selection;//make selection a human number
if (grid[r][selection] == ' ')//while r(row) & selection is empty char
{
grid[r][selection] = userChip(count);//drop R or Y into grid
}
else if (grid[r][selection] != ' ')//while r selection has a letter value
{
int x = r--;//set x to value of decremented r
grid[x][selection] = userChip(count);//drop chip in next row of same column
}
}
这是我在终端中的输出示例:
| | | | | | |
| | | | | | |
| | | | | | |
| | | | |R| |
| | | |Y| | |
| | |R| | | |
Yellow player's turn.
Input a value between 1 and 6 to drop your chip: 3
| | | | | | |
| | | | | | |
| | |Y| | | |
| | | | |R| |
| | | |Y| | |
| | |R| | | |
问题:为什么第 2 列(人类为 3 个)中的 Y 和 R 没有直接堆叠在一起? r 的值应该是方法调用的局部值,而不是全局设置为负值,对吧?是什么赋予了?
在这里添加完整的程序以防有人想看得更深:
import java.util.*;
//create connectFour class
public class connectFour
{ //main method and public declarations
public static Scanner input = new Scanner(System.in);
public static int selection = 0;
//create variable for square multidimensional array usage
public static int y = 6;
//build new two dimensional array of 3 rows with 3 columns
public static char[][] grid = new char[y][y];
public static int r = grid.length - 1;//6 as human number
public static int c = grid[r].length - 1;//6 as human number
public static void main(String[] args)
{
System.out.println();
System.out.println("===============START PROGRAM===============\n");
//create game prompt
String prompt = "Welcome to the classic Connect Four game!\n\nThis program will start with the red player\nthen move to the yellow player.\n\nYou will chose a numerical value\nbetween 1 and 6 as the column to drop your chip.\n\nOnce you have dropped your chip, the program will scan\nthe columns and rows looking for a win.\nYou can win by having four chips stacked either\nhorizontally, vertically or diagonally.\n\nGood Luck!\n";
System.out.print(prompt);
//call the loadBlanks method with the variable of grid
loadBlanks(grid);
controller();
//check4Win(grid, selection);
System.out.println("===============END PROGRAM===============");
}
public static void controller()
{
//set maximum number of user attempts (36 in this case)
int maxAttempts = r * c;
//create an empty int
int count = 0;
//while the count value is less than maxAttempts
while (count < maxAttempts)
{
//determine which user turn it is sending count number
userTurn(count);
//print prompt for user disc
System.out.print("Input a value between 1 and 6 to drop your chip: ");
//store user value in selection
selection = input.nextInt();
System.out.println();
//send human number of selection to method that drops chip along with count value
dropChip(selection, count);
//print the connect four game
printValues(grid);
//increment value of count
count++;
}
}
public static void loadBlanks(char[][] grid)
{//while row is < total count of rows
for (int row = 0; row < grid.length; row++)
{//while column in row is < total count of columns
for (int column = 0; column < grid[row].length; column++)
{//fill grid with blank values
grid[row][column] = ' ';
}//end inner loop
}//end outer loop
}
public static void dropChip(int selection, int count)
{
--selection;//make selection a human number
int x = grid.length - 1;
if (grid[x][selection] == ' ')
{
grid[x][selection] = userChip(count);
}
else if (grid[x][selection] == 'R' || grid[x][selection] == 'Y')//(grid[x][selection] == 'R' || grid[r][selection] == 'Y')
{
grid[x-1][selection] = userChip(count);
}
}
//print all of the values in the array
public static void printValues(char[][] grid)
{//while row is < total count of rows
for (int row = 0; row < grid.length; row++)
{//while column in row is < total count of columns
for (int col = 0; col < grid[row].length; col++)
{//print inner loop bracket
System.out.print("|" + grid[row][col]);
}//end inner loop
System.out.println("|");//print outer loop bracket
}//end outer loop
}
//return the value of the user chip based on turn as char
public static char userChip(int count)
{
//set userColor to random value
char userColor = ' ';
//if the passed value of int x is evenly divisibly by 2, set char to R
if (count % 2 == 0)
{
userColor = 'R';
}//end if
//else if the int of x modulo 2 != 0, set char to Y
else if (count % 2 != 0)
{
userColor = 'Y';
}//end elseif
return userColor;//set value of char to userColor
}
//calculate user turn based on count value starting with red
public static void userTurn(int count)
{
String color = " ";
if (count % 2 == 0)
{
color = "Red";
}//end if
//else if the int of x modulo 2 != 0, set char to Y
else if (count % 2 != 0)
{
color = "Yellow";
}//end elseif
System.out.println();//whitespace for terminal
System.out.println(color + " player\'s turn.");//print user turn
}
好的,从字面上调试您的代码。我在你的 static void dropChip(int selection, int count)
方法声明中看到了这个 -
else if (grid[x][selection] == 'R' || grid[x][selection] == 'Y') {
grid[x - 1][selection] = userChip(count);
}
这就是避免重叠的原因。因为如果您在当前块中找到 'R'
或 'Y'
,您会将新字符放置在同一列(就在其上方)的网格的 x-1
行中。
如果您只想覆盖当前块中的现有值,您可以将 dropChip 方法更改为 -
public static void dropChip(int selection, int count) {
--selection;
int x = grid.length - 1;
grid[x][selection] = userChip(count);
}
此外,如果您改为使用
int x = r--;
然后是行数,每次到达此语句时都会减 1。导致向上块填充问题中共享的样本输出。
Edit - 附带说明,因为您在 controller
中只接受单一输入,所以我不接受查看您想要填充当前逻辑中倒数第二行以上的行(或更新逻辑中的最后一行。)
建议 - 尝试根据行和列进行输入,以便用所需的字符填充确切的块。
我昨天很晚才找到答案。通过使用 r 的全局值,只要 --r 选择的值等于 R 或 Y 字符,我的代码就会在全局范围内递减该值。为了解决这个问题,我使用了一个带有 x 的局部 for 循环,其中 x = grid.length-1 并在每次通过该方法时递减该值。我的 dropChip 方法的工作示例如下。感谢大家的comments/help!
public static void dropChip(int selection, int count)
{
--selection;//make selection a human number
for (int x = grid.length-1; x >= 0; --x)//set x to value of decremented r
{
if (grid[x][selection] == ' ')//while r(row) & selection is empty char
{
grid[x][selection] = userChip(count);//drop R or Y into grid
break;
}
//else grid[x][selection] = userChip(count);//drop chip in next row of same column
}
}