【问题标题】:Using 2D Array for Tic Tac Toe in Visual Basic在 Visual Basic 中使用 2D 数组进行井字游戏
【发布时间】:2014-04-09 04:08:15
【问题描述】:

我必须为我的 Visual Basic 类制作一个井字游戏,该类使用数组来跟踪游戏的状态,但我不知道如何设置这个数组。该程序应该有 2 个视图(一个我已经完成的 GUI,以及一个使用控制台的文本视图,我不知道该怎么做),有两个控制器(用户可以单击他们想要播放的按钮,或者他们可以使用键盘上的 1-9 键来选择他们的位置),并且是在人和计算机之间播放。

这并不多,但这是我目前所拥有的:

  Module Module1
Const intMAX_ROWS As Integer = 2
Const intMAX_COL As Integer = 2
Public gameArray(intMAX_ROWS, intMAX_COL) As String

Button1.Text = gameArray(0,0) 
Button2.Text = gameArray(0,1)
Button3.Text = gameArray(0,2)
Button4.Text = gameArray(1,0)
Button5.Text = gameArray(1,1)
Button6.Text = gameArray(1,2)
Button7.Text = gameArray(2,0)
Button8.Text = gameArray(2,1)
Button9.Text = gameArray(2,2)

End Module

我在所有 Button.Text 行上都收到错误提示

Declaration expected

关于如何解决这个问题的任何想法?

任何帮助或建议将不胜感激。

【问题讨论】:

    标签: arrays vb.net multidimensional-array tic-tac-toe


    【解决方案1】:

    要让程序编译为按钮赋值的语句,需要在函数内部:

    Sub SetButtons()
        Button1.Text = gameArray(0, 0)
        Button2.Text = gameArray(0, 1)
        Button3.Text = gameArray(0, 2)
        Button4.Text = gameArray(1, 0)
        Button5.Text = gameArray(1, 1)
        Button6.Text = gameArray(1, 2)
        Button7.Text = gameArray(2, 0)
        Button8.Text = gameArray(2, 1)
        Button9.Text = gameArray(2, 2)
    End Sub
    

    由于您需要 2 个视图、一个 GUI 和一个文本视图,我建议您以 3 个项目结束:

    • 用于游戏逻辑的井字游戏库(共享)
    • 用于 GUI 视图(引用库)的 WinForms 或 WPF 应用程序
    • 文本视图的控制台应用程序(引用库)

    以下是在控制台应用程序中绘制板的示例:

    Dim board(3, 3) As Char
    ' Set a O in the middle
    board(1, 1) = "O"
    ' Set an X at the bottom right
    board(2, 2) = "X"
    ' Show board
    Console.WriteLine(board(0, 0) + "|" + board(1, 0) + "|" + board(2, 0))
    Console.WriteLine("-----")
    Console.WriteLine(board(0, 1) + "|" + board(1, 1) + "|" + board(2, 1))
    Console.WriteLine("-----")
    Console.WriteLine(board(0, 2) + "|" + board(1, 2) + "|" + board(2, 2))
    

    给出:

     | |
    -----
     |O|
    -----
     | |X
    

    对于 GUI 方面的一点启发,这里有一个简短的 Silverlight 示例(写在 F#):Tic-Tac-Toe

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      相关资源
      最近更新 更多