【问题标题】:Tic Tac Toe : Java井字游戏:Java
【发布时间】:2015-11-01 02:36:59
【问题描述】:

我真的迷路了,我在构建这个游戏时遇到了麻烦。我需要你们的帮助。几天来一直在努力解决这个问题。我有三个类,驱动程序,董事会和球员。我有司机,我认为板级在控制之下。我主要是在播放器类上苦苦挣扎。这样做的目的是让计算机玩家能够随机输入到数组中,然后让玩家能够输入他们想要玩的地方的选择。

public class Driver
{
    public static void main(String[] args)
    {
        //new tic-tac-toe board
        Board board = new Board();

        //two new players (conputer and human)
        Player computer = new Player(board, "X");   //Give computer player access to board and assign as X.
        Player human = new Player(board, "O");      //Give human player access to board and assign as O.
        board.print();
        computer.computerMove();

        //while the game is not over
        while(!board.gameOver())
        {
            //let computer move first
            computer.computerMove();

            //print tic-tac-toe board
            board.print();

            //if the game is not over yet
            if (!board.gameOver())
            {
                //let the human make a move
                human.humanMove();

                //if the game is over
                if (board.gameOver())
                {
                    //print the board
                    board.print();
                }
            }
        } 

        //print out the winner (if there is one) of the game
        board.printWinner();
    }
}

板级

public class Board
{
    private String player = "X";
    private String cpu = "O";
    int row = 3;
    int column = 3;
    private String[][] theBoard = new String[row][column] ;

    public Board()
    {
        theBoard = theBoard;
    }

    public boolean gameOver()
    { 
       if  (theBoard[0][0] == player && theBoard[0][1] == player && theBoard[0][2] == player || // 1st row
            theBoard[1][0] == player && theBoard[1][1] == player && theBoard[1][2] == player || // 2nd row
            theBoard[2][0] == player && theBoard[2][1] == player && theBoard[2][2] == player || // 3rd row
            theBoard[0][0] == player && theBoard[1][0] == player && theBoard[2][0] == player || // 1st col.
            theBoard[0][1] == player && theBoard[1][1] == player && theBoard[2][1] == player || // 2nd col.
            theBoard[0][2] == player && theBoard[1][2] == player && theBoard[2][2] == player || // 3rd col.
            theBoard[0][0] == player && theBoard[1][1] == player && theBoard[2][2] == player || // Diagonal          \ 
            theBoard[2][0] == player && theBoard[1][1] == player && theBoard[0][2] == player) //   Diagonal      /
            {
                return false;
            }
        else if (theBoard[0][0] == cpu && theBoard[0][1] == cpu && theBoard[0][2] == cpu || // 1st row
            theBoard[1][0] == cpu && theBoard[1][1] == cpu && theBoard[1][2] == cpu || // 2nd row
            theBoard[2][0] == cpu && theBoard[2][1] == cpu && theBoard[2][2] == cpu || // 3rd row
            theBoard[0][0] == cpu && theBoard[1][0] == cpu && theBoard[2][0] == cpu || // 1st col.
            theBoard[0][1] == cpu && theBoard[1][1] == cpu && theBoard[2][1] == cpu || // 2nd col.
            theBoard[0][2] == cpu && theBoard[1][2] == cpu && theBoard[2][2] == cpu || // 3rd col.
            theBoard[0][0] == cpu && theBoard[1][1] == cpu && theBoard[2][2] == cpu || // Diagonal          \ 
            theBoard[2][0] == cpu && theBoard[1][1] == cpu && theBoard[0][2] == cpu) //   Diagonal      /

            {
                return false;
            }
       else{

           return true;
        }
    }

    public void print()
    {

        System.out.println(theBoard[0][0] + " | " + theBoard[0][1]+ " | " + theBoard[0][2] + "\n----------");

        System.out.println(theBoard[1][0] + " | " + theBoard[1][1]+ " | " + theBoard[1][2] + "\n----------");

        System.out.println(theBoard[2][0] + " | " + theBoard[2][1]+ " | " + theBoard[2][2] + "\n");

    }

