Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Blackjackfix;
- uses crt;
- var my:array[1..5] of integer;
- opp:array[1..5] of integer;
- deck:array[1..11] of integer;
- i,l,d,j,temp,stay,my_total,opp_total,k,SpellCheck1:integer;
- // i, l, d stands sequently for my deck, opponent's deck and the deck itself
- // j for temporary hold of randoming the deck
- // temp for temporary hold for swapping the deck
- // stay for count the number, if both stay (stay=2) then end the game
- // my_total and opp_total stands sequently for my total and opponent's total
- // k for printing out the players' decks
- eog:boolean; // To decide to end the game or not
- s:string; // To store the input of player
- //Subprograms declaration
- procedure SpellCheck(s:string); // This returns 3 values, either "1" - "draw", "2" - "stay", or "3" - nonsense
- begin
- SpellCheck1:=3;
- if s='draw' then SpellCheck1:=1 else
- if s='stay' then SpellCheck1:=2;
- end;
- procedure MyTurn(); // Holder
- begin
- while SpellCheck1=3 do
- begin
- writeln('This is your turn. Write down "stay" or "draw" without quotes then Enter');
- writeln('This input message is repeated if you enter something different from those above');
- write('Input: ');
- readln(s);
- spellcheck(s);
- writeln;
- end;
- if SpellCheck1=1 then
- begin
- my[i]:=deck[d]; // Draw a card
- write('You drew a(n) ',my[i]);
- writeln;
- inc(i); inc(d); // Increase mine and deck's
- stay:=0;
- my_total:=0;
- for k:=1 to 5 do
- my_total:=my_total+my[k]; // Count the total of my cards
- end
- else
- if SpellCheck1=2 then stay:=stay+1; // End of my turn
- end;
- procedure OppTurn(); // Holder
- begin
- if stay=1 then stay:=stay+1 // This is opp's turn
- else
- begin
- write('Opponent drew a card');
- writeln;
- writeln;
- opp[l]:=deck[d]; // Draw a card
- inc(l); inc(d); // Increase both
- stay:=0;
- opp_total:=0;
- for k:=1 to 5 do
- opp_total:=opp_total+opp[k]; // Count the total of opp's cards
- end; // End of opp's turn
- end;
- procedure MidPrint(); // Print out both players cards mid game, remember to censor opponent's card
- begin
- // First, print out my cards
- write('Your cards: ');
- for k:=1 to 5 do
- write(my[k],' ');
- writeln;
- // Then, check the total and change color
- if my_total=21 then
- begin
- write('Your total: ');
- textcolor(green); // Set color to green if my total reaches 21
- write(my_total);
- textcolor(grey); // Set color back to normal
- write('/21');
- writeln;
- end
- else
- if my_total>21 then
- begin
- write('Your total: ');
- textcolor(red); // Set color to red if my total surpasses 21
- write(my_total);
- textcolor(grey); // Set color to normal
- write('/21');
- writeln;
- end
- else
- if my_total<21 then // If none of both above then print out normally
- begin
- write('Your total: ',my_total,'/21');
- writeln;
- end;
- // Next, print out opponent's cards, remember to censor
- write('Opponent''s cards: ? '); // Write down opponent's cards
- for k:=2 to 5 do
- write(opp[k],' '); // This writes "Opponent"s total: ? 8 10 2" for example
- writeln;
- // Final, check the total and change color
- if (opp_total-opp[1])>21 then
- begin
- write('Opponent''s total: ? + ');
- textcolor(red); // Set color to red if opponent's total surpasses 21
- // Should not set color to green because we don't know the hidden card
- write(opp_total-opp[1]);
- textcolor(grey); // Set color back to normal
- write('/21');
- writeln;
- end
- else
- if (opp_total-opp[1])<21 then
- write('Opponent''s total: ? + ',opp_total-opp[1],'/21');
- end;
- procedure PostPrint(); // Print out both players card post game, when both stay
- begin
- writeln('--------------------');
- writeln('Final score:');
- writeln;
- // First, print out my cards
- write('Your cards: ');
- for k:=1 to 5 do
- write(my[k],' ');
- writeln;
- // Then, check the total and change color
- if my_total=21 then
- begin
- write('Your total: ');
- textcolor(green); // Set color to green if my total reaches 21
- write(my_total);
- textcolor(grey); // Set color back to normal
- write('/21');
- writeln;
- end
- else
- if my_total>21 then
- begin
- write('Your total: ');
- textcolor(red); // Set color to red if my total surpasses 21
- write(my_total);
- textcolor(grey); // Set color to normal
- write('/21');
- writeln;
- end
- else
- if my_total<21 then // If none of both above then print out normally
- begin
- write('Your total: ',my_total,'/21');
- writeln;
- end;
- // Next, print out opponent's cards, {remember to censor} - no need
- write('Opponent''s cards: '); // Write down opponent's cards
- for k:=1 to 5 do
- write(opp[k],' '); // Fully write down opponent's cards
- writeln;
- // Final, check the total and change color
- if (opp_total)>21 then
- begin
- write('Opponent''s total: ');
- textcolor(red); // Set color to red if opponent's total surpasses 21
- write(opp_total);
- textcolor(grey); // Set color back to normal
- write('/21');
- writeln;
- end
- else
- if (opp_total)<21 then
- write('Opponent''s total: ',opp_total,'/21') else
- if (opp_total)=21 then
- begin
- write('Opponent''s total: ');
- textcolor(green);
- write(opp_total);
- textcolor(grey);
- write('/21');
- writeln;
- end;
- end;
- procedure PreDeck(); // Initialise and random the deck
- begin
- // Initialise the deck array
- for i:=1 to 11 do
- deck[i]:=i;
- //Randomise the deck
- for i:=1 to 11 do
- begin
- j:=random(10-i)+1;
- temp:=deck[j];
- deck[j]:=deck[11-i+1];
- deck[11-i+1]:=temp;
- end;
- end;
- procedure EmptyDeck(); // Empty both decks, to make sure all elements are 0, to prevent counting error of my_total and opp_total
- // Use only for pregame
- begin
- // Empty both's deck
- for i:=1 to 5 do
- begin
- my[i]:=0;
- opp[i]:=0;
- end;
- end;
- procedure FirstCard(); // Initialise first card for both decks
- begin
- my[1]:=deck[1];
- opp[1]:=deck[2];
- end;
- procedure Calculate(); // Calculate both scores, and decide the winner
- begin
- if (my_total=opp_total) then write('You both draw') else
- if (my_total<21) and (opp_total<21) then
- if (my_total<opp_total) then write('You lose') else write('You won') else
- if (my_total>21) and (opp_total>21) then
- if (my_total<opp_total) then write('You won') else write('You lose') else
- if (my_total<21) and (opp_total>21) then write('You won') else
- if (my_total>21) and (opp_total<21) then write('You lose');
- end;
- begin // Main program
- clrscr; // Clear out the screen
- randomize; // First call to run the generator
- // Pregame section
- PreDeck();
- EmptyDeck();
- FirstCard();
- my_total:=my[1];
- opp_total:=opp[1];
- i:=2; l:=2; d:=3;
- // Midgame section
- eog:=false;
- while not eog do // While eog=false do
- begin
- stay:=0; // No one has stayed just yet
- while stay<2 do // So then the game continues
- begin
- SpellCheck1:=3;
- MidPrint();
- writeln;
- writeln;
- writeln('****************');
- writeln('****************');
- writeln;
- MyTurn();
- OppTurn();
- if stay=2 then
- begin
- PostPrint();
- writeln;
- writeln;
- Calculate();
- eog:=true;
- end;
- end;
- readln;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement