2014年1月9日 星期四

C# 期末作業 Brave road

為啥叫Brave road?因為為了做這個遊戲期末都沒在看書!?不知道為啥!就直覺吧!
為了堅持純手工兼自創,音樂是用MuseScore做的,圖片是用小畫家繪圖,GimpPhotoshop修圖,太醜請別見怪!


首先來一下遊戲介紹:

這是主畫面
可以選擇新遊戲繼續遊戲功能選項離開

這是功能表
可以選擇背景音效刪除遊戲紀錄
(PS.千萬別帶耳機....)

當選擇新遊戲時
就會出現前置背景故事,好像每個遊戲都要這樣搞~


現在開始才真正進入遊戲
可以選擇改變夥伴戰鬥有兩種:故事模式跟亂鬥模式、強化卡片抽卡片
當然還有儲存返回主選單

這裡可以選擇你要的卡片夥伴

這是故事模式
有1~5關
只有在故事模式中進行挑戰,勝利後才會開放後續關卡

這是戰鬥的情況
規則是:先猜拳,勝利平手情況下才可以攻擊
再依屬性剋性分別會有不同的加成效果

按下中間有攻擊方向的按鈕就可以進行攻擊
當然只要在戰鬥後獲勝都可以獲得1枚幻幣
待會抽卡包時會很好用~

這是抽卡包的情況
每抽一次要3幻幣,賭個運氣吧

這裡是強化卡片
你可以選擇你所需要的卡片進行強化~~
PS.這部分快搞死我了......
好了,遊戲介紹完畢
再來聊聊有關我最棘手的一個功能:強化

流程:選擇卡片>鎖定卡片>強化卡片>刪除卡片
當然還是有一些小細節要注意,不然程式會出現一堆莫名其妙的問題(雖然都自己搞得......)。

首先宣告

int[] arrayS = new int[6];          //陣列S 用來存放所有選擇的卡片
int[] arrayC = new int[5];          //陣列C 用來存放強化素材
int n, button_key;                     //n是選取值 , button_key是用來判斷第幾個按鈕的變數

初始化(將資料載入listbox)


listBox_selectcard.Items.Add("Null");
global.addlistbox(listBox_selectcard);
listBox_selectcard.Visible = false;

返回鈕的判別

if (button_return.Text == "返回")
{
     listBox_selectcard.Items.Clear();
     Form2 frm = new Form2();
     frm.Show();
     this.Hide();
 }//返回遊戲功能表
else
{
     if (arrayS[button_key] != 0)
           listBox_selectcard.Items[arrayS[button_key]] = "-----此卡鎖定-----";
     clslistbox();
}//返回強化選單

這是要選擇強化卡片的按鈕(其他按鈕都長差不多不贅述)

button_key = 0;                       //非常實用的東西,可以用將listbox中所選定的值帶入陣列中
oplistbox(button_key);

清除鈕,基本上就重設listbox


resetlistbox();
clearpic();

強化鈕(整個功能這裡關鍵之一)

if (button_mixture.Text == "強化")
{
      int count = 0;
      for (int i = 1; i < 6; i++)
           if (arrayS[i] != 0)
                   count++;
       if (arrayS[0] != 0 && count != 0)
       {
             mixture();
             MessageBox.Show("強化成功 !");
             clearpic();
             checkpartner();
        }
        else if(arrayS[0]==0)
               MessageBox.Show("請選擇要強化的卡片 !");
        else
               MessageBox.Show("請選擇強化素材 !");                  
}//判斷是否可以強化
else
{
        selectreturn(button_key);
        clslistbox();
        switch (button_key)
       {
              case 0:
                    if (arrayS[button_key] != 0)
                            global.imageload(global.PC[arrayS[button_key] - 1].C_number, pictureBox, filename......);      //用了10張圖,所以filename會有10個  case情況相似不贅述
                        else
                            pictureBox1.Image = null;
                        break;
              case 1
                        break;
              case 2:
                        break;
              case 3:
                        break;
              case 4:
                        break;
              case 5:
                        break;
       }//顯示被選定的卡片圖案
}//確定選取的卡片資料

PS.載入圖片的函式還好縮減這樣,不然我眼睛一定會花掉

