home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l360 / 3.ddi / APPC.@EM / BATTLE.CPY < prev    next >
Encoding:
Text File  |  1991-04-08  |  16.9 KB  |  410 lines

  1.       *******************************************************************
  2.       *                                                                 *
  3.       *                                                                 *
  4.       *                  (C) Micro Focus Ltd. 1990                      *
  5.       *                                                                 *
  6.       *                         BATTLE.CPY                              *
  7.       *                                                                 *
  8.       *    Common procedure division code for both player programs      *
  9.       *                                                                 *
  10.       *******************************************************************
  11.  
  12.        Battle-Ships.
  13.            Perform Start-Ships
  14.            Perform Play-Ships
  15.            Perform End-Ships
  16.            STOP RUN.
  17.  
  18.        Start-Ships.
  19.            Move all "01" to Grid-Att-Table
  20.       *-----------------------------------------------------------------
  21.       *    the following procedures calls have been commented to show
  22.       *    possible enhancements you may like to make to allow players
  23.       *    to define grids dynamically and validate grids.
  24.       *
  25.       *-----------------------------------------------------------------
  26.       *    Perform Setup-Grid-Coords
  27.       *    Perform Validate-Grid-Coords
  28.            Display Battle-Screen
  29.            Perform Connect-Opponent.
  30.  
  31.        Play-Ships.
  32.       *-----------------------------------------------------------------
  33.       *    Battleship is played by taking turns.  Player 1 always starts
  34.       *    first.  So while player 1 has its turn,  player 2 is
  35.       *    servicing player 1's turn eg. receiving coordinates,
  36.       *    assessing damage and reporting back.
  37.       *    When it is player 2's turn, the roles are reversed, such
  38.       *    that player 2 is now sending coordinates and player 1 is
  39.       *    servicing player 2.  This continues until one of the players
  40.       *    has sunk all the ships of the other player.
  41.       *
  42.       *-----------------------------------------------------------------
  43.            Perform Until Game-Over
  44.                If Player-Id = "PLAYER1"
  45.                    Perform Local-Turn
  46.                    If not Game-Over
  47.                        Perform Remote-Turn
  48.                    End-If
  49.                Else
  50.                    Perform Remote-Turn
  51.                    If not Game-Over
  52.                        Perform Local-Turn
  53.                    End-If
  54.                End-If
  55.            End-Perform.
  56.  
  57.        End-Ships.
  58.            Perform Disconnect-Opponent
  59.            Perform Display-Game-Outcome
  60.            Perform Get-Keystroke.
  61.  
  62.       *-----------------------------------------------------------------
  63.       *    Each turn consists of getting grid coordinates, sending the
  64.       *    coordinates to the opponent, waiting for a damage report and
  65.       *    assessing that damage.  The opponents turn acts in reverse
  66.       *    to your turn - so while you send grid coordinates it is
  67.       *    waiting to receive them at the remote end and when you wait
  68.       *    for a damage report it is assessing the damage caused and
  69.       *    then it sends the damage report to you etc.
  70.       *
  71.       *-----------------------------------------------------------------
  72.        Local-Turn.
  73.            Perform Get-Coords
  74.            Perform Send-Grid-Coords
  75.            Perform Receive-Opponent-Damage
  76.            Perform Assess-Opponent-Damage
  77.            Display Battle-Damage
  78.            Display Battle-Field.
  79.  
  80.       *-----------------------------------------------------------------
  81.       *    The remote turn consists of receiving grid coordinates from
  82.       *    the opponent, assessing the damage caused and returning a
  83.       *    damage report to the opponent.  While this procedure is
  84.       *    running on this machine, the remote machine is running the
  85.       *    Local-Turn procedure.
  86.       *
  87.       *-----------------------------------------------------------------
  88.        Remote-Turn.
  89.            Perform Opponents-Turn
  90.            Perform Receive-Grid-Coords
  91.            Perform Assess-Own-Damage
  92.            Display Battle-Damage
  93.            Display Battle-Field
  94.            Perform Send-Own-Damage.
  95.  
  96.  
  97.        Get-Coords.
  98.            Move "YOUR TURN" to Screen-Msg-1
  99.            Display Battle-Turn
  100.            Move spaces to Grid-Coordinates
  101.            Accept Battle-Coords
  102.            If Not (Grid-1 = "Q" OR Grid-1 = "q")
  103.                Perform until (Grid-1 >="A" AND Grid-1 <= "M" AND
  104.                               Grid-2 >="1" AND Grid-2 <= "9")
  105.                    If Grid-1 >= 'a' AND Grid-1 <= 'z'
  106.       *                fold to upper case
  107.                        Subtract 32 from Grid-1-Asc
  108.                    Else
  109.                        Move "Invalid Coordinates" to Screen-Msg-4
  110.                        Display Battle-Msg
  111.                        Accept Battle-Coords
  112.                        Move spaces to Screen-Msg-4
  113.                        Display Battle-Msg
  114.                    End-If
  115.                End-Perform
  116.            End-if
  117.            Move "AWAITING REPORT" to Screen-Msg-1
  118.            Move spaces to Screen-Msg-2
  119.            Display Battle-Msg-1-2.
  120.  
  121.        Opponents-Turn.
  122.            Move "OPPONENTS TURN" to Screen-Msg-1
  123.            Display Battle-Turn.
  124.  
  125.        Assess-Opponent-Damage.
  126.            Perform Evaluate-Grid-Position
  127.            Evaluate Damage-Msg
  128.                When "MISS"  Perform Miss-Ship
  129.                When "HIT"   Perform Hit-Ship
  130.                When "SINK"  Perform Sink-Ship
  131.                When "WIN"   Perform Win-Battle
  132.            End-Evaluate.
  133.  
  134.        Miss-Ship.
  135.            Move 3 to Grid-Att-Array(Grid-Row,Grid-Col)
  136.            Move "YOU MISSED" to Screen-Msg-2.
  137.        Hit-Ship.
  138.            Move 4 to Grid-Att-Array(Grid-Row,Grid-Col)
  139.            Move "YOU HIT SHIP" to Screen-Msg-2.
  140.        Sink-Ship.
  141.            Perform Display-Sunk-Ship
  142.            Move "YOU SUNK SHIP" to Screen-Msg-2.
  143.        Win-Battle.
  144.            Perform Display-Sunk-Ship
  145.            Move "YOU SUNK SHIPS" to Screen-Msg-2
  146.            Set Win-Game to True.
  147.  
  148.        Display-Sunk-Ship.
  149.            Move Sink-Coords to Sunk-Ship-Locations
  150.            Perform varying Ship-Sector from 1 by 1 until Ship-Sector > 4
  151.                Move Sunk-Ship-Row of Sunk-Ship(Ship-Sector) to Temp-Row
  152.                Move Sunk-Ship-Col of Sunk-Ship(Ship-Sector) to Temp-Col
  153.                If Temp-Row not = 0 AND Temp-Col not = 0
  154.                    Move 0 to Grid-Att-Array(Temp-Row,Temp-Col)
  155.                End-If
  156.            End-Perform.
  157.  
  158.        Assess-Own-Damage.
  159.            Perform Evaluate-Grid-Position
  160.            Move Grid-Array(Grid-Row,Grid-Col) to Target-Object
  161.            If Target-Object = space
  162.       *            a miss
  163.                Move "MISS" to Damage-Msg
  164.                Move "OPPONENT MISSED SHIP" to Screen-Msg-2
  165.            Else
  166.       *            a hit
  167.                Move "HIT" to Damage-Msg
  168.                Move "OPPONENT HIT SHIP" to Screen-Msg-2
  169.                Evaluate Target-Object
  170.                    When "B"
  171.                    When "A"
  172.                    When "F"
  173.                    When "G"
  174.                    When "*"   Perform Check-Ship-Sunk
  175.                    When other Move "MISS" to Damage-Msg
  176.                               Move "OPPONENT MISSED SHIP"
  177.                               to Screen-Msg-2
  178.                End-Evaluate
  179.            End-If.
  180.  
  181.  
  182.        Evaluate-Grid-Position.
  183.            Move Grid-1 to Grid-Chr
  184.            Subtract 64 from Grid-Asc
  185.            Move Grid-Asc to Grid-Col
  186.            Move Grid-2 to Grid-Chr
  187.            Subtract 48 from Grid-Asc
  188.            Move Grid-Asc to Grid-Row.
  189.  
  190.        Check-Ship-Sunk.
  191.            Move Grid-Array(Grid-Row,Grid-Col) to Target-Object
  192.            Move "*" to Grid-Array(Grid-Row,Grid-Col)
  193.       *        assume sunk unless we can prove otherwise
  194.            Set Ship-Sunk to True
  195.            Move low-values to Sunk-Ship-Locations
  196.            Perform Check-Horizontal-Axis
  197.            If Ship-Sunk
  198.                Perform Check-Vertical-Axis
  199.                If Ship-Sunk
  200.                    Move "SINK" to Damage-Msg
  201.                    Move "OPPONENT SUNK SHIP" to Screen-Msg-2
  202.                    Perform varying Ship-Sector from 1 by 1
  203.                            until Ship-Sector > 4
  204.                        Move Sunk-Ship-Row of Sunk-Ship(Ship-Sector)
  205.                                    to Temp-Row
  206.                        Move Sunk-Ship-Col of Sunk-Ship(Ship-Sector)
  207.                                    to Temp-Col
  208.                        If Temp-Row not = 0 AND Temp-Col not = 0
  209.                            Move space to Grid-Array(Temp-Row,Temp-Col)
  210.                        End-If
  211.                    End-Perform
  212.                    Move Sunk-Ship-Locations to Sink-Coords
  213.                    Perform Check-All-Sunk
  214.                End-If
  215.            End-If.
  216.  
  217.        Check-Horizontal-Axis.
  218.            Set Not-End-of-Ship to true
  219.       *        go to far left of ship
  220.            Move Grid-Col to Temp-Col
  221.            Perform until Temp-Col= 0 OR
  222.                   (Grid-Array(Grid-Row,Temp-Col) not = "*" AND
  223.                    Grid-Array(Grid-Row,Temp-Col) not = Target-Object)
  224.                Subtract 1 from Temp-Col
  225.            End-Perform
  226.            Add 1 to Temp-Col
  227.       *        start scanning right
  228.            Move 1 to Ship-Sector
  229.            Perform until End-Of-Ship or Ship-Not-Sunk
  230.                Evaluate Grid-Array(Grid-Row,Temp-Col)
  231.                    When Target-Object  Set Ship-Not-Sunk to True
  232.                    When "*"
  233.                        Move Temp-Col to Sunk-Ship-Col of
  234.                                                   Sunk-Ship(Ship-Sector)
  235.                        If Sunk-Ship-Row of Sunk-Ship(Ship-Sector) = zero
  236.                            Move Grid-Row to Sunk-Ship-Row of
  237.                                                   Sunk-Ship(Ship-Sector)
  238.                        End-If
  239.                    When other Set End-Of-Ship to true
  240.                End-Evaluate
  241.                Add 1 to Ship-Sector
  242.                If Temp-Col = Max-Col
  243.                    Set End-Of-Ship to true
  244.                End-If
  245.                Add 1 to Temp-Col
  246.            End-Perform.
  247.  
  248.        Check-Vertical-Axis.
  249.            Set Not-End-of-Ship to true
  250.       *        go to top of ship
  251.            Move Grid-Row to Temp-Row
  252.            Perform until Temp-Row = 0 OR
  253.                   (Grid-Array(Temp-Row,Grid-Col) not = "*" AND
  254.                    Grid-Array(Temp-Row,Grid-Col) not = Target-Object)
  255.                Subtract 1 from Temp-Row
  256.            End-Perform
  257.            Add 1 to Temp-Row
  258.       *        start scanning down
  259.            Move 1 to Ship-Sector
  260.            Perform until End-Of-Ship or Ship-Not-Sunk
  261.                Evaluate Grid-Array(Temp-Row,Grid-Col)
  262.                    When Target-Object  Set Ship-Not-Sunk to True
  263.                    When "*"
  264.                        Move Temp-Row to Sunk-Ship-Row(Ship-Sector)
  265.                        If Sunk-Ship-Col(Ship-Sector) = zero
  266.                            Move Grid-Col to Sunk-Ship-Col(Ship-Sector)
  267.                        End-If
  268.                    When other Set End-Of-Ship to true
  269.                End-Evaluate
  270.                Add 1 to Ship-Sector
  271.                If Temp-Row = Max-Row
  272.                    Set End-Of-Ship to true
  273.                End-If
  274.                Add 1 to Temp-Row
  275.            End-Perform.
  276.  
  277.        Check-All-Sunk.
  278.            If No-Ships-Left
  279.                Move "OPPONENT SUNK SHIPS" to Screen-Msg-2
  280.                Move "WIN " to Damage-Msg
  281.                Set Lose-Game to True
  282.            End-If.
  283.  
  284.        Display-Game-Outcome.
  285.            If Win-Game
  286.                Move "YOU WIN!   G A M E   O V E R" to Screen-Msg-3
  287.            Else
  288.                Move "YOU LOSE!  G A M E   O V E R" to Screen-Msg-3
  289.            End-If
  290.            Display Battle-Over.
  291.  
  292.        Game-Quit.
  293.            Move "         QUIT GAME          " to Screen-Msg-3
  294.            Display Battle-Over
  295.            Stop Run.
  296.  
  297.        Get-keystroke.
  298.            Call x"83" using key-char
  299.            Call x"e4".
  300.  
  301.  
  302.       *-----------------------------------------------------------------
  303.       * Routines to communicate with opponent
  304.       *-----------------------------------------------------------------
  305.  
  306.       *-----------------------------------------------------------------
  307.        Connect-Opponent.
  308.       *    This sets up a conversation with the opponent
  309.       *
  310.       *-----------------------------------------------------------------
  311.            Move Connect-Code to Battle-Op-Code
  312.            Move Player-Id to Battle-Buffer
  313.            Perform Communicate-Opponent.
  314.  
  315.       *-----------------------------------------------------------------
  316.        Disconnect-Opponent.
  317.       *    This brings down a conversation with the opponent at the end
  318.       *    of a game
  319.       *
  320.       *-----------------------------------------------------------------
  321.            Move Disconnect-Code to Battle-Op-Code
  322.            Perform Communicate-Opponent.
  323.  
  324.       *-----------------------------------------------------------------
  325.        Send-Own-Damage.
  326.       *    Send damage report to opponent - the damage is contained
  327.       *    in the call parameter Battle-Buffer it contains either:
  328.       *        "MISS" - indicating opponent missed
  329.       *        "HIT"  - indicating opponent hit part of a ship
  330.       *        "SINK" - indicating opponent hit and sunk a ship
  331.       *        "WIN"  - indicating opponent hit and sunk last remaining
  332.       *                 ship and has won the game
  333.       *    the coordinates for a sunk ship are also provided so that
  334.       *    the opponent can mark the position on their screen.
  335.       *
  336.       *    You may like to enhance the program so that more information
  337.       *    is sent regarding the type of ship that was sunk or hit -
  338.       *    this information could then be displayed on the opponent's
  339.       *    screen.
  340.       *
  341.       *-----------------------------------------------------------------
  342.            Move Send-Report-Code to Battle-Op-Code
  343.            If Ship-Sunk or Lose-Game
  344.                Move Damage-Msg to Battle-Buffer(1:4)
  345.                Move Sink-Coords to Battle-Buffer(5:8)
  346.            Else
  347.                Move Damage-Msg to Battle-Buffer
  348.            End-If
  349.            Perform Communicate-Opponent.
  350.  
  351.       *-----------------------------------------------------------------
  352.        Receive-Opponent-Damage.
  353.       *    The damage report received is the same as that sent above.
  354.       *
  355.       *-----------------------------------------------------------------
  356.            Move Receive-Report-Code to Battle-Op-Code
  357.            Perform Communicate-Opponent
  358.            If Battle-Buffer(1:4) = "SINK" or Battle-Buffer(1:4) = "WIN "
  359.                Move Battle-Buffer(1:4) to Damage-Msg
  360.                Move Battle-Buffer(5:8) to Sink-Coords
  361.            Else
  362.                Move Battle-Buffer to Damage-Msg
  363.            End-If.
  364.  
  365.       *-----------------------------------------------------------------
  366.        Send-Grid-Coords.
  367.       *    Send coordinates in Battle-Buffer to opponent
  368.       *
  369.       *-----------------------------------------------------------------
  370.            Move Send-Coords-Code to Battle-Op-Code
  371.            Move Grid-Coordinates to Battle-Buffer
  372.            Perform Communicate-Opponent
  373.            If Battle-Buffer(1:1) = "Q" or Battle-Buffer(1:1) = "q"
  374.                Perform Game-Quit
  375.            End-If.
  376.  
  377.       *-----------------------------------------------------------------
  378.        Receive-Grid-Coords.
  379.       *    receive coordinates in Battle-Buffer from opponent
  380.       *
  381.       *-----------------------------------------------------------------
  382.            Move Receive-Coords-Code to Battle-Op-Code
  383.            Perform Communicate-Opponent
  384.            If Battle-Buffer(1:1) = "Q" or Battle-Buffer(1:1) = "q"
  385.                Perform Game-Quit
  386.            End-If.
  387.            Move Battle-Buffer to Grid-Coordinates.
  388.  
  389.       *-----------------------------------------------------------------
  390.        Communicate-Opponent.
  391.       *    Call communications module.
  392.       *    The communications module is treated as a black box - this
  393.       *    program is not concerned with how the communication is
  394.       *    achieved - It only understands several high level
  395.       *    operations that can be called to talk between itself and the
  396.       *    remote program.  You could conceivably change the
  397.       *    communications module to use some other protocol - this
  398.       *    program should not have to change.
  399.       *
  400.       *-----------------------------------------------------------------
  401.            Call "BATTAPPC" using
  402.                                 by value Battle-Op-Code
  403.                                 by reference Battle-Buffer
  404.            If Return-Code > 0
  405.       *            Error handling here is very simple.
  406.       *            You may like to enhance this area by implementing
  407.       *            some sort of recovery routine
  408.                STOP RUN
  409.            End-If.
  410.