Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 修改代码,使其不会';I don’我没有显示船只的位置_Vb.net - Fatal编程技术网

Vb.net 修改代码,使其不会';I don’我没有显示船只的位置

Vb.net 修改代码,使其不会';I don’我没有显示船只的位置,vb.net,Vb.net,我正在创建一个战舰游戏,这是一个单人游戏,战舰的坐标已经被硬编码在里面,你可以猜到它们的位置。我很难让它识别董事会的哪些部分被船舶占用,哪些部分没有。目前,整个板块显然没有被船只占据,当我输入被船只占据的坐标时,它表明我错过了 我不想让棋盘显示船只的位置,否则会让游戏变得太简单 下面是我的代码: Module Module1 Sub Main() Dim board(9, 9) As Char Dim bombs As Byte = 50 Dim xcoord, ycoo

我正在创建一个战舰游戏,这是一个单人游戏,战舰的坐标已经被硬编码在里面,你可以猜到它们的位置。我很难让它识别董事会的哪些部分被船舶占用,哪些部分没有。目前,整个板块显然没有被船只占据,当我输入被船只占据的坐标时,它表明我错过了

我不想让棋盘显示船只的位置,否则会让游戏变得太简单

下面是我的代码:

Module Module1
Sub Main()
    Dim board(9, 9) As Char
    Dim bombs As Byte = 50
    Dim xcoord, ycoord, hits As Byte
    Dim gameover As Boolean

    Do
        Call displayallboard(board)
        Call getcoords(xcoord, ycoord)
        bombs = bombs - 1
        Call checkhit(xcoord, ycoord, board, hits)
        Call checkwins(hits, bombs, gameover)
    Loop Until gameover = True
    Console.WriteLine()
    Console.WriteLine()
    If hits = 16 Then
        Console.WriteLine("You win!!! All ships hit")
    Else
        Console.WriteLine("You lose!!! You are out of ammunition")
    End If

    Console.ReadLine()
End Sub
Sub displayallboard(ByRef board(,) As Char)
    Dim row As Integer
    Dim column As Integer
    Console.WriteLine("  |1 2 3 4 5 6 7 8 9")
    Console.WriteLine("--+------------------")
    For row = 1 To 9
        Console.Write(row & " |")
        For column = 1 To 9
            Console.Write(board(column, row) & " ")
        Next
        Console.WriteLine()
    Next
    Console.WriteLine()
End Sub
Sub addships(ByRef board(,) As Char, ByRef xcoord As Byte, ByRef ycoord As Byte)
    board(9, 1) = "S"
    board(9, 2) = "S"
    board(1, 2) = "S"
    board(1, 3) = "S"
    board(1, 4) = "S"
    board(1, 5) = "S"
    board(4, 4) = "S"
    board(5, 4) = "S"
    board(6, 4) = "S"
    board(4, 5) = "S"
    board(5, 5) = "S"
    board(6, 5) = "S"
    board(3, 8) = "S"
    board(4, 8) = "S"
    board(5, 8) = "S"
    board(6, 8) = "S"
End Sub
Sub getcoords(ByRef xcoord As Byte, ByRef ycoord As Byte)
    Console.Write("Enter the X coordinate: ")
    xcoord = Console.ReadLine
    Console.Write("Enter the Y coordinate: ")
    ycoord = Console.ReadLine
End Sub
Sub checkhit(ByRef xcoord As Byte, ByRef ycoord As Byte, ByRef board(,) As Char, ByRef hits As Byte)
    Console.Clear()
    If board(xcoord, ycoord) = "S" Then
        Console.WriteLine("Hit!")
        hits = hits + 1
        board(xcoord, ycoord) = "X"
    Else
        Console.WriteLine("Miss!")
        board(xcoord, ycoord) = "M"
    End If
End Sub
Sub checkwins(ByRef hits As Byte, ByRef bombs As Byte, ByRef gameover As Boolean)
    If hits = 50 Or bombs = 0 Then gameover = True
End Sub
End Module

感谢您的帮助:)

乍一看,您需要第二个阵列来保持船的位置,并检查第二个阵列是否命中目标

Dim shipPos(9, 9) As Char
然后更改addships subs以使用此数组

Sub addships(ByRef shipPos(,) As Char)
    shipPos(9, 1) = "S"
    shipPos(9, 2) = "S"
    shipPos(1, 2) = "S"
    shipPos(1, 3) = "S"
    shipPos(1, 4) = "S"
    shipPos(1, 5) = "S"
    shipPos(4, 4) = "S"
    shipPos(5, 4) = "S"
    shipPos(6, 4) = "S"
    shipPos(4, 5) = "S"
    shipPos(5, 5) = "S"
    shipPos(6, 5) = "S"
    shipPos(3, 8) = "S"
    shipPos(4, 8) = "S"
    shipPos(5, 8) = "S"
    shipPos(6, 8) = "S"
End Sub
最后根据shipos阵列检查命中率

Sub checkhit(ByRef xcoord As Byte, ByRef ycoord As Byte, ByRef board(,) As Char, ByRef hits As Byte)
    Console.Clear()
    If shipPos(xcoord, ycoord) = "S" Then
        Console.WriteLine("Hit!")
        hits = hits + 1
        board(xcoord, ycoord) = "X"
    Else
        Console.WriteLine("Miss!")
        board(xcoord, ycoord) = "M"
    End If
End Sub
另外,您的实际代码错过了对
addships
的调用,我认为您还需要一个初始化例程,用空格填充电路板,否则输出显示将无法正确对齐

addships(shippos)
initboard(board)
do
   ....
while
这些问题很容易解决,但是需要一个更复杂的算法来将这些船舶定位在随机位置,而不会在它们之间产生冲突