Date: May 14th, 2013
Ernest Kirstein
Yuxiang Guo
1.1 - Identification (Yuxiang)
1.2 - System Overview (Yuxiang)
1.3 - Document Overview (Yuxiang)
3 - System Wide Design Decisions (Yuxiang)
4 - System Architecture Design (Ernest)
4.1 - ttt.model Package Design
4.2.1 - TicTacToeFrame (Ernest)
4.2.3 - HTMLDisplayDialog (Ernest)
4.2.4 - NewGameDialog (Ernest)
4.2.5 - PlayerSelectPanel (Ernest)
4.3 - ttt.controller Package Design
4.3.2 - GameFlowController (Ernest)
4.3.4 - NewGameListener (Ernest)
4.3.5 - MenuBarListener (Ernest)
4.3.6 - BoardClickListener (Ernest)
4.4 - Concept of Execution (Yuxiang)
5 - Fundamental Models (Yuxiang)
This document is the first version of Software Design Description (SDD, for short) for the Tic-Tac-Toe Game, and it provides the description of Tic-Tac-Toe Game (ttt, for short) at the application level. The Tic-Tac-Toe Game is a small PC game that can be played by one or two players. This document shows the details of this application and the relationships between the application packages.
The Tic-Tac-Toe Game is a PC application that can be played by one or two players. It shall use a Model-View-Controller architecture. The Model-View-Controller architecture shall contains three application packages: view, controller and model; and the three application packages shall be connected to each other. Through the three application packages, the player shall select the types of game (PVE or PVP), start a new game, and play the game on a board panel. At the same time, the application shall record the player’s information, handel all actions and provide varying levels of artificial intelligence PVE.
Section 1: provides the identification information of document and the system overview.
Section 2: identifies the documents that are referenced within this document.
Section 3: provides the design decisions that shall be applied to the Tic-Tac-Toe Game across the entire project.
Section 4: provides architecture design of the application and internal interfaces among the three application packages.
Section 5: provides the fundamental models of the solutions that explain and justify the design decisions.
- Wikipedia - For help developing the artificial intelligence.
http://en.wikipedia.org/wiki/Minimax - http://bluehawk.monmouth.edu/~btepfenh/Courses/SE505/Sections/applicationdesign.html - For designing the Tic-Tac-Toe Game at the application level.
- http://docs.oracle.com/javase/tutorial/ - For Java Programming.
In this design, there are several design decisions that apply across the entire project. They are listed as following:
1. The Tic-Tac-Toe Game will be supported by the following operating systems of PC:
Windows XP/Vista/7/8
Linux Kernel version 2.4 and higher
Mac OS X 10.4 (Tiger)
2. All user interfaces shall be easy and clear to guide the customers to operate them and get the related information.
3. All contents of the Tic-Tac-Toe Game shall follow the local laws.
4. The Tic-Tac-Toe Game shall support up to 2 players to use at the same time.
5. The Tic-Tac-Toe Game shall respond quickly when the player operates them.
These design decisions shall be propagated to all components for which the decision is relevant.
Figure 4.1 (Ernest)
This system will use a Model-View-Controller architecture. The Model for this TicTacToe game includes a mutable GameState which has two Player’s. The Model also contains a simple Move class for storing row-column pairs. The View for this system has a main TicTacToeFrame which contains the other instantiated objects of this package. The BoardPanel displays the GameState for the TicTacToe game. The three dialogs will display information about the system and menus for starting a new game. The Controller contains the Main class as well as the GameFlowController which controls the flow of this turn based game. The Controller also contains the three event listeners which will handle all actions and mouse events which occur in the View. Finally, the Controller contains an AI class which provides varying levels of artificial intelligence for a human player to play against.
This package contains the Model part of the model-view-controller architecture. The sections below describe the individual classes of this package more completely.
The GameState class contains all the information about the current state of a Tic Tac Toe game. The squares array contains a character value for each square in the Tic Tac Toe board. An empty square is represented by a space character. The player array contains the Players, who each have their own specified symbol which is used to fill the squares array. The playerTurn integer is an index which corresponds to the player in the player array whose turn it is. The constructor creates an empty Tic Tac Toe board, both Players, and initializes the playerTurn index to zero. The reseatBoard method returns the board to an empty state and resets the playerTurn to zero.
The doMove method and the undoMove method methods perform and perform a move in the Tic Tac Toe game. They also increment and decrement the playerTurn index respectively. These methods expect a valid move but will not throw an error if an invalid move is given. The getMoves method returns a java.util.LinkedList of all the valid moves that are available at the current game state. The getRandomMove method returns a random valid move.
The getSquare and setSquare method take row-column integer pairs and a value character as parameters. They provide public access to the squares array. The getPlayer methods provide access to the players array by index and by the character symbol of the desired Player. The methods incrementPlayerTurn and decrementPlayerTurn do as their name implies but in the modular domain (once playerTurn goes past a valid index, it loops back to a valid one).
The isTie method determines whether or not the game is in a tie. The getWinner method returns the Player who has won the current game or null if there is no winner or the game is tied. The getLength method is used by the getWinner method. It returns the length of same continuous Player symbols (not empty spaces) in the vector direction <dr,dc> from the point (r,c).
The toString method provides a command-line-printable string which shows the state of the Tic Tac Toe board.
The Player class is applied to manage the player’s information. The number of wins and symbol of the player should be got by the methods in the Player class. The variables and methods in Player class are described as follows:
wins: integer variable, represents the number of wins;
symbol: char variable, represents the symbol that is used in the board by the player;
Player (symbol: char): constructor, creates a new player, defines the new player’s symbol and initializes the number of wins is equal to zero;
getWins(): obtains the number of wins;
addWins(): the number of wins is increased 1;
getSymbol(): obtains the symbol of the player.
The Move class is applied to mark the location of the symbol by the row-column method. The variables and methods in Move class are described as follows:
row: integer variable, represents the row number of the Tic-Tac-Toe board;
col: integer variable, represents the column number of the Tic-Tac-Toe board;
Move (r:int, c:int): constructor, according to the two integer parameters r and c, this method sets the location of the symbol on Tic-Tac-Toe board.
Figure 4.2.1 (Ernest)
This package contains the View part of the model-view-controller architecture. The sections below describe the individual classes of this package more completely.
The TicTacToeFrame (shown in figure 4.2.2) has a menu bar, the BoardPanel, and can open the three dialogs. The final static String values contain the file locations of the help and about HTML files respectively. This class extends java.swing.JFrame.
The constructor instantiates all of the components, their listeners, and the dialogs. It also, resizes, and centers the frame but it does not make it visible yet. The initMenuBar method is called by the constructor to build the menu bar.
The getBoard method returns the board variable. The update repaints and performs any necessary update functions to make sure the view represents the current GameState. The openNewGameDialog, openHelpDialog, and openAboutDialog methods open the respective dialogs. The announceEnd announces the end of a game and displays Player p as the winner; if p is null, then this method announces a tie. The askPlayAgain method prompts the user to play again and returns their choice (true for yes).
Figure 4.2.2 (Ernest)
The BoardPanel (see Figure 4.2.2) displays the squares of the GameState. The SQUARE_SIZE is 100 pixels. The state attribute is a reference to the mutable GameState. The constructor initializes the state attribute and adds a new BoardClickListener to this objects mouse event handler list. This class extends javax.swing.JPanel.
The paint method overrides the same method in the superclass; it draws what is shown in figure 4.2.2. The private methods (paintSquare, drawX, drawO) are used by the main paint method to draw the board.
Figure 4.2.3 (Ernest)
The HTMLDisplayDialog (see Figure 4.2.3) is for displaying simple HTML documents in a stand-alone window. This class is used for the About dialog as well as the Help dialog in this program. Then constructor for the modal HTMLDisplayDialog takes in a file and a title and produces the dialog, but it does not make it visible. The initJEP method initializes the jep instance variable. The HTMLDisplayDialog extends javax.swing.JDialog.
Figure 4.2.4 (Ernest)
The NewGameDialog (see Figure 4.2.4) is used to set up a new Tic Tac Toe game. The listener attribute is applied to the buttons at the bottom of the dialog box. The playerX and playerO attributes are the left and right complex panels respectively. The NewGameDialog extends javax.swing.JDialog.
The constructor initializes all of the components and the listener, but does not make the dialog visible. The getButtons method is used to initialize the buttons panel. The getXAI and getOAI methods call the getAI method for the playerX and playerO attributes respectively; so they return either an AI object (for a computer player) or null (for a human player).
The PlayerSelectPanel (see Figure 4.2.4) is used to set up a single player (computer or human) for a new game. It has four javax.swing.JRadioButtons which each correspond to a human player or some level of artificial intellegence. There is also an instance variable c which corresponds to the symbol (X or O) of this player. PlayerSelectPanel extends javax.swing.JPanel.
The constructor initializes the radio buttons. The method getAI returns null for human, an AI with a depth of 1 for easy, 3 for medium, and 5 for hard. The hard AI cannot be beaten, only tied.
Figure 4.4.1 (Ernest)
This package contains the Controller part of the model-view-controller architecture. The sections below describe the individual classes of this package more completely.
The Main class performs the main function that creates two objects by the GameFlowController class and the TicTacToeFrame class. Through the Main class, all the functions of the Tic-Tac-Toe game should be invoked and performed.
The GameFlowController is responsible for triggering AI moves, preventing invalid human moves, and initiating end-game procedures after a tie or a win has occured. The state variable contains the game state. The playerX, and playerO, variables contain the AI for the X and O players respectively; if these values are null then those players are humans. The view variable is a reference to the main display frame.
The constructor initializes the GameState. The setView method must be called externally before the view is made visible. The newGame method resets the game state, notifies the view, and sets the two AI’s. It also attempts to make an AI move, in case the X player is not human. The method getState simply returns the state variable.
The method tryPlayMove handles all attempted moves from either the AI’s or the BoardClickListener. This method guards against invalid human moves and triggers AI moves or end game events when they are applicable. The methods attemptAIMove and gameOver are called in those two events and handle potential AI moves and game-over events respectively.
The AI class handles the artificial intelligence for the computer players using the minimax algorithm (see references). The depth variable determines the number of moves ahead to look each turn. The me character is the symbol (X or O) of the player that this AI represents. Then constructor passes its parameters to the two instance variables.
The method getHeuristicValue analyzes the given GameState based on it’s favorability for the player with the character me (with positive values being good and negative values being bad). The method getValue implements the minimax algorithm to determine the favorability of a state by looking ahead recursively for the given depth. Finally, the public getMove method can be called to retrieve the best-guess Move from this artificial player.
The NewGameListener handles the button click events from the NewGameDialog. It is an implementation of the java.awt.ActionListener interface. In the actionPerformed method, when an “OK” command is received the method uses the data from it’s parent dialog to trigger a new game in the GameFlowController. If any event is received, the method disposes of it’s parent.
The MenuBarListener handles the menu button click events from the TicTacToeFrame. It is an implementation of the java.awt.ActionListener interface. In the actionPerformed method, when an action command is received (“New Game”, “Help”, or “About”) the method opens the dialog which corresponds to that command via it’s parent class.
The BoardClickListener handles the mouse clicks from the BoardPanel. It is an implementation of the java.awt.MouseListener interface. In the mouseReleased method, the MouseEvent’s location is converted into a row-column pair, then a move is attempted via the GameFlowController. The other methods from the MouseListener interface are left empty.
The board panel of the Tic-Tac-Toe Game contains three options: “About”, “Help”, “New Game”. The player shall view the information of the Tic-Tac-Toe Game by the “About” option, view the solutions of FAQ by the “Help” option, and start a new game by “New Game” option. The basic execution process of this application is listed as follow:
1. The user shall open the Tic-Tac-Toe Game application on his/her PC.
2. The application shall display a board panel on the screen of PC.
3. The user shall select the “New Game” option.
4. The application shall pop up a dialog that lets the user to select the symbol and the type of game (Human or AI).
5. The user shall select the symbol and the type of game, then click on the “OK” button.
6. If the user selects the “Human” type, the application shall enter into the PVP mode, if the user selects the “AI” type, the application shall enter into the PVE mode. (In the PVP mode, the application shall be played by two players; and in the PVE mode, the application shall be played by one player.)
7. The application shall display the squares of the game state on the board panel.
8. The player shall start the game and place his/her symbol in the squares.
9. The application shall handle all the actions of player or AI, and estimate who wins.
10. The application shall display the winner or the tie.
This section of the document describes the fundamental models of how the solution operates.
Tic-Tac-Toe Game application has two possibilities to fail during it runs. The failure points are described as follow:
Failure 1: The Tic-Tac-Toe Game application shall not run due to the version of PC operation system is too old. If the version of PC operation system is too old, when the player wants to open the application, the screen of PC shall not display the board panel of the application. Actually, the game application shall not be run by the old PC operation system. For running the Tic-Tac-Toe Game application, the operation system of PC shall not be older than Windows XP, Linux Kernel version 2.4, and Mac OS X 10.4.
Failure 2: The Java platform doesn’t be installed the javax.swing package, the view part of the Tic-Tac-Toe Game application shall not run well. Maybe the board panel of the application shall not be displayed on the screen of PC. For running this application, the java platform must be installed the javax.swing package.
AI - Artificial Intelligence
PVE - Player vs Enviroment
PVP - Player vs Player
PC - Personal Computer
FAQ - Frequently Asked Question