UNIVERSIDAD NACIONAL DE SAN AGUSTÍN ESCUELA PROFESIONAL DE INGENÍERIA INDUSTRIAL CURSO: LENGUAJE DE PROGRAMACION JUEG
Views 90 Downloads 8 File size 348KB
UNIVERSIDAD NACIONAL DE SAN AGUSTÍN
ESCUELA PROFESIONAL DE INGENÍERIA INDUSTRIAL
CURSO: LENGUAJE DE PROGRAMACION
JUEGO DE TRES EN RAYA CON INTELIGENCIA ARTIFICIAL ALUMNA:
AREQUIPA, 2014
1. OBJETIVOS: Aplicar los conceptosde leguaje de programación a problemas de autómatas celular. Determinar una aplicación de autómatas celulares. 2. CONCEPTOS GENERALES
CODIGO EN VISUAL BASIC DEL JUEGO TRES EN RAYA CLASE CODIGO.VB Public Class Codigo Private nTablero(2, 2) As Integer Private nGanador As Integer = -1 Private nContar As Integer = 0 Private ub(2) As Integer Public Function getTabler() Return nTablero End Function Public Function EmpezarPartida() For x As Integer = 0 To 2 For y As Integer = 0 To 2 nTablero(x, y) = -1 Next Next nGanador = -1 Return "" End Function Public Function pulsaBoton(ByVal n, ByVal m) If n >= 0 And n < 3 And m < 3 And nTablero(n, m) = -1 Then If nGanador = -1 Then nTablero(n, m) = 0 nGanador = GanarPartida() PonerFichaOrdenador() End If End If End Function Public Function GanarPartida() 'Diagonal \ If nTablero(0, 0) -1 And nTablero(0, 0) = nTablero(1, 1) And nTablero(0, 0) = nTablero(2, 2) Then Return nTablero(0, 0) End If 'Diagonal / If nTablero(0, 2) -1 And nTablero(0, 2) = nTablero(1, 1) And nTablero(0, 2) = nTablero(2, 0) Then Return nTablero(0, 2) End If 'Filas horizontales y verticales
For n As Integer = 0 To 2 If nTablero(n, 0) -1 And nTablero(n, 0) = nTablero(n, 1) And nTablero(n, 0) = nTablero(n, 2) Then Return nTablero(n, 0) End If If nTablero(0, n) -1 And nTablero(0, n) = nTablero(1, n) And nTablero(0, n) = nTablero(2, n) Then Return nTablero(0, n) End If Next Return -1 End Function Public Function GetGanador() Return nGanador End Function Public Function GetUltimoBoton() Return ub End Function 'MINIMAX Public Function TableroCompleto() For x As Integer = 0 To 2 For y As Integer = 0 To 2 If nTablero(x, y) = -1 Then Return False End If Next Next Return True End Function Public Function FinPartida() Return TableroCompleto() Or GanarPartida() -1 End Function Public Function PonerFichaOrdenador() If Not FinPartida() Then Dim f As Integer = 0 Dim c As Integer = 0 Dim v As Integer = Integer.MinValue Dim aux As Integer For x As Integer = 0 To 2 For y As Integer = 0 To 2 If nTablero(x, y) = -1 Then nTablero(x, y) = 1 aux = Min() If aux > v Then v = aux f = x c = y End If nTablero(x, y) = -1 End If Next Next nTablero(f, c) = 1 ub(0) = f ub(1) = c End If End Function Private Function Max() If FinPartida() Then If GanarPartida() -1 Then
Return -1 Else
End Dim Dim For
Return 0 End If If v As Integer = Integer.MinValue aux As Integer x As Integer = 0 To 2 For y As Integer = 0 To 2 If nTablero(x, y) = -1 Then nTablero(x, y) = 1 aux = Min() If aux > v Then v = aux End If nTablero(x, y) = -1 End If Next
Next Return v End Function Private Function Min() If FinPartida() Then If GanarPartida() -1 Then Return 1 Else Return 0 End If End If Dim v As Integer = Integer.MaxValue Dim aux As Integer For x As Integer = 0 To 2 For y As Integer = 0 To 2 If nTablero(x, y) = -1 Then nTablero(x, y) = 0 aux = Max() If aux < v Then v = aux End If nTablero(x, y) = -1 End If Next Next Return v End Function End Class
CODIGO FORM1.VB Public Class Form1 Dim c As New Codigo Dim nGanador As Integer = -1 Dim nTablero(2, 2) As Integer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load c.EmpezarPartida() nTablero = c.getTabler End Sub Private Sub CheckGanador()
Dim ub(2) As Integer ub = c.GetUltimoBoton If ub(0) = 0 And ub(1) = 0 Then B1.Text = "O" End If If ub(0) = 0 And ub(1) = 1 Then B2.Text = "O" End If If ub(0) = 0 And ub(1) = 2 Then B3.Text = "O" End If If ub(0) = 1 And ub(1) = 0 Then B4.Text = "O" End If If ub(0) = 1 And ub(1) = 1 Then B5.Text = "O" End If If ub(0) = 1 And ub(1) = 2 Then B6.Text = "O" End If If ub(0) = 2 And ub(1) = 0 Then B7.Text = "O" End If If ub(0) = 2 And ub(1) = 1 Then B8.Text = "O" End If If ub(0) = 2 And ub(1) = 2 Then B9.Text = "O" End If If nGanador = 0 Then Label2.Text = "HAS GANADO =D" End If If nGanador = 1 Then Label2.Text = "HAS PERDIDO =C" End If If nGanador = -1 Then If c.TableroCompleto() = True Then Label2.Text = "EMPATE" End If End If End Sub Private Sub B1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B1.Click If nTablero(0, 0) = -1 Then B1.Text = "X" c.pulsaBoton(0, 0) nGanador = c.GanarPartida() Call CheckGanador() B1.ForeColor = Color.Red End If End Sub Private Sub B2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B2.Click If nTablero(0, 1) = -1 Then B2.Text = "X" c.pulsaBoton(0, 1) nGanador = c.GanarPartida() Call CheckGanador() B2.ForeColor = Color.Red End If
End Sub Private Sub B3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B3.Click If nTablero(0, 2) = -1 Then B3.Text = "X" c.pulsaBoton(0, 2) nGanador = c.GanarPartida() Call CheckGanador() B3.ForeColor = Color.Red End If End Sub Private Sub B4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B4.Click If nTablero(1, 0) = -1 Then B4.Text = "X" c.pulsaBoton(1, 0) nGanador = c.GanarPartida() Call CheckGanador() B4.ForeColor = Color.Red End If End Sub Private Sub B5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B5.Click If nTablero(1, 1) = -1 Then B5.Text = "X" c.pulsaBoton(1, 1) nGanador = c.GanarPartida() Call CheckGanador() B5.ForeColor = Color.Red End If End Sub Private Sub B6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B6.Click If nTablero(1, 2) = -1 Then B6.Text = "X" c.pulsaBoton(1, 2) nGanador = c.GanarPartida() Call CheckGanador() B6.ForeColor = Color.Red End If End Sub Private Sub B7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B7.Click If nTablero(2, 0) = -1 Then B7.Text = "X" c.pulsaBoton(2, 0) nGanador = c.GanarPartida() Call CheckGanador() B7.ForeColor = Color.Red End If End Sub Private Sub B8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B8.Click If nTablero(2, 1) = -1 Then B8.Text = "X" c.pulsaBoton(2, 1)
nGanador = c.GanarPartida() Call CheckGanador() B8.ForeColor = Color.Red End If End Sub Private Sub B9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B9.Click If nTablero(2, 2) = -1 Then B9.Text = "X" c.pulsaBoton(2, 2) nGanador = c.GanarPartida() Call CheckGanador() B9.ForeColor = Color.Red End If End Sub Private Sub BtnReiniciar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReiniciar.Click c = New Codigo c.EmpezarPartida() nTablero = c.getTabler nGanador = -1 B1.Text = "" B2.Text = "" B3.Text = "" B4.Text = "" B5.Text = "" B6.Text = "" B7.Text = "" B8.Text = "" B9.Text = "" B1.ForeColor = Color.Black B2.ForeColor = Color.Black B3.ForeColor = Color.Black B4.ForeColor = Color.Black B5.ForeColor = Color.Black B6.ForeColor = Color.Black B7.ForeColor = Color.Black B8.ForeColor = Color.Black B9.ForeColor = Color.Black End Sub End Class