Re: [.NET] VB2008 斜率計算
Public Class Form1
Dim chess_board(8, 8) As Boolean
Function safe(ByVal chess_board(,) As Boolean, ByVal x As Integer, ByVal y As Integer) As Boolean
Dim is_safe As Boolean
Dim i As Integer
is_safe = True
For i = 1 To 8
If chess_board(x, i) Then
is_safe = False
ElseIf chess_board(i, y) Then
is_safe = False
End If
Next
┌
│*
└
Return is_safe
End Function
Sub draw_chess_board(ByVal board)
Dim g = Me.CreateGraphics()
Dim i, j, k As Integer
For i = 1 To 9
g.DrawLine(Pens.Black, i * 15, 15, i * 15, 135)
g.DrawLine(Pens.Black, 15, i * 15, 135, i * 15)
Next
j = TextBox1.Text
k = TextBox2.Text
For j = 1 To 8
For k = 1 To 8
If board(j, k) Then
g.DrawEllipse(Pens.Red, j * 15 + 4, k * 15 + 4, 7, 7)
End If
Next
Next
g.Dispose()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If (safe(chess_board, TextBox1.Text, TextBox2.Text)) = True Then
chess_board(TextBox1.Text, TextBox2.Text) = True
draw_chess_board(chess_board)
Else
MsgBox("Not Safe! Please try again.")
End If
End Sub
End Class
我雖然說是要寫8 queen problem的程式,
可是並不完全一樣,
事實上,
這是一份作業,
要求如下:
”當使用者輸入一個新的皇后的坐標時,
程式要呼叫這個函數,
檢查這個新的皇后會不會被棋盤上現有的其他皇后吃掉。
如果不會,就直接把這個新的皇后加到棋盤上,
如果會,則利用"MsgBox( )"函數顯示一個警告訊息,告訴使用者必須再試一次。”
*的部分是我還沒寫出來的@@
老師給我們的提示是”對角線斜率=1或-1”,
所以我原PO才會想問斜率的算法!
請大大們教教我吧,
感激不盡!(鞠躬
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.231.24.16
推
12/03 23:41, , 1F
12/03 23:41, 1F
討論串 (同標題文章)
Visual_Basic 近期熱門文章
PTT數位生活區 即時熱門文章