2 problems, 20 points for #1 and 30 points for #2
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:
Board object of the
requested size (using the constructor from the Board
class).
There are no additional required methods to write in the CS5App class
Required Data Members to have in the Board class
Board class should have three data members:
private means
that they can only be accessed within the Board class.
data is a 2d array
of characters. You should represent an empty slot by
' ', the space character. You should represent player X's
checkers with an 'X' (the capital X character) and you should represent
player O's checkers with an 'O' (the capital O character).
Methods required for the Board class Some are provided
in the zip file... .
public Board(int rows, int cols) (provided) Board objects that
takes two input arguments: the number of rows (rows)
and the number of columns (cols). Inside this
constructor, you should set the values of all
three data members of the Boardobject,
including the definition of data as a new
2d array of the appropriate size. Also,
you should go through this 2d char array
and initialize each element to ' ', the space character.
public void print() (provided) Board object that
calls it. Here is an example from a 6-row, 7-column Board:
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |O| |O| | |
| |X|X|X|O| | |
---------------
0 1 2 3 4 5 6
Basically, each "checker" takes up one space and all columns
are separated by vertical bars (|). The columns are labeled at
the bottom. When the label numbers reach double digits,
you should only print the final digit (the user will be able
to tell which column in which). For example, here is
a 6-row, 15-column board:
| | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | |
-------------------------------
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
See the example run for
more examples of what the board should look like as a game is played.
public boolean allowsMove(int c) true if the calling
Board object can allow a move into column c
(because there is space available).
It returns false if c does not have space available
or if it is not a valid column. Thus, this method should check to
be sure that c is within the
range from 0 to the last column and make sure that
there is still room left in that column!
public void addMove(int c, char player) (provided) player's checker
into column c. Note that the code will have to find the highest
row number available in column c and put the checker in that row.
The highest row number available is the first row with a space
character ' ' in column c. Notice that the
hoghest row number corresponds to the lowest physical
row on the board.
public boolean winsFor(char player) true if the calling Board
is a winner for the character player. It should return
false otherwise. You may assume that
player will be an 'X' or an 'O'.
Important Note: you need to check if the player has
won horizontally, vertically, or diagonally. There are
two directions for a diagonal win: from NW to SE and from NE to SW.
public boolean isFull() true if the calling Board
is completely full, tha is, if there are no spaces left for additional moves.
Otherwise, it should return false.
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.
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:
Date class representing that date.
You may assume here and throughout that the user will
input a valid date!
(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
Which choice would you like?
Of course, you will need to implement the different options
on this menu. In doing so you will need to use the following
methods.
Additional required methods to write in the CS5App class
public static void printMenu()main
method.
The Date class -- data members
Date class should have three data members:
private keyword in Java indicates that
no other class will be able to access these data members
directly.
The Date class -- required method to write
public Date(int m, int d, int y) (provided in class) Date objects that
takes three input arguments: the month m, the
day d, and the year y.
public void print() (provided in class) Date object
that calls it in the following familiar (month/day/year) format:
11/14/2001
public boolean isLeapYear() (provided in class) public void yesterday() Date object that
called it by moving it one day back in time.
It should not prompt the user or print out anything (that will
be done elsewhere). It should, however, take leap years into
account. (See Notes #1 and #2 below.)
public void tomorrow() Date object that
called it by moving it one day forward in time.
It should not prompt the user or print out anything (that will
be done elsewhere). Again, it should take leap years into account.
public boolean isBefore(Date d2) true if the calling
Date is earlier in time
than the input argument d.
It should return false otherwise.
public int diff(Date d2) Date object that calls the method and
the Date object d2 that is input.
If d2 is later than the calling object,
the result should be positive. If d2 is earlier
than the calling object, the result should be negative.
public int diffNoPrinting(Date d2) public String dayOfWeek() String that indicates
the day of the week of the Date object that calls it.
That is, this method returns one of the following Strings:
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", or "Sunday". This method
should work by creating a new Date whose day of the week you
already know. Then, it should call diffNoPrinting and
take the result modulo 7 (% 7) to obtain the day of the
week in question.
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.
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).