I defined two class : 1- robi 2- police .in this thread program police should catch robber.
in Form1.cs I defined array of police object and robi object .compiler say me :
Error 1:
Inconsistent accessibility: field type 'WindowsFormsApplication1.police[]' is less accessible than field 'WindowsFormsApplication1.Form1.pol'
Error 2:
Inconsistent accessibility: field type 'WindowsFormsApplication1.robi[]' is less accessible than field 'WindowsFormsApplication1.Form1.rob'
All thing in form1.cs[design] is public.
this in my code:
police class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
class police
{
public int number;
public int x;
public int y;
public police(int X, int Y, int num)
{
var form = Form.ActiveForm as Form1;
x = X;
y = Y;
number = num;
}
public void Move()
{
bool bol = true;
int target = 0;
Random rand = new Random();
var form = Form.ActiveForm as Form1;
for (int i = 0; i < Convert.ToInt32(form.textBox2.Text); i++)
{
bol = bol && form.rob[i].status;
}
while(bol == true)
{
do
{
target = rand.Next(0,Convert.ToInt32(form.textBox2.Text));
}
while (form.rob[target].status == false);
while ( true)
{
if (Math.Abs(x - form.rob[target].x) < 3 && Math.Abs(y - form.rob[target].y) < 3)
{
form.rob[target].status = false;
break;
}
if (x < form.rob[target].x)
{
x++;
}
if (x > form.rob[target].x)
{
x--;
}
if (y < form.rob[target].y)
{
y++;
}
if (y > form.rob[target].y)
{
y--;
}
}
for (int i = 0; i < Convert.ToInt32(form.textBox2.Text); i++)
{
bol = bol && form.rob[i].status;
}
}
}
}
}
robi class:
<pre lang="c#">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
namespace WindowsFormsApplication1
{
class robi
{
public int x;
public int y;
public int number;
public bool status;
public robi(int X, int Y, int num)
{
status = true;
x = X;
y = Y;
number = num;
}
public void Run()
{
}
}
}
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;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public string[,] map;
public police []pol;
public robi[] rob;
Bitmap map_bit;
Graphics Map;
Pen policepen;
Pen robipen;
Thread showmap;
Thread[] policeman;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
map = new string[pictureBox1.Size.Width, pictureBox1.Size.Height];
map_bit = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
Map = Graphics.FromImage(map_bit);
policepen = new Pen(Color.Blue);
robipen = new Pen(Color.Red);
for (int i = 0; i < pictureBox1.Size.Width; i++)
{
for (int j = 0; j < pictureBox1.Size.Height; j++)
{
map[i, j] = " ";
}
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Point coordinate = new Point();
Random rand = new Random();
pol = new police[Convert.ToInt32(textBox1.Text)];
for (int i = 0; i < Convert.ToInt32(textBox1.Text); i++)
{
coordinate.X = rand.Next(0, 400);
coordinate.Y = rand.Next(0, 400);
pol[i] = new police(coordinate.X, coordinate.Y,i);
map[coordinate.X, coordinate.Y] = "P" + Convert.ToString(pol[i].number);
}
rob = new robi[Convert.ToInt32(textBox2.Text)];
for (int i = 0; i < Convert.ToInt32(textBox2.Text); i++)
{
coordinate.X = rand.Next(0, 400);
coordinate.Y = rand.Next(0, 400);
rob[i] = new robi(coordinate.X, coordinate.Y, i);
map[coordinate.X, coordinate.Y] = "R" + Convert.ToString(rob[i].number);
}
RUN();
}
public void convertMapToImg()
{
string temp = "";
char ch;
bool bol = true;
int p;
for (int i = 0; i < Convert.ToInt32(textBox2.Text); i++)
{
bol = bol && rob[i].status;
}
while(bol)
{
for (int i = 0; i < pictureBox1.Size.Width; i++)
{
for (int j = 0; j < pictureBox1.Size.Height; j++)
{
temp = map[i, j];
ch = temp[0];
if (ch == 'P')
{
ch = temp[1];
p = ch - 48;
Map.DrawEllipse(policepen, pol[p].x, pol[p].y,10,10);
}
ch = temp[0];
if (ch == 'R')
{
ch = temp[1];
p = ch - 48;
Map.DrawEllipse(robipen, rob[p].x, rob[p].y, 10, 10);
}
}
}
pictureBox1.Image = map_bit;
for (int i = 0; i < Convert.ToInt32(textBox2.Text); i++)
{
bol = bol && rob[i].status;
}
}
}
public void RUN()
{
showmap = new Thread(this.convertMapToImg);
showmap.Start();
policeman = new Thread[Convert.ToInt32(textBox1.Text)];
for (int i = 0; i <Convert.ToInt32(textBox1.Text) ; i++)
{
policeman[i] = new Thread(pol[i].Move);
policeman[i].Start();
}
}
}
}