Характеристика даних і їх умовні позначення, Текст програми - Розробка гри в С# "Корови та бики"

Mas - змінна типу String, число введене гравцем, як тексту;

Mas1 - массив даних типу char, число, загадане програмою, по одній цифрі у кожному елементі масива;

Mas2 - масив даних типу char, число, введене гравцем, по одній цифрі у кожному елементі масива;

Rnd - змінна типу int, число від 1000 до 9999, загаданого програмою;

Korovy - змінна типу int, кількість "корів" для певного варіанту числа;

Byky - змінна типу int, кількість "биків" для певного варіанту числа;

Tries - змінна типу int, кількість спроб гравця вгадати число;

I - змінна типу int, лічильник ітерацій циклу;

J - змінна типу int, лічильник ітерацій циклу

Текст програми

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. Windows. Forms;

Namespace Cowbulls

{

Public partial class Form1 : Form

{

String mas;

Int rnd, korovy=0, byky=0,tries=1;

Char[] mas1 = { '0', '0', '0', '0' };

Char[] mas2 = { '0', '0', '0', '0' };

Public Form1()

{

InitializeComponent();

Rand();

}

Private void button1_Click(object sender, EventArgs e)

{

If (Chislo. Text == mas)

{

MessageBox. Show("Ви вгадали число " + mas + " з " + tries. ToString() + "-ї спроби", "Кінець гри", MessageBoxButtons. OK, MessageBoxIcon. Asterisk);

Tries = 1;

Rand();

Cls();

}

If (tries > 10)

{

MessageBox. Show( "Ви програли!","Кінець гри",MessageBoxButtons. OK, MessageBoxIcon. Stop);

Tries = 1;

Rand();

Cls();

}

If (Chislo. Text!= string. Empty &;&; Chislo. TextLength==4)

{

Result. Text = rnd. ToString();

Mas = Chislo. Text;

Mas2 = mas. ToCharArray();

Byky = 0; korovy = 0;

For (int i = 0; i < 4; i++)

{

For (int j = 0; j < 4; j++)

{

If (mas1[i] == mas2[j])

{

Byky++;

Break;

}

}

For (int j = 0; j < 4; j++)

{

If (mas1[i] == mas2[j] &;&; i == j)

Korovy++;

}

}

Tries. AppendText(Chislo. Text +"("+ byky. ToString()+","+korovy. ToString()+")"+" | ");

Bulls. Text = byky. ToString();

Cows. Text = korovy. ToString();

Chislo. Text = string. Empty;

Tries++;

}

}

Bool flag = true;

Public void Rand()

{

For(int j=0;;j++)

{

Random rand = new Random();

Rnd = rand. Next(1000, 9999);

Mas = rnd. ToString();

Mas1 = mas. ToCharArray();

Flag = true;

For (int i = 1; i < 4; i++)

{

If (mas1[0].Equals(mas[i])==true)

{

Flag = false;

Break;

}

}

For (int i = 2; i < 4; i++)

{

If (mas1[1].Equals(mas[i])==true)

{

Flag=false;

Break;

}

}

If (mas1[2].Equals(mas[3])==true)

{

Flag=false;

}

If (flag == true)

{

Break;

}

Else

{

Continue;

}

}

Result. Text = mas;

}

Private void Cls()

{

Chislo. Text = string. Empty;

Cows. Text = string. Empty;

Bulls. Text = string. Empty;

Result. Text = string. Empty;

Tries. Text = string. Empty;

}

Private void NewGame_Click(object sender, EventArgs e)

{

Tries = 1;

Cls();

Rand();

}

Private void Author_Click(object sender, EventArgs e)

{

Form2 author = new Form2();

Author. ShowDialog();

}

Private void button2_Click(object sender, EventArgs e)

{

Form3 rules = new Form3();

Rules. ShowDialog();

}

Private void Chislo_KeyPress(object sender, KeyPressEventArgs e)

{ char c = e. KeyChar;

E. Handled = !(char. IsDigit(c) || c == '');

}

}

}

Похожие статьи




Характеристика даних і їх умовні позначення, Текст програми - Розробка гри в С# "Корови та бики"

Предыдущая | Следующая