Search code examples
javaarraylistpointtic-tac-toe

Add an element in an ArrayList in position x,y


I am trying to finish this assignment of a Tic Tac Toe game. I managed to finish all the other requirements except for the methods AddPiece and GetPieceAt. I've googled just about everything on how to implement this into the ArrayList and how to set it up in an (x,y) of an ArrayList. I feel that I might be understanding the assignment incorrectly but at this point I have no idea what to do. I have some ideas written down on it but I've deleted off most of the things i thought would go in those two methods.

In order to make it less of a hassle to add all the other files here, this is the link of where the assignment is posted. http://go.code-check.org/codecheck/files/1404121614cuepj4pvhuprowa1awz8s0642

Any help and guidance would be really appreciated.

This is the code that i have for the filename 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);
}

}


Solution

  • Your TicTacToePiece contains x and y value

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

    And your GamePieces is consist of TicTacToePieces

    GamePieces = new ArrayList<TicTacToePiece>();
    

    And since your AddPiece function takes x,y,newPiece as inputs, you would probably have to

    1. check if the location (x,y) in the board is occupied, you can do this by iterating through the arraylist
    2. if the location is not occupied, add the new piece to the board (arraylist) after setting the newPieces x, y values appropriately if necessary.

    edit:::

    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
        }
    }
    

    The above is equivalent to below code

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