Guest User

Untitled

a guest
Jan 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 14.22 KB | None | 0 0
  1. program finalfinalbattleships;
  2.  
  3. uses
  4.     SysUtils, StrUtils;
  5.  
  6. type
  7.     TShipData = array [1..4] of integer; // x | y | v | t
  8.     TCoordSet = array [0..5,1..2] of integer;
  9.     TGrid = array [1..10, 1..10] of integer;
  10.  
  11. var
  12.    grids : array[1..2,1..2] of TGrid;
  13.    currentply : integer;
  14.  
  15. procedure printTitle(); // prints 'BATTLESHIPS: ' (GUI).
  16. var
  17.    n : integer;
  18. begin
  19.   writeln();
  20.   writeln( 'BATTLESHIPS by Stuart Ashforth' );
  21.   writeln( '------------------------------' );
  22.   for n := 1 to 19 do
  23.       writeln();
  24.  
  25.   writeln( 'Press enter to continue! ' );
  26.   writeln();
  27.   readln();
  28.   writeln();
  29.  
  30. end;
  31.  
  32. procedure clearWindow(); // clears the window, avoiding cheating.
  33. var
  34.    k : integer;
  35. begin
  36.   for k := 1 to 100 do
  37.       writeln();
  38. end;
  39.  
  40. procedure clearAllGrids(); // sets the 'board' to empty.
  41. var
  42.    x, y, k : integer;
  43.    grid : TGrid;
  44. begin
  45.  
  46.   for k := 1 to 4 do
  47.       begin
  48.  
  49.       case k of
  50.       1 : grid := grids[1,1];
  51.       2 : grid := grids[1,2];
  52.       3 : grid := grids[2,1];
  53.       4 : grid := grids[2,2];
  54.       end;
  55.  
  56.       for y := 1 to 10 do
  57.           begin
  58.           for x := 1 to 10 do
  59.               grid[x,y] := 0;
  60.           end;
  61.       end;
  62. end;
  63.  
  64. function getSymbol( val : integer) : string; // changes data to symbols (GUI).
  65. var
  66.    sym : string;
  67. begin
  68.   case val of
  69.   0 : sym := ' ';
  70.   -1 : sym := 'x';
  71.   -2 : sym := 'o';
  72.   1 : sym := '&';
  73.   2 : sym := '/';
  74.   3 : sym := '=';
  75.   4 : sym := '+';
  76.   5 : sym := '%';
  77.   6 : sym := '&';
  78.   7 : sym := '/';
  79.   8 : sym := '=';
  80.   9 : sym := '+';
  81.   10 : sym := '%';
  82.   end;
  83.   if sym = '' then sym := intToStr(val);
  84.   sym := sym + ' ';
  85.   getSymbol := sym;
  86. end;
  87.  
  88. function getGrid( ship : TShipData ) : TGrid; // gets the grid a ship should be on.
  89. var
  90.    id : integer;
  91. begin
  92.   id := ship[4];
  93.  
  94.   if id <= 5 then
  95.      getGrid := grids[1,1]
  96.   else
  97.       getGrid := grids[2,1];
  98. end;
  99.  
  100. function getPly( ship : TShipData ) : integer; // gets the player a ship belongs to.
  101. var
  102.    id : integer;
  103. begin
  104.   id := ship[4];
  105.  
  106.   if id <= 5 then
  107.      getPly := 1
  108.   else
  109.       getPly := 2;
  110. end;
  111.  
  112. function idToLength( id : integer ) : integer; // converts a shipID to the length of a ship.
  113. var
  114.    length : integer;
  115. begin
  116.   case id of
  117.  
  118.   1 : length := 2;
  119.   2 : length := 3;
  120.   3 : length := 3;
  121.   4 : length := 4;
  122.   5 : length := 5;
  123.  
  124.   6 : length := 2;
  125.   7 : length := 3;
  126.   8 : length := 3;
  127.   9 : length := 4;
  128.   10 : length := 5;
  129.  
  130.   end;
  131.  
  132.   idToLength := length;
  133. end;
  134.  
  135. procedure listGrid( ply : integer ); // lists a specific player's board (GUI).
  136. var
  137.    x, y, c : integer;
  138.    sym : string;
  139.  
  140. begin
  141.  
  142.   writeln( '--------------------------------------------------' );
  143.   writeln( '|    | A B C D E F G H I J | A B C D E F G H I J |' );
  144.   writeln( '--------------------------------------------------' );
  145.  
  146.   for y := 1 to 9 do
  147.       begin
  148.       write( '| ',y,'  | ' );
  149.       for x := 1 to 10 do
  150.           begin
  151.           c := grids[ply,1][x,y];
  152.           sym := getSymbol( c );
  153.           write( sym );
  154.           end;
  155.       write( '| ' );
  156.       for x := 1 to 10 do
  157.           begin
  158.           c := grids[ply,2][x,y];
  159.           sym := getSymbol( c );
  160.           write( sym );
  161.           end;
  162.       writeln( '|');
  163.  
  164.       end;
  165.  
  166.  
  167.   write( '| 10 | ' );
  168.   for x := 1 to 10 do
  169.           begin
  170.           c := grids[ply,1][x,10];
  171.           sym := getSymbol( c );
  172.           write( sym );
  173.           end;
  174.   write( '| ' );
  175.   for x := 1 to 10 do
  176.       begin
  177.       c := grids[ply,2][x,10];
  178.       sym := getSymbol( c );
  179.       write( sym );
  180.       end;
  181.  
  182.   writeln( '|' );
  183.   writeln( '--------------------------------------------------' );
  184.   writeln( '| & : destroyer   + : battleship   / : submarine |' );
  185.   writeln( '| = : cruiser     % : carrier      o : miss      |' );
  186.   writeln( '|                                  x : hit       |' );
  187.   writeln( '--------------------------------------------------' );
  188. end;
  189.  
  190. function createShipData( x, y, v, id : integer ) : TShipData; // composes a TShipData array from its integer parts.
  191. var
  192.    ship : TShipData;
  193. begin
  194.   ship[1] := x;
  195.   ship[2] := y;
  196.   ship[3] := v;
  197.   ship[4] := id;
  198.   createShipData := ship;
  199. end;
  200.  
  201. function boolStrToInt( str : string ) : integer; // converts y/n to true/false.
  202. begin
  203.   if str = 'y' then
  204.      boolStrToInt := 1
  205.   else
  206.       boolStrToInt := 0;
  207. end;
  208.  
  209. function getCoords( ship : TShipData ) : TCoordSet; // gets the set of coordinates corresponding to a particular ship.
  210. var
  211.    x,y, length, k : integer;
  212.    coords : TCoordSet;
  213.    v : boolean;
  214. begin
  215.  
  216.   length := idToLength( ship[4] );
  217.  
  218.   x := ship[1];
  219.   y := ship[2];
  220.   v := (ship[3] = 1);
  221.   coords[0,1] := length;
  222.  
  223.   if v then
  224.      begin
  225.      for k := 1 to length do
  226.          begin
  227.          coords[k,1] := x;
  228.          coords[k,2] := y + k - 1;
  229.          end;
  230.      end
  231.   else
  232.       begin
  233.       for k := 1 to length do
  234.           begin
  235.           coords[k,1] := x + k - 1;
  236.           coords[k,2] := y;
  237.           end;
  238.       end;
  239.  
  240.   getCoords := coords;
  241.  
  242. end;
  243.  
  244. procedure createShip( ship : TShipData); // actually places the ship on its grid.
  245. var
  246.    coords : TCoordSet;
  247.    length, id, k, x, y, ply : integer;
  248. begin
  249.  
  250.   coords := getCoords( ship );
  251.   ply := getPly( ship );
  252.   length := coords[0,1];
  253.   id := ship[4];
  254.  
  255.   for k := 1 to length do
  256.       begin
  257.       x := coords[k,1];
  258.       y := coords[k,2];
  259.       grids[ply,1][x,y] := id;
  260.       end;
  261. end;
  262.  
  263. function checkCoords( ship : TShipData ) : boolean; // checks to see if a ship has valid coordinates.
  264. var
  265.    grid : TGrid;
  266.    coords : TCoordSet;
  267.    x,y, k, length : integer;
  268.    valid : boolean;
  269.  
  270. begin
  271.  
  272.   grid := getGrid( ship );
  273.   coords := getCoords( ship );
  274.   length := coords[0,1];
  275.   valid := true;
  276.  
  277.   for k := 1 to length do
  278.       begin
  279.       x := coords[k,1];
  280.       y := coords[k,2];
  281.  
  282.       if valid and ((x > 10) or (x < 1) or (y > 10) or (y < 1)) then
  283.          begin
  284.          valid := false;
  285.          writeln();
  286.          writeln( 'Error: proposed ship is outside the grid!' );
  287.          writeln();
  288.          end;
  289.  
  290.       if valid and ( not( grid[x,y] = 0 ) ) then
  291.          begin
  292.          valid := false;
  293.          writeln();
  294.          writeln( 'Error: proposed ship intersects with an existing one!' );
  295.          writeln();
  296.          end;
  297.       end;
  298.  
  299.   checkCoords := valid;
  300.  
  301. end;
  302.  
  303. function checkValidFormat( inputStr : string ) : boolean; // checks to see if input is like 'A9'.
  304. var
  305.    valid : boolean;
  306.    xstr, ystr : string;
  307.    y : integer;
  308. begin
  309.   valid := true;
  310.   if length( inputStr ) = 2 then
  311.      begin
  312.      xstr := inputStr[1];
  313.      ystr := inputStr[2];
  314.      end
  315.   else
  316.       if length( inputStr ) = 3 then
  317.          begin
  318.          xstr := inputStr[1];
  319.          ystr := rightStr( inputStr, 2 );
  320.          end;
  321.  
  322.   try y := strToInt( ystr );
  323.   except
  324.     valid := false;
  325.   end;
  326.  
  327.   if (y < 1) or (y > 10) then
  328.      valid := false;
  329.  
  330.   if valid then
  331.      begin
  332.  
  333.      valid := false;
  334.  
  335.      if xstr = 'A' then valid := true;
  336.      if xstr = 'B' then valid := true;
  337.      if xstr = 'C' then valid := true;
  338.      if xstr = 'D' then valid := true;
  339.      if xstr = 'E' then valid := true;
  340.      if xstr = 'F' then valid := true;
  341.      if xstr = 'G' then valid := true;
  342.      if xstr = 'H' then valid := true;
  343.      if xstr = 'I' then valid := true;
  344.      if xstr = 'J' then valid := true;
  345.      if xstr = 'a' then valid := true;
  346.      if xstr = 'b' then valid := true;
  347.      if xstr = 'c' then valid := true;
  348.      if xstr = 'd' then valid := true;
  349.      if xstr = 'e' then valid := true;
  350.      if xstr = 'f' then valid := true;
  351.      if xstr = 'g' then valid := true;
  352.      if xstr = 'h' then valid := true;
  353.      if xstr = 'i' then valid := true;
  354.      if xstr = 'j' then valid := true;
  355.  
  356.      end;
  357.  
  358.   checkValidFormat := valid;
  359.  
  360. end;
  361.  
  362. function stripX( inputStr : string ) : integer; // returns the x coordinate from e.g. 'A9'.
  363. var
  364.    x : integer;
  365.    xstr : string;
  366. begin
  367.      xstr := inputStr[1];
  368.      if xstr = 'A' then x := 1;
  369.      if xstr = 'B' then x := 2;
  370.      if xstr = 'C' then x := 3;
  371.      if xstr = 'D' then x := 4;
  372.      if xstr = 'E' then x := 5;
  373.      if xstr = 'F' then x := 6;
  374.      if xstr = 'G' then x := 7;
  375.      if xstr = 'H' then x := 8;
  376.      if xstr = 'I' then x := 9;
  377.      if xstr = 'J' then x := 10;
  378.      if xstr = 'a' then x := 1;
  379.      if xstr = 'b' then x := 2;
  380.      if xstr = 'c' then x := 3;
  381.      if xstr = 'd' then x := 4;
  382.      if xstr = 'e' then x := 5;
  383.      if xstr = 'f' then x := 6;
  384.      if xstr = 'g' then x := 7;
  385.      if xstr = 'h' then x := 8;
  386.      if xstr = 'i' then x := 9;
  387.      if xstr = 'j' then x := 10;
  388.      stripX := x;
  389. end;
  390.  
  391. function stripY( inputStr : string ) : integer; // returns the y coordinate from e.g. 'A9'.
  392. var
  393.    y : integer;
  394. begin
  395.   if length(inputStr) = 2 then
  396.      y := strToInt( inputStr[2] );
  397.   if length(inputStr) = 3 then
  398.      y := strToInt( rightStr(inputStr, 2) );
  399.   stripY := y;
  400. end;
  401.  
  402. function idToBoatName( id : integer ) : string; // returns the ship name (e.g. 'destroyer) from a shipID.
  403. var
  404.    name : string;
  405. begin
  406.  
  407.   id := ((id - 1) mod 5) + 1;
  408.  
  409.   if id = 1 then name := 'destroyer';
  410.   if id = 2 then name := 'submarine';
  411.   if id = 3 then name := 'cruiser';
  412.   if id = 4 then name := 'battleship';
  413.   if id = 5 then name := 'carrier';
  414.  
  415.   idToBoatName := name;
  416. end;
  417.  
  418. procedure inputBoat ( id : integer ); // gets player input for a ship of a particular shipID.
  419.  
  420. var
  421.    x,y,v : integer;
  422.    boatname, vstr, inputStr : string;
  423.    valid : boolean;
  424.    ship : TShipData;
  425.  
  426. begin
  427.   repeat
  428.   boatname := idToBoatName( id );
  429.   write( 'Top left coordinate of your ',boatname, ': ');
  430.   readln( inputStr );
  431.   valid := true;
  432.  
  433.   if checkValidFormat( inputStr ) then
  434.      begin
  435.      x := stripX( inputStr );
  436.      y := stripY( inputStr );
  437.      end
  438.   else
  439.       begin
  440.       writeln();
  441.       writeln( 'That is not a valid coordinate! Example coordinate: A9' );
  442.       writeln();
  443.       valid := false;
  444.       end;
  445.  
  446.   if valid then
  447.      begin
  448.      write( 'Place ship vertically? (y/n): ' );
  449.      readln( vstr );
  450.      v := boolStrToInt( vstr );
  451.      ship := createShipData( x, y, v, id );
  452.      valid := checkCoords( ship );
  453.  
  454.      end;
  455.  
  456.   until valid;
  457.  
  458.   createShip( ship );
  459. end;
  460.  
  461. procedure setupTitle( ply : integer ); // prints 'Player 1 Setup' (GUI).
  462. begin
  463.      writeln( '----------------' );
  464.      writeln( 'Player ',ply,' set up:' );
  465.      writeln( '----------------' );
  466.      writeln();
  467. end;
  468.  
  469. procedure playerSetUp( ply : integer ); // gets a player to put his/her ships down.
  470. var
  471.    n : integer;
  472. begin
  473.  
  474.   setUpTitle( ply );
  475.   listGrid( ply );
  476.  
  477.   for n := (((ply-1)*5) + 1) to (((ply-1)*5) + 5) do
  478.       begin
  479.       writeln();
  480.       inputBoat( n );
  481.       writeln();
  482.       setupTitle( ply );
  483.       listGrid( ply );
  484.       end;
  485.  
  486. end;
  487.  
  488. function otherPly( ply : integer ) : integer; // returns the player that isn't the current one.
  489. begin
  490.   otherPly := ( ply mod 2) + 1;
  491. end;
  492.  
  493. procedure turnTitle( ply : integer ); // prints 'Player 1's turn' (GUI).
  494. begin
  495.      writeln( '----------------' );
  496.      writeln( 'Player ',ply,'''s turn:' );
  497.      writeln( '----------------' );
  498.      writeln();
  499. end;
  500.  
  501. procedure nextPlayerWait(); // the set processes that switch from one player's turn to the other's.
  502. var
  503.    nextPly : integer;
  504. begin
  505.   nextPly := otherPly( currentPly );
  506.   writeln();
  507.   writeln( 'Player ',nextPly,'''s turn! Press enter to continue!' );
  508.   writeln();
  509.   readln();
  510.   clearWindow();
  511.   writeln( 'Player ',nextPly,' are you ready? Press enter to continue!' );
  512.   writeln();
  513.   readln();
  514.   currentPly := nextPly;
  515. end;
  516.  
  517. function validMove( x, y, ply : integer) : boolean;
  518. var
  519.    valid : boolean;
  520. begin
  521.  
  522.   valid := true;
  523.  
  524.   if ((x > 10) or (x < 1) or (y > 10) or (y < 1)) then
  525.          begin
  526.          valid := false;
  527.          writeln();
  528.          writeln( 'Error: proposed target is outside the grid!' );
  529.          writeln();
  530.          end;
  531.   if valid and (not(grids[ply,2][x][y] = 0)) then
  532.          begin
  533.          valid := false;
  534.          writeln();
  535.          writeln( 'Error: target has already been tried!' );
  536.          writeln();
  537.          end;
  538.  
  539.   validMove := valid;
  540. end;
  541.  
  542. procedure execMove( x, y, ply : integer);
  543. var
  544.    hit : boolean;
  545. begin
  546.  
  547.   hit := false;
  548.  
  549.   if grids[otherPly(ply),1][x,y] > 0 then
  550.      begin
  551.      grids[otherPly(ply),1][x,y] := -1;
  552.      grids[ply,2][x,y] := -1;
  553.      hit := true;
  554.      end
  555.   else
  556.       grids[ply,2][x,y] := -2;
  557.  
  558.   turnTitle( ply );
  559.   listGrid( ply );
  560.   if hit then
  561.      begin
  562.      writeln();
  563.      writeln( 'You hit player ',otherPly(ply),'''s ship!' );
  564.      writeln();
  565.      end
  566.   else
  567.       begin
  568.       writeln();
  569.       writeln( 'That was a miss.' );
  570.       writeln();
  571.       end;
  572.  
  573. end;
  574.  
  575. procedure inputGo( ply : integer);
  576. var
  577.    x, y : integer;
  578.    inputStr: string;
  579.    valid : boolean;
  580. begin
  581.  
  582.   turnTitle( ply );
  583.   listGrid( ply );
  584.   writeln();
  585.  
  586.   repeat
  587.  
  588.   valid := true;
  589.   write( 'Desired coordinate of attack: ' );
  590.   readln( inputStr );
  591.  
  592.   if checkValidFormat( inputStr ) then
  593.      begin
  594.      x := stripX( inputStr );
  595.      y := stripY( inputStr );
  596.      end
  597.   else
  598.       begin
  599.       writeln();
  600.       writeln( 'That is not a valid coordinate!' );
  601.       writeln();
  602.       valid := false;
  603.       end;
  604.  
  605.   if valid then
  606.      valid := validMove( x, y, ply );
  607.  
  608.   until valid;
  609.  
  610.   execMove( x, y, ply );
  611.  
  612. end;
  613.  
  614. function getPiecesLeft( ply: integer ) : integer;
  615. var
  616.    grid : TGrid;
  617.    x,y, count : integer;
  618. begin
  619.   grid := grids[ply,1];
  620.   count := 0;
  621.  
  622.   for y := 1 to 10 do
  623.       for x := 1 to 10 do
  624.           begin
  625.           if grid[x,y] > 0 then
  626.              count := count + 1;
  627.           end;
  628.   getPiecesLeft := count;
  629. end;
  630.  
  631. begin
  632.  
  633.   printTitle();
  634.   clearAllGrids();
  635.  
  636.   currentPly := 1;
  637.  
  638.   playerSetUp( currentPly ); // set up player 1
  639.   nextPlayerWait();
  640.  
  641.   playerSetUp( currentPly ); // set up player 2
  642.   nextPlayerWait();
  643.  
  644.   repeat
  645.  
  646.   inputGo( currentPly );
  647.  
  648.   if getPiecesLeft( otherPly( currentPly ) ) = 0 then
  649.      begin
  650.      writeln();
  651.      writeln( 'Congratulations! Player ',currentPly,' has won! :D' );
  652.      writeln();
  653.      currentPly := otherPly( currentPly );
  654.      end
  655.   else
  656.       nextPlayerWait();
  657.  
  658.   until getPiecesLeft( currentPly ) = 0;
  659.  
  660.   writeln( 'Game over!' );
  661.   readln()
  662. end.
Add Comment
Please, Sign In to add comment