- •Оглавление
- •Введение
- •1.Теоретическая часть
- •1.1.Объектно-ориентированное программирование
- •1.2. Инкапсуляция
- •1.3. Полиморфизм
- •1.4.Наследование
- •1.5. Абстрагирование
- •1.6.Статические классы и методы
- •1.7.Виртуальные функции и чистые виртуальные функции
- •1.8. Множественное наследование. Интерфейсы
- •2. Практическая часть
- •2.1. Постановка задачи
- •2.2.Программирование задачи
- •2.3. Результаты работы
- •3.Заключение
- •Листинг программы Файл Form1.Cs
- •Файл Ships.Cs
- •Файл Places.Cs
- •Файл ShipAi.Cs
- •Список литературы
2.3. Результаты работы
Рис1. При запуске игры.
Рис2. Расстановка кораблей.
Рис3.Атака-промах.
3.Заключение
После проделанной работы была освоена технология объектно-ориентированного программирования. В ходе работы была создана компьютерная игра, работающей на платформе .NETFramework. Были приобретены навыки работы вVisualStudio,WinForms, а так же был изучен объектно-ориентированный язык С#.
Листинг программы Файл 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.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
static int count = 9;
static int count_2 = 23;
//==================================================================================================================
Places map = new Places();
Places map_AI = new Places();
Ships ship_4 = new Ships(4);
Ships ship_3 = new Ships(3);
Ships ship_2 = new Ships(2);
Ships ship_2_1 = new Ships(2);
Ships ship_2_2 = new Ships(2);
Ships ship_1 = new Ships(1);
Ships ship_1_1 = new Ships(1);
Ships ship_1_2 = new Ships(1);
Ships ship_1_3 = new Ships(1);
//==================================================================================================================
ShipAI shipAI_4 = new ShipAI(4);
ShipAI shipAI_3 = new ShipAI(3);
ShipAI shipAI_2 = new ShipAI(2);
ShipAI shipAI_2_1 = new ShipAI(2);
ShipAI shipAI_2_2 = new ShipAI(2);
ShipAI shipAI_1 = new ShipAI(1);
ShipAI shipAI_1_1 = new ShipAI(1);
ShipAI shipAI_1_2 = new ShipAI(1);
ShipAI shipAI_1_3 = new ShipAI(1);
//===================================================================================================================
public void enter_coordinates(Ships ship)
{
label1.Text = "";
ship.coordX = Convert.ToInt32(textBox1.Text);
ship.coordY = Convert.ToInt32(textBox2.Text);
if (comboBox1.Text == "Вертикально")
{
ship.vert(map);
print(label1, map);
}
else
{
ship.fight_places(map);
print(label1, map);
}
}
//===================================================================================================================
public Form1()
{
InitializeComponent();
}
//===================================================================================================================
private void Form1_Load(object sender, EventArgs e)
{
map.create();
map_AI.create();
comboBox1.Items.Add("Вертикально");
comboBox1.Items.Add("Горизонтально");
print(label1, map);
print(label2, map_AI);
}
//===================================================================================================================
private void label3_Click(object sender, EventArgs e)
{
}
//===================================================================================================================
private void label1_Click(object sender, EventArgs e)
{
}
//===================================================================================================================
private void button1_Click(object sender, EventArgs e)
{
enter_for_AI();
//while(count_2!=0)
//{
//}
}
//===================================================================================================================
private void button3_Click(object sender, EventArgs e)
{
switches();
count--;
}
//===================================================================================================================
public void switches()
{
switch (count)
{
case 9: enter_coordinates(ship_4); break;
case 8: enter_coordinates(ship_3); break;
case 7: enter_coordinates(ship_2); break;
case 6: enter_coordinates(ship_2_1); break;
case 5: enter_coordinates(ship_2_2); break;
case 4: enter_coordinates(ship_1); break;
case 3: enter_coordinates(ship_1_1); break;
case 2: enter_coordinates(ship_1_2); break;
case 1: enter_coordinates(ship_1_3); break;
}
}
//===================================================================================================================
private void textBox1_TextChanged(object sender, EventArgs e)
{ }
//===================================================================================================================
private void button2_Click(object sender, EventArgs e)
{
Close();
}
//===================================================================================================================
static void print(Label l, Places p)
{
for (int i = 1; i < 11; i++)
{
for (int j = 0; j < 11; j++)
{
l.Text += Convert.ToString(p.map[i, j]) + " ";
}
}
}
//===================================================================================================================
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
//===================================================================================================================
private void button5_Click(object sender, EventArgs e)
{
textBox1.ClearUndo();
textBox1.Text = "";
textBox2.ClearUndo();
textBox2.Text = "";
}
//===================================================================================================================
private void button4_Click(object sender, EventArgs e)
{
attack(map_AI);
}
//===================================================================================================================
public virtual void attack(Places p)
{
int a, b;
a= Convert.ToInt32(textBox1.Text);
b = Convert.ToInt32(textBox2.Text);
for (int i = a; i < (a + 1); i++)
{
for (int j = b; j < (b + 1); j++)
{
if (p.map[i, j] == Convert.ToChar("X"))
{
MessageBox.Show("Попадание!", "Попадание", MessageBoxButtons.OK, MessageBoxIcon.Information);
count_2--;
attack(p);
}
else
{
MessageBox.Show("Промах!", "Промах", MessageBoxButtons.OK, MessageBoxIcon.Stop); break;
}
}
}
}
//===================================================================================================================
/// <summary>
///
/// </summary>
public void enter_for_AI()
{
shipAI_4.fight_places(map_AI);
shipAI_3.fight_places(map_AI);
shipAI_2.fight_places(map_AI);
shipAI_2_1.fight_places(map_AI);
shipAI_2_2.fight_places(map_AI);
shipAI_1.fight_places(map_AI);
shipAI_1_1.fight_places(map_AI);
shipAI_1_2.fight_places(map_AI);
}
}
}