将多个对象添加到2D阵列(TicTacToe游戏)

时间:2016-02-20 02:11:11

标签: java arrays

我看了,但是我没有找到答案,所以我想问一下。

https://web.cs.dal.ca/~srini/cs1101/Assign2.1101.16.pdf以下是分配详细信息的PDF文件,但我的详细信息和代码应该足够了。

我正在为Tic Tac Toe游戏进行多课程作业,而且我在添加所有对象后无法编译为什么会遇到麻烦。它返回一个null,即使我用内容初始化了整个2D数组。

这是我的完整程序,我将在底部包含可疑问题的代码。

这是X或O对象类

public class XO{
private String name;
private static int turn = 1;

public XO(String name)
{
    if(name == "-")
    {
        name = "-";
    }
    else if(2%turn == 0)
    {
        turn++;
        name = "X";
    }
    else if (2%turn == 1)
    {
        turn++;
        name = "O";
    }
}
public String getName()
{
    return name;
}
public int getTurn()
{
    return turn;
}
public boolean equals(XO xo)
{
    if(xo.getName().equals(name))
    {
        return true;
    }
    else
        return false;
}
public String toString()
{
    return name;

}}

这是董事会" super" TicTakToeBoard类的类

public class Board {
protected int rows = 2;
protected int cols = 2;

public Board(int x, int y)
{
    rows = x;
    cols = y;
}
public int getRows()
{
    return rows;
}
public int getCols()
{
    return cols;
}
public void setRows(int r)
{
    rows = r;
}
public void setCols(int c)
{
    cols = c;
}}

这是TicTacToe特定主板

public class TicTakToeBoard extends Board{
private static XO board [][];
private int turnCnt = 1;

public TicTakToeBoard(int r, int c)
{
    super(r, c);
    rows = r;
    cols = c;
    board = new XO[rows][cols];
    setXOBoard(rows, cols);
}
public int getBoardRow()
{
    return rows;
}
public int getBoardCols()
{
    return cols;
}
public int getTurnCnt()
{
    return turnCnt;
}
public void setXOBoard(int r, int c)
{
    XO empty = new XO("-");
    XO[][] board = new XO[getBoardRow()][getBoardCols()];
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++) 
        {
            board[i][j] = (empty);
        }
    }
}
public XO getXOBoard(int r, int c)
{
    return board[r][c];
}
public boolean add(int x, int y)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 2; i > cols; j--)
        {
            if(board [x][y] != board [i][j])
            {
                if(x <= 2 || x >= 0 && y <= 2 || y >= 0)
                {
                    if(2%turnCnt == 0)
                        board[x][y] = new XO("X");
                    if(2%turnCnt == 1)
                        board[x][y] = new XO("O");
                    turnCnt++;
                    return true;
                }
                else if(x > 2 || x < 0 && y > 2 || y < 0)
                {
                    return false;
                }
            }
        }
    }
    return false;
}
public boolean winner(XO a, XO b)
{
    if(board [0][0] == a && board [1][0] == a && board [2][0] == a)
    {
        return true;
    }
    if(board [0][1] == a && board [1][1] == a && board [2][1] == a)
    {
        return true;
    }
    if(board [0][2] == a && board [1][2] == a && board [2][2] == a)
    {
        return true;
    }
    if(board [0][0] == a && board [0][1] == a && board [0][2] == a)
    {
        return true;
    }
    if(board [1][0] == a && board [1][1] == a && board [1][2] == a)
    {
        return true;
    }
    if(board [2][0] == a && board [2][1] == a && board [2][2] == a)
    {
        return true;
    }
    if(board [0][0] == a && board [1][1] == a && board [2][2] == a)
    {
        return true;
    }
    if(board [2][0] == a && board [1][1] == a && board [0][2] == a)
    {
        return true;
    }
    //Above player 1 wins, below player 2 wins
    if(board [0][0] == b && board [1][0] == b && board [2][0] == b)
    {
        return true;
    }
    if(board [0][1] == b && board [1][1] == b && board [2][1] == b)
    {
        return true;
    }
    if(board [0][2] == b && board [1][2] == b && board [2][2] == b)
    {
        return true;
    }
    if(board [0][0] == b && board [0][1] == b && board [0][2] == b)
    {
        return true;
    }
    if(board [1][0] == b && board [1][1] == b && board [1][2] == b)
    {
        return true;
    }
    if(board [2][0] == b && board [2][1] == b && board [2][2] == b)
    {
        return true;
    }
    if(board [0][0] == b && board [1][1] == b && board [2][2] == b)
    {
        return true;
    }
    if(board [2][0] == b && board [1][1] == b && board [0][2] == b)
    {
        return true;
    }
    else
        return false;
}
public String toString()
{
    return board[0][0]+"\t"
+board[1][0]+"\t"
            +board[2][0]+"\n"
+board[0][1]+"\t"
            +board[1][1]+"\t"
+board[1][2]+"\n"
            +board[0][2]+"\t"
+board[1][2]+"\n"
            +board[2][2];
}}

