Go Fish Translation

General FreeBASIC programming questions.
Post Reply
NorbyDroid
Posts: 70
Joined: May 21, 2016 22:55

Go Fish Translation

Post by NorbyDroid »

I was wondering if I could get some assistance translating code.

To make things easier I am posting the code here:

Code: Select all

' Go Fish ~ ¡Pesca!

Const cartas = "A234567890JQK"

Declare Sub Reparto_Cartas
Declare Sub Pescar_Carta_Jug
Declare Sub Pescar_Carta_CPU
Declare Sub Comprobar_Libro_Jug
Declare Sub Comprobar_Libro_CPU
Declare Sub Comprobar_Fin_Partida
Declare Sub Intro

Dim Shared As Integer play(13), compu(13), deck(13), guess(13), poss(13), asked(13)
Dim Shared As String nombre, Snombre, CartaPedida
Dim Shared puntos(2) As Byte = {0,0}
Dim Shared As Integer remca = 4*Len(cartas)
Dim Shared As Integer i, k, j, cn
For i = 1 To 13
    deck(i) = 4
Next i
For i = 1 To 9
    Reparto_Cartas
    deck(k) -= 1
    compu(k) += 1
    Reparto_Cartas
    deck(k) -= 1
    play(k) += 1
Next i
Dim As Integer v, po

Sub Reparto_Cartas
    remca -= 1
    Dim As Integer sc = remca * Rnd + 1
    For k = 1 To 13
        sc -= deck(k)
        If sc <= 0 Then Return
    Next k
End Sub

Sub Pescar_Carta_Jug
    Reparto_Cartas
    Print " " &Mid(cartas,k,1) &"."
    deck(k) -= 1
    play(k) += 1
End Sub

Sub Pescar_Carta_CPU
    Reparto_Cartas
    Print "a carta."
    deck(k) -= 1
    compu(k) += 1
End Sub

Sub Comprobar_Libro_Jug
    For i = 1 To 13
        If play(i) <> 4 Then
        Else
            Color 11: Print Snombre &" completa el libro de " &Mid(cartas,i,1) &"'s.": Color 7
            play(i) = 0
            puntos(0) += 1
        End If
    Next i
End Sub

Sub Comprobar_Libro_CPU
    For i = 1 To 13
        If compu(i) <> 4 Then
        Else
            Color 11: Print Snombre &" completa el libro de " &Mid(cartas,i,1) &"'s.": Color 7
            compu(i) = 0
            puntos(1) += 1
        End If
    Next i
End Sub

Sub Comprobar_Fin_Partida
    Dim As Integer np = 0, nc = 0
    For i = 1 To 13
        np += play(i)
        nc += compu(i)
    Next i
    If remca = 0 Or np = 0 Or nc = 0 Then         
        Color 15: Print
        Print "*** ­FIN de la partida! ***"
        Print
        If puntos(0) < puntos(1) Then 
            Print "La CPU ha ganado."
        Elseif puntos(0) > puntos(1) Then 
            Print nombre &" ha ganado." 
        Else 
            Print "­Es un empate!"
        End If
        Sleep: End
    End If
End Sub

Sub Intro
    Color 15 
    Print "                __ _     _      "
    Print "  __ _  ___    / _(_)___| |__   "
    Print " /  ` |/ _ \  | |_| / __| '_ \  "
    Print "| (_) | (_) | |  _| \__ \ | | | "
    Print " \__, |\___/  |_| |_|___/_| |_| "
    Print " |___/                          "
    Print "                                "
    Color 14: Locate 10, 2: Input "Como te llamas: ", nombre
End Sub

'--- Programa Principal ---
Cls
Randomize Timer
Intro
Do
    Dim As boolean MuestraMano = false
    While MuestraMano = false
        Color 15: Print Chr(10) &"Puntos >> " &nombre &": "; puntos(0); "  CPU: "; puntos(1)
        Color 13: Print Chr(10) &space(10) &remca &" cartas restantes"
        Color 14: Print Chr(10) &"Tu mano: ";
        For i = 1 To 13
            If Not play(i) Then 
                For j = 1 To play(i)
                    Print Mid(cartas,i,1); " ";
                Next j
            End If
        Next i
        Print
        
        Dim As boolean PideCarta = false
        While PideCarta = false
            Comprobar_Fin_Partida
            Snombre = nombre
            Color 7: Print
            Input "¨Que carta pides... "; CartaPedida
            Print
            If CartaPedida <> "" Then cn = Instr(cartas, Ucase(CartaPedida)): PideCarta = true
            If cn = 0 Then 
                Print "Lo siento, no es una opción valida.": PideCarta = false
                Elseif play(cn) = 0 Then Color 12: Print "­No tienes esa carta!": Color 7: PideCarta = false
            End If
        Wend
        
        guess(cn) = 1
        If compu(cn) = 0 Then 
            Print Snombre &", ";
            Color 15: Print "­ve a pescar!"
            Color 7: Print Snombre &" pesca un";: Pescar_Carta_Jug
            Comprobar_Libro_Jug
            MuestraMano = true
        Else
            v = compu(cn)
            compu(cn) = 0
            play(cn) += v
            Print Snombre &" consigue " &v &" carta(s) mas."
            Comprobar_Libro_Jug
            MuestraMano = false
        End If
    Wend

    Snombre = "CPU"
    For i = 1 To 13
        asked(i) = 0
    Next i
    Dim As boolean Turno_CPU_2 = false
    While Turno_CPU_2 = false
        Comprobar_Fin_Partida
        po = 0
        For i = 1 To 13
            If (compu(i) > 0) And (guess(i) > 0) Then poss(i) = 1: po += 1
        Next i
        If po = 0 Then 
            Do
                k = (Rnd*12)+1
            Loop While compu(k) = 0 Or asked(k)
        Else
            Do
                k = (Rnd*12)+1
            Loop While poss(k) = 0
            guess(k) = 0
            asked(k) = 1
        End If

        Print: Print Snombre &" quiere tus " &Mid(cartas,k,1) &"'s."
        asked(k) = 1
        If play(k) = 0 Then 
            Print Snombre &", ";
            Color 15: Print "­ve a pescar!"
            Color 7:Print Snombre &" pesca un";: Pescar_Carta_CPU
            Comprobar_Libro_CPU
            Turno_CPU_2 = true
        Else
            v = play(k)
            play(k) = 0
            compu(k) += v
            Print Snombre &" consigue " &v &" carta(s) mas."
            Comprobar_Libro_CPU
            Turno_CPU_2 = false
        End If
    Wend 
Loop
End
Original Code: https://rosettacode.org/wiki/Go_Fish

My Translation:

Code: Select all

' Go Fish ~ ¡Pesca!

Const Letters = "A234567890JQK"

Declare Sub Cast_Letters
Declare Sub Fish_Letter_Player
Declare Sub Fish_Letter_CPU
Declare Sub Check_Book_Player
Declare Sub Check_Book_CPU
Declare Sub Check_End_Game
Declare Sub Intro

Dim Shared As Integer play(13), compu(13), deck(13), guess(13), poss(13), asked(13)
Dim Shared As String Name, SName, LetterRequested
Dim Shared Points(2) As Byte = {0,0}
Dim Shared As Integer remca = 4*Len(Letters)
Dim Shared As Integer i, k, j, cn
For i = 1 To 13
    deck(i) = 4
Next i
For i = 1 To 9
    Cast_Letters
    deck(k) -= 1
    compu(k) += 1
    Cast_Letters
    deck(k) -= 1
    play(k) += 1
Next i
Dim As Integer v, po

Sub Cast_Letters
    remca -= 1
    Dim As Integer sc = remca * Rnd + 1
    For k = 1 To 13
        sc -= deck(k)
        If sc <= 0 Then Return
    Next k
End Sub

Sub Fish_Letter_Player
    Cast_Letters
    Print " " &Mid(Letters,k,1) &"."
    deck(k) -= 1
    play(k) += 1
End Sub

Sub Fish_Letter_CPU
    Cast_Letters
    Print "a Letter."
    deck(k) -= 1
    compu(k) += 1
End Sub

Sub Check_Book_Player
    For i = 1 To 13
        If play(i) <> 4 Then
        Else
            Color 11: Print SName &" Complete the book of " &Mid(Letters,i,1) &"'s.": Color 7
            play(i) = 0
            Points(0) += 1
        End If
    Next i
End Sub

Sub Check_Book_CPU
    For i = 1 To 13
        If compu(i) <> 4 Then
        Else
            Color 11: Print SName &" Complete the book of " &Mid(Letters,i,1) &"'s.": Color 7
            compu(i) = 0
            Points(1) += 1
        End If
    Next i
End Sub

Sub Check_End_Game
    Dim As Integer np = 0, nc = 0
    For i = 1 To 13
        np += play(i)
        nc += compu(i)
    Next i
    If remca = 0 Or np = 0 Or nc = 0 Then         
        Color 15: Print
        Print "*** ­END of the game! ***"
        Print
        If Points(0) < Points(1) Then 
            Print "The CPU has won."
        Elseif Points(0) > Points(1) Then 
            Print Name &" has won." 
        Else 
            Print "­It's a tie!"
        End If
        Sleep: End
    End If
End Sub

Sub Intro
    Color 15 
    Print "                __ _     _      "
    Print "  __ _  ___    / _(_)___| |__   "
    Print " /  ` |/ _ \  | |_| / __| '_ \  "
    Print "| (_) | (_) | |  _| \__ \ | | | "
    Print " \__, |\___/  |_| |_|___/_| |_| "
    Print " |___/                          "
    Print "                                "
    Color 14: Locate 10, 2: Input "What's your name:", Name
End Sub

'--- Main Program ---
Cls
Randomize Timer
Intro
Do
    Dim As boolean SampleHand = false
    While SampleHand = false
        Color 15: Print Chr(10) &"Points >> " &Name &": "; Points(0); "  CPU: "; Points(1)
        Color 13: Print Chr(10) &space(10) &remca &" Letters restantes"
        Color 14: Print Chr(10) &"Your hand: ";
        For i = 1 To 13
            If Not play(i) Then 
                For j = 1 To play(i)
                    Print Mid(Letters,i,1); " ";
                Next j
            End If
        Next i
        Print
        
        Dim As boolean Ask_Letter = false
        While Ask_Letter = false
            Check_End_Game
            SName = Name
            Color 7: Print
            Input "¨What Letter do you ask for?... "; LetterRequested
            Print
            If LetterRequested <> "" Then cn = Instr(Letters, Ucase(LetterRequested)): Ask_Letter = true
            If cn = 0 Then 
                Print "Sorry, not a valid option.": Ask_Letter = false
                Elseif play(cn) = 0 Then Color 12: Print "­You don't have that Letter!": Color 7: Ask_Letter = false
            End If
        Wend
        
        guess(cn) = 1
        If compu(cn) = 0 Then 
            Print SName &", ";
            Color 15: Print "­ve a Fish!"
            Color 7: Print SName &" fishing a";: Fish_Letter_Player
            Check_Book_Player
            SampleHand = true
        Else
            v = compu(cn)
            compu(cn) = 0
            play(cn) += v
            Print SName &" Get " &v &" Letter(s) but."
            Check_Book_Player
            SampleHand = false
        End If
    Wend

    SName = "CPU"
    For i = 1 To 13
        asked(i) = 0
    Next i
    Dim As boolean Turno_CPU_2 = false
    While Turno_CPU_2 = false
        Check_End_Game
        po = 0
        For i = 1 To 13
            If (compu(i) > 0) And (guess(i) > 0) Then poss(i) = 1: po += 1
        Next i
        If po = 0 Then 
            Do
                k = (Rnd*12)+1
            Loop While compu(k) = 0 Or asked(k)
        Else
            Do
                k = (Rnd*12)+1
            Loop While poss(k) = 0
            guess(k) = 0
            asked(k) = 1
        End If

        Print: Print SName &" wants your " &Mid(Letters,k,1) &"'s."
        asked(k) = 1
        If play(k) = 0 Then 
            Print SName &", ";
            Color 15: Print "­ve a Fish!"
            Color 7:Print SName &" fishing a";: Fish_Letter_CPU
            Check_Book_CPU
            Turno_CPU_2 = true
        Else
            v = play(k)
            play(k) = 0
            compu(k) += v
            Print SName &" Get " &v &" Letter(s) but."
            Check_Book_CPU
            Turno_CPU_2 = false
        End If
    Wend 
Loop
End
Although I did translate using a translator, some variable are still confusing to me. Maybe someone can help. Thank you.
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Go Fish Translation

Post by fxm »

'Name' is already a FreeBASIC keyword.
Code with for example '_Name' instead:

Code: Select all

' Go Fish ~ ¡Pesca!

Const Letters = "A234567890JQK"

Declare Sub Cast_Letters
Declare Sub Fish_Letter_Player
Declare Sub Fish_Letter_CPU
Declare Sub Check_Book_Player
Declare Sub Check_Book_CPU
Declare Sub Check_End_Game
Declare Sub Intro

Dim Shared As Integer play(13), compu(13), deck(13), guess(13), poss(13), asked(13)
Dim Shared As String _Name, SName, LetterRequested
Dim Shared Points(2) As Byte = {0,0}
Dim Shared As Integer remca = 4*Len(Letters)
Dim Shared As Integer i, k, j, cn
For i = 1 To 13
    deck(i) = 4
Next i
For i = 1 To 9
    Cast_Letters
    deck(k) -= 1
    compu(k) += 1
    Cast_Letters
    deck(k) -= 1
    play(k) += 1
Next i
Dim As Integer v, po

Sub Cast_Letters
    remca -= 1
    Dim As Integer sc = remca * Rnd + 1
    For k = 1 To 13
        sc -= deck(k)
        If sc <= 0 Then Return
    Next k
End Sub

Sub Fish_Letter_Player
    Cast_Letters
    Print " " &Mid(Letters,k,1) &"."
    deck(k) -= 1
    play(k) += 1