    public void printWinner()
    {
       if  (theBoard[0][0] == player && theBoard[0][1] == player && theBoard[0][2] == player || // 1st row
            theBoard[1][0] == player && theBoard[1][1] == player && theBoard[1][2] == player || // 2nd row
            theBoard[2][0] == player && theBoard[2][1] == player && theBoard[2][2] == player || // 3rd row
            theBoard[0][0] == player && theBoard[1][0] == player && theBoard[2][0] == player || // 1st col.
            theBoard[0][1] == player && theBoard[1][1] == player && theBoard[2][1] == player || // 2nd col.
            theBoard[0][2] == player && theBoard[1][2] == player && theBoard[2][2] == player || // 3rd col.
            theBoard[0][0] == player && theBoard[1][1] == player && theBoard[2][2] == player || // Diagonal          \ 
            theBoard[2][0] == player && theBoard[1][1] == player && theBoard[0][2] == player) //   Diagonal      /
            {
                System.out.println("X - won!");
            }
        else if (theBoard[0][0] == cpu && theBoard[0][1] == cpu && theBoard[0][2] == cpu || // 1st row
            theBoard[1][0] == cpu && theBoard[1][1] == cpu && theBoard[1][2] == cpu || // 2nd row
            theBoard[2][0] == cpu && theBoard[2][1] == cpu && theBoard[2][2] == cpu || // 3rd row
            theBoard[0][0] == cpu && theBoard[1][0] == cpu && theBoard[2][0] == cpu || // 1st col.
            theBoard[0][1] == cpu && theBoard[1][1] == cpu && theBoard[2][1] == cpu || // 2nd col.
            theBoard[0][2] == cpu && theBoard[1][2] == cpu && theBoard[2][2] == cpu || // 3rd col.
            theBoard[0][0] == cpu && theBoard[1][1] == cpu && theBoard[2][2] == cpu || // Diagonal          \ 
            theBoard[2][0] == cpu && theBoard[1][1] == cpu && theBoard[0][2] == cpu) //   Diagonal      /

            {
                System.out.println("O - won!");
            }


    }
}

还有 Player 课,这是我最苦恼的课。

import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
public class Player

{
    String player = "X";
    String cpu = "O";
    private Board ticTac;
    public static Scanner scan = new Scanner(System.in);
    public Player(Board board, String inBoard )
    {
        ticTac = board;
    }
public void randomPlace()
    {
        for(int i = 0; i < 3; i ++)
        {
            for(int j = 0; j < 3; j++)
            {

            }
        }
    }
    public void computerMove()
    {


    }        

    public void humanMove()
    {

    }
}

打印

null | null | null
----------
null | null | null
----------
null | null | null

【问题讨论】:

标签: java arrays class 2d


【解决方案1】:

我想也许这对你有帮助。

import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
public class Player

{
    String player = "X";
    String cpu = "O";

    int row = 3;
    int column = 3;

    private Board ticTac;


    public static Scanner scan = new Scanner(System.in);
    public Player(Board board, String inBoard )
    {
        //here you have the board in player
        tictac = board;
    }

    public void computerMove()
    {   //here you can code something like this
        tictac.put(tictac.getRandomFreePlace(),cpu);
    }        

    public void humanMove(Position position)
    {
        tictac.put(position, human);
    }
}

您必须在 Board 中编写 put(Position, String) 和 getRandomFreePlace()

然后扫描玩家动作并打印棋盘。

----------更新--------------

哦,你想初始化你的板子吗?你可以用双倍的来做到这一点

  for(i=0;i<row;i++){
       for(j=0;j<column;j++){
        //here you can set the value you want
        theBoard[i][j]=0;
        }
    }

【讨论】:

  • 表示 Board(大写 B)无法转换为 java.lang.String[][]。感谢您的尝试,尽管我很欣赏任何想法。
  • 谢谢,但您认为我可以不编辑驱动程序吗?
  • OH nvm 我明白你做了什么;)
  • 是的,可以,我只改了humanMove,但是你可以不带参数使用,询问里面的玩家动作。
  • 感谢您的帮助我认为我的主要困惑是让 ticTac 用于板。我是二维数组的新手。
【解决方案2】:

不确定我是否理解您的问题,您可以重新措辞或举个例子吗?

据我了解,您希望在您的播放器类中使用您的 board 方法,对吗?

如果你创建了一个棋盘类并赋予它所需的任何特性,那么在你的玩家类中你可以创建一个新的“它”类,然后通过它的名称调用它来检索你赋予它的任何特性。

【讨论】:

    猜你喜欢
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    相关资源
    最近更新 更多