listBox 紀錄選取值

int count=0;                        //紀錄卡片是否重複
     for (int i = 0; i < 6; i++)
          if (arrayS[i] == listBox_selectcard.SelectedIndex)
         {
               if (arrayS[i] != 0)
              {
                    if (i == button_key)
                         count++;
                    else
                         count += 2;     //直接可以跟arrayS[0]==0 的條件避開,省下不少麻煩
               }
         }

    if (count < 2)
          n = listBox_selectcard.SelectedIndex;
    else
         {
               MessageBox.Show("此卡已被鎖定 !");
                listBox_selectcard.SelectedIndex = 0; 
          }
}//判斷卡片是否重複

打開listbox(這是按選擇鈕會會執行的)

listBox_selectcard.Visible = true;
button_clear.Visible = false;
listBox_selectcard.SelectedIndex = 0;
if(arrayS[a]!=0)
      listBox_selectcard.Items[arrayS[a]] = "ID: " + global.PC[arrayS[a] - 1].C_id + " 卡片no: " + global.PC[arrayS[a] - 1].C_number + " " + global.PC[arrayS[a] - 1].C_name + " " + global.PC[arrayS[a] - 1].C_attr + " ATK: " + global.PC[arrayS[a] - 1].C_atk + " ATK_Max: " + global.PC[arrayS[a] - 1].C_atkMax;
button_mixture.Text = "確認";
button_return.Text = "回強化介面";

關閉listbox(跟上面剛好是相反概念)

listBox_selectcard.Visible = false;
button_clear.Visible = true;
button_mixture.Text = "強化";
button_return.Text = "返回";

選擇時鎖定卡片

 private void selectreturn(int a)
{
      arrayS[a] = n;
      if (arrayS[a] != 0)
          listBox_selectcard.Items[arrayS[a]] = "-----此卡鎖定-----";
 }

強化卡片流程 

private void mixture()
{
     exchangesort();
     addatk();
     checkatk(arrayS[0]-1);
     removecard();
     resetlistbox();
}//強化所經過的步驟

交換排序(這是去年暑假時寫的東西,結果把程式逐步印出來我才知道在寫啥@@)

private void exchangesort()
{
      int count = 0;
      for (int i = 0; i < 5; i++)
          arrayC[i] = arrayS[i + 1];
      for (int i = 0; i < 5; i++)
     {
          for (int j = 0; j < 4; j++)
             if (arrayC[j] > arrayC[j+1])
                   count++;
             if (count == 4)
                   break;
             else
            {
                   count = 0;
                   for (int j = 1; j < 5; j++)
                   {
                        for (int k = j - 1; k >= 0; k--)
                        {
                            if (arrayC[k] < arrayC[j])
                            {
                                int tmp;
                                tmp = arrayC[j];
                                arrayC[j] = arrayC[k];
                                arrayC[k] = tmp;
                            }
                            else
                                break;
                        }
                  }
             }
       }
}//交換排序 大->小     為了防止待會在刪除List<T>時會發生錯誤,所以採用從後往前刪除的概念

增加攻擊力(依卡片張數做攻擊增加的基本,當滿5張同屬性則另有加成)

private void addatk()
{
      string attr = global.PC[arrayS[0]-1].C_attr;
      int countofcard = 0,countofsameattr=0;
      for (int i = 0; i < 5; i++)
     {
           if (arrayC[i] != 0)
          {
                countofcard++;
                if (global.PC[arrayC[0] - 1].C_attr == global.PC[arrayC[i] - 1].C_attr)
                    countofsameattr++;
          }
      }

      if (countofcard == 5)
      {
             if (countofsameattr == 5)
             {
                   if (attr == global.PC[arrayC[0] - 1].C_attr)
                        global.PC[arrayS[0] - 1].C_atk += countofcard + 5;
                   else
                        global.PC[arrayS[0] - 1].C_atk += countofcard + 3;
              }
              else
                    global.PC[arrayS[0] - 1].C_atk += countofcard;
      }
       else
              global.PC[arrayS[0]-1].C_atk += countofcard;
}

檢查攻擊力是否超過

