如何在连接4网格世界游戏中遍历和检查4个单元格的行?

时间:2014-04-03 00:51:37

标签: java gridworld

我遇到了一些问题,试图找到一种方法来检查BoundedGrid中的所有对角线是否为4"光盘",并尝试解决方案我现在没有工作一点都不我的尝试是在getWinner()方法中。有人有解决方案吗?提前抱歉有关间距。

import java.awt.Color;
import info.gridworld.grid.Grid;
import info.gridworld.world.World;
import info.gridworld.grid.Location;
import info.gridworld.grid.BoundedGrid;
import java.util.ArrayList;

public class ConnectFourWorld extends World<Piece>
{
private String whosTurn;
private boolean gameOver;

public ConnectFourWorld()
{
super(new BoundedGrid<Piece>(6,7));
whosTurn="Player 1";
gameOver=false;
  setMessage("Welcome to CONNECT 4!  - -  Click a spot - "+whosTurn+" turn.");
}


public boolean locationClicked(Location loc)
{
    Grid<Piece> grid = getGrid();
    if(grid==null)
        return false;

    //if the game is over, clear the board and get ready to play a new game
    if(!getWinner().equals("no winner")){
        gameOver=true;
    }
    if(gameOver==true)
    {
        //clear the board
        resetWorld();
        gameOver=false;
        setMessage("It's "+whosTurn+" click step button");
    }


    //this section will draw an X or an O
    Piece piece = grid.get(loc);
    if(whosTurn.equals("Player 1")&&piece==null)
    {
        add(loc,new Piece("Player 1",Color.BLACK,Color.RED));
        whosTurn="Player 2";
    }
    else if(whosTurn.equals("Player 2")&&piece==null)
    {

        add(loc,new Piece("Player 2",Color.BLACK,Color.GREEN));
        whosTurn="Player 1";

    }

    setMessage("It is " + whosTurn +"'s turn.");

    if(isWorldFull()){
        setMessage(getWinner() + "Click on the grid to start a new game");
    }
    if(!getWinner().equals("no winner")&&!getWinner().equals("cat's game - no winner!")){
        setMessage(getWinner() +  "Click on the grid to start a new game");
    }
    return true;
}

//this method will be called each time the step button is pressed
public void step()
{
if (whosTurn=="Player 1"){
    whosTurn="Player 2";
}
if (whosTurn=="Player 2"){
    whosTurn="Player 1";
}
}

//this method will determine if someone has won the game
public String getWinner()
{

Grid<Piece> grid = getGrid();
if(grid==null){
    return "no winner";
}
            //check horizontal winner
    String winner="";
    for (int r = 0; r<grid.getNumRows()-1; r++)
    {
        for(int col=0; col<grid.getNumCols()-3; col++){
            Piece x =grid.get(new Location(r,col));
            Piece x2 =grid.get(new Location(r,col+1));
            Piece x3 =grid.get(new Location(r,col+2));
            Piece x4 =grid.get(new Location(r,col+3));

            if(x==null||x2==null||x3==null||x4==null)
                continue;
            if(x.getName().equals(x2.getName())&&x.getName().equals(x3.getName())&&x.getName().equals(x4.getName()))
                winner=x.getName()+" wins horizontally!";
                break;
            }

        }


    //check for vertical winner
    for (int r = 0; r<grid.getNumRows()-1; r++)
    {
        for(int col=0; col<grid.getNumCols()-3; col++){
            Piece y =grid.get(new Location(r,col));
            Piece y2 =grid.get(new Location(r+1,col));
            Piece y3 =grid.get(new Location(r+2,col));
            Piece y4 =grid.get(new Location(r+3,col));
            if(y==null||y2==null||y3==null||y4==null)
                continue;
            if(y.getName().equals(y2.getName())&&y.getName().equals(y3.getName())&&y.getName().equals(y4.getName()))
                winner=y.getName()+" wins vertically!";
                break;
            }

    }




    //check for low left to up right diagonal winner
    for (int r = 0; r<grid.getNumRows()-3; r++)
    {
        for(int col=0; col<grid.getNumCols()-3; col++){
            Piece z =grid.get(new Location(r,col));
            Piece z2 =grid.get(new Location(r-1,col+1));
            Piece z3 =grid.get(new Location(r-2,col+2));
            Piece z4 =grid.get(new Location(r-3,col+3));

            if(z.getName().equals(z2.getName())&&z.getName().equals(z3.getName())&&z.getName().equals(z4.getName()))
                winner=z.getName()+" wins diagonally!";
            }
    }
    //check for up left to low right winner
    for (int r = grid.getNumRows()-1; r>=3; r--)
    {
        for(int col=0; col<grid.getNumCols()-3; col++){
            Piece zz =grid.get(new Location(r,col));
            Piece zz2 =grid.get(new Location(r-1,col+1));
            Piece zz3 =grid.get(new Location(r-2,col+2));
            Piece zz4 =grid.get(new Location(r-3,col+3));

            if(zz.getName().equals(zz2.getName())&&zz.getName().equals(zz3.getName())&&zz.getName().equals(zz4.getName()))
                winner=zz.getName()+" wins diagonally!";
            }
    }


   if(isWorldFull() && winner.length()==0){
       winner =  "cat's game - no winner!\n\n";
    }
    else if(!isWorldFull() && winner.length()==0){
        winner="no winner";
    }

return winner;
}

//this method will determine if the board if full of Xs and Os
public boolean isWorldFull(){
//getOccupiedLocations might prove handy
ArrayList<Location> locarray = getGrid().getOccupiedLocations();
if (locarray.size()==42){
    return true;
}
else{
     return false;
}


}

//this method will clear the board of all Xs and Os
public void resetWorld()
{
ArrayList<Location> locarray = getGrid().getOccupiedLocations();
for(Location loc : locarray){
    getGrid().remove(loc);
}
}
}

1 个答案:

答案 0 :(得分:0)

您已将您的电路板初始化为六元素数组(其中每个元素本身是七个元素的数组):

new BoundedGrid<Piece>(6,7)

(在内部,这是一个二维数组。)

六元素数组中的索引是0到5.但是您尝试使用

访问它
grid.get(new Location(r+2,col))

r的范围从04,如for循环中所指定:

    for (int r = 0; r<grid.getNumRows()-1; r++)  {

因为grid.getNumRows()-15

因此,当您尝试这些行时,以下是r的结果值。正如您所看到的,在许多情况下它会超出界限。

                                           //   When r is
                                           //  0  1  2  3  4
                                           // ---------------
Piece y =grid.get(new Location(r,col));    //  0  1  2  3  4
Piece y2 =grid.get(new Location(r+1,col)); //  1  2  3  4  5
Piece y3 =grid.get(new Location(r+2,col)); //  2  3  4  5  6
Piece y4 =grid.get(new Location(r+3,col)); //  3  4  5  6  7

这些问题存在类似问题:

Piece z =grid.get(new Location(r,col));
Piece z2 =grid.get(new Location(r-1,col+1));
Piece z3 =grid.get(new Location(r-2,col+2));
Piece z4 =grid.get(new Location(r-3,col+3));

(您可能还想仔细检查col索引。)

这都假定Location正在访问BoundedGrid对象中的元素。不知道gridworld,这是我能猜到的最好的。

(我建议在您的代码中永久地添加上述注释。通过详细的评论,总是会让代码更加清晰。)