最后,Play类(尚未完成)

import java.util.*; 
public class Play {
public static void main(String[]args){
    Scanner k = new Scanner(System.in);
    System.out.println("Hello and welcome to this Tic Tac Toe game!");
    System.out.print("Player 1 name: ");
    String player1 = k.next();
    System.out.print("Player 2 name: ");
    String player2 = k.next();
    TicTakToeBoard b = new TicTakToeBoard(2, 2);
    XO p1 = new XO("X");
    XO p2 = new XO("O");
    for(int i = 1; i==i;i++)
    {
        if(2%i == 0)
        {
            System.out.print("Enter a spot to place your mark. First number within 0 and 2, and the second 0, 2: ");
            int x = k.nextInt();
            int y = k.nextInt();
            b.add(x, y);
            System.out.println(b);  
            if(b.winner(p1,p2)==true)
            {
                System.out.println("Congrats ");
                break;
            }
        }
        if(2%i == 1)
        {
            System.out.print("Enter a spot to place your mark. First number within 0 and 2, and the second 0, 2: ");
            int x = k.nextInt();
            int y = k.nextInt();
            b.add(x, y);
            System.out.println(b);
            if(b.winner(p1,p2)==true)
            {
                System.out.println("Congrats ");
                break;
            }
        }
    }
    System.out.println(b);
}}

这是大多数问题的方法,但我可能有任何其他错误,我很乐意接受你指出。

public boolean add(int x, int y)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 2; i > cols; j--)
        {
            if(board [x][y] != board [i][j])
            {
                if(x <= 2 || x >= 0 && y <= 2 || y >= 0)
                {
                    if(2%turnCnt == 0)
                        board[x][y] = new XO("X");
                    if(2%turnCnt == 1)
                        board[x][y] = new XO("O");
                    turnCnt++;
                    return true;
                }
                else if(x > 2 || x < 0 && y > 2 || y < 0)
                {
                    return false;
                }
            }
        }
    }
    return false;}

我知道我在这里要求很多,但是我们不应该感谢任何帮助,我们并没有真正教过2D阵列或继承,所以我只需要从讲义中学习。这项任务至少可以说,我在春假期间花了几天时间,其余时间都在强调它。

非常感谢,我会在上面睡觉并明天回来:)

2 个答案:

答案 0 :(得分:1)

for(r = 0; r >= 3; r++)
{
    for(c = 0; c >= 3; c++)
    {
        board[r][c] = new XO("-");
    }
}

你的循环不会做任何事情; r开头为0,0不大于或等于3. r <= 3可能就是你想要的。

这是如何使其适用于任何电路板尺寸:

char[][] board = new char[rows][cols];

for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
        board[i][j] = '-';
    }
}

答案 1 :(得分:1)

你有一句话:

private static XO board [][];

您从未为其分配数组,即。你没有分配内存来保存你的XO。你需要做这样的事情:

board = new XO[rows][cols]; // allocate memory to hold your XO's
setXOBoard(rows,cols);