NetLogo banner

 Home
 Download
 Help
 Resources
 Extensions
 FAQ
 References
 Contact Us
 Donate

 Models:
 Library
 Community
 Modeling Commons

 User Manuals:
 Web
 Printable
 Chinese
 Czech
 Japanese

  Donate

NetLogo User Community Models

(back to the NetLogo User Community Models)

connect4

by Michael C Gizzi (Submitted: 11/18/2003 )

[screen shot]

Download connect4
If clicking does not initiate a download, try right clicking or control clicking and choosing "Save" or "Download".

(You can also run this model in your browser, but we don't recommend it; details here.)

WHAT IS IT?

This model is a Netlogo version of the classic Milton Bradley/Hasbro game "Connect Four: The Vertical Four-in-a-Row Checkers Game." It is intended to be a two-player game, and is entirely for fun!

The rules are simple. Each player places a colored-checker in one of the seven slots. The first player to get four in a row, column, or on a diagonal wins.

HOW IT WORKS

SETUP creates the game board, with seven columns and six rows. The grey space at the top of the screen is necessary because Netlogo's graphic canvas won't permit the exact dimensions of the game space. The white "holes" are turtles that serve as place-holders for the colored checkers. They have no other function in the game.

SETUP-COLUMNS-AND-ROWS is probably not necessary, but it simplifies the procedure used to evaluate whether the game has been won.

ADD-PIECE [x] is called by the seven buttons on the bottom of the screen. They are used by the players to add each checker to the board. ADD-PIECE [x] finds the first "empty" space in the relevant column, and inserts a checker of the player's color. This procedure uses a list to evaluate possible y coordinates to find the first empty space in a column. Once placed on the board, the new turtle sets its color to that of the player, and uses SET-POSSIBLE-HEADINGS to set its own list of possible headings for evaluating each move.

CHECK-FOR-WINNER is only run after seven moves have been completed. This procedure uses the single-headings and double-headings lists to look in each of the legal directions to determine if the most recently-played checker has won the game. SETUP-SINGLE-HEADINGS creates the list for each piece based on its row and column.

CHECK-FOR-WINNER looks first to see if the latest piece is at the end of a chain of 4 like-checkers (single-headings), and then looks to see if it is inside (not on the end) of a string of four checkers. This analysis involves looking forward two checkers on a heading, and then "turning around" (by adding 180 degrees to the heading) to look at the two checkers in the opposite direction. The double-heading analysis is done in three steps, with different rules depending on the xcor of the checker. The replication of code is necessary to avoid accidental wrapping of the screen producing a false win.

When the game is over, the command window prints which side won, and in how many steps. A dialog box is opened to play again.

HOW TO USE IT

It's pretty self-explanatory. Find a friend, and start seeing who can win the game first. Its the 1970's version of outwit, outlast, and outplay!

If you want to change the starting player's color, use the SELECT-PLAYER-1 choice and hit UPDATE COLORS.

Turn on SHOW-WINNER? to reveal the winning pattern when the game is over.

Turn on SHOW-LAST-PLAYED? to highlight the most recent checker piece played.

If you accidentally hit one of the column buttons twice, or if you made a move you want to take back, hit the UNDO MOVE button!

THINGS TO NOTICE

The code for this model is relatively short, with just three critical procedures. (STARTUP loads the model but isn't necessary, nor is UNDO-MOVES, SETUP-ROWS-AND-COLUMNS, and UPDATE-COLORS).

Notice how the ADD-PIECES and CHECK-FOR-WINNERS procedures enable the game to work. That is the heart of the model. While CHECK-FOR-WINNER is long, it basically repeats the same procedures multiple times.

The game will only run under Netlogo 2.0 and later because of its reliance on the "ask patch-at-heading-and-distance" primitive.

The model is not really "agent-based" in the traditional sense of the word, but it illustrates the robustness of the Netlogo environment and its ease of use. And it is just plain fun to play!

EXTENDING THE MODEL

One possible extension would be to build a procedure that would provide a "Help" mode - either pointing out existing patterns of three like-checkers or warning when a player is
"in check."

Another extension would be to have the checkers appear at the top of the screen, and then "drop" into place in each column.

Re-write the Add-Pieces code so that the last piece has a visual marker on it.

A one-player version of the game (with a computer opponent) is probably the most challenging extension to the model, and would certainly take longer than the initial model to code, but it would be an amazing accomplishment!

RELATED MODELS

This model is related to the game models in the Netlogo Models Library. Unlike Frogger and Pac-Man, which require the awkward use of buttons to replicate the original joysticks, CONNECT FOUR plays completely like the original - withthe one exceptions of having the ability to make all the checkers drop to the table.

CREDITS AND REFERENCES

This model was inspired by the Hasbro game "CONNECT FOUR" and is dedicated to Nicholas Gizzi, an enthusiastic six year-old who is fascinated by the original Connect Four, and is now learning to move virtual turtles around a computer screen! He dutifully beta-tested this model.

The model was created by Michael C. Gizzi with assistance from Boyce Baker and Richard Vail. The model is copyright 2003 by the Center for Agent-Based Modeling, Mesa State College, 2508 Blichman Avenue, Grand Junction, CO 81505. http://www.modelingcomplexity.org.

The original board game CONNECT FOUR is copyright Hasbro 1998.

THE MODEL IS INTENDED SOLELY FOR EDUCATIONAL AND ENTERTAINMENT PURPOSES. IT MAY NOT BE REPRODUCED FOR COMMERCIAL USE WITHOUT VIOLATING EITHER OF THE COPYRIGHTS LISTED HERE.

(back to the NetLogo User Community Models)