private void checkatk(int a)
{
      if (global.PC[a].C_atk > global.PC[a].C_atkMax)
           global.PC[a].C_atk = global.PC[a].C_atkMax;
}

刪除卡片資料(將資料從List<T>中移除)

private void removecard()
{
      for (int i = 0; i < 5;i++ )
     {
           if(arrayC[i]!=0)
                 global.PC.RemoveAt(arrayC[i]-1);
     }
}

重置listbox

private void resetlistbox()
{
      for (int i = 0; i < 6; i++)
           arrayS[i] = 0;
      for (int i = 0; i < 5; i++)
           arrayC[i] = 0;
      listBox_selectcard.Items.Clear();
      listBox_selectcard.Items.Add("Null");
      global.addlistbox(listBox_selectcard);
}

清除圖片

private void clearpic()
{
      pictureBox1.Image = null;
      pictureBox2.Image = null;
      pictureBox3.Image = null;
      pictureBox4.Image = null;
      pictureBox5.Image = null;
      pictureBox6.Image = null;
}

檢查夥伴卡片是否存在(很重要,因為夥伴卡片被強化掉了,BUG就來了....)

 private void checkpartner()
 {
      int check=0;
      for (int i = 0; i < global.PC.Count; i++)
          if (global.PC[i].C_id == global.PS[0].partnerID)
               check++;
      if (check == 0)
     {
          global.PS[0].partnerID = global.PC[0].C_id;
          global.checkpartner(0);
     }
      else
     {
           int Cid=0;
           Cid=global.searchid(global.PS[0].partnerID,Cid);
           global.checkpartner(Cid);
           global.PS[0].partnerID = global.CP[0].C_id;
     }
}

已經2點半了,我該睡了,明天做最後展示
附上專案檔,晚安~~
C#專案下載處:Brave road

2013年9月29日 星期日

C#-陣列相加、相乘


題目:
           陣列相加、相乘(需使用double)。

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            double[,] a = { { 1.0, 2.3, 5.1 }, { 4.1, 0.2, 8.7 } }; // 宣告陣列A
            double[,] b = { { 0.1, 1.7, 8.5 }, { 6.1, 3.4, 7.2 } }; // 宣告陣列B
            double[,] d = { { 0.1, 6.1 }, { 1.7, 3.4 }, { 8.5, 7.2 } }; // 宣告陣列C

            matrixAdd(a, b); // 執行陣列相加函式
            Console.WriteLine("      "); // 空一行
            matrixMul(a, d); // 執行陣列相乘函式

            Console.ReadLine(); // 輸入,用來暫停程式
        }

        static void matrixAdd(double[,] a, double[,] b) // 相加函式
        {
            double[,] c = new double[a.GetLength(0), a.GetLength(1)]; // 宣告空陣列C(X向量為3,Y向量為2)
            for (int i = 0; i < a.GetLength(0); i++)
            {
                for (int j = 0; j < a.GetLength(1); j++)
                {
                    c[i, j] = a[i, j] + b[i, j]; // 陣列C=陣列A+陣列B
                    Console.WriteLine("c[" + i + "," + j + "]=" + c[i, j] + " "); // 印出陣列的值
                }
            }

        }

        static void matrixMul(double[,] a, double[,] b) // 相乘函式
        {
            double[,] c = new double[a.GetLength(0), b.GetLength(1)]; // 宣告空陣列C(X向量為2,Y向量為2)
            for (int i = 0; i < a.GetLength(0); i++)
            {
                for (int j = 0; j < a.GetLength(1); j++)
                {
                    c[i, i - i] += a[i, j] * b[j, i - i]; // 陣列C=陣列A與陣列B的內積
                    c[i, i - i + 1] += a[i, j] * b[j, i - i + 1];
                }  
                Console.WriteLine("c[" + i + "," + (i-i) + "]=" + c[i, i-i] + " "); // 印出陣列C[i,i-i]
                Console.WriteLine("c[" + i + "," + (i - i + 1) + "]=" + c[i, i - i + 1] + " "); // 印出陣列C[i,i-i+1]
             }
        }
    }
}

檔案:AddMultiply.cs

C#-內積


題目:

