JAVA连接四种方法错误地减少行

时间:2016-12-01 00:19:01

标签: java arrays multidimensional-array decrement

我为connectFour编写了一个程序,接受:

  1. 用户输入为int(作为变量选择)
  2. count int(自动计算的变量,从返回char的R或Y插入到多维数组中)
  3. 然后我编写了一个名为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列中的Y和R(人类中的3)没有直接堆叠在一起? 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
        }
    

2 个答案:

答案 0 :(得分:0)

好的按字面意思调试代码。我在你的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.导致向上的块填充问题中共享的样本输出。

编辑 - 旁注,由于您只接受controller中的单个输入,因此我看不到您要填写的方式当前逻辑中第二个末尾上方的行(或更新逻辑中的最后一行)。

建议 - 尝试按行和列进行输入,以使用所需字符填充确切的块。

答案 1 :(得分:0)

我昨天晚些时候找到了答案。通过使用r的全局值,我的代码在--r选择的值等于R或Y字符的任何时候全局递减该值。为了解决这个问题,我使用了一个带x的局部for循环,其中x = grid.length-1,并在每次传递方法时递减该值。我的dropChip方法的工作示例如下。我感谢大家的评论/帮助!

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
    }

}