协助连接4代码?

时间:2015-04-08 17:27:20

标签: java

因此,就我测试了我的代码而言,我运行的connect 4程序运行正常,只有一个问题......似乎红色的第一步是两次提示。

在main方法的循环中是否存在错误,导致调用方法的位置,或for循环中的某些内容导致循环提示红色两次?

import java.util.*;
public class Ideas
{
    public static void main(String[] args)
{
    char[][] gameBoard = new char[6][7];
    loadValue(gameBoard);
    printBoard(gameBoard);

    boolean loop = true;
    int count = 0;
    while(loop)
    {
        if(count % 2 == 0)
        {
            redMove(gameBoard);
        }
        else
        {
            yellowMove(gameBoard);
        }
        count++;
        if(count == 42)
        {
            System.out.println("The game has ended in a tie");
            break;
        }
        System.out.println("The count is " + count);
        printBoard(gameBoard);
        if(count % 2 == 0 && checkWin(gameBoard) == true)
        {
            System.out.println("Congradulations to the winner yellow");
            loop = false; 
            break;
        }
        else if(count % 2 != 0 && checkWin(gameBoard) == true)
        {
            System.out.println("Congradulations to the winner red");
            loop = false;
            break;
        }
    }
}
public static void loadValue(char[][] gameBoard)
{
    for(int row = 0; row < gameBoard.length; row++)
    {
        for(int col = 0; col < gameBoard[row].length; col++)
        {
            gameBoard[row][col] = ' ';
        }
    }
    redMove(gameBoard);
}
public static void printBoard(char[][] gameBoard)
{
    for(int row = 0; row < gameBoard.length; row++)
    {
        for(int col = 0; col < gameBoard[row].length; col++)
        {
            System.out.print("|" + gameBoard[row][col]);
        }
        System.out.println("|");
    }
}
public static void redMove(char[][] gameBoard)
{
   System.out.println("Drop a red disk at column(0-6): ");
   Scanner input = new Scanner(System.in);
   int col = input.nextInt();

   for(int row = 5; row > 0; row--)
   {
      if(gameBoard[row][col] == ' ')
      {
          gameBoard[row][col] = 'R';
          break;
      }
   }
   checkWin(gameBoard);
}
public static void yellowMove(char[][] gameBoard)
{
    System.out.println("Drop a yellow disk at column(0-6): ");
    Scanner input = new Scanner(System.in);
    int col = input.nextInt();

   for(int row = 5; row > 0; row--)
   {
      if(gameBoard[row][col] == ' ')
      {
          gameBoard[row][col] = 'Y';
          break;
      }
   }
   checkWin(gameBoard);
}
public static boolean checkWin(char[][] gameBoard)
{
    // Create four boolean variables, one for each set of rows. Initialize all of them to false.
    boolean foundRow = false;
    boolean foundCol = false;
    boolean foundMjrD = false;
    boolean foundMinD = false;
    // Check to see if four consecutive cells in a row match.
    // check rows
    for (int r = 0; r <= 5; r++)
    {
        for (int c = 0; c <= 3; c++)
        {
            if (gameBoard[r][c] == gameBoard[r][c + 1] && gameBoard[r][c] == gameBoard[r][c + 2] && gameBoard[r][c] == gameBoard[r][c + 3] && gameBoard[r][c] != ' ')
            {
                foundRow = true;
                break;
            }
        }
    }
    // Check to see if four columns in the same row match
    // check columns
    for (int r = 0 ; r <= 2; r++)
    {
        for (int c = 0; c <= 6; c++)
        {
            if (gameBoard[r][c] == gameBoard[r + 1][c] && gameBoard[r][c] == gameBoard[r + 2][c] && gameBoard[r][c] == gameBoard[r + 3][c] && gameBoard[r][c] != ' ')
            {
                foundCol = true;
                break;
            }
        }
    }
    // Check to see if four diagonals match (top left to bottom right)
    // check major diagonal
    for (int r = 0; r <= 2; r++)
    {
        for (int c = 0; c <= 3; c++)
        {
            if (gameBoard[r][c] == gameBoard[r + 1][c + 1] && gameBoard[r][c] == gameBoard[r + 2][c + 2] && gameBoard[r][c] == gameBoard[r + 3][c + 3] && gameBoard[r][c] != ' ')
            {
                foundMjrD = true;
                break;
            }
        }
    }
    // Check to see if four diagonals in the other direction match (top right to bottom left)
    // check minor diagonal
    for (int r = 0; r <= 2; r++)
    {
        for (int c = 3; c <= 6; c++)
        {
            if (gameBoard[r][c] == gameBoard[r + 1][c - 1] && gameBoard[r][c] == gameBoard[r + 2][c - 2] && gameBoard[r][c] == gameBoard[r + 3][c - 3] && gameBoard[r][c] != ' ')
            {
                foundMinD = true;
                break;
            }
        }
    }
    // If ONE of the booleans is true, we have a winner.
    // checks boolean for a true
    if (foundRow || foundCol || foundMjrD || foundMinD)
        return true;
    else
        return false;
}

}

2 个答案:

答案 0 :(得分:2)

在方法loadValue的末尾,您正在调用redMove

然后在代码中你有这个

        if(count % 2 == 0)
        {
            redMove(gameBoard);
        }

当您从count=0开始时,再次调用redMove方法。

答案 1 :(得分:1)

看起来你正在两次调用RedMove。一旦在LoadValue结束,然后再在主循环中。