81. Inteligencia Artificial (10). Tres en raya con Minimax
- En este capítulo del curso de programación en c# con visual studio 2017 vamos a continuar con nuestra serie de entradas relacionadas con un tema de moda: la inteligencia artificial. En la entrada anterior vimos la lógica del algortimo minimax necesaria para aplicarla al juego del tres en raya.
- En esta entrada os muestro el inicio de la implementación de nuestro ejemplo. Para hacer este desarrollo hemos creado una aplicación windows forms. El tablero donde calcularemos los movimientos es una matriz, igual que en el ejemplo del ajedrez, aunque bastante más pequeña. Sera una matriz de este tipo: private int[,] matriz = new int[2, 2] .
- Este ejemplo consta de dos clases principales. Una de las clases es la que muestra la representación del juego en pantalla y la otra clase es la que calcula el mejor movimiento de nuestra IA. En el vídeo os lo cuento con más detalles.
- Para finalizar os dejo el código de la clase Forms vista en el vídeo. En la siguiente entrada del blog finalizaremos el ejemplo y os dejare código de la clase que falta y un enlace para descargar el proyecto al completo.
- Form1.Designer.cs:
namespace TresEnRaya
{
partial class Form1
{
/// <summary>
/// Variable del diseñador necesaria.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Limpiar los recursos que se estén usando.
/// </summary>
/// <param name="disposing">true si los recursos administrados se deben desechar; false en caso contrario.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Código generado por el Diseñador de Windows Forms
/// <summary>
/// Método necesario para admitir el Diseñador. No se puede modificar
/// el contenido de este método con el editor de código.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(106, 104);
this.button1.TabIndex = 0;
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button2.Location = new System.Drawing.Point(155, 12);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(106, 104);
this.button2.TabIndex = 1;
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button3.Location = new System.Drawing.Point(302, 12);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(106, 104);
this.button3.TabIndex = 2;
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button4.Location = new System.Drawing.Point(302, 152);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(106, 104);
this.button4.TabIndex = 3;
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button5.Location = new System.Drawing.Point(155, 152);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(106, 104);
this.button5.TabIndex = 4;
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button6.Location = new System.Drawing.Point(12, 152);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(106, 104);
this.button6.TabIndex = 5;
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button7
//
this.button7.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button7.Location = new System.Drawing.Point(302, 290);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(106, 104);
this.button7.TabIndex = 6;
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// button8
//
this.button8.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button8.Location = new System.Drawing.Point(155, 290);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(106, 104);
this.button8.TabIndex = 7;
this.button8.UseVisualStyleBackColor = true;
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// button9
//
this.button9.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button9.Location = new System.Drawing.Point(12, 290);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(106, 104);
this.button9.TabIndex = 8;
this.button9.UseVisualStyleBackColor = true;
this.button9.Click += new System.EventHandler(this.button9_Click);
//
// button10
//
this.button10.Location = new System.Drawing.Point(127, 465);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(197, 38);
this.button10.TabIndex = 9;
this.button10.Text = "Nueva Partida";
this.button10.UseVisualStyleBackColor = true;
this.button10.Click += new System.EventHandler(this.button10_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(449, 31);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(0, 13);
this.label1.TabIndex = 10;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(602, 537);
this.Controls.Add(this.label1);
this.Controls.Add(this.button10);
this.Controls.Add(this.button9);
this.Controls.Add(this.button8);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Tres en raya";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button10;
private System.Windows.Forms.Label label1;
}
}
- Forms.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 TresEnRaya
{
public partial class Form1 : Form
{
TresEnRaya tresEnRaya = new TresEnRaya();
private int[,] matriz = new int[2, 2];
private int ganador = -1;
public Form1()
{
InitializeComponent();
tresEnRaya.inicializarPartida();
matriz = tresEnRaya.Matriz;
}
private void comprobarGanador()
{
int[] ultMov = tresEnRaya.UltimoMovimientoMaquina;
if (ultMov[0] == 0 && ultMov[1] == 0)
button1.Text = "0";
if (ultMov[0] == 0 && ultMov[1] == 1)
button2.Text = "0";
if (ultMov[0] == 0 && ultMov[1] == 2)
button3.Text = "0";
if (ultMov[0] == 1 && ultMov[1] == 0)
button6.Text = "0";
if (ultMov[0] == 1 && ultMov[1] == 1)
button5.Text = "0";
if (ultMov[0] == 1 && ultMov[1] == 2)
button4.Text = "0";
if (ultMov[0] == 2 && ultMov[1] == 0)
button9.Text = "0";
if (ultMov[0] == 2 && ultMov[1] == 1)
button8.Text = "0";
if (ultMov[0] == 2 && ultMov[1] == 2)
button7.Text = "0";
if (ganador == 0) MessageBox.Show("GANASTE");
if (ganador == 1) MessageBox.Show("PERDISTE");
if(ganador==-1 && tresEnRaya.tableroLleno())
MessageBox.Show("EMPATE");
}
private void eventoBotones( int x, int y,Button boton)
{
if (matriz[x, y] == -1)
{
tresEnRaya.seleccionarPosicion(x,y);
ganador = tresEnRaya.ganaPartida();
comprobarGanador();
boton.Text = "X";
}
}
private void button1_Click(object sender, EventArgs e)
{
eventoBotones(0, 0, button1);
}
private void button2_Click(object sender, EventArgs e)
{
eventoBotones(0, 1, button2);
}
private void button3_Click(object sender, EventArgs e)
{
eventoBotones(0, 2, button3);
}
private void button6_Click(object sender, EventArgs e)
{
eventoBotones(1, 0, button6);
}
private void button5_Click(object sender, EventArgs e)
{
eventoBotones(1, 1, button5);
}
private void button4_Click(object sender, EventArgs e)
{
eventoBotones(1,2, button4);
}
private void button9_Click(object sender, EventArgs e)
{
eventoBotones(2, 0, button9);
}
private void button8_Click(object sender, EventArgs e)
{
eventoBotones(2, 1, button8);
}
private void button7_Click(object sender, EventArgs e)
{
eventoBotones(2, 2, button7);
}
private void button10_Click(object sender, EventArgs e)
{
tresEnRaya = new TresEnRaya();
tresEnRaya.inicializarPartida();
matriz = tresEnRaya.Matriz;
ganador = -1;
label1.Text = button1.Text = button2.Text = button3.Text = button4.Text = button5.Text = button6.Text = button7.Text = button8.Text = button9.Text = String.Empty;
}
}
}
No hay comentarios:
Publicar un comentario