在位置x,y的ArrayList中添加一个元素

时间:2014-04-22 04:17:58

标签: java arraylist point tic-tac-toe

我正在尝试完成这个Tic Tac Toe游戏的任务。除了方法AddPiece和GetPieceAt之外,我设法完成了所有其他要求。我已经搜索了关于如何在ArrayList中实现它的所有内容以及如何在ArrayList的(x,y)中设置它。我觉得我可能错误地理解了作业,但此时我不知道该怎么做。我有一些想法写下来,但我已经删除了大部分我认为会用这两种方法的东西。

为了减少在此处添加所有其他文件的麻烦,这是发布作业的链接。 http://go.code-check.org/codecheck/files/1404121614cuepj4pvhuprowa1awz8s0642

任何帮助和指导都会非常感激。

这是我对文件名TicTacToeBoard.java

的代码
import java.util.ArrayList;


public class TicTacToeBoard extends GameBoard
{
/**
 * The pieces in this game.
 */
ArrayList<TicTacToePiece> GamePieces;

/**
 * Constructor. Instantiate the GamePieces ArrayList.
 */
public TicTacToeBoard()
{
    // YOUR CODE HERE
    super(0, 0);
    GamePieces = new ArrayList<TicTacToePiece>();
}

/**
 * empty out the GamePieces ArrayList
 */
public void Reset()
{
    // YOUR CODE HERE
    GamePieces.clear();
}


/**
 * Fill a space with the newPiece, IF THAT SPACE IS EMPTY.
 * 
 * @param x the first, horizontal coordinate for the next move 
 * @param y the second, vertical coordinate for the next move
 * @newPiece the piece to place at the location
 */
public void AddPiece(int x, int y, TicTacToePiece newPiece)
{
    // YOUR CODE HERE
    GamePiece gp = new GamePiece(x,y);


    gp.GetPosition();


//      GamePieces.add((int) gp.GetPosition(), newPiece);


}

/**
 * Get a GamePiece at a specific position.
 * 
 * @param x the first, horizontal coordinate for the next move 
 * @param y the second, vertical coordinate for the next move
 * @return the game piece at position x, y. or null if there is none
 */
public TicTacToePiece GetPieceAt(int x, int y)
{
    // YOUR CODE HERE


    return null;

}

/**
 * Checks the board for win or draw conditions and update the GameState property appropriately.
 * 
 * @return the GameStatus of this game
 */
public GameStatus CheckForWin()
{
    TicTacToeGame t = new TicTacToeGame();


    if(t.GetGameState() == GameStatus.ON)
        return GameStatus.ON;
    else if(t.GetGameState() == GameStatus.WIN_PLAYER_1)
        return GameStatus.WIN_PLAYER_1;
    else if(t.GetGameState() == GameStatus.WIN_PLAYER_2)
        return GameStatus.WIN_PLAYER_2;
    else
        return GameStatus.DRAW;


    // YOUR CODE HERE

}

/**
 * Create a Board[][] array. This is a helper function that I used so that I could reuse code from Assignment 1. You do not have to implement this method. 
 * 
 * @return a two dimensional array of Strings
 */
private String[][] GetGameBoard()
{
    // YOUR CODE HERE
    String[][] Board = new String[3][3];

    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
            Board[i][j] = "-";

    return Board;
}

//  /**
//   * Checks a string for win conditions. If three in a row occur, then it returns the proper GameState.
//   * This is a helper function that I used, but is not required for you to implement.
//   * 
//   * @param Input a representation of a row, column, or diagonal in the game. 
//   * 
//   * @return the proper GameStatus for a row, column, or diagonal represented by the Input String
//   *         "---" would indicate an entirely free row, column or diagonal, in which case it should return GameStatus.ON.
//   *         "000" indicates a row, column, or diagonal that has been won by player     1.
//   *         "111" indicates a row, column, or diagonal that has been won by player 2.
//   */
//  private GameStatus CheckStringForThree(String Input)
//  {
//      // YOUR CODE HERE
//  }

/**
 * Print the game board to stdout.
 * 0 should be used to represent moves by player 1.
 * 1 should be used to represent moves by player 2.
 * - should be used to represent a free space.
 * One blank space should occur between each space.
 * So an empty game board would be
 * - - -
 * - - -
 * - - -
 * And a game might look like
 * 0 - 1
 * 0 - -
 * 1 - 0
 * WARNING: If you are storing the game board as Board[x][y], then the traditional nested loops won't 
 * print the board properly. x should be the horizontal coordinate. y should be the vertical coordinate.
 */
public void Print()
{
    // YOUR CODE HERE

    for(int r = 0; r < 3; r++)
    {
        for(int c = 0; c < 3; c++)
        {
                System.out.print(GetGameBoard()[r][c]);
        }
        System.out.println();
    }
    System.out.println(GamePieces);
}

}

2 个答案:

答案 0 :(得分:0)

您的TicTacToePiece包含x和y值

public TicTacToePiece(int newX, int newY, int newPlayerNumber, String newSymbol)

您的GamePieces由TicTacToePieces

组成
GamePieces = new ArrayList<TicTacToePiece>();

由于您的AddPiece函数将x,y,newPiece作为输入,您可能需要

  1. 检查棋盘中的位置(x,y)是否被占用,你可以通过遍历arraylist来实现这个目的
  2. 如果位置未被占用,请在必要时适当设置newPiece s x,y值后,将新片添加到电路板(arraylist)。
  3. 编辑:::

    for (TicTacToePiece tttp : GamePieces) {
    /* Note that this 'tttp' is each element in the Gameieces arraylist
       You are just iterating through all elements in the array checking if condition meets */
        if (tttp.x == x && tttp.y == y) {
            //x and y value match, do something
        }
    }
    

    以上相当于以下代码

    for (int i = 0; i < GamePieces.size(); i++) {
        if (GamePieces.get(i).x == x && GamePieces.get(i).y == y)
               //dosomething
        }
    }
    

答案 1 :(得分:0)

TicTacToe位置也可以在ArrayList中表示。在这里,我们不必使用二维数据结构制作电路板。

例如TicTacToe游戏有九个位置,您可以将位置存储在大小为9的arraylist中,并存储0到8个索引的位置。

如果要将X和Y位置映射到ArrayList,请使用公共HashMap,它将X和Y位置与ArrayList索引映射如下(此映射应该在最初完成)

HashMap<Integer, Integer> hMap=new HashMap<Integer, Integer>();
        hMap.put(00, 0);
        hMap.put(01, 1);
        hMap.put(02, 2);
        hMap.put(10, 3);
        hMap.put(11, 4);
        hMap.put(12, 5);
        hMap.put(20, 6);
        hMap.put(21, 7);
        hMap.put(22, 8);

在映射到ArrayList中的第0个索引的hMap 00中,01映射到第1个索引并继续。

因此,当您从function参数获取x和y值时,构造'xy'字符串并从ArrayList获取相应的位置,如下所示

        int pos=0;
        int x=1;
        int y=2;

        pos=Integer.parseInt(String.valueOf(x)+String.valueOf(y));
        int listPos=hMap.get(pos);

要获取X和Y位置,遍历所有ArrayList对象,获取索引并使用索引从HashMap获取X和Y值。