home *** CD-ROM | disk | FTP | other *** search
- // --------- ttt.cpp
- #include "ttt.h"
-
- CURSORLIST(TicTacToe)
- MOUSE_CURSOR( 70, 18, 128, 75, UPCURSOR, position1)
- MOUSE_CURSOR( 136, 18, 200, 75, UPCURSOR, position2)
- MOUSE_CURSOR( 208, 18, 266, 75, UPCURSOR, position3)
- MOUSE_CURSOR( 70, 82, 128, 146, UPCURSOR, position4)
- MOUSE_CURSOR( 136, 82, 200, 146, UPCURSOR, position5)
- MOUSE_CURSOR( 208, 82, 266, 146, UPCURSOR, position6)
- MOUSE_CURSOR( 70, 154, 128, 210, UPCURSOR, position7)
- MOUSE_CURSOR( 136, 154, 200, 210, UPCURSOR, position8)
- MOUSE_CURSOR( 208, 154, 266, 210, UPCURSOR, position9)
- ENDCURSORLIST
-
- // --- the playing board
- char TicTacToe::board[9];
-
- // --- winning combinations
- int TicTacToe::wins [8][3] = {
- {1,2,3}, {4,5,6}, {7,8,9}, // winning rows
- {1,4,7}, {2,5,8}, {3,6,9}, // winning columns
- {1,5,9}, {3,5,7} // winning diagonals
- };
-
- // ---- construct the board game
- TicTacToe::TicTacToe() : SceneryDirector("ttt.pcx")
- {
- piece = new Piece(this);
- voice = new Voice(this);
- initialize_play();
- }
- TicTacToe::~TicTacToe()
- {
- delete piece;
- }
- void TicTacToe::display()
- {
- SceneryDirector::display();
- mouse_visible();
- }
- void TicTacToe::hide()
- {
- mouse_invisible();
- SceneryDirector::hide();
- }
- void TicTacToe::initialize_play()
- {
- play = 0;
- isover = 0;
- for (int i = 0; i < 9; i ++)
- board[i] = 0;
- }
- // ---- computer says "hmm"
- void TicTacToe::say(soundclip clip)
- {
- voice->play_sound_clip(clip);
- while (voice->sound_clip_is_playing())
- ;
- }
- // ---- player selects a move
- void TicTacToe::position(int pos)
- {
- if (isover) {
- initialize_play();
- display_original_scenery();
- return;
- }
- if (board[pos-1] != 0) {
- say(notthere);
- return;
- }
- piece->placemarker(pos, player);
- if (play == 0) {
- say(hmm);
- if (pos != 5)
- piece->placemarker(5, computer);
- else
- piece->placemarker(1, computer);
- play = 2;
- return;
- }
- play++;
- if (won()) {
- say(youwin);
- return;
- }
- if (play == 9) {
- isover = 1;
- say(tie);
- return;
- }
- // ---- compute computer's next move
- int mv;
- if ((mv = canwin(computer)) != 0)
- // --- win if possible
- piece->placemarker(mv, computer);
- else {
- if ((mv = canwin(player)) != 0) {
- // --- block player's win potential
- say(ohno);
- piece->placemarker(mv, computer);
- }
- else {
- say(nowwhat);
- nextmove();
- }
- }
- if (won()) {
- say(iwin);
- return;
- }
- play++;
- }
- // --- find next available open space for a dumb move
- void TicTacToe::nextmove()
- {
- int lmv = -1;
- for (int i = 0; i < 9; i++)
- if (board[i] == 0) {
- lmv = i+1;
- board[lmv-1] = computer;
- if (canwin(computer)) {
- board[lmv-1] = 0;
- piece->placemarker(lmv, computer);
- return;
- }
- board[lmv-1] = 0;
- }
- if (lmv != -1)
- piece->placemarker(lmv, computer);
- }
- // --- test to see if a player (n) can win this time
- int TicTacToe::canwin(int n)
- {
- int i, w;
-
- for (i = 0; i < 8; i++)
- if ((w = trywin(n, wins[i])) != 0)
- return w;
- return 0;
- }
- // ---- test a row, column, or diagonal for a win
- int TicTacToe::trywin(int n,int *wn)
- {
- int nct = 0, zct = 0, i;
-
- for (i = 0; i < 3; i++)
- if (board[*(wn + i)-1] == 0)
- zct = i+1;
- else if (board[*(wn + i)-1] == n)
- nct++;
- if (nct == 2 && zct)
- return *(wn + zct - 1);
- return 0;
- }
- // ------ update the board with a move
- void TicTacToe::update(int square, int piece)
- {
- if (square)
- board[square-1] = piece;
- refresh_display();
- }
- // ------ test to see if the game has been won
- int TicTacToe::won()
- {
- int i, k;
- for (i = 0; i < 8; i++) {
- if (board[wins[i][0]-1] == 0)
- continue;
- for (k = 1; k < 3; k++)
- if (board[wins[i][0]-1] != board[wins[i][k]-1])
- break;
- if (k == 3) {
- piece->drawline(i);
- isover = 1;
- return 1;
- }
- }
- return 0;
- }
-