MP2: ConnectN

Maybe you play them, maybe you don’t, maybe you carry your gaming desktop with you at all times—but regardless of how you feel about it, a fair number of people use computers to play games. Some of those games can be instructive, some can be fun. But an MP where you get to build a fun game is both fun and instructive! For MP2 you’ll be learning Java class design and object-oriented programming as you complete a simple Connect-4-like game. You’ll also have a chance to continue sharpening your Android development skills.

MP2 is due Monday 3/4/2019 @ 5PM. To receive full credit, you must submit by this deadline. In addition, 10% of your grade on MP2 is for submitting code that earns at least 50 points by Monday 2/25/2019 @ 5PM. Note that, unlike MP1, you have to do a bit more work on MP2 to get to 50 points.

As usual, late submissions will be subject to the MP late submission policy.

1. Learning Objectives

The purpose of MP2 is to begin introducing you to object-oriented programming. MP2 does not explore more advanced topics like inheritance, interfaces, or polymorphism. It sticks to the basics: instance and class variables, getters and setters, constructors, and instance and class methods. You’ll begin to learn how to:

  1. design a class that meets a given specification

  2. implement that class including both properly-protected variables and appropriate methods

We’ll also introduce you some new parts of this simple Android app—including multiple activities, dynamic layouts, and modifying UI element visibility.

We’ll also continue to reinforce the learning objectives from MP0, and MP1: imperative programming, working with 2D arrays 1, and Android app development and testing.

2. Assignment Structure

Like MP0 and MP1, MP2 is a single Android app with its source code divided into two pieces:

  • /lib/: a library that manages the ConnectN game state. 60 / 100 points on the MP are for completing the library, which requires that you complete the implementation of the ConnectN class in

  • /app/: an Android app for you to use for your own interactive testing. The Android app is almost complete, but needs a few modifications from you. 20 / 100 points on the MP are for completing the app, which requires that you understand the code that we have provided and make minor changes. And, to work correctly, you need to finish the library in lib.

Your primary job is to complete the game logic in Like MP1, this file doesn’t even exist yet. You’ll have to create it, and then declare, complete, and document the required functions as listed in the official online documentation. These make use of a player class defined in These functions are called by the app and, once you complete them, your app will be almost completely functional—and we’ll be happy to help you with the rest.

As always, you may find our official MP2 online documentation helpful in understanding what each function and class is supposed to do.

The MP2 app is a bit more complicated than the MP1 app 2. It has two activities: and, each corresponding to one screen of the app. Unlike the previous apps, this app has two different screens: one to set up the game and a second to render it as it is being played. also includes code that creates a custom game layout, allowing the game board to be sized according to parameters entered by the user.

As usual you don’t need to fully understand either file yet, but you do need to explore and experiment with them to finish the app and earn full credit.

2.1. Obtaining MP2

Use this GitHub Classroom invitation link to fork your copy of MP2. Once your repository has been created, import it into Android Studio following our assignment Git workflow guide.

2.2. Your Goal

At this point you should be familiar with the requirements from previous MPs. See the grading description below.

3. Approaching MP2

While the concepts are new, MP2 is not a lot of code to write, and you have two weeks. But here are the bits that are likely to trip you up.

3.1. Read the Fine Manual

To complete MP2 you must understand the specification. That’s your starting point, and your returning point when you get confused.

Writing code to a specification is an important skill to learn. In many software companies, designing code and implementing it are not always done by the same group. A project manager or program manager may write a specification, which is provided to developers to implement. Or a team of developers may come up with a specification, and then divide up the implementation work between them.

Regardless of where it comes from, writing code in groups almost always requires a specification to be successful—unlike when you do independent projects and can make up things as you go. A lot of the design work and high-level decisions end up going in to the specification, so by the time the implementation begins the hard and interesting choices have been made. That’s not to say it still isn’t fun at that point—even developing to a spec provides huge opportunities for simultaneous creativity and correctness in that unique programming way.

3.2. Getters and Setters

MP2 requires you to implement some simple getters and setters (just return or change the variable), and some ones with more complicated behavior. Be sure to read the documentation carefully.

