[127] | 1 | package agents.qoagent2;
|
---|
| 2 |
|
---|
| 3 | /*****************************************************************
|
---|
| 4 | * Class name: GameTime
|
---|
| 5 | * Goal: Creating the stop-watch and displaying it using threads.
|
---|
| 6 | * Description: None.
|
---|
| 7 | * Input: None.
|
---|
| 8 | * Output: None.
|
---|
| 9 | ****************************************************************/
|
---|
| 10 | public class QGameTime implements Runnable
|
---|
| 11 | {
|
---|
| 12 | private QOAgent m_agent;
|
---|
| 13 | private int m_nMaxTurn;
|
---|
| 14 | private boolean m_bIsTurn;
|
---|
| 15 | private long m_nTime; // the timer
|
---|
| 16 | private boolean m_bRun; // should the clock continue to run?
|
---|
| 17 | private boolean m_bCountUp;
|
---|
| 18 | private int m_nStartSeconds;
|
---|
| 19 | private int m_nStartMinutes;
|
---|
| 20 | private int m_nStartHours;
|
---|
| 21 |
|
---|
| 22 | /*****************************************************************
|
---|
| 23 | * Method name: GameTime()
|
---|
| 24 | * Goal: Constructor.
|
---|
| 25 | * Description: Initialize the class variables.
|
---|
| 26 | * Input: None.
|
---|
| 27 | * Output: None.
|
---|
| 28 | ****************************************************************/
|
---|
| 29 | public QGameTime(boolean bCountUp,int nHours,int nMinutes, int nSeconds,QOAgent agent, boolean TurnOrNeg)
|
---|
| 30 | {
|
---|
| 31 | m_bIsTurn=TurnOrNeg;
|
---|
| 32 | m_agent = agent;
|
---|
| 33 | m_nMaxTurn = m_agent.getMaxTurns();
|
---|
| 34 | m_bCountUp = bCountUp;
|
---|
| 35 | m_bRun = false;
|
---|
| 36 |
|
---|
| 37 | if (!bCountUp)
|
---|
| 38 | {
|
---|
| 39 | m_nStartMinutes = nMinutes;
|
---|
| 40 | m_nStartSeconds = nSeconds;
|
---|
| 41 | m_nStartHours = nHours;
|
---|
| 42 |
|
---|
| 43 | m_nTime = nHours*3600+ nMinutes*60 + nSeconds;
|
---|
| 44 | }
|
---|
| 45 | else
|
---|
| 46 | {
|
---|
| 47 | m_nStartSeconds = 0;
|
---|
| 48 | m_nStartMinutes = 0;
|
---|
| 49 | m_nStartHours = 0;
|
---|
| 50 | m_nTime = 0;
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public void SetTime(boolean bCountUp,int nHours,int nMinutes, int nSeconds,boolean TurnOrNeg)
|
---|
| 55 | {
|
---|
| 56 | m_bIsTurn=TurnOrNeg;
|
---|
| 57 | m_bCountUp = bCountUp;
|
---|
| 58 | m_bRun = false;
|
---|
| 59 |
|
---|
| 60 | if (!bCountUp)
|
---|
| 61 | {
|
---|
| 62 | m_nStartMinutes = nMinutes;
|
---|
| 63 | m_nStartSeconds = nSeconds;
|
---|
| 64 | m_nStartHours = nHours;
|
---|
| 65 |
|
---|
| 66 | m_nTime = nHours*3600+ nMinutes*60 + nSeconds;
|
---|
| 67 | }
|
---|
| 68 | else
|
---|
| 69 | {
|
---|
| 70 | m_nStartSeconds = 0;
|
---|
| 71 | m_nStartMinutes = 0;
|
---|
| 72 | m_nStartHours = 0;
|
---|
| 73 | m_nTime = 0;
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /*****************************************************************
|
---|
| 78 | * Method name: GameTime()
|
---|
| 79 | * Goal: Constructor.
|
---|
| 80 | * Description: Initialize the class variables.
|
---|
| 81 | * Input: None.
|
---|
| 82 | * Output: None.
|
---|
| 83 | ****************************************************************/
|
---|
| 84 | public QGameTime()
|
---|
| 85 | {
|
---|
| 86 | m_bCountUp = true;
|
---|
| 87 | m_bRun = false;
|
---|
| 88 | m_nTime = 0;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /*****************************************************************
|
---|
| 92 | * Method name: run()
|
---|
| 93 | * Goal: Display and run the stop-watch.
|
---|
| 94 | * Description: Increasing the time every second while the game is in progress.
|
---|
| 95 | * Input: None.
|
---|
| 96 | * Output: None.
|
---|
| 97 | ****************************************************************/
|
---|
| 98 | public void run()
|
---|
| 99 | {
|
---|
| 100 | while(m_bRun)
|
---|
| 101 | {
|
---|
| 102 | try{
|
---|
| 103 | Thread.sleep(1000);
|
---|
| 104 | if (m_bCountUp)
|
---|
| 105 | m_nTime++; // increasing the time each 1 sec.
|
---|
| 106 | else
|
---|
| 107 | m_nTime--; // decreasing the time each 1 sec.
|
---|
| 108 | } catch(Exception e){}
|
---|
| 109 |
|
---|
| 110 | if (m_nTime == 0)
|
---|
| 111 | stopRunning();
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /*****************************************************************
|
---|
| 116 | * Method name: stopRunning()
|
---|
| 117 | * Goal: Stop the stop-watch.
|
---|
| 118 | * Description: None.
|
---|
| 119 | * Input: None.
|
---|
| 120 | * Output: None.
|
---|
| 121 | ****************************************************************/
|
---|
| 122 | public void stopRunning()
|
---|
| 123 | {
|
---|
| 124 | m_bRun = false;
|
---|
| 125 |
|
---|
| 126 | int nCurrentTurn = m_agent.getCurrentTurn();
|
---|
| 127 |
|
---|
| 128 | if((m_bIsTurn)&&(nCurrentTurn < m_nMaxTurn))
|
---|
| 129 | {
|
---|
| 130 | newGame(); //restart timer
|
---|
| 131 | m_bRun = true;
|
---|
| 132 | //m_agent.incrementCurrentTurn(); // incremented by the agent itself
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /*****************************************************************
|
---|
| 137 | * Method name: newGame()
|
---|
| 138 | * Goal: Start a new stop-watch.
|
---|
| 139 | * Description: None.
|
---|
| 140 | * Input: None.
|
---|
| 141 | * Output: None.
|
---|
| 142 | ****************************************************************/
|
---|
| 143 | public void newGame()
|
---|
| 144 | {
|
---|
| 145 | m_bRun = true;
|
---|
| 146 | m_nTime = m_nStartHours*3600 + m_nStartMinutes * 60 + m_nStartSeconds;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | public void setRunMethod(boolean bCountUp)
|
---|
| 150 | {
|
---|
| 151 | m_bCountUp = bCountUp;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | public void setRun(boolean bRun)
|
---|
| 155 | {
|
---|
| 156 | m_bRun = bRun;
|
---|
| 157 | }
|
---|
[1] | 158 | } // end class - GameTime |
---|