Connect 4 in GridWorld practice problem 2

Note: GridWorld will not be featured on the 2015 and subsequent AP CS Exams.

As the name suggests, the AP Computer Science GridWorld Case Study is based on a grid. The GridWorld Student Manual places Actor objects in the grid; however, it can actually store objects of any type. This makes the grid useful for many 2 dimensional games such as Connect 4, tic tac toe, chess, checkers and Go.

In this project, you will write several methods to complete an implementation of Connect 4. You can also extend the project with a rule based or artificially intelligent computer player. You and your friends can even write your own computer players and run them against each other.

Download the Connect 4 in GridWorld skeleton code and import it into your IDE. The code is setup as an applet so you can display your results on a website if you wish. (Eclipse works well with applets but not all IDEs do.) As with all GridWorld projects, you must setup your IDE to reference the gridworld.jar file distributed with the GridWorld Case Study.

The code to handle clicks on the board has been included for you; however, it depends on several methods of the BoardUtilies class that you must write. BoardUtilies contains placeholders for these methods as well as documentation for each incomplete method. Provide correct implementations for each method.

To create your own computer players, extend the Player class and implement the move method. A RandomPlayer class has been provided that selects a random valid move. RandomPlayer illustrates how a computer player can use the methods of BoardUtilies.

Demonstrations of completed implementations for 1 and 2 players are available on my other site.

See the completed BoardUtilities class or review it with AP CS Tutor Brandon Horn.

Get AP CS Help

GridWorld Practice Problems

GridWorld Case Study Explanations

2 thoughts on “Connect 4 in GridWorld practice problem

  1. Reply amanda Feb 5,2013 7:58 am

    heres my code can someone help me fill in the blanks?!

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

    public class TicTacToeWorld extends World
    {
    private String whosTurn;
    private boolean gameOver;

    public TicTacToeWorld()
    {
    super(new BoundedGrid(3,3));
    whosTurn=”X”;
    gameOver=false;
    setMessage(“Welcome to TIC TAC TOE! – – Click a spot – “+whosTurn+” turn.”);
    }

    //this method is called when a mouse click occurs
    //this method will draw Xs and Os at the location clicked
    public boolean locationClicked(Location loc)
    {
    Grid grid = getGrid();
    if(grid==null)
    return false;

    //if the game is over, clear the board and get ready to play a new game
    if(gameOver==true)
    {

    //clear the board

    return false;
    }

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

    }

    return true;
    }

    //this method will be called each time the step button is pressed
    public void step()
    {

    }

    //this method will determine if someone has won the game
    public String getWinner()
    {
    Grid grid = getGrid();
    if(grid==null)
    return “no winner”;

    String winner=””;
    for (int r = 0; r<grid.getNumRows(); r++)
    {
    Piece row0 = grid.get(new Location(r,0));
    Piece row1 = grid.get(new Location(r,1));
    Piece row2 = grid.get(new Location(r,2));

    if(row0==null||row1==null||row2==null) continue;

    if(row0.getName().equals(row1.getName())&&row0.getName().equals(row2.getName()))
    {
    winner=row0.getName()+" wins horizontally!";
    break;
    }
    }

    //check for vertical winner

    //check for diagonal winner

    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
    Grid grid = getGrid();

    return true;
    }

    //this method will clear the board of all Xs and Os
    public void resetWorld()
    {

    }
    }

Leave a Reply