將兩向量做內積。


Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] a = { 1.2, 2.5, 3.0, 4.7, 5.1 }; // 宣告陣列A
            double[] b = { 5.9, 4.4, 3.1, 2.3, 1.8 }; // 宣告陣列B

            innerProduct(a, b); // 執行內積函式

            Console.ReadLine(); // 輸入值,用來讓程式暫停
        }

        static void innerProduct(double[] a, double[] b)
        {
            int i; // 宣告變數 i
            double sum = 0; // 宣告變數 sum 為0 ,用來表示內積
            double[] c = new double[a.Length]; // 宣告空陣列C
            for (i = 0; i < a.Length; i++) // 從0開始、到陣列A的長度
            {
                c[i] = a[i] * b[i]; // 每個陣列C=A、B向量的相乘
                sum += c[i]; // 將每個陣列C的值相加
//                Console.WriteLine("c[i" + i + "]=" + c[i] + " "); // 顯示每個陣C的值
            }
            Console.WriteLine("內積=" + sum + " "); // 顯示內積
        }
    }
}

檔案: InnerProduct.cs

R-與誰最有緣

從班上學生隨機抽10000次,看看與誰最有緣。(本班有59人)

> x=sample(1:59, 10000, TRUE) // X樣本數從1到59,抽10000次,可放回重抽
> hist(x, breaks=0.5:60) // 顯示X樣本
結果我與14號最有緣。

R-感受中央極限定理


作業:
有100頂帽子,每次從中拿起一頂,可再放回,重複1000000次。

>x=sample(1:100, 1000000, T)  //X的樣本1000000次,每次從1到100中隨機選擇1個數字,T為可放回
>y=sample(1:100, 1000000, T)
>z=sample(1:100, 1000000, T)
>hist(x, breaks=0.5:101)  //  顯示X的可能 X斷點設為0.5,到101,最大為100.5

>hist(y, breaks=0.5:101)  // 顯示Y的可能

>hist(z, breaks=0.5:101)  // 顯示Z的可能

>hist(x+y, breaks=1.5:203) // 顯示X+Y的可能

>hist(x+y+z, breaks=2.5:305)  //顯示X+Y+Z的可能

2013年9月21日 星期六

C# 單位換算器

簡單的來說就是單位換算,因為Combobox的值需要因為Button切換而改變,故採用以下寫法。
// Bind combobox to dictionary
Dictionary<string, string>test = new Dictionary<string, string>();
        test.Add("1", "dfdfdf");
        test.Add("2", "dfdfdf");
        test.Add("3", "dfdfdf");
        comboBox1.DataSource = new BindingSource(test, null);
        comboBox1.DisplayMember = "Value";
        comboBox1.ValueMember = "Key";

// Get combobox selection (in handler)
string value = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value;

Debug過程:
1.float num_ori= Convert.ToDouble(txt.Text),無法一開始宣告,因為txt.Text一開始不存在,所以不宣告而採用在後面判斷時直接轉換。
2.Combobox的取值時機,在SelectedIndexChanged事件下,當選取長度、質量、容積,任一button時,SelectedIndex跟SelectedText的值就會觸發而帶入,所得到的值為0,以至於後續無法轉換;Click事件下,則是選取Combobox時,造成值的觸發,結果帶入一樣為0;SelectionChangeCommitted事件下,則是當確定選取完成時,SelectedIndex跟SelectedText的值才會帶入,故後面判斷才能進行。

用cmd執行 cd  Documents\Visual Studio 2010\Projects\unitconversion\unitconversion 到檔案所在的資料夾底下,再輸入 csc Form1.cs  Form1.Designer.cs Program.cs  ,最後打上 Program.exe執行。

以下附上一張執行成果


檔案連結:

參考資料:

ComboBox: Adding Text and Value to an Item



2013年4月22日 星期一

期中作業


作者姓名:林育任
作品名稱:紙箱人阿楞的日常
故事大綱:紙箱人的阿楞,一個人在草原上玩耍,會有甚麼意想不到的事情發生呢?讓我們繼                      續看下去。
靜態圖檔:https://docs.google.com/file/d/0By73yj5goZQNdXRsMm14dHdKZ00/edit
Blender專案:https://docs.google.com/file/d/0By73yj5goZQNSVRfSW5nVmhTMzQ/edit