summaryrefslogtreecommitdiff
path: root/662/DEPENDENCIES/play.sci
blob: 2cef17f547830f3ef945fa2a139b54f4f2da097c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//Programming Example 8.9
        //Second File
        
       //Simulate one complete game

function[]= play()
    printf("\n Please throw thw dice");
    dummy=scanf("%c");
    printf("\n");
    score1=throw();            //function call
    printf("\n score1 =%2d", score1);
    select(score1)
    
        //win on first throw
        case 7
            printf("\n Congratulations! you WIN on the first throw\n");    
        case 11
            printf(" \nCongratulations! you WIN on the first throw\n");
       
        //lose on first throw
        case 2
            printf("\nSorry, you LOSE on the first throw\n ");        
        case 3
            printf("\nSorry, you LOSE on the first throw\n ");
        case 12
            printf("\nSorry, you LOSE on the first throw\n ");
       
        //additional throws are required
        case 4
            code();
        case 5
            code();
        case 6
             code();
        case 7
            code();
        case 8
             code();
        case 9
            code();
        case 10
           code();
         end
        return;
endfunction


    //Simulate  one throw of a pair of dice
function[n] = throw()
    x1 = rand();
    x2 = rand();
    disp(x1);
    disp(x2);
    n1 = 1 + int16(6* x1);
    n2 = 1 + int16(6* x2);
    n= n1+n2;
    printf("\n\n n= %d\n\n", n);
    return n;
endfunction

//code for the cases requiring additional  throws
// as in scilab we need to write code for each case seperately
//so as not to repeat the same code again and again.
function[] = code()
    first="true";
            score2=throw();
            while((score2<> score1 & score2<>7) | first=="true")
                first="false";
                printf("\n Throw the dice again..");
                dummy=scanf("%c");
                score2=throw();
                printf("\n%2d", score2);
            end
            if(score2 == score1) then
                printf("     You WIN by matching your first score\n")
            else
                printf("      You LOSE by failing to match your first score\n");
            end
endfunction