Author |
Topic |
kselvia
Aged Yak Warrior
526 Posts |
Posted - 2004-08-10 : 03:12:36
|
[code]Create Procedure n (@pile int = null, @count int = null)/*A game I learned in 5th grade. Written by kselvia posted @ sqlteam.comForgive the non-SQLness of it. I pretty much translated it from a C versionI wrote a long time ago. August 10th, 2004. Run the procedure n for instructions.*/ASBEGINset nocount onDeclare @pile1 int, @pile2 int, @pile3 int, @table varchar(2000), @my_turn int, @i int, @j int, @x int, @y int, @z int, @idx int, @m_count int, @m_pile int, @m_say char, @msg int, @c_pile char, @SaveGame varbinary(2), @c_count charSELECT @SaveGame = context_info FROM master.dbo.sysprocessesWHERE spid = @@SPIDSelect @table = '00013L13W23W33W43W53W63W12L12W23W33W43W53W63W73W12W22WRRG13B23B33B'+'43B53B22W32W12BRRG13B23B33B43B32W42W22B12BRRG13B23B33B42W52W32B22B'+'12BRRG13B23B11L11W23W33W43W53W63W73W11WRRG13B23B33B43B53B63B22W12B'+'11BRRG13BRRG33B43B32W22BRRG11B23B33BRRG53B42W32B12B22B11BRRG13B23B'+'52W42B22B32BRRG11B23B33B11W21WRRG13B23B33B43B53B21W11B12BRRG13B23B'+'33B43BRRG13B22B33B43B53B63B73B12BRRG11B21B33B43B53B63B22B12B42B32B'+'21B11BRRG13B32B22B52B42B11B21B12BRRG21W31W11BRRG13B23B33B43B31W21B'+'RRG12B23B33B43B53B11BRRG31B23B33B43B53B63BRRG13B23B32B43B53B63B73B'+'RRG12B23B33B43B53B11BRRG22B32B42B52B21B31BRRG13B23B32B43B53B63B73B'+'12B22B32B42B31B21B11BRRG22B32B42B52B21B31BRRGRRB'If @pile Is Null and @Count Is NullBegin Print ' A man is sitting in a comfortable chair, seated at a weather-beaten table. He spends his final years relaxing and challenging bypassers to games of wit and skill. He is well dressed and adorned with expensive rings and jewelery. An old and tarnished medallion hangs from a chain around his neck. Perhaps another of the many items his challengers have wagered and lost. On the table in front of him are three piles of stones. He challenges you to take turns removing as many stones as each player likes from any single pile. The player forced to remove the last stone from the table LOSES the game. Enter "n <pile>,<number>" to remove <number> stones from pile <pile>. Enter "n 0" to have the game master play first. Enter "n" by itself to restart the game. ' Select @Pile1 = 3, @Pile2 = 5, @Pile3 = 7 Goto show_tableEndSelect @Count = Coalesce(@count,0)select @Pile1 = Left(cast ( cast ( @SaveGame as int) as char(3)),1) - 1, @Pile2 = Substring(cast ( cast ( @SaveGame as int) as char(3)),2,1) - 1, @Pile3 = Right(cast ( cast ( @SaveGame as int) as char(3)),1) - 1if (@pile = 0) if (@pile1 = 3) AND (@pile2 = 5) AND (@pile3 = 7 ) goto make_move else begin Print ' Game master says: The game has started already. It''s not my turn to play.' Goto show_table end if ((@count < 1) OR (@pile = 1 AND @count > @pile1) OR (@pile = 2 AND @count > @pile2) OR (@pile = 3 AND @count > @pile3) OR (@pile < 1) OR (@pile > 3)) begin Print ' You can''t take ' + Cast( @count as Varchar ) + ' stones from pile ' + Cast (@pile as varchar) + '!' Goto show_table end if (@pile = 1) Set @pile1 = @pile1 - @count if (@pile = 2) Set @pile2 = @pile2 - @count if (@pile = 3) Set @pile3 = @pile3 - @countmake_move: Select @i = 0, @j = 0, @x = 0 While @x < 4 Begin Set @y = 0 While @y < 6 Begin Set @z = 0 While @z < 8 Begin If (@pile1 = @x) And (@pile2 = @y) AND (@pile3 = @z) begin Set @j = @i * 3 end Set @i = @i + 1 Set @z = @z + 1 End Set @y = @y + 1 End Set @x = @x + 1 End Set @j = @j + 1 Select @c_count = substring(@table,@j,1), @c_pile = substring(@table,@j+1,1), @m_say = substring(@table,@j+2,1) If @c_count = 'R' Select @m_count = cast(((3) * rand() + 1) as int) Else Select @m_count = cast(@c_count as int) if (@c_pile = 'R') begin Select @m_pile = cast(((3) * rand() + 1) as int) if ((@m_pile = 1) AND (@pile1 = 0)) Set @m_pile=2 if ((@m_pile = 2) AND (@pile2 = 0)) Set @m_pile=3 if ((@m_pile = 3) AND (@pile3 = 0)) Set @m_pile=1 Set @m_count = 1 end Else Select @m_pile = Cast (@c_pile as int) if (@m_pile = 1) Set @pile1 = @pile1 - @m_count if (@m_pile = 2) Set @pile2 = @pile2 - @m_count if (@m_pile = 3) Set @pile3 = @pile3 - @m_count Select @msg = cast(((3) * rand() + 1) as int) if (@m_say = 'W') Begin Print ' Game master takes ' + Cast (@m_count as Varchar) + ' stones from pile ' + Cast(@m_pile as Varchar) if (@msg = 1) Print ' Game master says: Hehehe, you loose bozo.' if (@msg = 2) Print ' Game master says: You lose! Next!.' if (@msg = 3) Print ' Game master says: Another victory!' goto show_table End if (@m_say = 'L') Begin Print ' Game master takes ' + Cast (@m_count as Varchar) + ' stones from pile ' + Cast(@m_pile as Varchar) if (@msg = 1) Print ' Game master says: What! How could I have lost?' if (@msg = 2) Print ' Game master says: You are a master of logic!' if (@msg = 3) Print ' Game master says: I have been defeated?@#^!$' goto show_table End if (@m_say = 'B') Begin if (@msg = 1) Print ' Game master says: You are no match for me!' if (@msg = 2) Print ' Game master says: What a lame move! Hehehe.' Print ' Game master takes ' + Cast (@m_count as Varchar) + ' stones from pile ' + Cast(@m_pile as Varchar) goto show_table End if (@m_say = 'G') Begin if (@msg = 1) Print ' Game master''s brow furrows as he looks at the table.' if (@msg = 2) Print ' Game master nods his head in admiration.' Print ' Game master takes ' + Cast (@m_count as Varchar) + ' stones from pile ' + Cast(@m_pile as Varchar) goto show_table Endshow_table: Print ' Stones are piled on the table in three piles. Pile 1 contains ' + Replicate('0 ',@pile1) + ' stones. Pile 2 contains ' + Replicate('0 ',@pile2) + ' stones Pile 3 contains ' + Replicate('0 ',@pile3) + ' stones' If @m_say = 'W' or @m_say = 'L' Begin Select @pile1 = 3, @pile2 = 5, @pile3 = 7 Print ' A new game begins. Stones are piled on the table in three piles. Pile 1 contains ' + Replicate('0 ',@pile1) + ' stones. Pile 2 contains ' + Replicate('0 ',@pile2) + ' stones Pile 3 contains ' + Replicate('0 ',@pile3) + ' stones' End Select @SaveGame = cast ( (@Pile1 + 1) * 100 + (@Pile2 + 1) * 10 + (@Pile3 + 1) as varbinary(2)) SET CONTEXT_INFO @SaveGame END[/code]I was going to add a counter so it would record how many games you played before you won. Unless you have played the game before I doubt anyone will win in less than 20 games. Edit: Fixed a couple of bugs. And another.--Ken"Knowledge is a process of piling up facts; wisdom lies in their simplification." |
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2004-08-10 : 07:21:15
|
Hi Ken,Nice game.I found the fool proof winning method though....You move first and take all 7 stones from Pile 3, The game Master will always then take all 3 Stones from Pile 1, all you do then is take 4 Stones from Pile 2 leaving the game master with 1 Stone.Duane. |
 |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-08-10 : 08:33:07
