Class TTTpanel

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by TTTpanel
All Implemented Interfaces:
java.awt.event.MouseListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible

public class TTTpanel
extends javax.swing.JPanel
implements java.awt.event.MouseListener

Actual GUI part of the engine. Handles mouse clicks, checks for a winner, and draws the board.

Author:
James Barr
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
private  java.lang.String[] board
          string array representation of the TTT board
private static LearningAlgorithm curOpponent
          the current opponent
private static java.lang.String instigator
          the CCP ID of the module that started the auto mode game
private  javax.swing.JLabel lblMessage
          the message to be displayed on the GUI
private static java.lang.String player1
          the first player: "HUMAN" or "ROBOT"
private static boolean playerX
          TRUE for X's turn, FALSE for O's turn
private static boolean playing
          TRUE if game is currently being played, FALSE if no game
private static boolean tie
          TRUE if game is a tie, FALSE if no tie
private  TTTengine tttEngine
          reference to the main engine class
private static boolean win
          TRUE if someone has won, FALSE if no one has won
private static int[][] winIndex
          all the possible winning combinations
private static java.awt.geom.Line2D winLine
          line representing the winning TTT moves
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
TTTpanel(TTTengine tttEngine)
          Constructs a new tic tac toe board, accepts a reference to the CCP client, and sets it to X's turn
 
Method Summary
 void drawO(java.awt.Graphics2D g2, int i)
          Draws an O on the board in a given cell(1-9)
 void drawX(java.awt.Graphics2D g2, int i)
          Draws an X on the board in a given cell(1-9)
 java.lang.String[] getBoard()
          Return an array of strings representing the tic tac toe board
 LearningAlgorithm getCurAlgorithm()
          Get current opponent
 java.lang.String getInstigator()
          Get the name of the instigator
 java.lang.String getOtherPlayer(java.lang.String player)
          Find name of the other player given a player
 java.lang.String getPlayer1()
          Return the first player
 boolean getPlayerX()
          Gets whose turn it is(X's or O's turn)
 boolean isAWin()
          Sees if someone has one yet
 boolean isEmpty(int i)
          Checks if the current cell is empty
 boolean isHumanTurn()
          Find out if it is the Human's turn
 boolean isPlaying()
          See if game is currently going on
 void mouseClicked(java.awt.event.MouseEvent event)
          Invoked when the mouse is clicked, draws an X or O in the cell that was clicked on
 void mouseEntered(java.awt.event.MouseEvent arg0)
          Method that has to be implemented but is not necessary
 void mouseExited(java.awt.event.MouseEvent arg0)
          Method that has to be implemented but is not necessary
 void mousePressed(java.awt.event.MouseEvent arg0)
          Method that has to be implemented but is not necessary
 void mouseReleased(java.awt.event.MouseEvent arg0)
          Method that has to be implemented but is not necessary
 void paintComponent(java.awt.Graphics page)
          Draws the tic tac toe board and moves
 void setBoard(int i, java.lang.String letter)
          Set cell i equal to the given letter.
 void setCurAlgorithm(LearningAlgorithm opponent)
          Set the current opponent
 void setInstigator(java.lang.String instigator)
          Set new game instigator
 void setMessage(java.lang.String message)
          Sets the GUI message
 void setPlayer1(java.lang.String player1)
          Set the first player
 void setPlayerX(boolean playerX)
          Sets whose turn it is
 void setPlaying(boolean playing)
          Sets if game is going on or not
 void setupNewGame(java.lang.String player1)
          Various assignments to start a new game
 void setWin(boolean win)
          Sets a win flag so win line will show
 void swapPlayer()
          Switches to next player's turn
 java.lang.String whoWon(int lastMove)
          Checks to find a human win, robot win, or tie
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

board

private java.lang.String[] board
string array representation of the TTT board


tttEngine

private TTTengine tttEngine
reference to the main engine class


winLine

private static java.awt.geom.Line2D winLine
line representing the winning TTT moves


player1

private static java.lang.String player1
the first player: "HUMAN" or "ROBOT"


instigator

private static java.lang.String instigator
the CCP ID of the module that started the auto mode game


playerX

private static boolean playerX
TRUE for X's turn, FALSE for O's turn


playing

private static boolean playing
TRUE if game is currently being played, FALSE if no game


win

private static boolean win
TRUE if someone has won, FALSE if no one has won


tie

private static boolean tie
TRUE if game is a tie, FALSE if no tie


curOpponent

private static LearningAlgorithm curOpponent
the current opponent


lblMessage

private javax.swing.JLabel lblMessage
the message to be displayed on the GUI


winIndex

