My First OOP - A Solitaire Game
时间:2009-7-30 15:58 作者:fshell 分类: 无
I learned about the concept of the Object-oriented programming(OOP) when I was attending college some years ago. The professor introduced the concepts of common objects such as a stack, a queue and dequeue frequently used in computer programming. He also talked about advantage of programming in objects in general. I still remember vividly the example he used in his lecture to illustrate the concepts, a solitaire game: The deck of cards can be thought of a stack. The up-pile can be thought of a stack. The down-pile can be thought of a queue/dequeue. A poker card can be thought of an object that has the following properties:
Value(Ace,Two,Three,Four,Six,Seven,Eight,Nine,Ten,Jack,Queen,King) Color (Red,Black) Suite (Heart,Diamond,Club,Spade) Face (Front,Back)
- A poker card in the solitaire game can be checked if it has a move to other piles according to the game's rule. These checking correspond to its methods. * To check if the card can move to a up-pile, we use the function CanGoUpPile as follow:
Public Function CanGoUpPile(UpPile As Deck) As Boolean
If UpPile.Size > 0 Then
If UpPile.Top.Color = Me.Color And UpPile.Top.Value + 1 = Me.Value And UpPile.Top.Suit = Me.Suit Then
CanGoUpPile = True
Else
CanGoUpPile = False
End If
ElseIf Me.Value = ACE Then
CanGoUpPile = True
Else
CanGoUpPile = False
End If
End Function
- To check if the card can move to a down-pile, we use the function CanGoDownPile as follow:
Public Function CanGoDownPile(DownPile As Deck) As Boolean
If DownPile.Size > 0 Then
If DownPile.Front.CardImage = DownPile.Front.FaceImage Then
If DownPile.Front.Color <> Me.Color And DownPile.Front.Value - 1 = Me.Value Then
CanGoDownPile = True
Else
CanGoDownPile = False
End If
Else
CanGoDownPile = False
End If
ElseIf Me.Value = KING Then
CanGoDownPile = True
Else
CanGoDownPile = False
End If
End Function
The entire card object class written in vb can be read here. With these abstractions, it's easier to write the solitaire game. Here is my first solitaire game that you can download and play. The entire solitaire project's source code in vb can be downloaded here.