Holey_yan

[VB.NET] 撲克牌洗牌、發牌與排序

Sep 9th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.39 KB | None | 0 0
  1. Module Module1
  2.  
  3.     Sub Main()
  4.         ' 建立撲克牌
  5.         Dim poke As New List(Of PokeCard)
  6.         For Each color As String In New String() {"C", "D", "H", "S"}
  7.             For i As Integer = 1 To 13 Step 1
  8.                 poke.Add(New PokeCard(color, i))
  9.             Next
  10.         Next
  11.  
  12.         ' 洗牌
  13.         Dim ran As New Random
  14.         For i As Integer = 0 To 100 Step 1
  15.             Dim indexA As Integer = ran.Next(1, poke.Count),
  16.                 indexB As Integer = ran.Next(1, poke.Count)
  17.             Dim temp As PokeCard = poke(indexA)
  18.             poke(indexA) = poke(indexB)
  19.             poke(indexB) = temp
  20.         Next
  21.  
  22.         ' 發牌
  23.         Dim players As New Dictionary(Of String, List(Of PokeCard)) From {
  24.             {"Player A", New List(Of PokeCard)},
  25.             {"Player B", New List(Of PokeCard)},
  26.             {"Player C", New List(Of PokeCard)},
  27.             {"Player D", New List(Of PokeCard)}}
  28.         For Each key As String In players.Keys
  29.             For i As Integer = 0 To 12 Step 1
  30.                 players(key).Add(poke.First)
  31.                 poke.Remove(poke.First)
  32.             Next
  33.         Next
  34.  
  35.         ' 排序
  36.         For Each key As String In players.Keys.ToArray
  37.             players(key) = players(key).OrderBy(Function(x) x.color).ThenBy(Function(x) x.number).ToList
  38.         Next
  39.  
  40.         ' 輸出
  41.         For Each key As String In players.Keys
  42.             Console.ForegroundColor = ConsoleColor.Green
  43.             Console.WriteLine(key)
  44.             For Each card As PokeCard In players(key)
  45.                 Console.ForegroundColor = If(card.color.Equals("D") Or card.color.Equals("H"), ConsoleColor.Red, ConsoleColor.White)
  46.                 Console.Write(card.color + If(card.number = 1, "A",
  47.                                            If(card.number = 11, "J",
  48.                                            If(card.number = 12, "Q",
  49.                                            If(card.number = 13, "K",
  50.                                               card.number.ToString)))) + vbTab)
  51.             Next : Console.WriteLine(vbNewLine)
  52.         Next : Console.ReadKey()
  53.     End Sub
  54.  
  55.     Public Class PokeCard
  56.  
  57.         Public color As String
  58.         Public number As Integer
  59.  
  60.         Sub New(ByVal color As String, ByVal number As Integer)
  61.             Me.color = color
  62.             Me.number = number
  63.         End Sub
  64.  
  65.     End Class
  66.  
  67. End Module
Advertisement
Add Comment
Please, Sign In to add comment