Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 mutual_guess
- {
- public partial class Form1 : Form
- {
- char[] player1Value = new char[4];
- char[] player2Value = new char[4];
- int flow = 0, turns = 1;
- public Form1()
- {
- InitializeComponent();
- }
- private void player1Button_Click(object sender, EventArgs e)
- {
- switch (flow)
- {
- case 0:
- if (player1Input.Text == "")
- {
- MessageBox.Show("請輸入名稱!");
- }
- else
- {
- player1Name.Text = player1Input.Text;
- player1Input.Text = "";
- player1Input.Enabled = false;
- player1Button.Enabled = false;
- player2Input.Enabled = true;
- player2Button.Enabled = true;
- post.Text = "請輸入玩家2名稱";
- player1Input.MaxLength = 4;
- player1Input.PasswordChar = '*';
- }
- break;
- case 1:
- if (player1Input.Text.ToCharArray().Length < 4)
- {
- MessageBox.Show("請輸入四位數!");
- }
- else
- {
- player1Value = player1Input.Text.ToCharArray();
- player1Output.Text = "****";
- player1Input.Text = "";
- player1Input.Enabled = false;
- player1Button.Enabled = false;
- player2Input.Enabled = true;
- player2Button.Enabled = true;
- player1Input.PasswordChar = '\0';
- post.Text = "請" + player2Name.Text + "輸入厲害的數字(不要給對手知道!)";
- }
- break;
- case 2:
- if (player1Input.Text.ToCharArray().Length < 4)
- {
- MessageBox.Show("請輸入四位數!");
- }
- else
- {
- var tmp = player1Input.Text.ToCharArray();
- int[] output = new int[tmp.Length];
- for (int i = 0; i < tmp.Length; i++)
- {
- output[i] = Math.Abs(tmp[i] - player2Value[i]);
- }
- player1Output2.Text = "你剛剛猜:" + player1Input.Text + " 差:" + string.Concat(output);
- if (string.Concat(output) == "0000")
- {
- player1Input.Enabled = false;
- player1Button.Enabled = false;
- player1Output.Text = string.Concat(player1Value);
- player2Output.Text = string.Concat(player2Value);
- MessageBox.Show(player1Name.Text + "獲勝!" + "\n點擊確認再來一局!");
- flow = 0;
- turns = 1;
- player1Input.MaxLength = 20;
- player2Input.MaxLength = 20;
- player1Input.Enabled = true;
- player1Button.Enabled = true;
- player1Name.Text = player1Output.Text = player1Output2.Text = player1Input.Text =
- player2Name.Text = player2Output.Text = player2Output2.Text = player2Input.Text = "";
- post.Text = "請輸入玩家1名稱";
- }
- else
- {
- player1Input.Enabled = false;
- player1Button.Enabled = false;
- player2Input.Enabled = true;
- player2Button.Enabled = true;
- player2Input.Text = "";
- turns++;
- post.Text = player2Name.Text + "的回合(第" + turns + "回合)";
- }
- }
- break;
- default:
- break;
- }
- }
- private void player2Button_Click(object sender, EventArgs e)
- {
- switch (flow)
- {
- case 0:
- if (player2Input.Text == "")
- {
- MessageBox.Show("請輸入名稱!");
- }
- else
- {
- player2Name.Text = player2Input.Text;
- player2Input.Text = "";
- player2Input.Enabled = false;
- player2Button.Enabled = false;
- player1Input.Enabled = true;
- player1Button.Enabled = true;
- player2Input.MaxLength = 4;
- flow++;
- player2Input.PasswordChar = '*';
- post.Text = "請" + player1Name.Text + "輸入厲害的數字(不要給對手知道!)";
- }
- break;
- case 1:
- if (player2Input.Text.ToCharArray().Length < 4)
- {
- MessageBox.Show("請輸入四位數!");
- }
- else
- {
- player2Value = player2Input.Text.ToCharArray();
- player2Output.Text = "****";
- player2Input.Text = "";
- player2Input.Enabled = false;
- player2Button.Enabled = false;
- player1Input.Enabled = true;
- player1Button.Enabled = true;
- player2Input.PasswordChar = '\0';
- post.Text = player1Name.Text + "的回合(第" + turns + "回合)";
- flow++;
- }
- break;
- case 2:
- if (player2Input.Text.ToCharArray().Length < 4)
- {
- MessageBox.Show("請輸入四位數!");
- }
- else
- {
- var tmp = player2Input.Text.ToCharArray();
- int[] output = new int[tmp.Length];
- for (int i = 0; i < tmp.Length; i++)
- {
- output[i] = Math.Abs(tmp[i] - player1Value[i]);
- }
- player2Output2.Text = "你剛剛猜:" + player2Input.Text + " 差:" + string.Concat(output);
- if (string.Concat(output) == "0000")
- {
- player2Input.Enabled = false;
- player2Button.Enabled = false;
- player1Output.Text = string.Concat(player1Value);
- player2Output.Text = string.Concat(player2Value);
- MessageBox.Show(player2Name.Text + "獲勝!" + "\n點擊確認再來一局!");
- flow = 0;
- turns = 1;
- player1Input.MaxLength = 20;
- player2Input.MaxLength = 20;
- player1Input.Enabled = true;
- player1Button.Enabled = true;
- player1Name.Text = player1Output.Text = player1Output2.Text = player1Input.Text =
- player2Name.Text = player2Output.Text = player2Output2.Text = player2Input.Text = "";
- post.Text = "請輸入玩家1名稱";
- }
- else
- {
- player2Input.Enabled = false;
- player2Button.Enabled = false;
- player1Input.Enabled = true;
- player1Button.Enabled = true;
- player1Input.Text = "";
- turns++;
- post.Text = player1Name.Text + "的回合(第" + turns + "回合)";
- }
- }
- break;
- default:
- break;
- }
- }
- private void player1Input_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (flow == 1 || flow == 2)
- {
- if ((int)e.KeyChar <= 31)
- {
- player1Ouput3.Visible = false;
- return;
- }
- if (!char.IsDigit(e.KeyChar))
- {
- e.Handled = true;
- System.Media.SystemSounds.Beep.Play();
- player1Ouput3.Visible = true;
- return;
- }
- else
- {
- player1Ouput3.Visible = false;
- }
- }
- }
- private void player2Input_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (flow == 1 || flow == 2)
- {
- if ((int)e.KeyChar <= 31)
- {
- player2Ouput3.Visible = false;
- return;
- }
- if (!char.IsDigit(e.KeyChar))
- {
- e.Handled = true;
- System.Media.SystemSounds.Beep.Play();
- player2Ouput3.Visible = true;
- return;
- }
- else
- {
- player2Ouput3.Visible = false;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment