0% found this document useful (0 votes)
56 views23 pages

Ejercicios de Programación Aplicada: Ejercicio 1

The document describes code for a memory game application. It generates random numbers that correspond to images, assigns the images to picture boxes, and includes methods for clicking on pictures to make matches. When two pictures match, the score increases. The code also includes methods for resetting the game, flipping pictures face down, and displaying a message if the user wins by making all matches.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views23 pages

Ejercicios de Programación Aplicada: Ejercicio 1

The document describes code for a memory game application. It generates random numbers that correspond to images, assigns the images to picture boxes, and includes methods for clicking on pictures to make matches. When two pictures match, the score increases. The code also includes methods for resetting the game, flipping pictures face down, and displaying a message if the user wins by making all matches.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Ejercicios de Programación Aplicada

Ejercicio 1

1.1 Código

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 ejercicio24
{
public partial class Form1 : Form
{
int a, b, c, d;
int a1, b1, c1, d1;
int vg = 0, vg1 = 0;
int pulsap = 0;
int n2;
int acierto = 0;
Random r = new Random();
int[] al = new int[4];
int[] al2 = new int[4];
public Form1()
{
InitializeComponent();

}
int xd;

private void pictureBox1_Click(object sender, EventArgs e)


{

pictureBox1.Image = Image.FromFile(Application.StartupPath +
"/"+a+".jpg");
if (pulsap == 0)
{
pulsap++;
pulsa1(a);
vg = 1;
}
else if (pulsap == 1)
{
vg1 = 1;
pulsa2(a);
pulsap = 0;
}
}

private void pictureBox3_Click(object sender, EventArgs e)


{
pictureBox3.Image = Image.FromFile(Application.StartupPath + "/" + c
+ ".jpg");
if (pulsap == 0)
{
pulsap++;
pulsa1(c);
vg = 3;
}
else if (pulsap == 1)
{
vg1 = 3;
pulsa2(c);
pulsap = 0;
}
}

private void pictureBox6_Click(object sender, EventArgs e)


{
pictureBox6.Image = Image.FromFile(Application.StartupPath + "/" + d
+ ".jpg");
if (pulsap == 0)
{
pulsap++;
pulsa1(d);
vg = 6;
}
else if (pulsap == 1)
{
vg1 = 6;
pulsa2(d);
pulsap = 0;
}
}

private void pictureBox5_Click(object sender, EventArgs e)


{
pictureBox5.Image = Image.FromFile(Application.StartupPath + "/" + a1
+ ".jpg");
if (pulsap == 0)
{
pulsap++;
pulsa1(a1);
vg = 5;
}
else if (pulsap == 1)
{
vg1 = 5;
pulsa2(a1);
pulsap = 0;
}
}
private void pictureBox4_Click(object sender, EventArgs e)
{
pictureBox4.Image = Image.FromFile(Application.StartupPath + "/" + c1
+ ".jpg");
if (pulsap == 0)
{
pulsap++;
pulsa1(c1);
vg = 4;
}
else if (pulsap == 1)
{
vg1 = 4;
pulsa2(c1);
pulsap = 0;
}
}

private void pictureBox9_Click(object sender, EventArgs e)


{
pictureBox9.Image = Image.FromFile(Application.StartupPath + "/" + b1
+ ".jpg");
if (pulsap == 0)
{
pulsap++;
pulsa1(b1);

vg = 9;
}
else if (pulsap == 1)
{
vg1 = 9;
pulsa2(b1);
pulsap = 0;
}
}

private void pictureBox8_Click(object sender, EventArgs e)


{
pictureBox8.Image = Image.FromFile(Application.StartupPath + "/" + d1
+ ".jpg");
if (pulsap == 0)
{
pulsap++;
pulsa1(d1);
vg = 8;
}
else if (pulsap == 1)
{
vg1 = 8;
pulsa2(d1);
pulsap = 0;
}
}

private void pictureBox2_Click(object sender, EventArgs e)


{
pictureBox2.Image = Image.FromFile(Application.StartupPath + "/" + b +
".jpg");

if (pulsap == 0)
{
pulsap++;
pulsa1(b);
vg = 2;
}
else if (pulsap == 1) {
vg1 = 2;
pulsa2(b);
pulsap = 0;
}
}

public void reconocer() {


if (a == 1) {

}
}

public void pulsa1(int n) {


n2 = n;
timer1.Enabled = false;
}
public void pulsa2(int m) {
if (m == n2)
{
timer1.Enabled = false;
acierto++;
label2.Text = acierto.ToString();
if (acierto >= 4)
{

MessageBox.Show("Felicidades! GANASTE");
label2.Text = "0";
volteartodo();
reiniciar();

}
}
else
{
//ventanas a sus lugares
timer1.Enabled = true;
}
}

private void timer1_Tick(object sender, EventArgs e)


{
voltear(vg1);
voltear(vg);
}

private void label2_Click(object sender, EventArgs e)


{

public void generar() {

int i = 0;

al[i] = r.Next(1, 5);

for (i = 1; i < 4; i++)


{
al[i] = r.Next(1, 5);
for (int j = 0; j < i; j++)
{
if (al[i] == al[j])
{
i--;
}
}
}

int z = 0;

al2[z] = r.Next(1, 5);

for (z = 1;z < 4; z++)


{
al2[z] = r.Next(1, 5);
for (int j = 0; j < z; j++)
{
if (al2[z] == al2[j])
{
z--;
}
}
}

private void Form1_Load(object sender, EventArgs e)


{

generar();
asignar();
volteartodo();

private void button1_Click(object sender, EventArgs e)


{
reiniciar();
acierto = 0;
}

public void volteartodo() {


pictureBox1.Image = Image.FromFile(Application.StartupPath +
"/q.jpg");
pictureBox2.Image = Image.FromFile(Application.StartupPath +
"/q.jpg");
pictureBox3.Image = Image.FromFile(Application.StartupPath +
"/q.jpg");
pictureBox4.Image = Image.FromFile(Application.StartupPath +
"/q.jpg");
pictureBox5.Image = Image.FromFile(Application.StartupPath +
"/q.jpg");
pictureBox6.Image = Image.FromFile(Application.StartupPath +
"/q.jpg");
pictureBox7.Image = Image.FromFile(Application.StartupPath +
"/q.jpg");
pictureBox8.Image = Image.FromFile(Application.StartupPath +
"/q.jpg");
pictureBox9.Image = Image.FromFile(Application.StartupPath +
"/q.jpg");
}

public void voltear(int v)


{
switch (v)
{
case 0: break;
case 1: pictureBox1.Image =
Image.FromFile(Application.StartupPath + "/q.jpg"); break;
case 2: pictureBox2.Image =
Image.FromFile(Application.StartupPath + "/q.jpg"); break;
case 3: pictureBox3.Image =
Image.FromFile(Application.StartupPath + "/q.jpg"); break;
case 4: pictureBox4.Image =
Image.FromFile(Application.StartupPath + "/q.jpg"); break;
case 5: pictureBox5.Image =
Image.FromFile(Application.StartupPath + "/q.jpg"); break;
case 6: pictureBox6.Image =
Image.FromFile(Application.StartupPath + "/q.jpg"); break;
case 9: pictureBox9.Image =
Image.FromFile(Application.StartupPath + "/q.jpg"); break;
case 8: pictureBox8.Image =
Image.FromFile(Application.StartupPath + "/q.jpg"); break;
}
}

public void asignar() {


a = al[0];
b = al[1];
c = al[2];
d = al[3];
a1 = al2[0];
b1 = al2[1];
c1 = al2[2];
d1 = al2[3];
}

public void reiniciar() {


volteartodo();
generar();
asignar();
acierto = 0;
}
}
}
2. Prueba
2.Ejercicio 2

2.1 Código

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 Calle_peatona
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}
Boolean m;
int s = 10;
private void Form1_Load(object sender, EventArgs e)
{
label2.ForeColor = Color.Red;
pictureBox6.BackColor = Color.Red;
label1.Text = "";
label2.Text = "";
pictureBox3.BackColor = Color.White;
timer3.Enabled = true;
pictureBox2.BackColor = Color.Green;
timer2.Enabled = true;
timer2.Interval = timer2.Interval * s;
timer1.Enabled = true;
timer4.Interval = timer4.Interval * (s-1);
timer4.Enabled = true;
label3 = label2;
timer5.Interval = timer5.Interval * 2;
timer5.Enabled = true;
}

private void pictureBox1_Click(object sender, EventArgs e)


{

private void timer2_Tick(object sender, EventArgs e)


{

if (pictureBox2.BackColor == Color.Green)
{

label1.ForeColor = Color.Red;

pictureBox2.BackColor = Color.Black;
pictureBox4.BackColor = Color.Red;
}
else if (pictureBox4.BackColor == Color.Red) {
label1.ForeColor = Color.Green;

pictureBox2.BackColor = Color.Green;
pictureBox4.BackColor = Color.Black;
}
}

private void label1_Click(object sender, EventArgs e)


{

}
int co = 1;
int co2 = 0;
private void timer3_Tick(object sender, EventArgs e)
{
if (co < s|co2<s)
{
if (pictureBox2.BackColor == Color.Green)
{
co++;
co2 = 0;
label1.Text = Convert.ToString(s - co+1);
label2.Visible = false;
label1.Visible = true;
}
else if (pictureBox4.BackColor == Color.Red)
{
label1.Visible = false;
label2.Visible = true;
co2++;
co = 0;
label2.Text = Convert.ToString(s - co2+1);
}
}
}
int sw;
Boolean cru;
private void timer1_Tick(object sender, EventArgs e)
{
if (m == true)
{
if (cru == true)
{
if (sw == 0)
{
if (pictureBox5.Left + pictureBox5.Width <= 670)
{
pictureBox5.Left = pictureBox5.Left + 50;
}
else { sw = 1; cru = false; }
}
else if (cru == true)
{
if
(pictureBox5.Left >= 50)
{
pictureBox5.Left = pictureBox5.Left - 50;
}
else
{
sw = 0; cru = false;
}
}
}
}
else { }

private void pictureBox5_Click(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
if (pictureBox2.BackColor == Color.Green)
{
cru = true;
m = true;
}
if (pictureBox4.BackColor == Color.Red)
{
MessageBox.Show("ALTO! está en rojo");
cru = false;
m = false;
}
}

private void pictureBox6_Click(object sender, EventArgs e)


{

}
int co3=0;
int coa3 = 0;
int sui;
private void timer4_Tick(object sender, EventArgs e)
{

if (pictureBox8.BackColor == Color.Green)
{

pictureBox8.BackColor = Color.Black;
pictureBox7.BackColor = Color.Orange;
sui = 0;

}
else if (pictureBox6.BackColor == Color.Red)
{

sui = 1;
pictureBox7.BackColor = Color.Orange;
pictureBox6.BackColor = Color.Black;
}

private void timer5_Tick(object sender, EventArgs e)


{
if (pictureBox7.BackColor == Color.Orange)
{
if (sui == 0)
{
pictureBox7.BackColor = Color.Black;
pictureBox6.BackColor = Color.Red;
}

else if (sui == 1)
{
pictureBox7.BackColor = Color.Black;
pictureBox8.BackColor = Color.Green;
}
}

}
}
}

2.2 Prueba
3.Ejercicio

3.1 Código
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 PROYECTO_PING_PONG
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int VELOCIDAD = 15;
int PUNTAJE = 0;

bool ARRIBA;
bool IZQUIERDA;

private void timer1_Tick(object sender, EventArgs e)


{
if (pictureBox1.Top > pictureBox2.Top) // CUANDO SE PIERDE
{
timer1.Enabled = false;
MessageBox.Show("SU PUNTAJE ES: " + PUNTAJE.ToString());
MessageBox.Show("PERDIÓ");
this.Close();
}
//REBOTE DE LA PELOTA
if(pictureBox1.Left+pictureBox1.Width>=pictureBox2.Left &&
//VERIFICA DENTRO DEL RANGO DE IZQUIERDA
pictureBox1.Left+pictureBox1.Width<=pictureBox2.Left +
pictureBox2.Width && // dentro del rangoderecho
pictureBox1.Top+pictureBox1.Height>=pictureBox2.Top && //VERIFICA
QUE NO PASE DE LA PARTE DE ARRIBA
pictureBox1.Top +pictureBox1.Height<=pictureBox2.Top+
pictureBox2.Height) //VERIFICA QUE NO PASE DE LA PARTE DE ABAJODE LA IMG.2
{
ARRIBA = false;
PUNTAJE += 1;
this.Text = "PUNTACIÓN: " + PUNTAJE.ToString() + "";

}
if (PUNTAJE >= 30 && PUNTAJE <31)
{
timer1.Enabled = false;
MessageBox.Show("SU PUNTAJE ES: " + PUNTAJE.ToString());
MessageBox.Show("GANASTE");
this.Close();
}

if (pictureBox1.Top <= 0) //SI PEGA PARED DE ARRIBA


{
ARRIBA = true;
}
if (pictureBox1.Left >= this.Width - 40)//SI PEGA PARED DE la derecha
{
IZQUIERDA = false;
}
if (pictureBox1.Left <= 0) //SI PEGA PARED DE la izquierda
{
IZQUIERDA = true;
}

//MOVIMIENTO DE LA PELOTA.
if (IZQUIERDA==true)
{
pictureBox1.Left += VELOCIDAD; //VA PARA LA DERECHA
}
else
{
pictureBox1.Left -= VELOCIDAD;// VA PARA LA IZQUIERDA
}
if (ARRIBA==true)
{
pictureBox1.Top += VELOCIDAD; //VA PARA ARRIBA
}
else
{
pictureBox1.Top -= VELOCIDAD;// VA PARA ABAJO
}

private void Form1_MouseMove(object sender, MouseEventArgs e)


{
pictureBox2.Left = e.X;
}

private void Form1_Load(object sender, EventArgs e)


{
this.Text = "PUNTAJE: 0";
Random aleatorio = new Random(); //para que mueva a cualquier lado
pic. 1
pictureBox1.Location = new Point(aleatorio.Next(this.Height), 0);
ARRIBA = true;
IZQUIERDA = true;
timer1.Enabled = true;
PUNTAJE = 0;
}
}
}
4. Ejercicio 4

4.1 Código

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _3enraya
{
class analizar
{
public int v1=2, v2=9, v3=9, v4=9, v5=9, v6=9, v7=9, v8=9, v9=9;
public Boolean ganador = false; public int g;
public analizar(int v1, int v2, int v3, int v4, int v5, int v6, int v7,
int v8, int v9) {
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
this.v4 = v4;
this.v5 = v5;
this.v6 = v6;
this.v7 = v7;
this.v8 = v8;
this.v9 = v9;
m1();
m2();
m3();
m4();
m5();
m6();
m7();
m8();
}

public void comprobar() {

public void m1() {


int c = 0;
int c1 = 0;

if (v7 == 1) {
c++;
}
if (v5 == 1) {
c++;
}
if (v3 == 1) {
c++;
}
if (c==3) {
ganador = true;
g = 1;
}
if (v7 == 0)
{
c1++;
}
if (v5 == 0)
{
c1++;
}
if (v3 == 0)
{
c1++;
}
if (c1 == 3)
{
ganador = true;
g = 0;

}
}

public void m2() {


int c = 0;
int c1 = 0;

if (v1 == 1)
{
c++;
}
if (v5 == 1)
{
c++;
}
if (v9 == 1)
{
c++;
}
if (c == 3)
{
ganador = true;
g = 1;
}

if (v1 == 0)
{
c1++;
}
if (v5 == 0)
{
c1++;
}
if (v9 == 0)
{
c1++;
}
if (c1 == 3)
{
ganador = true;
g = 0;

}
}

public void m3() {


int c = 0;
int c1 = 0;

if (v1 == 1)
{
c++;
}
if (v2 == 1)
{
c++;
}
if (v3 == 1)
{
c++;
}
if (c == 3)
{
ganador = true;
g = 1;
}

if (v1 == 0)
{
c1++;
}
if (v2 == 0)
{
c1++;
}
if (v3 == 0)
{
c1++;
}
if (c1 == 3)
{
ganador = true;
g = 0;

}
}

public void m4() {


int c = 0;
int c1 = 0;

if (v4 == 1)
{
c++;
}
if (v5 == 1)
{
c++;
}
if (v6 == 1)
{
c++;
}
if (c == 3)
{
ganador = true;
g = 1;
}

if (v4 == 0)
{
c1++;
}
if (v5 == 0)
{
c1++;
}
if (v6 == 0)
{
c1++;
}
if (c1 == 3)
{
ganador = true;
g = 0;

}
}

public void m5() {


int c = 0;
int c1 = 0;

if (v7 == 1)
{
c++;
}
if (v8 == 1)
{
c++;
}
if (v9 == 1)
{
c++;
}
if (c == 3)
{
ganador = true;
g = 1;
}

if (v7 == 0)
{
c1++;
}
if (v8 == 0)
{
c1++;
}
if (v9 == 0)
{
c1++;
}
if (c1 == 3)
{
ganador = true;
g = 0;

}
}

public void m6() {


int c = 0;
int c1 = 0;

if (v2 == 1)
{
c++;
}
if (v5 == 1)
{
c++;
}
if (v8 == 1)
{
c++;
}
if (c == 3)
{
ganador = true;
g = 1;
}

if (v2 == 0)
{
c1++;
}
if (v5 == 0)
{
c1++;
}
if (v8 == 0)
{
c1++;
}
if (c1 == 3)
{
ganador = true;
g = 0;

}
}

public void m7() {


int c = 0;
int c1 = 0;

if (v1 == 1)
{
c++;
}
if (v4 == 1)
{
c++;
}
if (v7 == 1)
{
c++;
}
if (c == 3)
{
ganador = true;
g = 1;
}

if (v1 == 0)
{
c1++;
}
if (v4 == 0)
{
c1++;
}
if (v7 == 0)
{
c1++;
}
if (c1 == 3)
{
ganador = true;
g = 0;

}
}
public void m8() {
int c = 0;
int c1 = 0;

if (v3 == 1)
{
c++;
}
if (v6 == 1)
{
c++;
}
if (v9 == 1)
{
c++;
}
if (c == 3)
{
ganador = true;
g = 1;
}

if (v3 == 0)
{
c1++;
}
if (v6 == 0)
{
c1++;
}
if (v9 == 0)
{
c1++;
}
if (c1 == 3)
{
ganador = true;
g = 0;

}
}

4.2. Prueba

You might also like