3.3. Game Logic

You will need to do a bit of work to implement the game board. ConnectN is a Connect-4-like game. Tiles fall in the top (large Y values) and sit at the bottom. We’re in the proper first quadrant of 2D space, with X values going left to right and Y values going down to up:

(0, 2)

(1, 2)

(2, 2)

(0, 1)

(1, 1)

(2, 1)

(0, 0)

(1, 0)

(2, 0)

A ConnectN game ends when either player plays N consecutive tiles in either a single row or column 3. Tiles always have to be played at the lowest possible location in each column—in a real Connect-4 board they fall downward and rest on the tiles below them. Consult the Wikipedia page for more details.

3.4. Getting Help

The course staff is ready and willing to help you every step of the way! Please come to office hours, or post on the forum when you need help. You should also feel free to help each other, as long as you do not violate the academic integrity requirements.

4. More About Android

MP2 continues introducing new Android ideas. You’ll still have to poke around a bit to finish your app, although at this point you should be comfortable doing that from previous MPs.

4.1. Multiple Activities (or Screens)

Until this point all of our MP apps have only had a single activity (or screen). In MP0 that single screen showed location data. In MP1 that single screen showed your transformed image and background.

But a typical app has multiple screens which a user can navigate to, each displaying different information. For example, a chat app might have a contacts screen and a settings menu along with the main chat display. An email app might have a screen for navigating between different folders or labels, a settings screen, and the main inbox or folder screen.

In Android each screen is typically represented by a separate Activity, each in its own class. In the screencast above we walk through how activities are launched, can launch other activities, and can pass information to launched activities using Intents.

4.2. Dynamic Layout

Our previous MP apps also had a single static layout per activity, which we created using Android Studio’s UI designer. This is fairly normal for many apps when core components of the layout don’t change. You set up a layout using the designer and then populate each field with appropriate content depending on the data available to the app.

MP2, however, has different requirements, because configuring the game differently can and should produce different board views. To accomplish this we create a layout programmatically—using code in our app—rather than statically. The screencast above walks you through how this is done. If you are considering creating a game for your final project you may be curious in exploring this further, since many games need various kinds of dynamic layouts.

4.3. UI Element Properties

Finally, a common way to interact with the user is to modify the properties of existing UI elements. You might create a text view using the layout designer when you are creating your app, but then have the text shown in the view be determined and modified when the app runs. Alternatively, apps can modify the visibility of UI elements to react to changes in the app state—for example, to display or hide a warning message appropriately.

The screencast above uses an example from the MP2 configuration activity to show how this is done. You’ll need to do something similar in the board activity to earn full credit on MP2.

5. Grading

MP2 is worth 100 points total, broken down as follows:

  1. 60 points:

    • 15 points total for completing the width, height, and N value getters and setters

    • 10 points for completing the constructors

    • 15 points for completing the get and set board functions

    • 10 points for determining the game winner properly

    • 10 points for class static methods, including equality checks and factory creation methods

  2. 20 points:

    • 10 points for properly making the game tiles clickable

    • 10 points for adjusting the UI properly on each turn and when the game ends

  3. 10 points for no checkstyle violations

  4. 10 points for submitting code that earns at least 50 points before Monday 2/25/2019 @ 5PM.

5.1. Test Cases

As in previous MPs, we have provided exhaustive test cases for each part of MP2. Please review the MP0 testing instructions.

5.2. Autograding

Like MP0 and MP1, we have provided you with an autograding script that you can use to estimate your current grade as often as you want. Please review the MP0 autograding instructions.

6. Submitting Your Work

Follow the instructions from the submitting portion of the CS 125 workflow instructions.

And remember, you must submit something that earns 50 points before Monday 2/25/2019 @ 5PM to earn 10 points on the assignment.

7. Academic Integrity

Here’s how we’ll feel if we catch you cheating in CS 125:

CS 125 is now CS 124

This site is no longer maintained, may contain incorrect information, and may not function properly.

Created 10/24/2021
Updated 10/24/2021
Commit a44ff35 // History // View
Built 10/24/2021 @ 21:29 EDT