I gave advice for someone on the forums and he was making a card game. I explained to him that he needed to create a class for his monster and gave an example. I figured, heck it'd be pretty fun to make my own card game, so that's my new project. I almost have everything that runs the game completed, it's a console application game. I'm just stuck on saving the classes. First let me put out my code.
The monster or card
The player
Gameplay module to hold methods that will be used a lot
Now I ran my classes through formlesstree's function(here) and it said that both my player class is unserializable. I figure it's because I have a list(of monsters) for my deck. My question to y'all is, how would you save a player and a deck of monsters? I'm trying to wrap my head around it and can't think of anything.
The monster or card
Code:
Option Strict On
Option Explicit On
Public Class Monster
#Region "Properties"
''' <summary>
''' Gets/Sets the health of our monster
''' </summary>
Private hp As Integer = 0
Public Property Health() As Integer
Get
Return hp
End Get
Set(ByVal value As Integer)
hp = value
End Set
End Property
''' <summary>
''' Gets/Sets the attack of our monster
''' </summary>
Private atk As Integer = 0
Public Property Attack() As Integer
Get
Return atk
End Get
Set(ByVal value As Integer)
atk = value
End Set
End Property
''' <summary>
''' Gets/Sets the defence of our monster
''' </summary>
Private def As Integer = 0
Public Property Defence() As Integer
Get
Return def
End Get
Set(ByVal value As Integer)
def = value
End Set
End Property
''' <summary>
''' Gets the true/false value if our monster is in defence mode
''' </summary>
''' <remarks>If the monster is in defence mode, it cannot attack</remarks>
Private in_def As Boolean
Public ReadOnly Property IsInDefence() As Boolean
Get
Return in_def
End Get
End Property
''' <summary>
''' Gets the true/false value if our monster is hidden or not
''' </summary>
''' <remarks>If the monster is hidden, it can neither attack nor can it change from defence or attack mode</remarks>
Private is_hidden As Boolean
Public ReadOnly Property IsHidden() As Boolean
Get
Return is_hidden
End Get
End Property
#End Region
#Region "Methods/Functions"
''' <summary>
''' Attacks an opponent monster
''' </summary>
''' <param name="opponent">The opponents monster</param>
''' <remarks>The fight scene!</remarks>
Public Sub AttackMonster(ByVal opponent As Monster)
If opponent.IsInDefence AndAlso Not (is_hidden) AndAlso Not (in_def) Then
Dim our_atk As Integer = atk - opponent.Defence
If our_atk > 0 Then
opponent.Health -= our_atk
End If
ElseIf Not (is_hidden) AndAlso Not (in_def) Then
opponent.Health -= atk
End If
End Sub
''' <summary>
''' Changes if our monster is in attack or defence mode
''' </summary>
''' <remarks>Cannot change if monster hidden</remarks>
Public Sub ChangeDefenceMode()
If Not (is_hidden) Then
in_def = Not (in_def)
Else
Throw New Exception("Monster is currently hidden, cannot change the IsInDefence property.")
End If
End Sub
''' <summary>
''' Changes if our monster is hidden or not
''' </summary>
Public Sub ChangeHiddenMode()
is_hidden = Not (is_hidden)
End Sub
#End Region
#Region "Initialization"
'The different ways to initialize a monster
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal _hp As Integer)
hp = _hp
End Sub
Public Sub New(ByVal _hp As Integer, ByVal _atk As Integer)
hp = _hp
atk = _atk
End Sub
Public Sub New(ByVal _hp As Integer, ByVal _atk As Integer, ByVal _def As Integer)
hp = _hp
atk = _atk
def = _def
End Sub
Public Sub New(ByVal _hp As Integer, ByVal _atk As Integer, ByVal _def As Integer, ByVal def_mod As Boolean)
hp = _hp
atk = _atk
def = _def
in_def = def_mod
End Sub
Public Sub New(ByVal _hp As Integer, ByVal _atk As Integer, ByVal _def As Integer, ByVal def_mod As Boolean, ByVal hidden As Boolean)
hp = _hp
atk = _atk
def = _def
in_def = def_mod
is_hidden = hidden
End Sub
#End Region
End Class
Code:
Option Strict On
Option Explicit On
Public Class Player
#Region "Properties"
''' <summary>
''' Gets/Sets the name of the player
''' </summary>
Private _name As String = String.Empty
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
''' <summary>
''' Gets/Sets the health of our player
''' </summary>
Private hp As Integer = 0
Public Property Health() As Integer
Get
Return hp
End Get
Set(ByVal value As Integer)
hp = value
End Set
End Property
''' <summary>
''' Gets/Sets the list that will hold our monsters
''' </summary>
''' <remarks>The max limit of the deck should only be 50</remarks>
Private _deck As List(Of Monster)
Public Property Deck() As List(Of Monster)
Get
Return _deck
End Get
Set(ByVal value As List(Of Monster))
_deck = value
End Set
End Property
''' <summary>
''' Gets/Sets the list that will represent the hand
''' </summary>
Private hand As List(Of Monster)
Public Property CurrentHand() As List(Of Monster)
Get
Return hand
End Get
Set(ByVal value As List(Of Monster))
hand = value
End Set
End Property
#End Region
End Class
Code:
Option Strict On
Option Explicit On
Imports System.IO
Imports System.Xml.Serialization
Module Gameplay
Private r As New Random
''' <summary>
''' This will shuffle the deck
''' </summary>
''' <param name="deck">A list of monsters, generally represented by the player's own deck</param>
''' <remarks>It uses LINQ, if you don't have .net 3.5 or higher, uncomment the commented for loop, and comment the Enumberable statement</remarks>
Public Sub ShuffleDeck(ByVal deck As List(Of Monster))
'If an error occurs here, check the xml remark
Dim exclusive_numbers() As Integer = Enumerable.Range(0, deck.Count - 1).OrderBy(Function(n) r.Next(deck.Count)).ToArray
'Dim exclusive_numbers(deck.Count - 1) As Integer
'Dim counter As Integer = 0
'Do Until counter > exclusive_numbers.Count - 1
' Dim i As Integer = r.Next(0, deck.Count - 1)
' If exclusive_numbers.Contains(i) = False Then
' exclusive_numbers(counter) = i
' counter += 1
' End If
'Loop
Dim temp_deck As New List(Of Monster)
For Each i As Integer In exclusive_numbers
temp_deck.Add(deck.Item(i))
Next
deck = temp_deck
End Sub
''' <summary>
''' This will pull a card from a deck and place it into a hand
''' </summary>
''' <param name="hand">A list of monsters, generally represented by the player's own hand</param>
''' <param name="deck">A list of monsters, generally represented by the player's own deck</param>
Public Sub DrawCard(ByVal hand As List(Of Monster), ByVal deck As List(Of Monster))
hand.Add(deck.Item(0))
deck.RemoveAt(0)
End Sub
''' <summary>
''' This will serialize a deck of monsters
''' </summary>
''' <param name="deck">The deck to serialize</param>
''' <param name="path">The path to save the xml file</param>
Public Sub SaveDeck(ByRef deck As List(Of Monster), ByVal path As String)
Dim writer As New StreamWriter(path)
Dim x As New XmlSerializer(GetType(List(Of Monster)))
x.Serialize(writer, deck)
writer.Close()
End Sub
''' <summary>
''' This will deserialize a deck of monsters
''' </summary>
''' <param name="deck">The deck to be returned</param>
''' <param name="path">The path that represents the xml</param>
Public Sub LoadDeck(ByRef deck As List(Of Monster), ByVal path As String)
Dim reader As New StreamReader(path)
Dim x As New XmlSerializer(GetType(List(Of Monster)))
deck = DirectCast(x.Deserialize(reader), List(Of Monster))
reader.Close()
End Sub
''' <summary>
''' This will serialize a player
''' </summary>
''' <param name="user">The player to serialize</param>
''' <param name="path">The path to save the xml file</param>
Public Sub SavePlayer(ByRef user As Player, ByVal path As String)
If File.Exists(path) = False Then
File.Create(path)
End If
Dim writer As New StreamWriter(path)
Dim x As New XmlSerializer(GetType(Player))
x.Serialize(writer, user)
writer.Close()
End Sub
''' <summary>
''' This will deserialize a player
''' </summary>
''' <param name="player">The player to be returned</param>
''' <param name="path">The path that represents the xml</param>
Public Sub LoadPlayer(ByRef player As Player, ByVal path As String)
Dim reader As New StreamReader(path)
Dim x As New XmlSerializer(GetType(Player))
player = DirectCast(x.Deserialize(reader), Player)
reader.Close()
End Sub
End Module