Advertisement
linkedparadise

Blackjack fix 28-02-2017

Feb 28th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 7.18 KB | None | 0 0
  1. program Blackjackfix;
  2. uses crt;
  3. var my:array[1..5] of integer;
  4.     opp:array[1..5] of integer;
  5.     deck:array[1..11] of integer;
  6.     i,l,d,j,temp,stay,my_total,opp_total,k,SpellCheck1:integer;
  7.     // i, l, d stands sequently for my deck, opponent's deck and the deck itself
  8.     // j for temporary hold of randoming the deck
  9.     // temp for temporary hold for swapping the deck
  10.     // stay for count the number, if both stay (stay=2) then end the game
  11.     // my_total and opp_total stands sequently for my total and opponent's total
  12.     // k for printing out the players' decks
  13.     eog:boolean; // To decide to end the game or not
  14.     s:string; // To store the input of player
  15.  
  16. //Subprograms declaration
  17. procedure SpellCheck(s:string); // This returns 3 values, either "1" - "draw", "2" - "stay", or "3" - nonsense
  18. begin
  19.     SpellCheck1:=3;
  20.     if s='draw' then SpellCheck1:=1 else
  21.     if s='stay' then SpellCheck1:=2;
  22. end;
  23.  
  24. procedure MyTurn(); // Holder
  25. begin
  26.     while SpellCheck1=3 do
  27.     begin
  28.         writeln('This is your turn. Write down "stay" or "draw" without quotes then Enter');
  29.         writeln('This input message is repeated if you enter something different from those above');
  30.         write('Input: ');
  31.         readln(s);
  32.                 spellcheck(s);
  33.         writeln;
  34.     end;
  35.     if SpellCheck1=1 then
  36.     begin
  37.         my[i]:=deck[d]; // Draw a card
  38.         write('You drew a(n) ',my[i]);
  39.         writeln;
  40.         inc(i); inc(d); // Increase mine and deck's
  41.         stay:=0;
  42.         my_total:=0;
  43.         for k:=1 to 5 do
  44.             my_total:=my_total+my[k]; // Count the total of my cards
  45.     end
  46.     else
  47.         if SpellCheck1=2 then stay:=stay+1; // End of my turn
  48. end;
  49.  
  50. procedure OppTurn(); // Holder
  51. begin
  52.     if stay=1 then stay:=stay+1 // This is opp's turn
  53.     else
  54.     begin
  55.         write('Opponent drew a card');
  56.         writeln;
  57.         writeln;
  58.         opp[l]:=deck[d]; // Draw a card
  59.         inc(l); inc(d); // Increase both
  60.         stay:=0;
  61.         opp_total:=0;
  62.         for k:=1 to 5 do
  63.             opp_total:=opp_total+opp[k]; // Count the total of opp's cards
  64.     end; // End of opp's turn
  65. end;
  66.  
  67. procedure MidPrint(); // Print out both players cards mid game, remember to censor opponent's card
  68. begin
  69.     // First, print out my cards
  70.     write('Your cards: ');
  71.     for k:=1 to 5 do
  72.         write(my[k],' ');
  73.     writeln;
  74.        
  75.     // Then, check the total and change color
  76.     if my_total=21 then
  77.     begin
  78.         write('Your total: ');
  79.         textcolor(green);   // Set color to green if my total reaches 21
  80.         write(my_total);
  81.         textcolor(grey);    // Set color back to normal
  82.         write('/21');
  83.         writeln;
  84.     end
  85.     else
  86.     if my_total>21 then
  87.     begin
  88.         write('Your total: ');
  89.         textcolor(red); // Set color to red if my total surpasses 21
  90.         write(my_total);
  91.         textcolor(grey);    // Set color to normal
  92.         write('/21');
  93.         writeln;
  94.     end
  95.     else
  96.     if my_total<21 then // If none of both above then print out normally
  97.     begin
  98.         write('Your total: ',my_total,'/21');
  99.         writeln;
  100.     end;
  101.    
  102.     // Next, print out opponent's cards,  remember to censor
  103.     write('Opponent''s cards: ? '); // Write down opponent's cards
  104.     for k:=2 to 5 do
  105.         write(opp[k],' '); // This writes "Opponent"s total: ? 8 10 2" for example
  106.     writeln;
  107.    
  108.     // Final, check the total and change color
  109.     if (opp_total-opp[1])>21 then
  110.     begin
  111.         write('Opponent''s total: ? + ');
  112.         textcolor(red); // Set color to red if opponent's total surpasses 21
  113.         // Should not set color to green because we don't know the hidden card
  114.         write(opp_total-opp[1]);
  115.         textcolor(grey);    // Set color back to normal
  116.         write('/21');
  117.         writeln;
  118.     end
  119.     else
  120.     if (opp_total-opp[1])<21 then
  121.         write('Opponent''s total: ? + ',opp_total-opp[1],'/21');
  122. end;
  123.  
  124. procedure PostPrint(); // Print out both players card post game, when both stay
  125. begin
  126.     writeln('--------------------');
  127.     writeln('Final score:');
  128.     writeln;
  129.     // First, print out my cards
  130.     write('Your cards: ');
  131.     for k:=1 to 5 do
  132.         write(my[k],' ');
  133.     writeln;
  134.        
  135.     // Then, check the total and change color
  136.     if my_total=21 then
  137.     begin
  138.         write('Your total: ');
  139.         textcolor(green);   // Set color to green if my total reaches 21
  140.         write(my_total);
  141.         textcolor(grey);    // Set color back to normal
  142.         write('/21');
  143.         writeln;
  144.     end
  145.     else
  146.     if my_total>21 then
  147.     begin
  148.         write('Your total: ');
  149.         textcolor(red); // Set color to red if my total surpasses 21
  150.         write(my_total);
  151.         textcolor(grey);    // Set color to normal
  152.         write('/21');
  153.         writeln;
  154.     end
  155.     else
  156.     if my_total<21 then // If none of both above then print out normally
  157.     begin
  158.         write('Your total: ',my_total,'/21');
  159.         writeln;
  160.     end;
  161.    
  162.     // Next, print out opponent's cards,  {remember to censor} - no need
  163.     write('Opponent''s cards: '); // Write down opponent's cards
  164.     for k:=1 to 5 do
  165.         write(opp[k],' '); // Fully write down opponent's cards
  166.     writeln;
  167.    
  168.     // Final, check the total and change color
  169.     if (opp_total)>21 then
  170.     begin
  171.         write('Opponent''s total: ');
  172.         textcolor(red); // Set color to red if opponent's total surpasses 21
  173.         write(opp_total);
  174.         textcolor(grey);    // Set color back to normal
  175.         write('/21');
  176.         writeln;
  177.     end
  178.     else
  179.     if (opp_total)<21 then
  180.         write('Opponent''s total: ',opp_total,'/21') else
  181.     if (opp_total)=21 then
  182.         begin
  183.             write('Opponent''s total: ');
  184.             textcolor(green);
  185.             write(opp_total);
  186.             textcolor(grey);
  187.             write('/21');
  188.             writeln;
  189.         end;
  190. end;
  191.  
  192. procedure PreDeck(); // Initialise and random the deck
  193. begin
  194.     // Initialise the deck array
  195.     for i:=1 to 11 do
  196.         deck[i]:=i;
  197.  
  198.     //Randomise the deck
  199.     for i:=1 to 11 do
  200.         begin
  201.             j:=random(10-i)+1;
  202.             temp:=deck[j];
  203.             deck[j]:=deck[11-i+1];
  204.             deck[11-i+1]:=temp;
  205.         end;
  206. end;
  207.  
  208. procedure EmptyDeck(); // Empty both decks, to make sure all elements are 0, to prevent counting error of my_total and opp_total
  209.                         // Use only for pregame
  210. begin
  211.     // Empty both's deck
  212.      for i:=1 to 5 do
  213.          begin
  214.              my[i]:=0;
  215.              opp[i]:=0;
  216.          end;
  217. end;
  218.  
  219. procedure FirstCard(); // Initialise first card for both decks
  220. begin
  221.     my[1]:=deck[1];
  222.     opp[1]:=deck[2];
  223. end;
  224.  
  225. procedure Calculate(); // Calculate both scores, and decide the winner
  226. begin
  227.     if (my_total=opp_total) then write('You both draw') else
  228.     if (my_total<21) and (opp_total<21) then
  229.         if (my_total<opp_total) then write('You lose') else write('You won') else
  230.     if (my_total>21) and (opp_total>21) then
  231.         if (my_total<opp_total) then write('You won') else write('You lose') else
  232.     if (my_total<21) and (opp_total>21) then write('You won') else
  233.     if (my_total>21) and (opp_total<21) then write('You lose');
  234. end;
  235.  
  236. begin // Main program
  237.     clrscr; // Clear out the screen
  238.     randomize; // First call to run the generator
  239.    
  240.     // Pregame section
  241.     PreDeck();
  242.     EmptyDeck();
  243.     FirstCard();
  244.    
  245.     my_total:=my[1];
  246.     opp_total:=opp[1];
  247.     i:=2; l:=2; d:=3;
  248.    
  249.     // Midgame section
  250.     eog:=false;
  251.     while not eog do // While eog=false do
  252.     begin
  253.         stay:=0; // No one has stayed just yet
  254.         while stay<2 do // So then the game continues
  255.         begin
  256.             SpellCheck1:=3;
  257.             MidPrint();
  258.             writeln;
  259.             writeln;
  260.             writeln('****************');
  261.             writeln('****************');
  262.             writeln;
  263.             MyTurn();
  264.             OppTurn();
  265.             if stay=2 then
  266.             begin
  267.                 PostPrint();
  268.                 writeln;
  269.                 writeln;
  270.                 Calculate();
  271.                 eog:=true;
  272.             end;
  273.         end;
  274.         readln;
  275.     end;
  276. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement