Homework 10



Due: 11:59 pm on Sunday, November 7, 2004 (M/T sections)
or 11:59 pm on Monday, November 8, 2004 (W/Th sections)

Reading: CS 5 notes, week 10

2 problems, 20 points for #1 and 30 points for #2



Problem 1     Connect Four     [topics: practice with Classes and Objects]

Connect four is a variation of tic-tac-toe played on a rectangular board:



The game is played by two players, alternating turns, with each trying to place four checkers in a row vertically, horizontally, or diagonally. One constraint in the game is that because the board stands vertically, the checkers can not be placed in any position. A checker may only be placed at the top of one of the currently existing columns (or it may start a new column).

The Board class -- a preview

In this problem, you will need to create a class named Board that implements some of the features of the Connect Four game. The Board class will have three data members: there will be a 2d array of chars to represent the game area, an int holding the number of rows, and an int holding the number of columns. The details of the Board class appear after a description of what main in the CS5App class should do.


The CS5App class

The main method of the CS5App class will allow two people to play the game. (Next week's assignment will ask you to write an automated player that will take on human -- or computer -- opponents.)

The players will be represented by characters -- the player to go first will be represented by 'X' (a capital X) and the player to go second will be 'O' (a capital O).

Your main method inside the CS5App class should do the following:

There are no additional required methods to write in the CS5App class


Required Data Members to have in the Board class

Methods required for the Board class    Some are provided in the zip file... .


Sample Run

Welcome to Connect Four!

Please enter the number of rows and columns you would 
like the game board to have (between 4 and 15 inclusive):  6 6


| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
-------------
 0 1 2 3 4 5

X's choice:  3

| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | |X| | |
-------------
 0 1 2 3 4 5

O's choice:  4

| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | |X|O| |
-------------
 0 1 2 3 4 5

X's choice:  2

| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | |X|X|O| |
-------------
 0 1 2 3 4 5

O's choice:  4

| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | |O| |
| | |X|X|O| |
-------------
 0 1 2 3 4 5

X's choice:  1

| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | |O| |
| |X|X|X|O| |
-------------
 0 1 2 3 4 5

O's choice:  2

| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | |O| |O| |
| |X|X|X|O| |
-------------
 0 1 2 3 4 5

X's choice:  0


X wins -- Congratulations!

| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | |O| |O| |
|X|X|X|X|O| |
-------------
 0 1 2 3 4 5

Submission    Be sure to submit your CS5App.java file under homework 10, problem 1.






Problem 2     Virtual Art!     [topics: Writing a class and using objects]

For this problem, you'll be implementing Professor Art Benjamin (of HMC's math department). Among Prof. Benjamin's many talents is the ability to compute in his head facts about the calendar. (You should ask him what day of the week you were born some time...). In this problem, you'll write a program that will be able to stand in for Prof. Benjamin -- at least in terms of calendar computations -- during this year's sabbatical.

The Date class -- just a warning

This problem is the first in which you will need to write your own Java class. It should be named Date. This class should be placed in your CS5App.java file, outside of the CS5App.java class. The Date class should contain three data members and a few methods. All of the details are explained below, after an explanation of what should go in the CS5App class:


The CS5App class

Your main method inside the CS5App class should do the following:

Additional required methods to write in the CS5App class


The Date class -- data members

The Date class -- required method to write


Sample Run

Here is an exmple run. Notice that the date is always printed out before the menu -- as a result, you don't have to print out the date an additional time after choosing options 1, 2, or 3. (Yes, this makes option #1 quite easy to implement!)

To save space, the menu has been replaced by <Menu Here> after the first couple of times:

Welcome to the date calculator...

Enter a date you're interested in.
  Enter a month (1-12): 10
  Enter a legal day (of that month): 28
  Enter a year: 1929

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

The date under consideration is  10/28/1929


(0) Enter a new date of interest
(1) Print the current date of interest
(2) Move one day forward in time
(3) Move one day backward in time
(4) Day difference finder
(5) Find the day of the week

(9) Quit

   Your choice:  1

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

The date under consideration is  10/28/1929

<Menu Here>

   Your choice:  2

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

The date under consideration is  10/29/1929

<Menu Here>

   Your choice:  3

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

The date under consideration is  10/28/1929

<Menu Here>

   Your choice:  4

Enter another valid date:
  Enter a month (1-12): 11
  Enter a legal day (of that month): 1
  Enter a year: 2004

10/31/2004
10/30/2004
10/29/2004
... lots of dates omitted ...
10/30/1929
10/29/1929

Between 10/28/1929 and 11/1/2004 there are  27398  days.

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

The date under consideration is  10/28/1929

<Menu Here>

   Your choice:  5

The date 10/28/1929 is a Monday.

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

The date under consideration is  10/28/1929

<Menu Here>

   Your choice:  0

Enter a new date of interest:
  Enter a month (1-12): 7
  Enter a legal day (of that month): 4
  Enter a year: 2076

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

The date under consideration is  7/4/2076

<Menu Here>

   Your choice:  5

The date 7/4/2076 is a Saturday.

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

The date under consideration is  7/4/2076

<Menu Here>

   Your choice:  4

Enter another valid date:
  Enter a month (1-12): 11
  Enter a legal day (of that month): 1
  Enter a year: 2004

11/2/2004
11/3/2004
... lots of dates omitted ...
7/3/2076
7/4/2076

Between 7/4/2076 and 11/1/2004 there are -26178 days.

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

The date under consideration is  7/4/2076

<Menu Here>

   Your choice: 9

NOTE: Recall the rules for whether or not a year is a leap year:

Submission    Be sure to submit your CS5App.java file under homework 10, problem 2.



Extra Credit     Mouse input for connect-four     [topics: event-handling with graphical interfaces]

For extra credit this week, you may augment problem 1 so that the user can input a checker by clicking anywhere in the appropriate column with the mouse. Also, consider adding an animation that shows the "checker drop," as well as the "clear the board" effect that makes the physical game as appealing as it is... . Any other additions or capabilities are also welcome.

Important Note     If you do this extra credit, do not submit it in place of Hw10 Pr1 -- instead, complete the regular connect-four problem and submit it, then work on this problem. Be sure to start from the third zip file, Hw10ExCr.zip, which has a hook so that you can access mouse information from the CS5App class.

Hints    The Hw10ExCr.zip code has an extra method named handleMousePressed in CS5App. It is called whenever the mouse button is pressed, and it print the row and column coordinates of the mouse location -- note that they are doubles. Here is one way you might approach this:

There are a couple of other things to worry about, but that's why it's extra credit after all! - Good luck!

version of problem 1 in the usual spot (Hw10 Pr1).