Skip to content

Instantly share code, notes, and snippets.

@tedliou
Created December 12, 2015 09:46
Show Gist options
  • Save tedliou/0722b119214d882743be to your computer and use it in GitHub Desktop.
Save tedliou/0722b119214d882743be to your computer and use it in GitHub Desktop.
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;
using System.Data.SqlClient;
namespace 樂透
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//宣告全域
int allscore = 0;
private void button1_Click(object sender, EventArgs e)
{
int ch = 0;
int[] tbarray = new int[] { int.Parse(nu1.Text), int.Parse(nu2.Text), int.Parse(nu3.Text), int.Parse(nu4.Text), int.Parse(nu5.Text), int.Parse(sp1.Text) };
//檢查數字範圍輸入錯誤
if (tbarray[0] == tbarray[1] || tbarray[0] == tbarray[2] || tbarray[0] == tbarray[3] || tbarray[0] == tbarray[4] || tbarray[1] == tbarray[2] || tbarray[1] == tbarray[3] || tbarray[1] == tbarray[4] || tbarray[2] == tbarray[3] || tbarray[2] == tbarray[4] || tbarray[3] == tbarray[4])
{
label13.Text = "數值重複";
ch -= 2;
}
if (tbarray[0] <= 0 || tbarray[0] > 20 || tbarray[1] <= 0 || tbarray[1] > 20 || tbarray[2] <= 0 || tbarray[2] > 20 || tbarray[3] <= 0 || tbarray[3] > 20 || tbarray[4] <= 0 || tbarray[4] > 20 || tbarray[5] <= 0 || tbarray[5] > 8)
{
label13.Text = "範圍錯誤";
ch -= 2;
}
if (ch == -4)
{
label13.Text = "選號錯誤";
}
if (ch==0)
{
//開始抽獎
nu1.Enabled = false;
nu2.Enabled = false;
nu3.Enabled = false;
nu4.Enabled = false;
nu5.Enabled = false;
sp1.Enabled = false;
op.Enabled = false;
op.Text = "選號完成";
startapp.Enabled = true;
button3.Enabled = true;
startapp.Text = "抽獎!";
button3.Text = "重新選號";
label13.Text = "抽獎時間!";
}
}
private void button2_Click(object sender, EventArgs e)
{
int[] randomize = new int[5];
Random rnd = new Random();
for (int i = 0; i < 5; i++)
{
//將1~20亂數依序放進陣列randomize中
randomize[i] = rnd.Next(1, 21);
for (int j = 0; j < i; j++)
{
// 檢查是否有重複的亂數,如果有就重新產生
while (randomize[j] == randomize[i])
{
j = 0;
randomize[i] = rnd.Next(1, 21);
}
}
//顯示中獎亂數號碼
label1.Text = randomize[0].ToString("00");
label2.Text = randomize[1].ToString("00");
label3.Text = randomize[2].ToString("00");
label4.Text = randomize[3].ToString("00");
label5.Text = randomize[4].ToString("00");
label11.Text = rnd.Next(1, 9).ToString("00");
}
//比對號碼,計分
string BRecord = "";
if (nu1.Text.PadLeft(2, '0') == label1.Text)
{
allscore += 3;
BRecord = nu1.Text.PadLeft(2, '0') + ",";
}
if (nu2.Text.PadLeft(2, '0') == label2.Text)
{
allscore += 3;
BRecord = BRecord + nu2.Text.PadLeft(2, '0') + ",";
}
if (nu3.Text.PadLeft(2, '0') == label3.Text)
{
allscore += 3;
BRecord = BRecord + nu3.Text.PadLeft(2, '0') + ",";
}
if (nu4.Text.PadLeft(2, '0') == label4.Text)
{
allscore += 3;
BRecord = BRecord + nu4.Text.PadLeft(2, '0') + ",";
}
if (nu5.Text.PadLeft(2, '0') == label5.Text)
{
allscore += 3;
BRecord = BRecord + nu5.Text.PadLeft(2, '0') + ",";
}
int cost = allscore * 500;
int spcost = 0;
if (sp1.Text == label11.Text)
{
allscore += 1;
spcost = 500;
BRecord = BRecord + sp1.Text;
}
//最後統整
string Record = nu1.Text.PadLeft(2, '0') + "," + nu2.Text.PadLeft(2, '0') + "," + nu3.Text.PadLeft(2, '0') + "," + nu4.Text.PadLeft(2, '0') + "," + nu5.Text.PadLeft(2, '0') + "," + sp1.Text.PadLeft(2, '0');
string ARecord = label1.Text.PadLeft(2, '0') + "," + label2.Text.PadLeft(2, '0') + "," + label3.Text.PadLeft(2, '0') + "," + label4.Text.PadLeft(2, '0') + "," + label5.Text.PadLeft(2, '0') + "," + label11.Text;
//新增到紀錄中
listView1.Items.Insert(0, Record);
listView1.Items[0].SubItems.Add(ARecord);
listView1.Items[0].SubItems.Add(BRecord);
listView1.Items[0].SubItems.Add(allscore.ToString());
listView1.Items[0].SubItems.Add(Convert.ToString(cost + spcost));
int allcose = cost + spcost;
//資料庫
string cmdsql = "INSERT INTO ttt (yn, gn, an, sc, am) VALUES ('" + Record + "','" + ARecord + "','" + BRecord + "','" + allscore + "','" + allcose + "')";
SqlConnection sql = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\liusql.mdf;Integrated Security=True;Connect Timeout=30");
SqlCommand cmd = new SqlCommand(cmdsql, sql);
sql.Open();
cmd.ExecuteNonQuery();
sql.Close();
}
private void button3_Click(object sender, EventArgs e)
{
//重新選號
startapp.Text = "請先選號";
startapp.Enabled = false;
op.Enabled = true;
button3.Enabled = false;
button3.Text = "請先選號";
nu1.Enabled = true;
nu2.Enabled = true;
nu3.Enabled = true;
nu4.Enabled = true;
nu5.Enabled = true;
sp1.Enabled = true;
}
private void button1_Click_1(object sender, EventArgs e)
{
SqlConnection sql = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\liusql.mdf;Integrated Security=True;Connect Timeout=30");
SqlCommand cmd = new SqlCommand("delete from ttt", sql);
sql.Open();
cmd.ExecuteNonQuery();
sql.Close();
listView1.Items.Clear();
MessageBox.Show("資料已順利清除");
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection sql = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\liusql.mdf;Integrated Security=True;Connect Timeout=30");
SqlCommand cmd = new SqlCommand("select * from ttt", sql);
sql.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
listView1.Items.Insert(0, reader[0].ToString());
listView1.Items[0].SubItems.Add(reader[1].ToString());
listView1.Items[0].SubItems.Add(reader[2].ToString());
listView1.Items[0].SubItems.Add(reader[3].ToString());
listView1.Items[0].SubItems.Add(reader[4].ToString());
}
}
reader.Close();
sql.Close();
}
private void button2_Click_1(object sender, EventArgs e)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment