修改代码,使其不显示船只的位置

时间:2017-01-01 13:45:50

标签: vb.net

我正在创建一个战舰游戏,它是一个单人游戏,船只的坐标已经硬编码,你想要猜测它们的位置。我遇到一些麻烦,让它认识到哪些部分是由船只占用的,哪些不是。目前,整个电路板显然没有被船只占用,当我输入船舶占用的坐标时,它说我错过了。

我不希望电路板显示船只的位置,否则会让游戏变得太容易。

以下是我的代码:

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

感谢任何帮助:)

1 个答案:

答案 0 :(得分:1)

乍一看,我会说你需要第二个阵列来保持船位,并检查第二个阵列是否有击中目标

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

这些问题很容易解决,但是需要更复杂的算法来将这些船舶放置在随机位置而不会发生冲突