|
Ken - thats pretty cool... i enjoyed it! Corey |
 |
|
Wanderer
Master Smack Fu Yak Hacker
1168 Posts |
Posted - 2004-08-10 : 09:11:28
|
Hmm - quite cool, but I think there's a bug.Enter:n 3,5should leave 2 stones in pile 3, but the game master consistently takes 3 stones from pile 3, and does not re-draw the "board"*##* *##* *##* *##* Chaos, Disorder and Panic ... my work is done here! |
 |
|
kselvia
Aged Yak Warrior
526 Posts |
Posted - 2004-08-10 : 11:16:38
|
I'll take a look at the bugs. Game master should not give up the game like that!--Ken"Knowledge is a process of piling up facts; wisdom lies in their simplification." |
 |
|
kselvia
Aged Yak Warrior
526 Posts |
Posted - 2004-08-10 : 11:44:22
|
Thanks Wanderer and Duane. I fixed the bugs you found. I hope there are not too many more. I remember fixing these when it was a C program. I guess I dug up a pre-bug-fix version to translate to T-SQL.I'm waiting for Corey to write it in one statement. Come on - you know you want to :)--Ken"Knowledge is a process of piling up facts; wisdom lies in their simplification." |
 |
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2004-08-10 : 18:50:40
|
Another bug (but I still haven't won, damn!!!): Moving any more than 1 stone from pile 3 as first move makes the game a little fuzzy.--Lumbago"Real programmers don't document, if it was hard to write it should be hard to understand" |
 |
|
kselvia
Aged Yak Warrior
526 Posts |
Posted - 2004-08-10 : 19:03:59
|
Thanks Lumbago. I fixed those 2 bugs. Please let me know if you find any others. I should have tested better :-)--Ken"Knowledge is a process of piling up facts; wisdom lies in their simplification." |
 |
