Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!tut.cis.ohio-state.edu!ucbvax!mis.mcw.edu!TENAGLIA From: TENAGLIA@mis.mcw.edu (Chris Tenaglia - 257-8765) Newsgroups: comp.lang.icon Subject: Draw Poker Game Message-ID: <67EEF267C0600950@mis.mcw.edu> Date: 15 Dec 90 12:36:00 GMT Sender: daemon@ucbvax.BERKELEY.EDU Distribution: inet Organization: The Internet Lines: 176 Since the submission of the solitaire game, I felt it might be nice if I could contribute one. Here's a draw poker game. I use ICON on VAX/VMS 5.2 with VT terminals, so I do everything with ANSI escape sequences. I also realize that I could have done some of the table manipulations more efficiently, but I thought in this case, tab["key"] := something, helped the readability of the code. Invoke it with ICONX DRAW [credits]. The parameter is how much virtual money you want to start with. 3 is default. Have fun! Chris Tenaglia (System Manager) | Medical College of Wisconsin 8701 W. Watertown Plank Rd. | Milwaukee, WI 53226 (414)257-8765 | tenaglia@mis.mcw.edu, mcwmis!tenaglia ######################################################### # # # DRAW.ICN 12/15/90 BY TENAGLIA # # # # SIMPLE BUT FUN DRAW POKER GAME. WORKS ON ANSI SCREEN. # # USAGE : ICONX DRAW [starting credits] # # # ######################################################### global money, message procedure main(param) money := integer(param[1]) | 3 write("\e[2J\e[2H \e#3\e[1;7mDRAW : Nothing Wild\e[m") write(" \e#4\e[1;7mDRAW : Nothing Wild\e[m") repeat { deck := shuffle() message := "" hand := [] every 1 to 5 do put(hand,pop(deck)) display(hand,1,4) repeat { keep := obtain("\e[12;1fKeep which ones (1 2 3 4 5):\e[J") if map(input("\e[12;50fAre you sure y/n :\e[K"))[1] == "y" then break } all := set() ; every i := 1 to 5 do insert(all,i) every delete(all,integer(!keep)) every hand[!all] := pop(deck) money +:= evaluate(hand) display(hand,1,15) if map(input("\e[23;1fAnother game? y/n :\e[K"))[1] ~== "y" then break } write("\e[2J\e[H") end # # THIS SECTION OBTAINS THE VALIDATES INPUT # procedure obtain(prompt) bad := 0 repeat { nums := parse(input(prompt),' \t,') every num := !nums do { integer(num) | { input(num || " not a number. Press ") ; bad := 1 ; next } (integer(num) > 0) | { input(num || " must be between 1 and 5. Press ") ; bad := 1 ; next } (integer(num) < 6) | { input(num || " must be between 1 and 5. Press ") ; bad := 1 ; next } } (bad = 1) | return nums } end # # THIS ROUTINE DISPLAYS THE CURRENT HAND AT THE GIVEN X,Y COORDINATES # procedure display(cards,x,y) all := [] every card := !cards do { j := y if find(card[2],"CS") then card := "\e[1;7m" || card || "\e[m" shape := ["\e["||(j+:=1)||";"||x||"f\e(0lqqqqqqqk\e(B"] put(shape,"\e["||(j+:=1)||";"||x||"f\e(0x " || card || " x\e(B") put(shape,"\e["||(j+:=1)||";"||x||"f\e(0x x\e(B") put(shape,"\e["||(j+:=1)||";"||x||"f\e(0x x\e(B") put(shape,"\e["||(j+:=1)||";"||x||"f\e(0x x\e(B") put(shape,"\e["||(j+:=1)||";"||x||"f\e(0x " || card || " x\e(B") put(shape,"\e["||(j+:=1)||";"||x||"f\e(0mqqqqqqqj\e(B") put(all,shape) x +:= 14 } while shape := pop(all) do every writes(!shape) writes(message,"\e[",y+2,";",x-3,"fCredits\e[K") writes("\e[",y+3,";",x-5,"f \e[1m",right(money,7),"\e[m\e[K") end # # THIS ROUTINE SHUFFLES THE CARD DECK # procedure shuffle() static faces, suits local cards initial { &random := map(&clock,":","7") # initial on multiple shuffles faces := ["2","3","4","5","6","7","8","9","T","J","Q","K","A"] suits := ["D","H","C","S"] } cards := [] every put(cards,!faces || !suits) swaps := 52 + ?12 every 1 to swaps do cards[?52] :=: cards[?52] return cards end # # THIS SECTION EVALUATES THE FINAL HAND, TALLIES WINNINGS OR LOOSINGS # procedure evaluate(cards) static hash1,hash2 initial { hash1 := "23456789TJQKA" hash2 := "CSHD" } temp := table(0) ; suit := table(0) ; result := table(0) four := 0 ; three := 0 ; twopair := 0 ; job := 0 ; points := -1 flush := 0 ; full := 0 ; royal := 0 ; straight:= 0 ; pair:= 0 every card := !cards do temp[card[1]] +:= 1 every tmp := key(temp) do case temp[tmp] of { 4 : { result["four"] := 1 ; result["job"] := 1 } 3 : { result["three"] := 1 ; result["job"] := 1 } 2 : { result["pair"] +:= 1 ; result["job"] := if find(tmp,hash1) > 9 then 1 } } if result["pair"] = 2 then { result["twopair"] := 1 ; result["job"] := 1 } every card := !cards do suit[card[2]] +:= 1 every tmp := key(suit) do if suit[tmp] = 5 then { result["flush"] := 1 ; result["job"] := 1 } tmp := sort(cards) ; test := "" every card := !tmp do test ||:= card[1] if (result["three"] = 1) & (result["pair"] = 1) then { result["full"] := 1 ; result["job"] := 1 } if find(test,hash1) then { result["straight"] := 1 result["job"] := 1 if test[1] == "T" then result["royal"] := 1 } if result["job"] = 1 then points +:= 1 if result["twopair"] = 1 then points +:= 1 if result["three"] = 1 then points +:= 5 if result["full"] = 1 then points +:= 10 if result["flush"] = 1 then points +:= 10 if result["straight"]= 1 then points +:= 10 if result["four"] = 1 then points +:= 100 if result["royal"] = 1 then points +:= 500 message := "\e[14;1fHand Evaluation -> " every thing := key(result) do message ||:= thing || ", " return points end # # THIS ROUTINE PARSES A STRING WITH RESPECT TO SOME DELIMITER # procedure parse(line,delims) static chars chars := &cset -- delims tokens := [] line ? while tab(upto(chars)) do put(tokens,tab(many(chars))) return tokens end # # THIS ROUTINE PROMPTS FOR INPUT AND RETURNS A STRING # procedure input(prompt) writes(prompt) return read() end