简体   繁体   English

协助连接4个代码?

[英]Assistance with connect 4 code?

So as far as I've tested my code, the connect 4 program I have runs just as expected with one problem... it seems that the first move for red is prompted twice. 因此,就我已经测试过的代码而言,我已经运行的connect 4程序按预期运行,但有一个问题……似乎红色的第一个举动被提示了两次。

Is it a fault in the loop in the main method causing that, where calling methods are placed, or something in the for loops causing the loop to prompt red twice? 是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;
}

} }

At the end of method loadValue you are calling redMove . loadValue方法的末尾,您将调用redMove

Then in code you have this 然后在代码中你有这个

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

As you start with count=0 , the redMove method is called once more. count=0开始, redMove调用redMove方法。

It looks like you are calling RedMove twice. 您好像两次调用RedMove。 Once at the end of LoadValue and then again in your main loop. 一次在LoadValue的末尾,然后再次在主循环中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM