0% found this document useful (0 votes)
255 views

CIS 163, Fall 2013, Project 2 Connect Four Game (DRAFT)

The document provides requirements for a Connect Four game project in Java. It outlines 9 steps to create the basic game functionality with a 10x10 grid, and then additional requirements to allow custom board sizes, additional players, an undo button, and tracking of wins per player. The key classes are ConnectFourGame to manage the game logic, ConnectFourPanel for the GUI, and a GameStatus enum. MVC design patterns are also discussed.

Uploaded by

Robert Lewis
Copyright
© Attribution Non-Commercial (BY-NC)
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)
255 views

CIS 163, Fall 2013, Project 2 Connect Four Game (DRAFT)

The document provides requirements for a Connect Four game project in Java. It outlines 9 steps to create the basic game functionality with a 10x10 grid, and then additional requirements to allow custom board sizes, additional players, an undo button, and tracking of wins per player. The key classes are ConnectFourGame to manage the game logic, ConnectFourPanel for the GUI, and a GameStatus enum. MVC design patterns are also discussed.

Uploaded by

Robert Lewis
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 6

CIS 163, Fall 2013, Project 2 Connect Four Game (DRAFT)

Due Date: Friday, October 4, 2013 Before Starting the Project


Review Chapters 6 and 7 from the CIS163 book Read this entire project description before starting

Objectives
Use design that separates view (user interface) from model (game logic/engine). Your instructor will discuss the concept of model view separation in class. Use 2-dimensional array of GUI components Use enum data type Use nested loops to solve complex problems that involve 2-dimensional arrays Pass and/or return array of objects to/from methods

Problem Statement
Write a Java program that plays the Connect Four game and a bit more. You can play this game online here: http://www.mathsisfun.com/games/connect4.html. A player wins the game by connecting four (default) pieces horizontally, vertically, or diagonally. Listed below are the requirements for your program. Sample screen and this figure doesnt include all the requirements of the project.

You must complete steps 1-6 (i.e. basic game) before attempting the requirements specified in steps 7, 8 and 9. Step 1: Create an Eclipse project named ConnectFourPrj Create a package named: project2 Create a class named: ConnectFour

o Chapter 6 has several examples of creating a main method and associated panel class. Create a class named: ConnectFourPanel 1

o See Chapter 6 of your book for a typical example of a panel class. Create a class named: ConnectFourGame o This class contains all the methods for the game of ConnectFour and is shown in Step 6. Create an enum named: GameStatus o See Step 2 for details.

NO OTHER classes can be created without the instructors approval. Step 2: Implement the enum class GameStatus using the following:
There are only 4 possible states the game can be in: {P1WON, P2WON, TIE, INPROGRESS}

Step 3: Implement the class named: ConnectFour:


Using the main program found in Chapter 6 of your book as a guide, creates a JPanel object (ConnectFourPanel). Create two JMenuItems: quitItem and newGameItem. Pass these two JMenuItem objects to the ConnectFourPanel. The quitItem JMenuItem quits the game and the newGameItem starts a completely new game.

Step 4: Implement the class named: ConnectFourPanel: Step 4 does the simple version of connect four with only a 10 X 10 board. Step 7 allows the user to enter in a different size board and different connection lengths.
Create the following properties for the ConnectFourPanel JPanel (add more if you wish): private private private private private private JLabel[][] board; JButton[] selection; ConnectFourGame game; final static in BRDSIZE = 10; // default board size JMenuItem newGameItem; JMenuItem quitItem;

