
Tortuga y liebre
Publicado por nopsus (1 intervención) el 25/04/2018 00:30:09
hola solicito su ayuda en este codigo
la idea es Implementar Habrá un thread que implementará la tortuga y otro la liebre. Cada uno se suspenderá durante un segundo y luego evaluará lo que ha pasado según unas probabilidades
tortu 50% rapido
tortu 20% resbalon
liebre 20% duerme
liebre 20% gran salto
liebre 10% resbalon grande
mi codigo que e avanzado es
la idea es Implementar Habrá un thread que implementará la tortuga y otro la liebre. Cada uno se suspenderá durante un segundo y luego evaluará lo que ha pasado según unas probabilidades
tortu 50% rapido
tortu 20% resbalon
liebre 20% duerme
liebre 20% gran salto
liebre 10% resbalon grande
mi codigo que e avanzado es
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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;
using System.Threading;
namespace Carrera_hilos
{
public partial class Form1 : Form
{
delegate void dele(PictureBox pb, int laY, int vel);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//esta linea permite ejecutar varios hilos. Configuración del IDE
CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
//aqui se pintaran las lineas necesarias
Pen lapiz = new Pen(Color.Black, 1); //color y grosor
Graphics pintor = CreateGraphics();
//linea 1,2,3
pintor.DrawLine(lapiz, new Point(12, 75), new Point(500, 75));
pintor.DrawLine(lapiz, new Point(12, 175), new Point(500, 175));
pintor.DrawLine(lapiz, new Point(12, 275), new Point(500, 275));
}
private void btnIniciar_Click(object sender, EventArgs e)
{
btnIniciar.Enabled = false;
Thread[] hilos = new Thread[2];
for (int i = 0; i < hilos.Length; i++)
{
hilos[i] = new Thread(metodo);
hilos[i].Name = "h" + i;
hilos[i].Start();
}
btnIniciar.Enabled = true;
}
public void metodo()
{
//el auto uno es mas rápido. El autoTres es el más lento
dele elDelegado = new dele(mover);
if (Thread.CurrentThread.Name.Equals("h0"))
{
elDelegado.Invoke(tortu, tortu.Location.Y, 20);
}
else if (Thread.CurrentThread.Name.Equals("h1"))
{
elDelegado.Invoke(liebre, liebre.Location.Y, 60);
}
else if (Thread.CurrentThread.Name.Equals("h2"))
}
public void mover(PictureBox pb, int laY, int velocidad)
{
for (int i = 0; i < 450; i++)
{
pb.Location = new Point(i, laY);
//if para control de velocidad
if (Thread.CurrentThread.Name.Equals("h0"))
{
Thread.Sleep(velocidad);
}
else if (Thread.CurrentThread.Name.Equals("h1"))
{
Thread.Sleep(velocidad);
}
}
}
}
}
Valora esta pregunta


0