samira2004
New Member
[align=center:03372ead92]با سلام.كسي مي تونه يه لينك از يه سورس كد ساده براي بازيtic-tac-toe كه تو صف حه 4*4 باشه به من بده؟[/align:03372ead92]
<SCRIPT name = "JavaScript">
function variables(){
t = 1;
change = 1;
empty1 = -1;
empty2 = -1;
empty3 = -1;
empty4 = -1;
empty5 = -1;
empty6 = -1;
empty7 = -1;
empty8 = -1;
empty9 = -1;
return;
whogoesnow = "نوبت نفر اول"
}
function win(){
if (empty1==empty2 && empty2==empty3 && empty3==0 ||
empty4==empty5 && empty5==empty6 && empty6==0 ||
empty7==empty8 && empty8==empty9 && empty9==0 ||
empty1==empty4 && empty4==empty7 && empty7==0 ||
empty2==empty5 && empty5==empty8 && empty8==0 ||
empty3==empty6 && empty6==empty9 && empty9==0 ||
empty1==empty5 && empty5==empty9 && empty9==0 ||
empty3==empty5 && empty5==empty7 && empty7==0 ){
alert ("نفر دوم برد");
history.go(0)
}
if (empty1==empty2 && empty2==empty3 && empty3==1 ||
empty4==empty5 && empty5==empty6 && empty6==1 ||
empty7==empty8 && empty8==empty9 && empty9==1 ||
empty1==empty4 && empty4==empty7 && empty7==1 ||
empty2==empty5 && empty5==empty8 && empty8==1 ||
empty3==empty6 && empty6==empty9 && empty9==1 ||
empty1==empty5 && empty5==empty9 && empty9==1 ||
empty3==empty5 && empty5==empty7 && empty7==1 ){
alert ("نفر اول برد");
history.go(0)
}
}
function turnchange(t){
if (change == 1) {
if (t == 0){
t = 1;
whogoesnow = "نوبت نفر اول "
}
else {
t = 0;
whogoesnow = "نوبت نفر دوم "
}
}
else {
t = t;
}
change = 1
return (t);
}
function changing(clicked){
if (clicked == 1){
spot = empty1 ;}
if (clicked == 2){
spot = empty2 ;}
if (clicked == 3){
spot = empty3 ;}
if (clicked == 4){
spot = empty4 ;}
if (clicked == 5){
spot = empty5 ;}
if (clicked == 6){
spot = empty6 ;}
if (clicked == 7){
spot = empty7 ;}
if (clicked == 8){
spot = empty8 ;}
if (clicked == 9){
spot = empty9 ;}
if (spot == -1){
if (t == 0){
xo = " O " ;
spot = 0
}
else {
xo = " X " ;
spot = 1
}
if (clicked == 1){
empty1 = spot;}
if (clicked == 2){
empty2 = spot ;}
if (clicked == 3){
empty3 = spot ;}
if (clicked == 4){
empty4 = spot ;}
if (clicked == 5){
empty5 = spot ;}
if (clicked == 6){
empty6 = spot ;}
if (clicked == 7){
empty7 = spot ;}
if (clicked == 8){
empty8 = spot ;}
if (clicked == 9){
empty9 = spot ;}
}
else {
if (spot == 0){
xo = " O ";}
if (spot == 1){
xo = " X ";}
change = 0
}
return (xo) ;
}
function change_b1(form){
clicked = 1;
changing(clicked) ;
t = turnchange(t) ;
form.b1.value = xo;
form.whoseturn.value = whogoesnow
win();
return;
}
function change_b2(form){
clicked = 2;
changing(clicked) ;
form.b2.value = xo;
t = turnchange(t) ;
form.whoseturn.value = whogoesnow
win();
return;
}
function change_b3(form){
clicked = 3;
changing(clicked) ;
form.b3.value = xo;
t = turnchange(t) ;
form.whoseturn.value = whogoesnow
win();
return;
}
function change_b4(form){
clicked = 4;
changing(clicked) ;
form.b4.value = xo;
t = turnchange(t) ;
form.whoseturn.value = whogoesnow
win();
return;
}
function change_b5(form){
clicked = 5;
changing(clicked) ;
form.b5.value = xo;
t = turnchange(t) ;
form.whoseturn.value = whogoesnow
win();
return;
}
function change_b6(form){
clicked = 6;
changing(clicked) ;
form.b6.value = xo;
t = turnchange(t) ;
form.whoseturn.value = whogoesnow
win();
return;
}
function change_b7(form){
clicked = 7;
changing(clicked) ;
form.b7.value = xo;
t = turnchange(t) ;
form.whoseturn.value = whogoesnow
win();
return;
}
function change_b8(form){
clicked = 8;
changing(clicked) ;
form.b8.value = xo;
t = turnchange(t) ;
form.whoseturn.value = whogoesnow
win();
return;
}
function change_b9(form){
clicked = 9;
changing(clicked) ;
form.b9.value = xo;
t = turnchange(t) ;
form.whoseturn.value = whogoesnow
win();
return;
}
</SCRIPT>
<SCRIPT name = "JavaScript">
variables();
</SCRIPT>
<FORM>
<input type = "button" name = "whoseturn"
value = "نوبت نفر اول">
<br><br><br><br>
<table>
<tr><td><input type = "button" name = "b1" value = " " onClick =
change_b1(this.form)>
<td><input type = "button" name = "b2" value = " " onClick = change_b2(this.form)>
<td><input type = "button" name = "b3" value = " " onClick =
change_b3(this.form)>
<tr><td><input type = "button" name = "b4" value = " " onClick =
change_b4(this.form)>
<td><input type = "button" name = "b5" value = " " onClick = change_b5(this.form)>
<td><input type = "button" name = "b6" value = " " onClick =
change_b6(this.form)>
<tr><td><input type = "button" name = "b7" value = " " onClick =
change_b7(this.form)>
<td><input type = "button" name = "b8" value = " " onClick = change_b8(this.form)>
<td><input type = "button" name = "b9" value = " "
onClick = change_b9(this.form)>
</table>
<br><br>
<INPUT TYPE="button" VALUE="از اول" onClick="history.go(0)">
</center>
samira2004 گفت:[align=center:13ba415200]با سلام.كسي مي تونه يه لينك از يه سورس كد ساده براي بازيtic-tac-toe كه تو صف حه 4*4 باشه به من بده؟[/align:13ba415200]
Attribute VB_Name = "AI"
Option Explicit
Public Const MAXVALUE = 1000000000
Public Const MINVALUE = -1000000000
Public BOARDSIZE As Integer
Public WinLength As Integer
Public Plys As Integer 'depth of search
Public BoardArray() As Byte '
Public MoveList() As Integer
Public AllDone As Boolean 'flag to signal end of game or quit
Private Values() As Long 'value of each combination
Public MoveCount As Long 'number of moves investigated
Public BoardValue As Long
Private Declare Function GetInputState Lib "user32" () As Long
Public Sub UpdateBoardValue(r%, c%)
Dim i%, player%, j%, wl%
Dim v&
Dim pcnt%, ocnt%
v& = 0
wl% = WinLength - 1
player% = BoardArray(r%, c%)
'check all possible vertical lines that include r%,c%
For i% = r% - wl% To r%
If (i% >= 0) And ((i% + wl%) <= BOARDSIZE) Then
pcnt% = 0
ocnt% = 0
For j% = i% To i% + wl%
Select Case BoardArray(j%, c%)
Case player%
If j% <> r% Then pcnt% = pcnt% + 1
Case 0
Case Else
ocnt% = ocnt% + 1
End Select
Next j%
If ocnt% > 0 Then
If pcnt% = 0 Then v& = v& + Values(ocnt%) 'get points for denying opponent
Else
If pcnt% = wl% Then GoTo BreakOnWin 'if line wins game, then break
v& = v& + Values(pcnt% + 1) - Values(pcnt%) 'get points for increasing length
End If
End If
Next i%
'check all possible horizontal lines that include r%,c%
For i% = c% - wl% To c%
If (i% >= 0) And ((i% + wl%) <= BOARDSIZE) Then
pcnt% = 0
ocnt% = 0
For j% = i% To i% + wl%
Select Case BoardArray(r%, j%)
Case player%
If j% <> c% Then pcnt% = pcnt% + 1
Case 0
Case Else
ocnt% = ocnt% + 1
End Select
Next j%
If ocnt% > 0 Then
If pcnt% = 0 Then v& = v& + Values(ocnt%) 'get points for denying opponent
Else
If pcnt% = wl% Then GoTo BreakOnWin
v& = v& + Values(pcnt% + 1) - Values(pcnt%) 'get points for increasing length
End If
End If
Next i%
'check diagonals from top left to bottom right
For i% = 0 To wl%
If ((c% - i%) >= 0) And (((c% - i%) + wl%) <= BOARDSIZE) And ((r% - i%) >= 0) And (((r% - i%) + wl%) <= BOARDSIZE) Then
pcnt% = 0
ocnt% = 0
For j% = 0 To wl%
Select Case BoardArray(r% - i% + j%, c% - i% + j%)
Case player%
If i% <> j% Then pcnt% = pcnt% + 1
Case 0
Case Else
ocnt% = ocnt% + 1
End Select
Next j%
If ocnt% > 0 Then
If pcnt% = 0 Then v& = v& + Values(ocnt%) 'get points for denying opponent
Else
If pcnt% = wl% Then GoTo BreakOnWin
v& = v& + Values(pcnt% + 1) - Values(pcnt%) 'get points for increasing length
End If
End If
Next i%
'check diagonals from bottom left to top right
For i% = 0 To wl%
If ((c% - i%) >= 0) And (((c% - i%) + wl%) <= BOARDSIZE) And ((r% + i%) - wl% >= 0) And (((r% + i%)) <= BOARDSIZE) Then
pcnt% = 0
ocnt% = 0
For j% = 0 To wl%
Select Case BoardArray(r% + i% - j%, c% - i% + j%)
Case player%
If i% <> j% Then pcnt% = pcnt% + 1
Case 0
Case Else
ocnt% = ocnt% + 1
End Select
Next j%
If ocnt% > 0 Then
If pcnt% = 0 Then v& = v& + Values(ocnt%) 'get points for denying opponent
Else
If pcnt% = wl% Then GoTo BreakOnWin
v& = v& + Values(pcnt% + 1) - Values(pcnt%) 'get points for increasing length
End If
End If
Next i%
If player% = 2 Then v& = -v& 'player 2 likes small boardvalues
BoardValue = BoardValue + v&
Exit Sub
BreakOnWin:
If player% = 2 Then BoardValue = MINVALUE Else BoardValue = MAXVALUE
End Sub
Public Sub InitMoveList()
'load movelist in such a way that centre moves are first in list
Dim r%, c%, i%, n%, v%, lasti%
Dim lv&
ReDim movevalue(1, (BOARDSIZE + 1) * (BOARDSIZE + 1)) As Integer
'uses a kind of linked list structure.
'movevalue(0,x) is the value for square x
'movevalue(1,x) is the index of the next square
'also init the values table
lv& = 0
ReDim Values(WinLength) As Long
For i% = 0 To WinLength
Values(i%) = lv&
lv& = lv& * 10 + 1
Next i%
n% = 1
movevalue(1, 0) = 0
For r% = 0 To BOARDSIZE
For c% = 0 To BOARDSIZE
v% = (Abs(r% - BOARDSIZE \ 2) + 1) * (Abs(c% - BOARDSIZE \ 2) + 1)
movevalue(0, n%) = v%
i% = movevalue(1, 0)
lasti% = 0
Do While i%
If v% < movevalue(0, i%) Then 'insert
movevalue(1, n%) = i%
movevalue(1, lasti%) = n%
Exit Do
End If
lasti% = i%
i% = movevalue(1, i%)
Loop
If i% = 0 Then 'no insertion, add to end
movevalue(1, n%) = 0
movevalue(1, lasti%) = n%
End If
n% = n% + 1
Next c%
Next r%
i% = movevalue(1, 0)
n% = 0
While i%
MoveList(n%, 0) = i% - 1
MoveList(n%, 1) = 0 'not picked
i% = movevalue(1, i%)
n% = n% + 1
Wend
End Sub
Public Function AlphaBetaSearch(ByVal ply%, ByVal limit&, ByVal player%, bestmove&) As Long
'returns the value of the board
'aborts if the branch it is searching is worse than the limit
Dim best&, i&, n&, v&, move&
Dim r%, c%, otherplayer%
Dim lastbv&
If AllDone Then Exit Function
If ply% Then 'not at bottom of recursive branch yet...
n& = (BOARDSIZE + 1&) * (BOARDSIZE + 1&) - 1&
If player = 1 Then
best& = MINVALUE
otherplayer% = 2
Else
best& = MAXVALUE
otherplayer% = 1
End If
'check all possible moves from this location...
For i& = 0 To n&
If GetInputState() Then DoEvents
If MoveList(i&, 1) = 0 Then
r% = MoveList(i&, 0) \ (BOARDSIZE + 1&)
c% = MoveList(i&, 0) Mod (BOARDSIZE + 1&)
'we've selected a move...
MoveList(i&, 1) = player '...so remove it from list...
BoardArray(r%, c%) = player '...and update board...
lastbv& = BoardValue '...but save the old boardvalue for later
UpdateBoardValue r%, c% 'calculate the new boardvalue
If Abs(BoardValue) = MAXVALUE Then 'break since win was found
v& = BoardValue
Else
v& = AlphaBetaSearch(ply% - 1, best&, otherplayer%, move&) 'and recursively follow branch
End If
If AllDone Then Exit For
'return board and movelist to original state
BoardValue = lastbv&
BoardArray(r%, c%) = 0
MoveList(i&, 1) = 0
If v& = best Then 'insert a bit of randomness
If Rnd() < 0.3 Then
bestmove& = i&
End If
End If
If player = 1 Then ' Pick largest
If v& > best& Then
best& = v&
bestmove& = i&
End If
If v& > limit Then 'we are past the limit, meaning there is no point exploring this branch further
Exit For
End If
Else 'Pick smallest
If v& < best& Then
best& = v&
bestmove& = i&
End If
If v& < limit Then
Exit For
End If
End If
End If
Next i&
AlphaBetaSearch = best&
Else 'we are at the bottom of the branch. Just return the board value
MoveCount = MoveCount + 1
AlphaBetaSearch = BoardValue
End If
End Function
Function VictoryCheck() As Integer
'checks for victory. Returns 1 or 2 for player 1 or 2 or 0 if no one won.
Dim r%, c%, i%, pl%, r1%, c1%, r2%, c2%, j%, wl%
wl% = WinLength - 1
For r% = 0 To BOARDSIZE
For c% = 0 To BOARDSIZE
If r% <= (BOARDSIZE - wl%) Then 'calculate vertical
pl% = 0
i% = 0
For r1% = r% To r% + wl%
Select Case BoardArray(r1%, c%)
Case 0 'nothing
Case 1
Select Case pl%
Case 0
pl% = 1
i% = 1
Case 1
i% = i% + 1
Case 2
i% = 0
Exit For
End Select
Case 2
Select Case pl%
Case 0
pl% = 2
i% = 1
Case 2
i% = i% + 1
Case 1
i% = 0
Exit For
End Select
End Select
Next r1%
If i% = WinLength Then
VictoryCheck = pl%
Exit Function
End If
End If
If c% <= (BOARDSIZE - wl%) Then 'calculate horizontal
pl% = 0
i% = 0
For c1% = c% To c% + wl%
Select Case BoardArray(r%, c1%)
Case 0 'nothing
Case 1
Select Case pl%
Case 0
pl% = 1
i% = 1
Case 1
i% = i% + 1
Case 2
i% = 0
Exit For
End Select
Case 2
Select Case pl%
Case 0
pl% = 2
i% = 1
Case 2
i% = i% + 1
Case 1
i% = 0
Exit For
End Select
End Select
Next c1%
If i% = WinLength Then
VictoryCheck = pl%
Exit Function
End If
End If
If (r% <= (BOARDSIZE - wl%)) And (c% <= (BOARDSIZE - wl%)) Then 'diag1
i% = 0
pl% = 0
For j% = 0 To wl%
Select Case BoardArray(r% + j%, c% + j%)
Case 0 'nothing
Case 1
Select Case pl%
Case 0
pl% = 1
i% = 1
Case 1
i% = i% + 1
Case 2
i% = 0
Exit For
End Select
Case 2
Select Case pl%
Case 0
pl% = 2
i% = 1
Case 2
i% = i% + 1
Case 1
i% = 0
Exit For
End Select
End Select
Next j%
If i% = WinLength Then
VictoryCheck = pl%
Exit Function
End If
End If
If (r% <= (BOARDSIZE - wl%)) And (c% >= wl%) Then 'diag2
i% = 0
pl% = 0
For j% = 0 To wl%
Select Case BoardArray(r% + j%, c% - j%)
Case 0 'nothing
Case 1
Select Case pl%
Case 0
pl% = 1
i% = 1
Case 1
i% = i% + 1
Case 2
i% = 0
Exit For
End Select
Case 2
Select Case pl%
Case 0
pl% = 2
i% = 1
Case 2
i% = i% + 1
Case 1
i% = 0
Exit For
End Select
End Select
Next j%
If i% = WinLength Then
VictoryCheck = pl%
Exit Function
End If
End If
Next c%
Next r%
DoEvents
VictoryCheck = 0
End Function