using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace _21point { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int card = 0; //整副牌被抽張數 int user_c, pc_c = 2; //玩家與電腦的手牌數 int user_point, pc_point = 0; //玩家與電腦的牌面點數 string[] array_s1 = new string[] { "黑桃", "紅心", "梅花", "方塊" }; string[] array_s2 = new string[] { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; string[] array_poke = new string[52]; //整副撲克牌 string[] array_user = new string[6]; //玩家手牌 string[] array_pc = new string[6]; //電腦手牌 // 開新局 private void start() { //清除前一局的資料 pictureBox1.Image = null; pictureBox2.Image = null; pictureBox3.Image = null; pictureBox4.Image = null; pictureBox5.Image = null; pictureBox6.Image = null; pictureBox7.Image = null; pictureBox8.Image = null; pictureBox9.Image = null; pictureBox10.Image = null; textBox1.Text = ""; card = 0; user_c = 2; pc_c = 2; user_point = 0; pc_point = 0; Array.Clear(array_user,0,array_user.Length); Array.Clear(array_pc,0,array_pc.Length); //復原按鈕可按 button2.Enabled = true; pictureBox11.Enabled = true; //依執行時的時間抓新的亂數種子 Random Rnd1 = new Random((int)DateTime.Now.Ticks); Random Rnd2 = new Random((int)DateTime.Now.Ticks + 1); //將兩個陣列組成一副牌組陣列 for (int i = 0; i < 4; i++) { for (int j = 0; j < 13; j++) { array_poke[i * 13 + j] = array_s1[i] + "," + array_s2[j]; } } // 洗牌 for (int k = 0; k < 100; k++) { int rnd1 = Rnd1.Next(0, array_poke.Length); int rnd2 = Rnd2.Next(0, array_poke.Length); string s_temp = array_poke[rnd1]; array_poke[rnd1] = array_poke[rnd2]; array_poke[rnd2] = s_temp; } //列出牌的陣列資料(除錯用) for (int i = 0; i < array_poke.Length; i++) { textBox1.Text += "poke[" + i + "]=" + array_poke[i] + "\r\n"; } textBox1.Text += "************************* \r\n"; //玩家與電腦各抽兩張牌 array_user[0] = array_poke[51 - card]; card += 1; array_pc[0] = array_poke[51 - card]; card += 1; array_user[1] = array_poke[51 - card]; card += 1; array_pc[1] = array_poke[51 - card]; card += 1; //列出玩家與電腦所抽到的牌(除錯用) for (int i = 0; i < array_user.Length; i++) { if (array_user[i] == null) { break; } textBox1.Text += "user[" + i + "]=" + array_user[i] + "\r\n"; } for (int i = 0; i < array_pc.Length; i++) { if (array_pc[i] == null) { break; } textBox1.Text += "pc[" + i + "]=" + array_pc[i] + "\r\n"; } //顯示玩家牌與電腦牌 show_user(); show_photo(); } // 取牌 private void more_one() { array_user[user_c] = array_poke[51 - card]; card += 1; textBox1.Text += "user[" + user_c + "]=" + array_user[user_c] + "\r\n"; user_c += 1; point(); if (pc_point <= 11) { array_pc[pc_c] = array_poke[51 - card]; card += 1; textBox1.Text += "pc[" + pc_c + "]=" + array_pc[pc_c] + "\r\n"; pc_c += 1; } else { Random Rnd = new Random((int)DateTime.Now.Ticks); int rnd = Rnd.Next(0,10); if (rnd >= 5) { array_pc[pc_c] = array_poke[51 - card]; card += 1; textBox1.Text += "pc[" + pc_c + "]=" + array_pc[pc_c] + "\r\n"; pc_c += 1; } } show_photo(); show_user(); point(); if (pc_point > 21) { show_pc(); if (user_point > 21) { MessageBox.Show("平手\r\n" + "User:" + user_point.ToString() + " PC:" + pc_point.ToString()); button2.Enabled = false; pictureBox11.Enabled = false; } else { MessageBox.Show("贏了\r\n" + "User:" + user_point.ToString() + " PC:" + pc_point.ToString()); button2.Enabled = false; pictureBox11.Enabled = false; } } else if (user_point > 21) { show_pc(); MessageBox.Show("輸惹\r\n" + "User:" + user_point.ToString() + " PC:" + pc_point.ToString()); button2.Enabled = false; pictureBox11.Enabled = false; } } // 算點數 private void point() { int point = 0; int point_A = 0; //point紀錄點數 point_A 紀錄幾張A,在總結時再做運算 for (int j = 0; j < user_c; j++) { switch (array_user[j][3]) { case '2': point += 2; break; case '3': point += 3; break; case '4': point += 4; break; case '5': point += 5; break; case '6': point += 6; break; case '7': point += 7; break; case '8': point += 8; break; case '9': point += 9; break; case '1': case 'J': case 'Q': case 'K': point += 10; break; case 'A': point_A += 1; break; default: break; } } for (int j = 0; j < point_A; j++) { if (point > 11) { point += 1; } else { point += 11; } } user_point = point; //============================================= point = 0; point_A = 0; for (int j = 0; j < pc_c; j++) { switch (array_pc[j][3]) { case '2': point += 2; break; case '3': point += 3; break; case '4': point += 4; break; case '5': point += 5; break; case '6': point += 6; break; case '7': point += 7; break; case '8': point += 8; break; case '9': point += 9; break; case '1': case 'J': case 'Q': case 'K': point += 10; break; case 'A': point_A += 1; break; default: break; } } for (int j = 0; j < point_A; j++) { if (point + 11 > 21) { point += 1; } else { point += 11; } } pc_point = point; } // 顯示電腦牌個數 private void show_photo() { pictureBox1.Image = imageList5.Images[0]; pictureBox2.Image = imageList5.Images[0]; if (pc_c > 2) pictureBox3.Image = imageList5.Images[0]; if (pc_c > 3) pictureBox4.Image = imageList5.Images[0]; if (pc_c > 4) pictureBox5.Image = imageList5.Images[0]; } // 顯示玩家牌 private void show_user() { int x_p = 0; string[] x = new string[20]; for (int i = 0; i < array_user.Length; i++) { if (array_user[i] == null) { break; } x = array_user[i].Split(','); switch (x[1]) { case "A": x_p = 0; break; case "2": x_p = 1; break; case "3": x_p = 2; break; case "4": x_p = 3; break; case "5": x_p = 4; break; case "6": x_p = 5; break; case "7": x_p = 6; break; case "8": x_p = 7; break; case "9": x_p = 8; break; case "10": x_p = 9; break; case "J": x_p = 10; break; case "Q": x_p = 11; break; case "K": x_p = 12; break; default: break; } if (x[0] == "黑桃") { if (i == 0) pictureBox6.Image = imageList1.Images[x_p]; if (i == 1) pictureBox7.Image = imageList1.Images[x_p]; if (i == 2) pictureBox8.Image = imageList1.Images[x_p]; if (i == 3) pictureBox9.Image = imageList1.Images[x_p]; if (i == 4) pictureBox10.Image = imageList1.Images[x_p]; } else if (x[0] == "梅花") { if (i == 0) pictureBox6.Image = imageList2.Images[x_p]; if (i == 1) pictureBox7.Image = imageList2.Images[x_p]; if (i == 2) pictureBox8.Image = imageList2.Images[x_p]; if (i == 3) pictureBox9.Image = imageList2.Images[x_p]; if (i == 4) pictureBox10.Image = imageList2.Images[x_p]; } else if (x[0] == "紅心") { if (i == 0) pictureBox6.Image = imageList3.Images[x_p]; if (i == 1) pictureBox7.Image = imageList3.Images[x_p]; if (i == 2) pictureBox8.Image = imageList3.Images[x_p]; if (i == 3) pictureBox9.Image = imageList3.Images[x_p]; if (i == 4) pictureBox10.Image = imageList3.Images[x_p]; } else if (x[0] == "方塊") { if (i == 0) pictureBox6.Image = imageList4.Images[x_p]; if (i == 1) pictureBox7.Image = imageList4.Images[x_p]; if (i == 2) pictureBox8.Image = imageList4.Images[x_p]; if (i == 3) pictureBox9.Image = imageList4.Images[x_p]; if (i == 4) pictureBox10.Image = imageList4.Images[x_p]; } } } // 顯示電腦牌 private void show_pc() { int x_p = 0; string[] x = new string[20]; for (int i = 0; i < array_pc.Length; i++) { if (array_pc[i] == null) { break; } x = array_pc[i].Split(','); switch (x[1]) { case "A": x_p = 0; break; case "2": x_p = 1; break; case "3": x_p = 2; break; case "4": x_p = 3; break; case "5": x_p = 4; break; case "6": x_p = 5; break; case "7": x_p = 6; break; case "8": x_p = 7; break; case "9": x_p = 8; break; case "10": x_p = 9; break; case "J": x_p = 10; break; case "Q": x_p = 11; break; case "K": x_p = 12; break; default: break; } if (x[0] == "黑桃") { if (i == 0)pictureBox1.Image = imageList1.Images[x_p]; if (i == 1)pictureBox2.Image = imageList1.Images[x_p]; if (i == 2)pictureBox3.Image = imageList1.Images[x_p]; if (i == 3)pictureBox4.Image = imageList1.Images[x_p]; if (i == 4)pictureBox5.Image = imageList1.Images[x_p]; } else if (x[0] == "梅花") { if (i == 0)pictureBox1.Image = imageList2.Images[x_p]; if (i == 1)pictureBox2.Image = imageList2.Images[x_p]; if (i == 2)pictureBox3.Image = imageList2.Images[x_p]; if (i == 3)pictureBox4.Image = imageList2.Images[x_p]; if (i == 4)pictureBox5.Image = imageList2.Images[x_p]; } else if (x[0] == "紅心") { if (i == 0)pictureBox1.Image = imageList3.Images[x_p]; if (i == 1)pictureBox2.Image = imageList3.Images[x_p]; if (i == 2)pictureBox3.Image = imageList3.Images[x_p]; if (i == 3)pictureBox4.Image = imageList3.Images[x_p]; if (i == 4)pictureBox5.Image = imageList3.Images[x_p]; } else if (x[0] == "方塊") { if (i == 0)pictureBox1.Image = imageList4.Images[x_p]; if (i == 1)pictureBox2.Image = imageList4.Images[x_p]; if (i == 2)pictureBox3.Image = imageList4.Images[x_p]; if (i == 3)pictureBox4.Image = imageList4.Images[x_p]; if (i == 4)pictureBox5.Image = imageList4.Images[x_p]; } } } //-------------------------------------------------- private void Form1_Load(object sender, EventArgs e) { start(); pictureBox12.Image = imageList5.Images[0]; pictureBox11.Image = imageList5.Images[0]; } private void button1_Click(object sender, EventArgs e) { more_one(); } private void button2_Click(object sender, EventArgs e) { if (pc_point <= 11) { array_pc[pc_c] = array_poke[51 - card]; card += 1; textBox1.Text += "pc[" + pc_c + "]=" + array_pc[pc_c] + "\r\n"; pc_c += 1; } else { Random Rnd = new Random((int)DateTime.Now.Ticks); int rnd = Rnd.Next(0, 10); if (rnd >= 5) { array_pc[pc_c] = array_poke[51 - card]; card += 1; textBox1.Text += "pc[" + pc_c + "]=" + array_pc[pc_c] + "\r\n"; pc_c += 1; } } point(); show_pc(); if (pc_point > 21) { show_pc(); if (user_point > 21) { MessageBox.Show("平手\r\n" + "User:" + user_point.ToString() + " PC:" + pc_point.ToString()); button2.Enabled = false; pictureBox11.Enabled = false; } else { MessageBox.Show("贏了\r\n" + "User:" + user_point.ToString() + " PC:" + pc_point.ToString()); button2.Enabled = false; pictureBox11.Enabled = false; } } else if (user_point > 21) { show_pc(); MessageBox.Show("輸惹\r\n" + "User:" + user_point.ToString() + " PC:" + pc_point.ToString()); button2.Enabled = false; pictureBox11.Enabled = false; } else if (user_point == 21) { if (pc_point == 21) { MessageBox.Show("平手\r\n" + "User:" + user_point.ToString() + " PC:" + pc_point.ToString()); button2.Enabled = false; pictureBox11.Enabled = false; } else { MessageBox.Show("贏了\r\n" + "User:" + user_point.ToString() + " PC:" + pc_point.ToString()); button2.Enabled = false; pictureBox11.Enabled = false; } } else if (pc_point == 21) { MessageBox.Show("輸惹\r\n" + "User:" + user_point.ToString() + " PC:" + pc_point.ToString()); button2.Enabled = false; pictureBox11.Enabled = false; } else if (user_point == pc_point) { MessageBox.Show("平手\r\n" + "User:" + user_point.ToString() + " PC:" + pc_point.ToString()); button2.Enabled = false; pictureBox11.Enabled = false; } else { if (user_point > pc_point) { MessageBox.Show("贏了\r\n" + "User:" + user_point.ToString() + " PC:" + pc_point.ToString()); button2.Enabled = false; pictureBox11.Enabled = false; } else { MessageBox.Show("輸惹\r\n" + "User:" + user_point.ToString() + " PC:" + pc_point.ToString()); button2.Enabled = false; pictureBox11.Enabled = false; } } } private void button3_Click(object sender, EventArgs e) { start(); } private void pictureBox11_Click(object sender, EventArgs e) { pictureBox12.Location = pictureBox11.Location; pictureBox12.Visible = true; timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { if (pictureBox12.Top > 250) { pictureBox12.Visible = false; timer1.Enabled = false; more_one(); } pictureBox12.Top += 10; } private void timer2_Tick(object sender, EventArgs e) { if (pictureBox12.Top < 20) { pictureBox12.Visible = false; timer2.Enabled = false; } pictureBox12.Top -= 10; } } }
2012年8月1日 星期三
訂閱:
張貼留言 (Atom)
0 意見:
張貼留言