End Sub

Sub Fish_Letter_CPU
    Cast_Letters
    Print "a Letter."
    deck(k) -= 1
    compu(k) += 1
End Sub

Sub Check_Book_Player
    For i = 1 To 13
        If play(i) <> 4 Then
        Else
            Color 11: Print SName &" Complete the book of " &Mid(Letters,i,1) &"'s.": Color 7
            play(i) = 0
            Points(0) += 1
        End If
    Next i
End Sub

Sub Check_Book_CPU
    For i = 1 To 13
        If compu(i) <> 4 Then
        Else
            Color 11: Print SName &" Complete the book of " &Mid(Letters,i,1) &"'s.": Color 7
            compu(i) = 0
            Points(1) += 1
        End If
    Next i
End Sub

Sub Check_End_Game
    Dim As Integer np = 0, nc = 0
    For i = 1 To 13
        np += play(i)
        nc += compu(i)
    Next i
    If remca = 0 Or np = 0 Or nc = 0 Then         
        Color 15: Print
        Print "*** ­END of the game! ***"
        Print
        If Points(0) < Points(1) Then 
            Print "The CPU has won."
        Elseif Points(0) > Points(1) Then 
            Print _Name &" has won." 
        Else 
            Print "­It's a tie!"
        End If
        Sleep: End
    End If
End Sub

Sub Intro
    Color 15 
    Print "                __ _     _      "
    Print "  __ _  ___    / _(_)___| |__   "
    Print " /  ` |/ _ \  | |_| / __| '_ \  "
    Print "| (_) | (_) | |  _| \__ \ | | | "
    Print " \__, |\___/  |_| |_|___/_| |_| "
    Print " |___/                          "
    Print "                                "
    Color 14: Locate 10, 2: Input "What's your name:", _Name
End Sub

'--- Main Program ---
Cls
Randomize Timer
Intro
Do
    Dim As boolean SampleHand = false
    While SampleHand = false
        Color 15: Print Chr(10) &"Points >> " &_Name &": "; Points(0); "  CPU: "; Points(1)
        Color 13: Print Chr(10) &space(10) &remca &" Letters restantes"
        Color 14: Print Chr(10) &"Your hand: ";
        For i = 1 To 13
            If Not play(i) Then 
                For j = 1 To play(i)
                    Print Mid(Letters,i,1); " ";
                Next j
            End If
        Next i
        Print
        
        Dim As boolean Ask_Letter = false
        While Ask_Letter = false
            Check_End_Game
            SName = _Name
            Color 7: Print
            Input "¨What Letter do you ask for?... "; LetterRequested
            Print
            If LetterRequested <> "" Then cn = Instr(Letters, Ucase(LetterRequested)): Ask_Letter = true
            If cn = 0 Then 
                Print "Sorry, not a valid option.": Ask_Letter = false
                Elseif play(cn) = 0 Then Color 12: Print "­You don't have that Letter!": Color 7: Ask_Letter = false
            End If
        Wend
        
        guess(cn) = 1
        If compu(cn) = 0 Then 
            Print SName &", ";
            Color 15: Print "­ve a Fish!"
            Color 7: Print SName &" fishing a";: Fish_Letter_Player
            Check_Book_Player
            SampleHand = true
        Else
            v = compu(cn)
            compu(cn) = 0
            play(cn) += v
            Print SName &" Get " &v &" Letter(s) but."
            Check_Book_Player
            SampleHand = false
        End If
    Wend

    SName = "CPU"
    For i = 1 To 13
        asked(i) = 0
    Next i
    Dim As boolean Turno_CPU_2 = false
    While Turno_CPU_2 = false
        Check_End_Game
        po = 0
        For i = 1 To 13
            If (compu(i) > 0) And (guess(i) > 0) Then poss(i) = 1: po += 1
        Next i
        If po = 0 Then 
            Do
                k = (Rnd*12)+1
            Loop While compu(k) = 0 Or asked(k)
        Else
            Do
                k = (Rnd*12)+1
            Loop While poss(k) = 0
            guess(k) = 0
            asked(k) = 1
        End If

        Print: Print SName &" wants your " &Mid(Letters,k,1) &"'s."
        asked(k) = 1
        If play(k) = 0 Then 
            Print SName &", ";
            Color 15: Print "­ve a Fish!"
            Color 7:Print SName &" fishing a";: Fish_Letter_CPU
            Check_Book_CPU
            Turno_CPU_2 = true
        Else
            v = play(k)
            play(k) = 0
            compu(k) += v
            Print SName &" Get " &v &" Letter(s) but."
            Check_Book_CPU
            Turno_CPU_2 = false
        End If
    Wend 
Loop
Post Reply