寻找在井字棋的赢家(Java中使用2-D数组) [英] Finding the winner in Tic-Tac-Toe (Java using 2-D arrays)

查看:296
本文介绍了寻找在井字棋的赢家(Java中使用2-D数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法搞清楚如何以显示该游戏的赢家在这个井字棋程序。

I'm having trouble figuring out how to display the winner of the game in this tic-tac-toe program.

 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);
    }


   }
}

该checkForWinner方法提供给我们的,不能改变,从我所看到的它会检查所有可能的赢状态,而无需考虑,因为这个我有点失去了对如何给玩家启动将显示胜利者的方法。

The checkForWinner method was provided to us and can't be changed, from what i've seen it checks for all the possible win states without taking into account the player because of this i'm kind of lost as to how to start the method that would display the winner.

任何输入,以什么我可以与该方法做将是巨大的。

Any input as to what I could do with that method would be great.

感谢您的关注。

编辑:新增的displayWinner方法我试过,这似乎并没有工作。

Added the displayWinner method that I tried, which doesn't seem to work.

推荐答案

呼叫 checkForWinner 每回合之后。一旦你回到真正,你知道谁是最后一个移动是赢家。

Call checkForWinner after each turn. Once you get back true, you know that whoever was the last one to move is the winner.

这是因为你之前的最后一步检查委员会,并出现了在当时没有赢家(否则,你会更早退出游戏)。现在我们有一个赢家,谁就取得了最后一步一定是谁赢得了球员。

This is because you have checked the board before the last move, and there has been no winner at the time (otherwise, you would have exited the game earlier). Now that we have a winner, whoever made the last move must be the player who won.

请注意,您当前的 checkForWinner 的调用是没用的,因为你忽略了返回值。目前,标志未分配

Note that your current call of checkForWinner is useless, because you ignored the return value. Currently, the flag remains unassigned.

中的呼叫 displayWinner 需要采取冠军数,而不是一个标志

The call of displayWinner needs to take the winner number, not a flag.

这篇关于寻找在井字棋的赢家(Java中使用2-D数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