The variable board is a 2-dim array that represents the GUI board that the user sees. The variable selection is used to select the specific column. The variables this.newGameItem and this.quitItem are used to save off the two parameters, newGameItem and quitItem, of the constructor for later use in the actionPerformed method. In the constructor ConnectFourPanel(JMenuItem quitItem, JMenuItem newGameItem) do the following: Create JPanel(s) as needed so that you have a nice looking GUI display Use GridLayout for the JPanel that represents the 10 X 10 board. You will need to use a nested loop, for example: for (int row = 0; row < BRDSIZE; row++) { for (int col = 0; col < BRDSIZE; col++) { board[row][col] = new JLabel (" "); panel.add(board[row][col]); } Create action listeners for every JButton in the 1-dimensional array selection. 2

selection = new JButton[BRDSIZE]; for (int col = 0; col < BRDSIZE; col++) { selection[col] = new JButton("Select"); selection[col].addActionListener(listener); bottom.add(selection[col]); } Set the title of the JFrame to ConnectFour Create a ConnectFourGame object. game = new ConnectFourGame(); Create a private inner class named ButtonListener that implements the ActionListener interface. Create an actionPerformed method that calls the different methods in the ConnectFourGame class using the game object instantiated above. For example, call the game.selectCol method when a user clicks a JButton on the board. The following is some of the code within the actionPerformed method that may be useful you: // Determine which button (i.e. column) was selected for (int col = 0; col < BRDSIZE; col++) { if (selection[col] == e.getSource()) { // tell the game which button was selected. // Game returns what row the chip falls to. // Returning a -1 could mean row is full. int row = game.selectCol(col); } } // Display information on the board using setForeground method (an icon/image could also be used) board[row][col].setForeground(color); // color is of type Color // Determine if there is a winner by asking the game object. (see step 6) if (game.getGameStatus() == GameStatus.P1WON) { JOptionPane.showMessageDialog(null, "Player 1 won + \n The game will reset"); }

Step 6: Implement the class named: ConnectFourGame: Step 6 continues step 5 and does the simple version of Connect Four game with only a 10 x 10 board. Step 7 allows the user to enter in a different size board and connection length.
This class handles ALL of the game activities, and the following methods and properties must be created. Create the following properties for the ConnectFourGame class (create more properties if needed): private int[][] board; private GameStatus status; private final static in BRDSIZE = 10; 3

public ConnectFourGame() A constructor method that initializes the board. For example: status = GameStatus.INPROGRESS; board = new int[BRDSIZE][BRDSIZE];

public selectCol(int col) this method is called from the ConnectFourPanel class and is invoked when the user has selected a JButton representing a column. This method determines what row the chip will fall into. public GameStatus getGameStatus() this method is called from the ConnectFourPanel class and it determines if a player has won the game after the select method (see above) was called. (The following are standard ConnectFour rules): o o o o Return a GameStatus.P1WON if player 1 has four in a row (horizontal, vertical, or diagonal) Return a GameStatus.P2WON if player 2 has four in a row (horizontal, vertical, or diagonal) Return a GameStatus.TIE if all the Cells in the board are not empty and there is no winner Return a GameStatus.INPROGRESS if the previous rules do not apply

Step 7: Add the following additional functionality to the game


Ask the user to enter the size of the board to be used and the number of connections to win the game. To accomplish this, you can use JOptionPane.showInputDialog method. For example, to ask the user to enter in board size the following could be used: String strBdSize = JOptionPane.showInputDialog (null, "Enter in the size of the board: ); int bSize = Integer.parseInt(strBdSize); the board size must be in the range 6 12 inclusive. The connection length should be in the range 3 6 inclusive. (Recommendation: ask the user within the ConnectFourPanel class constructor). If the user enters invalid input (e.g., abc, -10) then warn the user with a message Dialog box and use a default values. Note: if the user selects New Game from the JMenuItem, have the game re-ask these questions. Add on to the main GUI display new JLabel(s) that represent the number of times player 1 wins and the number of times player 2 wins. (Initialize to zero when the games starts). Ask the user who starts the game first? Player 1 or Player 2. To accomplish this, you can use JOptionPane.showInputDialog (null, "Who starts first? 1 or 2). Finally, if the user enters invalid input (e.g., Pizza, -1 then warn the user with a message Dialog box and use a default value.

Step 8: Add on an undo feature


Add on to the main GUI display one new JButton labeled: Undo Move. This JButton, when clicked will undo the previous move. Note, multi-undos is required, that is, continued clicking of this JButton will undo the game back to the beginning. Be careful to make sure the correct users turn stays in-sync after each click of the JButton.

Step 9: Add the following functionality to the game. Difficult! (Partial credit given here.)
Previous functionality must still work, such as, win count per player, undo button and so on. Ask the user to enter number of players (between 2 and 4, default 2). To accomplish this, you will need to add additional values to the enum type GameStatus.. For example: 4

String x = JOptionPane.showInputDialog(null, Enter in the number of Players: ); Add a different color and name for each player. It is suggested that you create an ArrayList of Strings for the different names of the players and an arrayList of Color for the different players colors. For example (assuming cList is an arraylist of type Color): cList.add(JColorChooser.showDialog(this, "Pick a color for player " + I, Color.RED);

General concepts about MVC Design Pattern and these concepts will be discussed in class
In MVC design, the model, view, and controller objects (classes) work together to provide the functionality of a program with a graphical user interface. In a simple MVC design, the view and the controller logic can be combined and placed in the view class. When a user interacts with the view (GUI), the following actions occur: The view class recognizes or determines (via event handlers) what user action (such as a button click, menu selection, etc.) has occurred. The event handler then calls appropriate method in the controller class (could be implemented as methods in the view class itself if view and controller are combined) to process the event. The controller accesses the model and calls appropriate method(s) on the model. As a result of actions on the model, if there are any changes in the model, the model notifies the view of the changes. Typically, the view object pulls changes form the model as needed.

Javadoc Commenting and Coding Style/Technique


Use http://www.cis.gvsu.edu/studentsupport/javaguide as a guide to document the source code in your project and observe good coding style practices.

Late Penalty 10 points per day late Weekend counts as one day late What/How to Turnin?
Upload source (*.java) files on Blackboard by midnight on Friday, October 4, 2013 GameStatus.java ConnectFour.java ConnectFourPanel.java ConnectFourGame.java

Project 2: Connect Four Program Rubric


Student Name Due Date Date Submitted, Days Late, Late Penalty Graded Item Javadoc Comments and Coding Style
(http://www.cis.gvsu.edu/studentsupport/javaguide) Code Indentation (auto format source code in IDE) Naming Conventions (see Java style guide) Proper access modifiers for fields and methods Use of helper (private) methods Javadoc comments for classes, methods, and fields Regular/inner comments where appropriate

Points 10

Points Secured

Comments

ConnectFourPanel class Implementation of user interface Model view separation ConnectFourGame class Implementation of game logic/engine Model view separation Step 7: Additional functionality

25

25

The board size can change Any player can start the game Number of winning connections can change Display number of wins for each player

5 5 5 5 10

Step 8: Additional functionality Undo feature Step 9: Additional functionality Multiusers can play the game Input name and color for players Total Additional Comments:

5 5 100

You might also like