| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1284 人关注过本帖, 2 人收藏
标题:求用vb写一个井字棋游戏程序
只看楼主 加入收藏
青涩年华
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2013-3-5
结帖率:100%
收藏(2)
已结贴  问题点数:10 回复次数:4 
求用vb写一个井字棋游戏程序
急求源码,感谢万分
搜索更多相关主题的帖子: 游戏程序 
2013-06-12 22:25
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:5 
不懂。。。。

AI 方面更不懂。。。。

要么你把所有的 可能都排出,然后给出 计算机应该怎么走的 也都排出来。

1、棋盘的 样子。
2、每个人的棋子个数。
3、从横开始,每一行用一个字符串表示,该位有子,就 人的棋子就写1,电脑的棋子就写2,无子就写0 。
4、然后这种情况,如果是人走,应该如何走,如果是 电脑走,应该如何走。走完后是如何的,也按上面编成字符串。
   把这些结果统统放数据库里就是了。

然后再考虑程序的问题吧。

授人于鱼,不如授人于渔
早已停用QQ了
2013-06-13 00:16
青涩年华
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2013-3-5
收藏
得分:0 
恩,多谢指教
2013-06-13 17:58
lowxiong
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:27
帖 子:653
专家分:3402
注 册:2008-5-7
收藏
得分:5 
新建一工程,窗口左上角放一按钮,拷贝下列代码,运行即可
井字棋.rar (2.72 KB)

'********************************************************
Dim qpX As Integer, qpY As Integer, qpL As Integer, qzR As Integer '用于存储棋盘左上角坐标、棋盘边长和棋子半径
Dim QJ(2, 2) As Integer '用3*3的数组存储棋局,数组中无棋子为0,人下的棋子为1,电脑棋子为2,这里参考了风兄提议,我以前设计围棋时通常是黑棋-1,白棋1
Dim runFlg As Boolean  '可以走棋标志

Private Function Winner() As Integer
  '判断输赢,并将赢家棋子颜色返回
  Dim i As Integer, j As Integer, c As Integer, k As Integer
  Winner = 0
  For i = 0 To 2
    '分别判断行列是否相同
    If QJ(i, 0) <> 0 Then
      c = QJ(i, 0)
      If QJ(i, 1) = c And QJ(i, 2) = c Then
        Winner = c
        Exit Function
      End If
    End If
    If QJ(0, i) <> 0 Then
      c = QJ(0, i)
      If QJ(1, i) = c And QJ(2, i) = c Then
        Winner = c
        Exit Function
      End If
    End If
  Next
  '再判断两个斜线是否相同
  If QJ(0, 0) <> 0 Then
    c = QJ(0, 0)
    If QJ(1, 1) = c And QJ(2, 2) = c Then
      Winner = c
      Exit Function
    End If
  End If
  If QJ(2, 0) <> 0 Then
    c = QJ(2, 0)
    If QJ(1, 1) = c And QJ(0, 2) = c Then
      Winner = c
      Exit Function
    End If
  End If
End Function
Private Sub DrawQJ()
  '画棋盘和棋子
  Dim i As Integer, j As Integer, l As Integer, k As Integer, x As Integer, y As Integer
  Me.Cls
  l = Me.ScaleWidth
  If l > Me.ScaleHeight Then l = Me.ScaleHeight
  qpL = 0.9 * l
  j = qpL / 3
  qzR = j * 0.6 * 0.5
  qpX = (Me.ScaleWidth - qpL) / 2
  qpY = (Me.ScaleHeight - qpL) / 2
  For i = 0 To 3
    Me.Line (qpX + i * j, qpY)-(qpX + i * j, qpY + qpL), vbBlue
    Me.Line (qpX, qpY + i * j)-(qpX + qpL, qpY + i * j), vbBlue
  Next
  For i = 0 To 2
    For k = 0 To 2
      x = i * j + qpX + j * 0.5
      y = k * j + qpY + j * 0.5
      If QJ(i, k) = 1 Then
        '人下的棋
        Me.FillStyle = 0
        Me.FillColor = vbWhite
        Me.Circle (x, y), qzR, vbBlack
      End If
      If QJ(i, k) = 2 Then
        '电脑下的棋
        Me.FillStyle = 0
        Me.FillColor = vbBlack
        Me.Circle (x, y), qzR, vbRed
      End If
    Next
  Next
End Sub
Private Sub autoComputer()
  '电脑自动下棋
  Dim i As Integer, j As Integer, k As Integer, c As Integer, b(8, 1) As Integer
  If Not runFlg Then Exit Sub
  c = 0
  For i = 0 To 2
    For j = 0 To 2
      If QJ(i, j) = 0 Then
        QJ(i, j) = 2
        '首先判定电脑下任何一步能不能赢
        If Winner = 2 Then
          DrawQJ
          MsgBox "电脑赢了!"
          runFlg = False
          Exit Sub
        Else
          QJ(i, j) = 0
        End If
      End If
    Next
  Next
  For i = 0 To 2
    For j = 0 To 2
      If QJ(i, j) = 0 Then
        QJ(i, j) = 1
        '其次判断对手下任何一步是否会赢
        If Winner = 1 Then
          QJ(i, j) = 2
          DrawQJ
          Exit Sub
        Else
          QJ(i, j) = 0
        End If
        b(c, 0) = i
        b(c, 1) = j
        c = c + 1
      End If
    Next
  Next
  If c < 2 Then
    MsgBox "和棋!"
    runFlg = False
    Exit Sub
  End If
  Randomize
  c = Rnd * c  '电脑随机走一步
  QJ(b(c, 0), b(c, 1)) = 2
  DrawQJ
  If Winner = 2 Then
    runFlg = False
    MsgBox "电脑赢了!"
  End If
End Sub

Private Sub Command1_Click()
  Dim i As Integer, j As Integer
  For i = 0 To 2
    For j = 0 To 2
      QJ(i, j) = 0
    Next
  Next
  '初始化棋局为无棋子状态
  Randomize
  If Rnd > 0.5 Then autoComputer   '如果随机数大于0.5则电脑先下,否则等待人先下
  DrawQJ  '画棋局
  runFlg = True
End Sub

Private Sub Form_Load()
  Command1_Click
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  Dim i As Integer, j As Integer, k As Integer, x1 As Integer, y1 As Integer, c As Integer
  If Button = 1 And runFlg Then
    For i = 0 To 2
      For k = 0 To 2
        j = qpL / 3
        x1 = i * j + qpX
        y1 = k * j + qpY
        If x > x1 And y > y1 And x < x1 + j And y < y1 + j Then
          If QJ(i, k) = 0 Then
            QJ(i, k) = 1
            DrawQJ
            If Winner = 1 Then
              MsgBox "你赢了!"
              runFlg = False
            End If
            autoComputer
          End If
        End If
      Next
    Next
  End If
End Sub

Private Sub Form_Resize()
  DrawQJ
End Sub
2013-06-14 11:27
青涩年华
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2013-3-5
收藏
得分:0 
三口油
2013-06-17 23:36
快速回复:求用vb写一个井字棋游戏程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024274 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved