CIS 163, Fall 2013, Project 2 Connect Four Game (DRAFT)
CIS 163, Fall 2013, Project 2 Connect Four Game (DRAFT)
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 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 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.
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
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