Task: 21 game

C: 



#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#if __STDC_VERSION__ >= 199901L || _MSC_VER >= 1800
#include <stdbool.h>
#else
#define bool int
#define true  1
#define false 0
#endif

#define GOAL               21
#define NUMBER_OF_PLAYERS   2
#define MIN_MOVE            1
#define MAX_MOVE            3
#define BUFFER_SIZE       256

#define _(STRING) STRING



static char DESCRIPTION[] = 
    "21 Game                                                          \n"
    "                                                                 \n"
    "21 is a two player game, the game is played by choosing a number \n"
    "(1, 2, or 3) to be added to the running total. The game is won by\n"
    "the player whose chosen number causes the running total to reach \n"
    "exactly 21. The running total starts at zero.                    \n\n";

static int total;


void update(char* player, int move)
{
    printf("%8s:  %d = %d + %d\n\n", player, total + move, total, move);
    total += move;
    if (total == GOAL)
        printf(_("The winner is %s.\n\n"), player);
}


int ai()
{

#if GOAL < 32 && MIN_MOVE == 1 && MAX_MOVE == 3
    static const int precomputed[] = { 1, 1, 3, 2, 1, 1, 3, 2, 1, 1, 3, 2, 1, 1,
        3, 2, 1, 1, 3, 2, 1, 1, 3, 2, 1, 1, 3, 2, 1, 1, 3 };
    update(_("ai"), precomputed[total]);
#elif MIN_MOVE == 1 && MAX_MOVE == 3
    static const int precomputed[] = { 1, 1, 3, 2};
    update(_("ai"), precomputed[total % (MAX_MOVE + 1)]);
#else
    int i;
    int move = 1;
    for (i = MIN_MOVE; i <= MAX_MOVE; i++)
        if ((total + i - 1) % (MAX_MOVE + 1) == 0)
            move = i;
    for (i = MIN_MOVE; i <= MAX_MOVE; i++)
        if (total + i == GOAL)
            move = i;
    update(_("ai"), move);
#endif
}


void human(void)
{
    char buffer[BUFFER_SIZE];
    int move;
 
    while ( printf(_("enter your move to play (or enter 0 to exit game): ")),
            fgets(buffer, BUFFER_SIZE, stdin), 
            sscanf(buffer, "%d", &move) != 1 ||
            (move && (move < MIN_MOVE || move > MAX_MOVE || total+move > GOAL)))
        puts(_("\nYour answer is not a valid choice.\n"));
    putchar('\n');
    if (!move) exit(EXIT_SUCCESS);
    update(_("human"), move);
}


int main(int argc, char* argv[])
{
    srand(time(NULL));
    puts(_(DESCRIPTION));
    while (true)
    {
        puts(_("\n---- NEW GAME ----\n"));
        puts(_("\nThe running total is currently zero.\n"));
        total = 0;

        if (rand() % NUMBER_OF_PLAYERS)
        {
            puts(_("The first move is AI move.\n"));
            ai();
        }
        else
            puts(_("The first move is human move.\n"));

        while (total < GOAL)
        {
            human();
            ai();
        }
    }
}










Fortran: 

subroutine ai
  common itotal, igoal
  if (itotal .lt. igoal) then
    move = 1
    do i = 1, 3
      if (mod(itotal + i - 1 , 4) .eq. 0) then
        move = i
      end if
    end do
    do i = 1, 3
      if (itotal + i .eq. igoal) then
        move = i
      end if
    end do
    print *, "      ai  ", itotal + move, " = ", itotal, " + ", move
    itotal = itotal + move
    if (itotal .eq. igoal) then
      print *, ""
      print *, "the winner is ai"
      print *, ""
    end if
  end if
end subroutine ai

subroutine human
  common itotal, igoal
  print *, ""
  do while (.true.)
    if (itotal + 1 .eq. igoal) then
      print *, "enter 1 (or 0 to exit): "
    else if (itotal + 2 .eq. igoal) then
      print *, "enter 1 or 2 (or 0 to exit): "
    else
      print *, "enter 1 or 2 or 3 (or 0 to exit)"
    end if
    read(*,*) move
    if (move .eq. 0) then
      stop
    else if (move .ge. 1 .and. move .le. 3 .and. move + itotal .le. igoal) then
      print *, "   human  ", itotal + move, " = ", itotal, " + ", move
      itotal = itotal + move
      if (itotal .eq. igoal) then
        print *, ""
        print *, "the winner is human"
        print *, ""
      end if
      return
    else
      print *, "a bad choice"
    end if
  end do
end subroutine human

program main
  common itotal, igoal
  print *,"game 21 - an example in fortran iv language for rosseta code."
  print *,""
  print *,"21 is a two player game, the game is played by choosing a number"
  print *,"(1, 2, or 3) to be added to the running total. the game is won"
  print *,"by the player whose chosen number causes the running total to reach"
  print *,"exactly 21. the running total starts at zero."
  print *,""
  i = irand(1)
  igoal = 21
  do while(.true.)
    print *, "---- new game ----"
    print *, ""
    print *, "the running total is currently zero."
    print *, ""
    itotal = 0
    if (mod(irand(0), 2) .eq. 0) then
      print *, "the first move is ai move."
      call ai
    else
      print *, "the first move is human move."
    end if
    print *, ""
    do while(itotal .lt. igoal)
      call human
      call ai
    end do
end do
end program main