{                   Chris Goodno and Matt Michie  }
{                   Pascal, pd. 4                 }
{                   Filename: ch3/gamech3         }
{                   Due Date: January 21, 1994    }
{Program description:                             }
{*************************************************}
{  Grading Criteria:                              }
{                                 Header ______   }
{                                                 }
{                    Program Description ______   }
{                                                 }
{                           Presentation ______   }
{                                                 }
{                         Run of Program ______   }
{                                                 }
{                            Final Grade ______   }
{                                                 }
{*************************************************}
program blackjacktwentyone (output, input);
  uses crt, graph3, graph;
  var
    device, mode, cardnum, computer, cwon, clost, pwon, plost, twon,
    loop, angle, x, color, c : integer;
    number, counterc, counterp: real;
    playagain : char;
{---------------------------------------------------------------------------}

procedure roundthing (times:integer);
  begin {roundthing}
    for loop:= 1 to times do
      begin {for loop}
        forwd (50);
        TurnRight (90);
        forwd (50);
        TurnRight (90);
        forwd (50);
        TurnRight (90);
        forwd (50);
        TurnRight (1);
        delay (20);
      end;  {for loop}
  end; {roundthing}

{---------------------------------------------------------------------------}
  begin {main}
    randomize;
    detectGraph (Device, Mode);
    initgraph (device, mode, 'C:\tp\bgi');
    SetColor (random(14)+1);
    Settextstyle (gothicfont, horizdir, 4);
    outtext ('Welcome to Blackjack ');
    settextstyle  (TriplexFont, HorizDir, 5);
    outtext ('Test');
   {outtext ('Please press enter to continue');}
    readLn;
    GraphColorMode;
    x:=1;
    angle:=90;
    for loop:= 1 to 150 do
      begin {for loop}
        Forwd (x);
        TurnRight (angle);
        delay (10);
        setpencolor(2);
        angle:=angle+120;
        x:=x+1;
      end;  {for loop}
    delay (1000);
    ClearScreen;
    setpencolor(1);
    roundthing (150);
    setpencolor(2);
    penup;
    forwd(random(50)+2);
    pendown;
    roundthing (150);
    penup;
    back (random(100)+1);
    setpencolor(1);
    pendown;
    roundthing (125);
    readLN;
    closegraph;
    clrscr;
    playagain := 'y';
    cwon := 0;
    pwon := 0;
    twon := 0;
    clost := 0;
    plost := 0;
    while playagain = 'y' do
      begin {Game loop}
        counterc := 0;
        counterp := 0;
        clrscr;
        randomize;
        {$I-}
          write ('How many cards do you want? ');
          textcolor(random(14)+1);
          readln (cardnum);
        {$I+}
        while ((IOresult <> 0) or (cardnum >= 5) or (cardnum >= maxint)) or (cardnum < 0) do
          begin {while loop to get good number of cards}
            writeln ('Bad card value, has to be less than 5, try again: ');
            {$I-}
              write ('How many cards do you want? ');
              readln (cardnum);
            {$I+}
          end;  {while loop to get good number of cards}
        while cardnum < 5 do
          begin {while loop}
            writeln;
            textcolor (random(14)+1);
            write ('Computer draws: ');
            for computer := 1 to cardnum do
              begin {for loop}
                number := random(11) + 1;
                delay (100);
                textcolor ((random(14)+1));
                write (number:2:0,' ');
                counterc := counterc + number;
              end;  {for loop}
            writeln;
            textcolor (random(14)+1);
            write ('Player draws:   ');
            for computer := 1 to cardnum do
              begin {for loop}
                number := random(11) + 1;
                delay (100);
                textcolor (random(14)+1);
                write (number:2:0,' ');
                counterp := counterp + number;
                cardnum := 5;
             end;  {for loop}
          end; {while loop}
        writeln;
        writeln;
        textcolor (random(14)+1);
        write ('Computer gets: ');
        textcolor (random(14)+1);
        writeln (counterc:2:0);
        textcolor (random(14)+1);
        write ('Player gets  : ');
        textcolor (random(14)+1);
        writeln (counterp:2:0);
        if (counterc > counterp) and (counterc < 22) or (counterc = 21 ) then
          begin {if then to get total won}
            cwon := cwon + 1;
            plost := plost + 1;
          end   {if then to get total won}
        else
        if (counterp > counterc) and (counterp < 22) or (counterp = 21) then
          begin {If then to get total won}
            pwon := pwon + 1;
            clost := clost + 1;
          end   {if then to get total won}
        else
        if (counterc > 21) and (counterp > 21) then
          twon := twon +1
        else
        if (counterc < counterp) and (counterp > 21) and (counterc < 22) then
          begin {if then to get total won}
            cwon := cwon + 1;
            plost := plost + 1;
          end   {if then to get total won}
        else
        if (counterp < counterc) and (counterc > 21) and (counterp < 22) then
          begin {if then to get total won}
            pwon := pwon + 1;
            clost := clost + 1;
          end   {if then to get total won}
        else
        if counterc = counterp then
          twon := twon + 1;
        writeln;
        textcolor (random(14)+1);
        write ('Computer:  ');
        textcolor (random(14)+1);
        write ('won:');
        textcolor (random(14)+1);
        write (cwon:3);
        textcolor (random(14)+1);
        write ('  lost:');
        textcolor (random(14)+1);
        writeln (clost:3);
        textcolor (random(14)+1);
        write ('Player  :');
        textcolor (random(14)+1);
        write ('  won:');
        textcolor (random(14)+1);
        write (pwon:3);
        textcolor (random(14)+1);
        write ('  lost:');
        textcolor (random(14)+1);
        writeln (plost:3);
        textcolor (random(14)+1);
        write ('Ties    :  ');
        textcolor (random(14)+1);
        writeln (twon);
        writeln;
        textcolor (random(14)+1);
        write ('Would You like to play again? ');
        readln (playagain);
        if playagain = 'Y' then
          playagain := 'y';
        end;  {Game loop}
    clrscr;
    number := random(6)+1;
    if cwon > pwon then
      begin {if then to get ending}
        textcolor (random(14)+1);
        if number = 1 then
          writeln ('Happy, Happy, Joy, Joy, I won ... Happy, Happy, etc.')
        else
        if number = 2 then
          writeln ('Huh, Huh, Hum, You stink, You lose, don''t play again.')
        else
        if number = 3 then
          writeln ('Me being arogant and all, I say I had a good game!!!')
        else
        if number = 4 then
          begin {losing words}
            writeln ('Ode to my Winnings   By: C.P.U.');
            writeln;
            writeln ('On the outside I look like a sad little computer.');
            writeln ('But only if you could see the inside of me....');
            writeln ('I would be laughing at your lost....');
          end   {losing words}
        else
        if number = 5 then
          writeln ('Cash can be paid up front to the disk drive, thank you')
        else
        if number = 6 then
          begin {losing words}
            writeln ('It was a really good game....');
            writeln (' If I were to look at it backwards!!!');
          end   {losing words}
        end   {if then to get ending}
    else
    if pwon > cwon then
      begin {if then to get ending}
        textcolor (random(14)+1);
        If number = 1 then
          writeln ('Good Job, now go away so that I may cheat in peace...')
        else
        if number = 2 then
          writeln ('Huh, huh, he thinks he''s cool just cause he won, cool!')
        else
        if number = 3 then
          begin {winning words}
            writeln ('If you look at it from a different perspective....');
            writeln (' I believe you cheated...');
          end  {winning words}
        else
        if number = 4 then
          begin {winning words}
            writeln ('Don''t flatter yourself for winning a game...');
            writeln ('  I let you win!!!!');
          end   {winning words}
        else
        if number = 5 then
          begin {winning words}
            writeln ('Don''t worry, I will beat you next time..');
            writeln ('Sure you don''t want to play again???!!');
          end   {winning words}
        else
        if number = 6 then
          writeln ('Go Away, Don''t come again another day!!!');
        end   {if then to get ending}
    else
    if pwon = cwon then
      begin {if then to get ending}
        textcolor (random(14)+1);
        writeln ('HEY, It''s A Tie...');
      end;  {if then to get ending}
    writeln;
    writeln ('Thank you for playing ....');
    writeln ('Au Revoire');
    writeln ('Chow Bombino');
    readln;
    end. {main}
