0% found this document useful (0 votes)
48 views7 pages

Design Phase Connect 4

This program implements a Connect 4 game with additional features. There are 8 classes that implement the model, view, controller design pattern and multi-threading. The game allows two players to drop colored disks (red and yellow) into columns over 10 seconds, after which the disk automatically drops. The goal is to connect 4 of your own disks vertically, horizontally or diagonally. When a player wins or all disks are placed, the game ends.

Uploaded by

api-570817284
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views7 pages

Design Phase Connect 4

This program implements a Connect 4 game with additional features. There are 8 classes that implement the model, view, controller design pattern and multi-threading. The game allows two players to drop colored disks (red and yellow) into columns over 10 seconds, after which the disk automatically drops. The goal is to connect 4 of your own disks vertically, horizontally or diagonally. When a player wins or all disks are placed, the game ends.

Uploaded by

api-570817284
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Connect 4

This program implements Graphic User Interface,


Model-View-Controller Design Pattern, and Multi-Threading.
The goal of this program is to replicate the game Connect 4
with a little twist.

Game Settings/Rules: This is a two player game in which


each player gets 21 one color disks. This means one player
gets the red disks while the other player gets the yellow disks.
The goal of this game is to drop a disk into any column in order
to connect four of your disks. This means there are 3 ways you
can connect the four disks: vertically, horizontally, and
diagonally. This program will have a rule added. That is the
users only have 10 seconds to decide where the disk should
drop. After 10 seconds the disk will drop on the column
currently below it. In this game player 1 holds the red disks
while player 2 holds the yellow disks. If each player plays their
disk before the 10 seconds the timer will restart and count to 10 when the next round starts.

There will be 8 classes to implement. Here is a brief description of what each class will do:
● View: A class that implements the Graphic User Interface
● Model: A class that holds the data of the game. The class will hold the data of the disks
for each player and will hold the data on where players place their disks.
● Controller: A class that will extend Thread and implement ActionListener. This classic
controls the game operation and checks when the game should end.
● LeftArrow: A class that extends AbstractAction that moves the disk to the left of the
GUI.
● RightArrow: A class that extends AbstractAction that moves the disk to the right of the
GUI.
● DownArrow: A class that extends AbstractAction that moves the disk down to a column.
● TimesOut: A class that extends Thread and acts like a timer. It will hold the ability to
count to 10 in order to drop the disk in the column if the user does not decide where to
place their disk.
● ConnectFourGame: A class that creates the View, Model, TimesOut, and Controller
objects. The class starts the threads and sets the GUI visible.
The View Class Extends JFrame

Private Member Data:


JPanel theGame, theDisks, theOther
ImageIcon gameBoard, redDisk, yellowDisk
Public Member Data:
JLabel [ ] [ ] grid
JLabel disk, winner
JButton playAgain:
Public Methods:
● View() - Default constructor. In the constructor initialize both private and public data
members. theGame will have the game board. theDisks will have the red and yellow
disk that will be displayed/moved from left, right, and down. theOther will display the
winner (if there is one) and the button to reset the game. Use the images from the
images folder to set the disks and board of the game.
● Void setRed(int row, int column) - will set a red disk where the user drops their disk.
This is where you will use grid [ ] [ ] to display the red disk on the game board.
● Void setYellow(int row, int column) - will set a yellow disk where the user drops their
disk. This is where you will use grid [ ] [ ] to display the yellow disk on the game board.
● Void setDisk (int player) - puts the correct colored disk on the GUI so the correct player
can move it from left/right/down.
● Void displayWinner(int player) - displayed who the winner is (if there is one) on the
GUI.

The GUI should look like this


The Model Class
Public Member Data:
Int redDisks, yellowDisks
String [ ] [ ] grid

Public Methods:
● Model() - Default constructor. Sets the two int variables (redDisks,yellowDisks) to 21
since these are the number of disks each player gets. Then is sets each index of the
two-dimensional array to empty.
● Void resetBoard() - Sets redDisks/yelloDisks to 21. Sets each index in the grid [ ] [ ]
array to “empty”.
● Int redDisk(int column) - sets “red” in the correct index of the grid [ ] [ ] array based on
where the disk would drop.
● Int yellowDisk(int column) - sets “yellow” in the correct index of the grid [ ] [ ] array
based on where the disk would drop.
● Boolean checkVerticalHorizontal(String disk) - disk string is either red or yellow. This
method should check for vertical and horizontal disk connections. Only 4 connected
disks are required to win the game. Therefore if four connected disks are found return
true. Else return false. (Reference image below)



● Boolean checkMinDiagonally(String disk) - disk string is either red or yellow. This disk
checks if there are four connected disks min diagonally. Only 4 connected disks are
required to win the game. Therefore if four connected disks are found return true. Else
return false. (Reference Image Below)
● Boolean checkMaxDiagonally(String disk) - disk string is either red or yellow. This
disk checks if there are four connected disks max diagonally. Only 4 connected disks are
required to win the game. Therefore if four connected disks are found return true. Else
return false. (Reference Image Below)
The Controller Class Extends Thread Implements ActionListener
Private Data Members:
View theView
Model theModel
TimesOut theTimer
Action LeftArrow
Action RightArrow
Action DownArrow
int col = 0;
int player = 1;

Public Methods:
● Controller(View view, Model model, TimesOut timer) - Constructor takes 3
parameters. The constructor should set the Controller class private data members and
the actionListener to the Play Again JButton from the View object.
● Void startThreads() - called by ConnectGameFour to start the TimesOut thread and
Controller thread.
● Void run() - Overrides the run method from Thread. It calls the timesOut() method after
10 seconds have passed and sets the seconds int variable from the TimeOuts object to
0. This method will call doNothing() to call sleep method and lastly this run method
should constantly be running.
● Void doNothing() - calls sleep method to implement the pause of the Controller Thread.
● Void checkGrid(int player) - calls the methods in the Model object to determine if there
is a winner. If there is a winner or if there are no more disks to play then the GUI should
be updated to display a winner or display a tie and it should suspend both Controller and
TimesOut threads.
● Void timesOut() - is called by the run() method after ten seconds have passed. The
method should place the disk that wasn't played by the user on the column it is above at
that moment.
● Void actionPerformed(ActionEvent e) - Overrides the actionPerformed method from
ActionListener. It should resume Controller and TimesOut threads and reset the game to
a new game.
LeftArrow Class Extends AbstractAction
● Void actionPerformed(ActionEvent e) - Overrides the actionPerformed method from
AbstractAction class. Moves the player’s disk to the left of the GUI. (Reference image
below)

RightArrow Class Extends AbstractAction


● Void actionPerformed(ActionEvent e) - Overrides the actionPerformed method from
AbstractAction class. Moves the player’s disk to the right of the GUI.

DownArrow Class Extends AbstractAction


● Void actionPerformed(ActionEvent e) - Overrides the actionPerformed method from
AbstractAction class. Moves the player’s disk down the selected column of the game
board. (reference image below)
TimesOut Class Extends Thread
Public Data Members:
int seconds = 0;
Public Methods:
● Void run() - Overrides the run method from Thread class. This method should increment
the int variable, seconds. It should call doNothing() in order to call the sleep method and
it should continue to run throughout the game.
● Void doNothing() - calls sleep method to implement the pause of the TimesOut Thread.

ConnectFourGame Class

Methods:
● Public static void main (String[ ] args) - This class should only create the following
objects inside the main method: View, Model, TimesOut, and Controller. Then it should
start both threads to start the multi-threading. Finally it needs to set the GUI visible.

You might also like