2016-12-01 50 views
0

我写为屏风式四子棋的程序接受:JAVA连接四个方法不正确地递减行

  1. 用户输入作为INT(如可变选择)
  2. 计数INT(自动计算变量从方法返回炭对于R或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| | | | 

问题:为什么是Y和R 2栏(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 
    } 
+0

首先,你的'由条件else'不需要嵌套的'if' – nullpointer

+0

..'r'不是本地声明,以保持它的价值必然......及其不知道什么'userChip() '确实 – nullpointer

+0

感谢@nullpointer。我在原始帖子的底部添加了完整的程序。请注意,我更改了dropChip()以尝试为grid.length-1包含一个局部变量(以前是r的全局可用变量)。任何见解都会被赞赏。 – Fergus

回答

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 
    } 

} 
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

非常感谢你的回复@nullpointer!我尝试用for循环更新我的代码,以减少r的本地值,而不是使用全局变量,并且工作正常。 – Fergus