private static final int[][] winIndex
all the possible winning combinations

Constructor Detail

TTTpanel

public TTTpanel(TTTengine tttEngine)
Constructs a new tic tac toe board, accepts a reference to the CCP client, and sets it to X's turn

Method Detail

paintComponent

public void paintComponent(java.awt.Graphics page)
Draws the tic tac toe board and moves

Overrides:
paintComponent in class javax.swing.JComponent
Parameters:
page - - The graphics object for painting the board

drawX

public void drawX(java.awt.Graphics2D g2,
                  int i)
Draws an X on the board in a given cell(1-9)

Parameters:
g2 - - The graphics object for painting the board
i - - The number of the cell(1-9)

drawO

public void drawO(java.awt.Graphics2D g2,
                  int i)
Draws an O on the board in a given cell(1-9)

Parameters:
g2 - - The graphics object for painting the board
i - - The number of the cell(1-9)

isEmpty

public boolean isEmpty(int i)
Checks if the current cell is empty

Parameters:
i - - The cell number to be checked
Returns:
- True if the cell is empty, False if the cell is occupied

whoWon

public java.lang.String whoWon(int lastMove)
Checks to find a human win, robot win, or tie

Parameters:
lastMove - - last move that filled the TTT board
Returns:
- the winner: "N", "HUMAN", "ROBOT", "TIE"

getBoard

public java.lang.String[] getBoard()
Return an array of strings representing the tic tac toe board

Returns:
- the TTT board string array

setBoard

public void setBoard(int i,
                     java.lang.String letter)
Set cell i equal to the given letter. If i = -1, then sets whole board to N's

Parameters:
i - - square to be set
letter - - letter to set square to

setupNewGame

public void setupNewGame(java.lang.String player1)
Various assignments to start a new game

Parameters:
player1 - - the starting player: "HUMAN" or "ROBOT"

getPlayerX

public boolean getPlayerX()
Gets whose turn it is(X's or O's turn)

Returns:
- True for X's turn, False for O's turn

setPlayerX

public void setPlayerX(boolean playerX)
Sets whose turn it is

Parameters:
playerX - - Set True for X's turn, False for O's turn

getOtherPlayer

public java.lang.String getOtherPlayer(java.lang.String player)
Find name of the other player given a player

Parameters:
player - - name of player to find the other one of
Returns:
- the other player: "HUMAN" or "ROBOT"

getPlayer1

public java.lang.String getPlayer1()
Return the first player

Returns:
- name of first player

setPlayer1

public void setPlayer1(java.lang.String player1)
Set the first player

Parameters:
player1 - - name of first player

setPlaying

public void setPlaying(boolean playing)
Sets if game is going on or not

Parameters:
playing - - True for playing game, False for not playing a game

isPlaying

public boolean isPlaying()
See if game is currently going on

Returns:
- TRUE if game is being played, FALSE if not being played

setMessage

public void setMessage(java.lang.String message)
Sets the GUI message

Parameters:
message - - message to be displayed on the GUI

setWin

public void setWin(boolean win)
Sets a win flag so win line will show

Parameters:
win - - TRUE if someone won, FALSE if no one won

isAWin

public boolean isAWin()
Sees if someone has one yet

Returns:
- TRUE if someone won, FALSE if no one won

setCurAlgorithm

public void setCurAlgorithm(LearningAlgorithm opponent)
Set the current opponent

Parameters:
opponent - - New opponent to be set

getCurAlgorithm

public LearningAlgorithm getCurAlgorithm()
Get current opponent

Returns:
- the learning algorithm object of the current opponent

swapPlayer

public void swapPlayer()
Switches to next player's turn


setInstigator

public void setInstigator(java.lang.String instigator)
Set new game instigator

Parameters:
instigator - - CCP ID of instigator

getInstigator

public java.lang.String getInstigator()
Get the name of the instigator

Returns:
- CCP ID of the instigator

isHumanTurn

public boolean isHumanTurn()
Find out if it is the Human's turn

Returns:
- TRUE if Human's turn, FALSE if Robot's turn

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent event)
Invoked when the mouse is clicked, draws an X or O in the cell that was clicked on

Specified by:
mouseClicked in interface java.awt.event.MouseListener
Parameters:
event - - Information about the mouse click

mousePressed

public void mousePressed(java.awt.event.MouseEvent arg0)
Method that has to be implemented but is not necessary

Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent arg0)
Method that has to be implemented but is not necessary

Specified by:
mouseReleased in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent arg0)
Method that has to be implemented but is not necessary

Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent arg0)
Method that has to be implemented but is not necessary

Specified by:
mouseExited in interface java.awt.event.MouseListener