Extrn GetDec:Far, PutDec:Far, PutDec:Far, Putstrng:Far, NewLine:Far ; #include "iostream.h" Data Segment ;######Game variables########### DAY dw 1 GlassesSold dw ? PriceperGlass dw 5 Income dw ? Expenses dw ? GlassesMade dw 4 AdvertisingMade dw 20 AdvertisingCost dw 15 Profit dw ? Assets dw ? CurrentAssets dw 200 ;starting amount DaystoPlay dw ? weathercond dw ? costperglass dw 5 ;######inputforday function prompts###### OnDay db "On day " ; length 7 LemonadeCost db ", The Cost of Lemonade is " ; length 26 ;######Random number function vars###### RAND_k dw 369 RAND_seed dw ? ;######prompts######### intro db "Welcome to lemonade stand" ;basic intro prompt 25 weather db 'Todays weather is' ;weather report 17 dailystatement db 'Lemonade stand Daily Statement:' ;daily statement 31 date db 'On day: ' ;day output sales db 'Glasses sold: ' ; glasses sold 14 priceprompt db 'Price per glass: ' ; price per glass 17 production db 'Glasses made: ' ; glasses made 14 advertsprompt db 'Signs made: ' ; signs made 12 profitprompt db 'Profit: ' ;profit prompt 8 AssetsPrompt db 'Assets: ' ;asset prompt 8 glassesprompt db 'How many glasses of lemonade do you wish to make? ';length 50 advertisingprompt db 'How many advertising signs do you wish to make (15 cents each)? ' ;64 lemonadepriceprompt db 'What price (in cents) do you wish to charge for lemonade? '; 56 daysprompt db 'Please enter the number of days to run the stand (1-30): ' ; 57 numberoutofrange db "Number out of range, try again" ; 30 cannotafford db 'You cannot afford this many ' ; 28 gameover db 'Game Over - you are out of money' ;32 ;########Weather report######## weather0 db 'Todays weather is hot and humid' ;32 weather1 db 'Todays weather is sunny' ;24 weather2 db 'Todays weather is fair' ;23 weather3 db 'Todays weather is cloudy and cool' ;34 weather4 db 'Todays weather is raining' ;26 ;#######Glasses Sold########### maxprice dw ? ;max price of lemondade for a given day maxglassessold dw ? ;max number of glasses sold in one day Glassmul dw ? Data Ends Code Segment Assume Cs:Code, Ds:Data, Ss:Sseg ;#####Convert to Dollar####### ;takes 1 value converts it front cents ;to dollars ;Returns Whole # in Ax ;Cents # in DX @CentstoDollars Proc Near C, @CASH:WORD push Bx xor dx,dx mov Ax, @Cash cwd mov Bx, 100 iDiv Bx pop Bx @ctdif: cmp Dx, 0 jl @ctdthen jmp @ctdendif @ctdthen: neg Dx jmp @ctdendif @ctdendif: ret @CentstoDollars Endp ;#####random number generator####### ;gets a large number, a seed, and an upper limit ;seed is wiped out initially ;system time is used for seed ;answer is returned in Ax @RANDOMNUMBER Proc Near C, @high:word push Bx push Cx push Dx xor Ax, Ax ;clear AX mov ah, 2Ch ;load 2Ch into ah int 21h ;load system time into registers mov RAND_seed, Dx ;dh holds seconds, dl holds ms, move to seed mov bx, @high add bx, 1 mov ax, RAND_seed xor dx,dx cwd div bx mov ax, dx pop Dx pop cx pop Bx ret @RANDOMNUMBER ENDp ;####ClearScreen###### ;clears the screen with a loop ;puts @line # of new lines on the screen @cLEARSCREEN Proc Near C, @lines:word push Ax push Bx push Cx push Dx mov Bx, @lines @CSfor: cmp bx, 0 ja @CSdo jmp @CSendfor @CSdo: call newline dec Bx jmp @CSfor @CSendfor: pop Dx pop Cx pop Bx pop Ax ret @CLEARSCREEN ENDp ;######outputs cents formated nicely##### ;outputs -3453 or 300 as $34.53 or $3.00 ;input is money in cents @outputdollars Proc Near C, @money:word push ax push bx push cx push dx invoke @CentstoDollars, @money ;break money apart into ;dollars and cents push Ax push dx xor ax, ax xor dx, dx mov ah, 02h mov Dl, 36 int 21h pop Dx pop Ax @optif2: cmp @money, 0 jl @optand2 jmp @optendif2 @optand2: cmp @money, -100 jg @optthen2 jmp @optendif2 @optthen2: push Ax push dx xor ax, ax xor dx, dx mov ah, 02h mov Dl, 45 int 21h pop Dx pop Ax jmp @optendif2 @optendif2: call putdec push Ax push dx xor ax, ax xor dx, dx mov ah, 02h mov Dl, 46 int 21h pop Dx pop Ax mov Ax, Dx @optif: cmp Ax, 10 Jb @optthen jmp @optendif @optthen: push Ax mov Ax, 0 call putdec pop Ax jmp @optendif @optendif: call putdec pop dx pop cx pop bx pop ax ret @outputdollars ENDp ;#########INPUT FOR DAY########### ;gets input for the day ;provides error handling for ;entering data also ;################################# @InputForDay Proc Near C push ax push bx push cx push dx Mov Cx, 7 Lea Di, onday Call Putstrng mov Ax, DAY call putdec Mov Cx, 26 Lea Di, lemonadecost Call Putstrng invoke @outputdollars, costPerglass call newline Mov Cx, 7 Lea Di, assetsprompt Call Putstrng invoke @outputdollars, currentassets @if1: call newline Mov Cx, 50 Lea Di, glassesprompt Call Putstrng call getdec mov GlassesMade, Ax call newline mul costperglass mov expenses, Ax cmp Ax, currentassets jg @then1 jmp @endif1 @then1: Mov Cx, 28 Lea Di, cannotafford Call Putstrng invoke @outputdollars, Ax jmp @if1 @endif1: @if2: call newline Mov Cx, 64 Lea Di, advertisingprompt Call Putstrng call getdec mov advertisingmade, Ax call newline mul advertisingcost add Ax, Expenses cmp Ax, currentassets jg @then2 jmp @endif2 @then2: Mov Cx, 28 Lea Di, cannotafford Call Putstrng invoke @outputdollars, Ax jmp @if2 @endif2: Mov Cx, 58 Lea Di, lemonadepriceprompt Call Putstrng call getdec mov priceperglass, Ax pop dx pop cx pop bx pop ax ret @InputForDay endp ;#######Asset Statement############ @AssetStatement Proc Near C push cx push ax invoke @clearscreen,2 mov ax,PricePerGlass mul GlassesSold mov Income,ax mov ax,AdvertisingCost mul AdvertisingMade mov Expenses,ax mov ax,CostPerGlass mul GlassesMade add Expenses,ax mov ax,Income sub ax,Expenses mov Profit,ax add ax,CurrentAssets mov Assets,ax mov Currentassets, Ax invoke @clearscreen,25 mov cx,31 lea di,dailystatement call putstrng call newline mov cx,8 lea di,date call putstrng mov ax,day call putdec call newline mov cx,12 lea di,advertsprompt call putstrng mov ax,AdvertisingMade call putdec call newline mov cx,14 lea di,production call putstrng mov ax,GlassesMade call putdec call newline mov cx,17 lea di,priceprompt call putstrng invoke @outputdollars,PriceperGlass call newline mov cx,14 lea di,sales call putstrng mov ax,GlassesSold call putdec call newline mov cx,8 lea di,profitprompt call putstrng invoke @outputdollars,Profit call newline lea di,assetsprompt call putstrng invoke @outputdollars,assets call newline inc day pop cx pop ax ret @AssetStatement ENDp @PromptforDays proc near C push cx push ax @pfdfor: mov cx,57 lea di,daysprompt call putstrng call getdec cmp ax,30 jg @pfdtryagain cmp ax,0 jle @pfdtryagain jmp @pfdendfor @pfdtryagain: mov cx,30 lea di,numberoutofrange call putstrng call newline jmp @pfdfor @pfdendfor: mov DaystoPlay,ax pop cx pop ax ret @promptforDays endp ;########Generate weather report######## ;Generates the weather report for the day @GenerateWeather Proc near C push ax push bx push cx push dx invoke @randomnumber, 4 @gswitch: cmp ax, 0 je @gcase0 cmp ax, 1 je @gcase1 cmp ax, 2 je @gcase2 cmp ax, 3 je @gcase3 cmp ax, 4 je @gcase4 jmp @gdefault @gcase0: Mov Cx, 31 Lea Di, weather0 Call Putstrng Call Newline mov weathercond, 0 jmp @gendswitch @gcase1: Mov Cx, 23 Lea Di, weather1 Call Putstrng Call Newline mov weathercond, 1 jmp @gendswitch @gcase2: Mov Cx, 22 Lea Di, weather2 Call Putstrng Call Newline mov weathercond, 2 jmp @gendswitch @gcase3: Mov Cx, 33 Lea Di, weather3 Call Putstrng Call Newline mov weathercond, 3 jmp @gendswitch @gcase4: Mov Cx, 25 Lea Di, weather4 Call Putstrng Call Newline mov weathercond, 4 jmp @gendswitch @gdefault: @gendswitch: pop dx pop cx pop bx pop ax ret @generateweather endp ;#########Glasses Sold######## ;Figures the #of glasses sold ;Based on the weather, advertising ;and price charged. @glassessold Proc Near push ax push bx push cx push dx invoke @randomnumber, 20 ;generate the max selling price mov maxprice, ax ;move into max selling variable mov Ax, weathercond ;Adjust max price to reflect the weather ;switch changes max price to a percentage multiplier @mxswitch: cmp ax, 0 je @mxcase0 cmp ax, 1 je @mxcase1 cmp ax, 2 je @mxcase2 cmp ax, 3 je @mxcase3 cmp ax, 4 je @mxcase4 jmp @mxdefault @mxcase0: mov weathercond, 40 jmp @mxendswitch @mxcase1: mov weathercond, 20 jmp @mxendswitch @mxcase2: mov weathercond, 4 jmp @mxendswitch @mxcase3: mov weathercond, 2 jmp @mxendswitch @mxcase4: mov weathercond, 1 jmp @mxendswitch @mxdefault: @mxendswitch: mov Ax, maxprice mov Bx, weathercond ;conditions xor dx,dx ;Divide by weather condition add result to cwd ;max price idiv bx ;effect is a higher max price based on higher add maxprice, Ax ;weather condition invoke @randomnumber,10 ;generate random number between 0-10 add Ax, 5 ;shifts the range to 5-15 mov maxglassessold, Ax mov Bx, weathercond xor dx,dx ;Divide by weather condition add result to cwd ;maxglasses sold idiv bx ;effect is a higher max glasses sold based on higher add maxglassessold, Ax ;weather condition mov ax, advertisingmade @adswitch: cmp ax, 5 jl @adcase0 cmp ax, 10 jl @adcase1 cmp ax, 15 jl @adcase2 cmp ax, 21 jl @adcase3 jmp @addefault @adcase0: mov glassmul, 15 jmp @adendswitch @adcase1: mov glassmul, 8 jmp @adendswitch @adcase2: mov glassmul, 3 jmp @adendswitch @adcase3: mov glassmul, 1 jmp @adendswitch @addefault: @adendswitch: mov Ax, maxglassessold ;further increase the max # of glasses sold by ;figure the multipier for Glasses based on advertising mov Bx, glassmul ;using the advertising multiplier xor dx, dx cwd idiv bx ;divide by the number add maxglassessold, Ax ;add it to maxglasses sold invoke @randomnumber, 10 ; randomly add/subtract from glasses @adif: cmp Ax, 5 jl @adthen jmp @adelse @adthen: invoke @randomnumber, 5 ;add a random component from max glasses sold add maxglassessold, ax @adelse: invoke @randomnumber, 5 ;subtract a random component from max glasses sold sub maxglassessold, ax @adendif: ;####### ;At this point your ideal max glasses sold and ideal maxprice is figured ;so if your price/glass = maxprice you're going to sell all your glasses up to ;maxglassessold ;####### mov Cx, Priceperglass @glif: cmp Cx, maxprice ;compare the price/glass to max price jg @glthen ;if it's greater then glasses sold jmp @glendif ;will need to be reduced proportionally @glthen: mov Ax, Priceperglass mov cx, maxprice xor dx, dx cwd div Cx ;divide priceperglass by max price mov bx, Ax mov Ax, maxglassessold xor dx, dx cwd div bx ;divide by divider mov maxglassessold, Ax ;replace the reduced maxglassessold with ;the divided value @glendif: ;####END IF mov bx, maxglassessold mov glassessold, bx ;populate glassessold with final number mov cx, glassesmade ;load how many were made into Cx @gsif: cmp cx, glassessold ;if glasses made > glasses sold then jl @gsthen jmp @gsendif @gsthen: mov glassessold, cx ;glasses sold = glasses made jmp @gsendif @gsendif: ;#### ;glassessold is ultimately the output of this function ;it is a global variable therefore the function is a void ;### pop dx pop cx pop bx pop ax ret @glassessold endp ;###########MAIN############# main Proc Far MOV AX,SEG DATA MOV DS,AX MOV ES,AX invoke @clearscreen, 25 Mov Cx, 25 Lea Di, intro Call Putstrng Call Newline invoke @promptfordays @for: cmp DaystoPlay,0 jg @do jmp @endfor @do: invoke @generateweather call newline invoke @inputforday call newline invoke @glassessold call newline invoke @assetstatement call newline mov Ax, costperglass @if: cmp currentassets, ax jl @then jmp @endif @then: Mov Cx, 32 Lea Di, gameover Call Putstrng Call Newline jmp @endfor @endif: dec daystoplay jmp @for @endfor: Mov Ah, 4Ch Int 21h main Endp Code Ends SSeg Segment STACK DB 254 dup(?) SSeg Ends End main