调用方法和返回值?井字棋

时间:2014-04-24 21:21:25

标签: java multidimensional-array tic-tac-toe

我在使用displayWinner方法时遇到问题。我相信我的调用并不像我认为的那样,因为我不太熟悉使用多种方法,因为这个概念最近才引入。

代码如下:

import java.util.*;
public class tic
{
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);

        boolean flag=false;

        char[][] board = 
        {
        {' ', ' ', ' '},
        {' ', ' ', ' '},
        {' ', ' ', ' '}
        };

        boolean done = false;
        int player = 1;
        int row = 0;
        int col = 0;

        while (flag != true) 
        {
          checkForWinner(board);
          System.out.println("Enter the row and column for your next move");
          row = in.nextInt();
          col = in.nextInt();
          if (player == 1) 
          {
            board[row][col] = 'X';
            player = 2;
            checkForWinner(board);
           }
          else 
          { 
            board[row][col] = 'O';
            player = 1;
            checkForWinner(board);
          }
          printBoard(board);
          checkForWinner(board);
        }

        displayWinner(player, flag);  


    }
    public static void printBoard(char[][] board) 
    {
    for (int row = 0; row < 3; row++) 
    {
        for (int col = 0; col < 3; col++) 
        {
            System.out.print("|" + board[row][col] + "|");
        }
        System.out.println();
        System.out.println("-------");
    }
    }
    public static boolean checkForWinner(char[][] board)
   {
       // checkForWinner() method determines if a pattern of data stored
       // in the 2 D char array indicates the a player has won the game.

        boolean flag = false;
        boolean flag1 = false;
        boolean flag2 = false;
        boolean flag3 = false;
        boolean flag4 = false;

        // checks the contents of each row for matching data   
        for (int i = 0; i <= 2; i++)
        {
            if ((board[i][0] == board[i][1] && board[i][1] == board[i][2]) && board[i][2] != ' ') 
                flag1 = true;
        }

         // checks the contents of each column for matching data
        for (int j = 0; j <= 2; j++)
        {
            if ((board[0][j] == board[1][j] && board[1][j] == board[2][j]) && board[2][j] != ' ') 
                flag2 = true;
        }

        // checks the contents of one diagonal for matching data
        if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) && board[2][2] != ' ') 
                flag3 = true;

        // checks the contents of the other diagonal for matching data
        if ((board[0][2] == board[1][1] && board[1][1] == board[2][0]) && board[2][0] != ' ') 
                flag4 = true;

        // checks if any of the previous conditions evaluated to true        
        if (flag1 == true || flag2 == true || flag3 == true || flag4 == true)
            flag = true;

       // returns true if a winner was found; returns false is no winner     
       return flag;
   } // end of checkForWinner method
   public static void displayWinner(int player, boolean flag)
   {

    if (flag == true)
    {
        int currentplayer;
        currentplayer=player;
        System.out.println("The winner of the game is" +currentplayer);
    }


   }
}

displayWinner方法位于底部,主方法中的while循环是玩游戏并为每个玩家提供轮流的方法。

编辑:我的问题是如何将checkForWinner方法中的标志传递给displayWinner方法,以便打印出游戏获胜者是谁?

2 个答案:

答案 0 :(得分:2)

您没有更新flag方法中声明的main

改变每一个
checkForWinner(board);

flag = checkForWinner(board);

如果没有此flag将永远不会更改,您永远不会离开while (flag != true)(可以简化为while(!flag))循环,这意味着您永远不会执行displayWinner(player, flag)方法。

答案 1 :(得分:0)

你需要让标志实际改变你有checkWinner(board);它应该是flag = checkWinner(board);你也调用该函数两次(一次在循环的开头,一次在结尾)没有必要,你应该在开始时取出一个。此外,您的checkWinner(board)方法将无效,因为如果行或列具有相同的char,它将返回true,并且在开头它们都具有相同的char(''),因此它将立即注册获胜,而不是让它检查行或列是否匹配其中一个玩家字符。