2016-12-01 46 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