![](me_bw.jpg)
|
Xinhong's Page
|
Program Specification
There are 10 classes implementing the program:
- Othello
- Board
- Wrapper
- Request
- Timer
- MSGbar
- MessageWin
- Position
- About
- Picture
General Design:
The othello applet has three component, button panel, board, message panel. There is a Timer thread running currently with board and button handling, showing time past on the message panel.
The board starts a thread Wrapper to handle the animation display while itself does all the computation.
Description of classes :
Othello extends the Applet class and is the main program. It creates a board (of class Board), the buttons to handle the game, and a timer( of class Timer) to calculate the time for each set of game.
Request: There are 7 kind of requests:
- FLIPPOS : repaint a position given a player it belongs to
- MAKEMOVE: show a hinting position
- SHOWHINT: stop showing a hinting position
- DESHOWHINT: stop showing a hinting position
- PRINTBOARD: print the board to standard ouput
- SKIPTURN: show up a skip-turn message window
- GAMEOVER: show up the final score window when game is over
Position is almost the same as that in connect-four game, except that each position has two states:
- state: the new state after a move is made and the affected flips are also made. This is for the computation of next valid move.
- display_state: the state during the animation display the flip one by one. Used for paint() method to check and paint the color accordingly.
Beause there is a delay between the time mouse is down and the time the position is repainted, two states are used to allow the concurrency of Board and Wrapper. Using one state requires locking the state when display the position, because it may change the real state of the position and make the computation in board take the wrong value.
The Wrapper handles almost all the paint jobs exception the showing for a valid move so when the wrapper animately display the flips, the valid move for the next player is still available. It picks one request from the head of the board's buffer, checks the request type, does some work accordingly, remove this request, and then sleep for a delay period.
Board: The board does all the computation and artificial intelligence (or kind of) stuff. Whenever it has display need, it sends a request to the buffer called myQueue, which is of Vector type, and then continue with next conputation. The only painting it does is to highlight the valid move, because it can be concurrent with the animation. The basic algorithm for choosing a best move only looks ahead once, which is:
- check_line(): give a position and a direction indicated by a rowInc, and colInc, check the line in this direction if there are any valid move and return the number of flips it returns.
- best_move(): for each position, check the total number of flip in all directions and choose the positions which have the biggest number of flips. If there is only one such position, return it as the best move, if there are more than one, choose the first one sitting in the corner or the edge. If no one sits at the edge, just pick the first one.
- make_flip(): does the move and sends all requests for the flipping.
- has_no_move(): check if the next player has any valid move, if not, there is two situations:
- the other player still has move, then display the Skip-turn message window.
- the other player has no move, the game is over.
Timer is a subclass of Thread used by Othello main program, when a new game begins, timer is spawned. When the game is over, timer is stopped.
MSGbar is a Panel for the timer to display.
MessageWin is the class for popping up message. It has two constructors, one for popping up a skip-turn message, the other for poping up the final score window when the game is over. An 'ok' button closes them.
About class pops up a small window to show the author information.
Picture class extends Canvas to show the picture of the author, used by About class.
Status of the program
I believe I have finished all four levels. But I am not sure whether the sound would play correctly because I don't have a sound card and speaker on my machine.
Defects of the program
- Best move Algorithm is not optimum;
- When game ends, there is alway a window skip-turn window shows up in addition to the final score window, which is unnecessary.
- The myQueue member of Board class is public, so not only Wrapper can access and modify it, but every other object can access it, which is not so good. Making it private and making Wrapper a friend of this method (if that possible?) may be better. I am not sure whether java allows to make one method