|
chadmat
The Chadinator
1974 Posts |
Posted - 2004-08-12 : 15:28:21
|
quote: I was going to add a counter so it would record how many games you played before you won. Unless you have played the game before I doubt anyone will win in less than 20 games
It only took me about 3 or four times to beat him. He doesn't talk as much trash when you are whippin that A@@-Chadhttp://www.clrsoft.comSoftware built for the Common Language Runtime. |
 |
|
kselvia
Aged Yak Warrior
526 Posts |
Posted - 2004-08-12 : 17:29:54
|
That's pretty impressive. It probably helps that he lets you know you are on the right or wrong track. It's a fun game to play with match sticks over a couple of beers (or margaritias!) because you don't have to give your opponent any hint as to how he is doing. It's a lot harder (I think!) to get a finger on a good strategy like that. In perfect play, the game is deterministic and the first player will always win. If the game master looses when you have him play first that would be another bug :)--Ken"Knowledge is a process of piling up facts; wisdom lies in their simplification." |
 |
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2004-08-13 : 00:10:36
|
Hey Ken - I like it.Now, how about a WYSIWYG word processor in T-SQL ?--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
Pace
Constraint Violating Yak Guru
264 Posts |
Posted - 2004-08-20 : 05:13:33
|
Call me proper thick, but hey I am new. How do I play the game? |
 |
|
chadmat
The Chadinator
1974 Posts |
Posted - 2004-08-23 : 12:46:01
|
Just copy and past the SP into QA. Then run n, and it gives you instructions.-Chadhttp://www.clrsoft.comSoftware built for the Common Language Runtime. |
 |
|
|