The YhatzeeDotNet AJAX.NET Game Live Demo Site
Here is a link to a live demo of the YhatzeeDotNet AJAX.NET Game. Click the link to check out the game and let me know what you think. ;)
Introduction
YhatzeeDotNet is a game developed using ASP.NET, VB.NET and AJAX.NET. What is cool about this game is that the page only loads once. After that, everything happens asynchronously!
This was my first time using AJAX.NET. I created this game because I wanted to "AJAX out" another project that I had been working on for some time, but I wanted to make sure I had it down before I went messing with code that I had so much time invested in.
Using the Code
The partial page loads are done with UpdatePanel
s. This is the status label that lets the player know what's going on while playing the game. As you can see, the update panel is nested within in an ASP table cell. The label is nested within the content template and the partial update can be triggered by all controls on the page that post back.
<asp:Table ID="tblStatus" runat="Server"
width="65%" HorizontalAlign="Center">
<asp:TableRow>
<asp:TableCell HorizontalAlign="Center" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblStatus" runat="server" ForeColor="Red">
</asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="btnRoll" EventName="Click" />
</Triggers>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="ckbOnes" EventName="CheckedChanged" />
</Triggers>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="ckbTwos" EventName="CheckedChanged" />
</Triggers>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="ckbThrees" EventName="CheckedChanged" />
</Triggers>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="ckbFours" EventName="CheckedChanged" />
</Triggers>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="ckbFives" EventName="CheckedChanged" />
</Triggers>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="ckbSixes" EventName="CheckedChanged" />
</Triggers>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="ckbChance" EventName="CheckedChanged" />
</Triggers>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="ckb3Kind" EventName="CheckedChanged" />
</Triggers>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="ckb4Kind" EventName="CheckedChanged" />
</Triggers>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="ckbFullHouse"
EventName="CheckedChanged" />
</Triggers>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="ckbSmallStright"
EventName="CheckedChanged" />
</Triggers>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="ckbLargeStright"
EventName="CheckedChanged" />
</Triggers>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="ckbYhatzee" EventName="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
This is the subroutine used to roll the dice. I am using session variables to hold the values of the dice.
Protected Sub btnRoll_Click(ByVal sender As Object,
ByVal e As System.EventArgs)
If CInt(Session("turn")) < 3 Or Session("turn") Is Nothing Then
Session("turn") = CInt(Session("turn")) + 1
Dim dValue(5) As Integer
dValue(1) = GetRandomNumber(1, 6)
dValue(2) = GetRandomNumber(1, 6)
dValue(3) = GetRandomNumber(1, 6)
dValue(4) = GetRandomNumber(1, 6)
dValue(5) = GetRandomNumber(1, 6)
Dim i As Integer = 0
For i = 1 To 5
Dim ckbHold As Control = Nothing
If i = 1 Then
ckbHold = CType(FindControl("hold1"), CheckBox)
End If
If i = 2 Then
ckbHold = CType(FindControl("hold2"), CheckBox)
End If
If i = 3 Then
ckbHold = CType(FindControl("hold3"), CheckBox)
End If
If i = 4 Then
ckbHold = CType(FindControl("hold4"), CheckBox)
End If
If i = 5 Then
ckbHold = CType(FindControl("hold5"), CheckBox)
End If
Dim dImg As String = Nothing
If i = 1 Then
dImg = "d1Img"
End If
If i = 2 Then
dImg = "d2Img"
End If
If i = 3 Then
dImg = "d3Img"
End If
If i = 4 Then
dImg = "d4Img"
End If
If i = 5 Then
dImg = "d5Img"
End If
Dim dVal As String = Nothing
If i = 1 Then
dVal = "dVal1"
End If
If i = 2 Then
dVal = "dVal2"
End If
If i = 3 Then
dVal = "dVal3"
End If
If i = 4 Then
dVal = "dVal4"
End If
If i = 5 Then
dVal = "dVal5"
End If
If CType(ckbHold, CheckBox).Checked = False Then
If dValue(i) = 1 Then
Session(dImg) = "Dice/d1.gif"
Session(dVal) = 1
End If
If dValue(i) = 2 Then
Session(dImg) = "Dice/d2.gif"
Session(dVal) = 2
End If
If dValue(i) = 3 Then
Session(dImg) = "Dice/d3.gif"
Session(dVal) = 3
End If
If dValue(i) = 4 Then
Session(dImg) = "Dice/d4.gif"
Session(dVal) = 4
End If
If dValue(i) = 5 Then
Session(dImg) = "Dice/d5.gif"
Session(dVal) = 5
End If
If dValue(i) = 6 Then
Session(dImg) = "Dice/d6.gif"
Session(dVal) = 6
End If
End If
Next
d1.ImageUrl = Session("d1Img")
d2.ImageUrl = Session("d2Img")
d3.ImageUrl = Session("d3Img")
d4.ImageUrl = Session("d4Img")
d5.ImageUrl = Session("d5Img")
If Session("turn") = "1" Then
lblStatus.Text = "ROLL 1"
End If
If Session("turn") = "2" Then
lblStatus.Text = "ROLL 2"
End If
If Session("turn") = "3" Then
lblStatus.Text = "ROLL 3"
End If
Else
lblStatus.Text = "You must place you score."
d1.ImageUrl = Session("d1Img")
d2.ImageUrl = Session("d2Img")
d3.ImageUrl = Session("d3Img")
d4.ImageUrl = Session("d4Img")
d5.ImageUrl = Session("d5Img")
End If
End Sub
This code is used to get the score for a large straight. The loop checks the roll of five dice against all possible outcomes in all possible orders for a large straight.
Dim dVals() As Integer = GetDVals()
Dim LargeStright As Integer = 0
Dim dVals() As Integer = GetDVals()
Dim v, w, x, y, z As Integer
For v = 0 To 5
For w = 0 To 5
For x = 0 To 5
For y = 0 To 5
For z = 0 To 5
If dVals(v) = 1 And _
dVals(w) = 2 And _
dVals(x) = 3 And _
dVals(y) = 4 And _
dVals(z) = 5 Or _
dVals(v) = 2 And _
dVals(w) = 3 And _
dVals(x) = 4 And _
dVals(y) = 5 And _
dVals(z) = 6 Then
LargeStright = 100
End If
Next
Next
Next
Next
Next
Note to the AJAX.NET Pros
As previously stated, I decided to take this project on in order to learn more about AJAX.NET before I tried to implement this technology in my other projects. Your feedback is not only welcome, but greatly appreciated. There are a couple of areas in particular that I would like to bring attention to:
- A good percentage of my ASP.NET code is all
Trigger
s nested in UpdatePanel
s. This was very repetitive and annoying. If there is a better way, I would love to know about it.
- I am using 37
UpdatePanel
s in one page. I'd love to use less, but I get errors if I try to nest the UpdatePanel
s outside of the ASP table cells.
History
- 11 August, 2007 -- Original version posted
- 15 August, 2007 -- Update: added link to a live demo of the project and more code
- 17 August, 2007 -- Update: fixed a few bugs in the code and updated the demo site with the new code