home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / EV_201 / HANOI / TDISK.CPP < prev    next >
C/C++ Source or Header  |  1993-07-03  |  6KB  |  164 lines

  1. /*
  2.    Module        : TDISK.CPP
  3.    Version       : 2.0
  4.    Revision date : July 3rd, 1993
  5.    Author(s)     : Remy Gendron
  6.  
  7.    Description   : tdisk's member functions.
  8. */
  9.  
  10.  
  11. // Headers ------------------------------------------------------------------
  12.  
  13. #include <conio.h>                                      // System's libraries
  14. #include <dos.h>
  15. #include <stdio.h>
  16. #pragma hdrstop
  17.  
  18. #include "main.hpp"                                          // Other modules
  19. #include "stdfcts.h"
  20.  
  21. #include "tdisk.hpp"                             // This module's header file
  22.  
  23.  
  24. // --------------------------------------------------------------------------
  25.  
  26. tdisk::tdisk                                                   // Constructor
  27. (
  28.    int           did,                                            // Disk's id
  29.    int           drow,                             // Initial disk's position
  30.    int           dcol,
  31.    twindow huge *twinptr                                // Ptr to work window
  32. )
  33.  
  34. {
  35.    assert ( (did>0)&&(did<11),"tdisk::tdisk","Invalid disk's id",1) ;
  36.  
  37.    id = did ;                                     // Initialises disk's infos
  38.    row = drow ;
  39.    col = dcol ;
  40.    winptr = twinptr ;
  41.  
  42.    switch (id)                                           // Chooses its color
  43.    {
  44.       case 1 : color = BLUE ; break ;
  45.       case 2 : color = GREEN ; break ;
  46.       case 3 : color = CYAN ; break ;
  47.       case 4 : color = RED ; break ;
  48.       case 5 : color = MAGENTA ; break ;
  49.       case 6 : color = LIGHTBLUE ; break ;
  50.       case 7 : color = LIGHTGREEN ; break ;
  51.       case 8 : color = LIGHTCYAN ; break ;
  52.       case 9 : color = LIGHTMAGENTA ; break ;
  53.       case 10: color = YELLOW ; break ;
  54.    }
  55.  
  56.  
  57.    return ;                                      // Disk has been initialised
  58. }                                                          // End constructor
  59.  
  60.  
  61. // --------------------------------------------------------------------------
  62.  
  63. tdisk::~tdisk ()                                                // Destructor
  64.  
  65. {
  66.    return ;                                                  // Nothing to do
  67. }                                                           // End Destructor
  68.  
  69.  
  70. // --------------------------------------------------------------------------
  71.  
  72. void tdisk::show ()                      // Displays disk at current position
  73.  
  74. {
  75.    switch (id)
  76.    {
  77.       case 1 : winptr->winwrite (" ░░░▒▒▒▒▓▓▓▓▓████▓▓▓▒▒░ ",row,col-1,0,color) ;
  78.                break ;
  79.       case 2 : winptr->winwrite (" ░░▒▒▒▒▓▓▓▓▓████▓▓▓▒▒ ",row,col,0,color) ;
  80.                break ;
  81.       case 3 : winptr->winwrite ("  ░▒▒▒▒▓▓▓▓▓████▓▓▓▒  ",row,col,0,color) ;
  82.                break ;
  83.       case 4 : winptr->winwrite ("   ▒▒▒▒▓▓▓▓▓████▓▓▓   ",row,col,0,color) ;
  84.                break ;
  85.       case 5 : winptr->winwrite ("    ▒▒▒▓▓▓▓▓████▓▓    ",row,col,0,color) ;
  86.                break ;
  87.       case 6 : winptr->winwrite ("     ▒▒▓▓▓▓▓████▓     ",row,col,0,color) ;
  88.                break ;
  89.       case 7 : winptr->winwrite ("      ▒▓▓▓▓▓████      ",row,col,0,color) ;
  90.                break ;
  91.       case 8 : winptr->winwrite ("       ▓▓▓▓▓███       ",row,col,0,color) ;
  92.                break ;
  93.       case 9 : winptr->winwrite ("        ▓▓▓▓██        ",row,col,0,color) ;
  94.                break ;
  95.       case 10: winptr->winwrite ("         ▓▓▓█         ",row,col,0,color) ;
  96.                break ;
  97.    }
  98.  
  99.  
  100.    return ;                                        // Disk has been displayed
  101. }                                                                 // End show
  102.  
  103.  
  104. // --------------------------------------------------------------------------
  105.  
  106. void tdisk::erase                        // Erases disk from current position
  107. (
  108.    int towercolor                                            // Tower's color
  109. )
  110.  
  111. {
  112.    if (row >= 5)
  113.       winptr->winwrite ("          ░█          ",row,col,0,towercolor) ;
  114.    else
  115.       winptr->winwrite ("                      ",row,col) ;
  116.  
  117.  
  118.    return ;                                           // Disk has been erased
  119. }                                                                // End erase
  120.  
  121.  
  122. // --------------------------------------------------------------------------
  123.  
  124. void tdisk::moveto                     // Moves disk from on tower to another
  125. (
  126.    int target_row,                                         // Destination row
  127.    int target_col,                                         // Destination col
  128.    int source_color,                                  // Source tower's color
  129.    int target_color,                             // Destination tower's color
  130.    int speed                                             // Animation's speed
  131. )
  132.  
  133. {
  134.    while (row > 2)                                           // Moves disk up
  135.    {
  136.       erase (source_color) ;                                   // Erases disk
  137.       row-- ;                                                   // Up one row
  138.       show () ;                                              // Displays disk
  139.       delay (4 * (9-speed)) ;
  140.    }
  141.  
  142.    while (col != target_col)                             // Moves horizontaly
  143.    {
  144.       if (target_col > col) col++ ;                            // Moves right
  145.       else col-- ;                                              // Moves left
  146.       show () ;                                              // Displays disk
  147.       delay (3 * (9-speed)) ;
  148.    }
  149.  
  150.    while (row < target_row)                                // Moves disk down
  151.    {
  152.       erase (target_color) ;                                   // Erases disk
  153.       row++ ;                                                 // Down one row
  154.       show () ;                                              // Displays disk
  155.       delay (4 * (9-speed)) ;
  156.    }
  157.  
  158.  
  159.    return ;                            // Disk has been moved to target tower
  160. }                                                               // End moveto
  161.  
  162.  
  163. // End source file ----------------------------------------------------------
  164.