//Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HW4
{
public partial class Form1 : Form
{
CardDeck mydeck = new CardDeck();
public Form1()
{
InitializeComponent();
mydeck.CreateDeck();
mydeck.ResetDeck();
}
private void btn_combobox_Click(object sender, EventArgs e)
{
switch (CBox_select.SelectedIndex)
{
case 0:
mydeck.ResetDeck();
txt_display.AppendText("combobox按花色牌列已被選定"+'\n');
messageshow();
break;
case 1:
mydeck.Shuffle();
txt_display.AppendText("combobox亂數洗牌已被選定" + '\n');
messageshow();
break;
case 2:
mydeck.resetbyrank();
txt_display.AppendText("combobox按點數大小排列已被選定" + '\n');
messageshow();
break;
default:
txt_display.AppendText("未選擇方法!!" + '\n');
break;
}
}
private void btn_checkbox_Click(object sender, EventArgs e)
{
if (checkBox_select1.Checked)
{
mydeck.ResetDeck();
txt_display.AppendText("checkBox按花色牌列已被選定" + '\n');
messageshow();
}
if (checkBox_select2.Checked)
{
mydeck.Shuffle();
txt_display.AppendText("checkBox亂數洗牌已被選定" + '\n');
messageshow();
}
if (checkBox_select3.Checked)
{
mydeck.resetbyrank();
txt_display.AppendText("checkBox按點數大小排列已被選定" + '\n');
messageshow();
}
}
private void messageshow()
{
string str=null;
for (int i=0; i < 52; i++)
{
str += mydeck.Cards[i].SuitToUni();
str += mydeck.Cards[i].RankToStr();
str += " ";
if (i % 13 == 12)
str += '\n';
}
MessageBox.Show(str);
}
}
}
//CardDeck.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW4
{
class CardDeck
{
public PokerCard[] Cards = new PokerCard[52];
public void CreateDeck()
{
int i;
for (i = 0; i < 52; i++)
Cards[i] = new PokerCard();
}
public void ResetDeck() //牌堆回復原始狀態
{
int i ;
for (i = 0; i < 52; i++)
{
Cards[i].Suit = i/13+3;
Cards[i].Rank = i%13+1;
}
}
public void Shuffle() //洗牌
{
Random rand = new Random();
for (int i = 0; i < 52; i++)
{
int n1 = rand.Next(52);
PokerCard temp = Cards[i];
Cards[i] = Cards[n1];
Cards[n1] = temp;
}
}
public void resetbyrank()
{
int i;
for (i = 0; i < 52; i++)
{
Cards[i].Suit = i % 4 + 3;
Cards[i].Rank = i / 4 + 1;
}
}
}
}
//PokerCard.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW4
{
class PokerCard
{
public int Suit;
public int Rank;
public string RankToStr()
{
switch (Rank)
{
case 1:
return "A";
case 11:
return "J";
case 12:
return "Q";
case 13:
return "K";
default:
return Convert.ToString(Rank);
}
}
public char SuitToAscii()
{
return Convert.ToChar(Suit);
}
public string SuitToUni()
{
switch (Suit)
{
case 3:
return "\u2665";
case 4:
return "\u2666";
case 5:
return "\u2663";
case 6:
return "\u2660";
default:
return " ";
}
}
}
}
第6祖神回復
回覆刪除把SetCard();移到前面為優
private void btn_ComboBoxTestEnsure_Click(object sender, EventArgs e)
{
txt_ShowYourChoice.Clear();
switch (cbo_ComboBoxTestChoice.SelectedIndex)
{
case 0:
SetCard();
txt_ShowYourChoice.AppendText("Combobox按花色排列 已被選定" + '\n');
MessageBoxShow();
break;
case 1:
SetCard();
Shuffle();
txt_ShowYourChoice.AppendText("Combobox亂數洗牌 已被選定" + '\n');
MessageBoxShow();
break;
case 2:
RangeCard();
txt_ShowYourChoice.AppendText("Combobox按點數大小 已被選定" + '\n');
MessageBoxShow();
break;
}
}
=>
private void btn_ComboBoxTestEnsure_Click(object sender, EventArgs e)
{
txt_ShowYourChoice.Clear();
SetCard();
switch (cbo_ComboBoxTestChoice.SelectedIndex)
{
case 0:
txt_ShowYourChoice.AppendText("Combobox按花色排列 已被選定" + '\n');
MessageBoxShow();
break;
case 1:
Shuffle();
txt_ShowYourChoice.AppendText("Combobox亂數洗牌 已被選定" + '\n');
MessageBoxShow();
break;
case 2:
RangeCard();
txt_ShowYourChoice.AppendText("Combobox按點數大小 已被選定" + '\n');
MessageBoxShow();
break;
}
}
這是我們上一版的CODE
刪除有新版本上傳喔!!~~
就沒有這個問題了
小地方
回覆刪除class CardDeck裡面的int i
可以跟for迴圈裡面的i合併
by 第九組
===林高遠===
回覆刪除你不會在書局買到只有空盒子的撲克牌,所以CarDeck被new 出來的時候就要馬上有52張有大小和花色的牌比較合理