将Type Cell的二维数组转换为Int

时间:2017-10-05 21:00:12

标签: java arrays swing

我试图在for循环中为Int值分配局部变量,但是信息包含在单元格的2D数组中。我试图使用parseInt,但它收到了parseInt不能用于类型单元格的错误。

package surroundpack;

import java.awt.*;
import java.util.*;


public class surroundgame {

    private Cell [][] board;
    private int sizeCol;
    private int sizeRow;
    private int whoTurn;
    private Status status;
    private ArrayList<Point> location;


    public surroundgame(int size, int start) {
        board = new Cell[sizeRow][sizeCol]; 
        sizeRow = size;
        sizeCol = size;
        status = Status.Running;
        whoTurn = start;
        board = new Cell[sizeRow][sizeCol];

        for (int row = 0; row < sizeRow; row++) {
            for (int col = 0; col < sizeCol; col++) {
                board[row][col] = Cell.isEmpty;

            }
        }

    }

    public Status getGameStatus() {
        isWinner();
        return status;
    }

    public int getSizeCol() {
        return sizeCol;
    }


    public void setSizeCol(int sizeCol) {
        this.sizeCol = sizeCol;
    }


    public int getSizeRow() {
        return sizeRow;
    }


    public void setSizeRow(int sizeRow) {
        this.sizeRow = sizeRow;
    }


    public int getWhoTurn() {
        return whoTurn;
    }


    public void setWhoTurn(int whoTurn) {
        this.whoTurn = whoTurn;
    }

    public Cell[][] getBoard() {
        return board;
    }

    public boolean isWinner() {

        for (int row = 0; row < this.sizeRow; row++) {
            for (int col = 0; col < this.sizeCol; col++) {
                Cell num = board[row][col];

                // not a winner if this cell is null
                if (num != null) {

                    // Look at above cell
                    Integer topNum = -1;
                    if (row > 0) {
                        board[row-1][col] = Integer.parseInt(board[row][col]);
                        topNum = board[row-1][col];
                        if (topNum == null) break;
                    }                 

                    // Look at right cell
                    Integer rightNum = -1;
                    if (col == this.sizeCol-1) {
                        rightNum = board[row][col++];
                        if (rightNum == null) break;
                    }

                    // Look at left cell
                    Integer leftNum = -1;
                    if (col == this.sizeCol-1) {
                        rightNum = board[row][col--];
                        if (leftNum == null) break;
                    }

                    // Look at bottom cell
                    Integer bottomNum = -1;
                    if (row > 0) {
                        topNum = board[row+1][col];
                        if (topNum == null) break;
                    }  


                    // do similar for left cell and bottom cell

                    // Check that topNum is not our current cell
                    // then check that topNum is the same number as right left and bottom OR those cells are -1, which
                    // means that side was on an edge.
                    if (!num.equals(topNum)) {
                        if ((topNum.equals(rightNum) || rightNum.equals(-1))
                          && (topNum.equals(leftNum) || leftNum.equals(-1))
                          && (topNum.equals(bottomNum) || bottomNum.equals(-1))) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    public void newGame() {
        board = new Cell[sizeRow][sizeCol];
        status = Status.Running;
        for (int row = 0; row < sizeRow; row++) {
            for (int col = 0; col < sizeCol; col++) {
                board[row][col] = Cell.isEmpty;

            }
        }

    }

    public void select (int row, int col) {
        if(board[row][col] == Cell.isEmpty) {
            if (whoTurn == 0) {
                board[row][col] = Cell.Zero;
                whoTurn = 1;

            }else {
                board[row][col] = Cell.One;
                whoTurn = 0;
            }
        }
    }
}

0 个答案:

没有答案