Connect4

Connect4

Due by midnight on Monday February 17th

Objectives

Become intimately familiar with the GameBase and AgentBase classes and use them to write a game and a bot that can be run by our web-app. Gain experience with Object Oriented Programming in Ruby. This assignment has nothing to do with Rails.

Introduction

As you know, we shall be working on an existing Rails web application in a couple of weeks. Project4 is intended to give you some idea of what our Rails web-app is actually used for. The web-app provides a general purpose engine for turn-based games (board games are the classic example) and bots (A.I.) that play those games against each other. So where do these games and bots come from? You have to write them in Ruby! Just to be clear, the games and the bots have absolutely nothing to do with Rails. The games and bots are Ruby programs that you upload to the web-app through your browser under a web-app user acount. Then you can ask the web-app to make specific bots play a death-match against each other for some particular game - it's like Gladiator but with A.I. Obviously the games and bots must all be written in a particular way so that the web-app knows how to run them, and that's what you're going to learn in this project, we just won't be using the web-app *yet*. Games and bots can be run locally like any other Ruby program which is far more convenient for their development and testing cycle, and that's how you'll run them for Project4. Think of your laptop as your training camp and the web-app as the Colloseum where you will one day bask in the glory of your victory while the corpse of your classmate's program lies beneath your sandal :)

Description

  1. Study the base classes


    We provide an object oriented programming contract in the form of base classes from which all games and bots MUST inherit.

    The contract's objectives are as follows:


    1. Inform game authors how to write a game that can be:
      • Run by our web-app.
      • Played by bots that also follow the contract.
    2. Inform bot authors how to write a bot that can play one or more game(s) in our web-app

    3. Be general enough to allow:
      • Game authors to write whatever game they want.
      • Bot authors to achieve whatever behavior/strategy they want.
    4. Be restrictive enough to prevent:
      • Incompatibility between games and the web-app or between bots and games.
      • Bot authors from being able to cheat.
    5. Provide framework code so that authors need only write code specific to their game or bot

    Study the files gamebase.rb and agentbase.rb. Consider how writing games and bots as classes that inherit from GameBase and AgentBase respectively achieve the stated objectives of our programming contract. How well do you think these base classes achieve the objectives? Can you think of any improvements? Write your answers in English (include code snippets only for clarity), in a file contract.txt. If you're feeling brave you may also look at stateholders.rb which provides data structures that game authors can use to expose game state to bots while preventing unwanted modifications to that state. DO NOT MODIFY THESE FILES since the programming contract only works if we all use precisely the same base classes.


    So that you can see example classes that inherit from the base classes, here is an example game, a simplified single-hand version of blackjack, and an example of a very stupid bot.

    To see the Blackjack and FirstPlayer classes in action, download their files to your machine and put them in the same directory along with the three base class files above. At a command prompt run

    ruby blackjack.rb

    and then study blackjack.rb to figure out how the output was produced. Note that to really understand you need to look in GameBase as well.


  2. RandomPlayer


    Write a class RandomPlayer that inherits from AgentBase. You should put your code in a file called randomplayer.rb. This bot should be able to play ANY game that follows our contract by simply making a move at random. You only need about 10 lines of code and two methods at the most.


  3. Connect4


    Write a class Connect4 that inherits from class GameBase. Put your code in a file called connect4.rb. The purpose of your class is to implement the game of Connect Four. The preceding link tells you all about it. If you have never played it, there is an applet you can play online here.


    1. To help you get started some skeleton code is provided for you: connect4_skeleton.rb. You should fill in all the empty methods. You may of course add your own helper methods but these should be made private. My Connect4 solution is under 100 lines long.
    2. Notice in GameBase#initialize that the first line is @state = Vector.new. This is an adequate container for a linear representation of the board state and for this assignment you are required to use it as is. So you need to play with Vector objects in irb to get a feel for them. Start by trying the following in irb:
      load 'stateholders.rb'
      v = GameBase::Vector.new
      v.class
      v.public_methods.sort  # looks like an array doesn't it
      v << 1
      v.length
      v << true
      v.length
      v[1]
      v[1] = false
      v[0] += 1
      v  # seems to act like an array too, until...
      v << "boom"  # strings are mutable in Ruby and so they are banned from Vector
      v << ["I am a real Array"]  # same goes for Arrays and Hashes
      

Grading Criteria

Your code will be graded on style and correctness and should be as elegant as possible. Marks will be deducted for needlessly long code. I will test your connect4.rb by running it as a stand alone program from the command prompt and I would appreciate a nicely formatted display of the final board state similar to the one I showed in class. Here is an example of appropriate pogram output.

Submission Checklist

  • contract.txt
  • randomplayer.rb
  • connect4.rb

DO NOT SUBMIT THE BASE CLASSES