Battleship for two players!


#1



I tried to add more functions to the game and now it's a game for players. Player 1 can type in the coordinate of the battleship and player 2 can guess the coordinate.

But the problem is that when player 1 type in the coordinate, the answer will be in the console and player 2 will see the coordinate directly. So I want to know how to hide the text in the console.


board = []

for i in range(5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

print "Let's play Battleship!"
print_board(board)

def type_row(board):
    board = int(raw_input("Type in the number of row(between 0 and 4): "))
    if board < 0 or board > 4:
        print "Invalid number of row."
        type_row(board)
    return board

def type_col(board):
    board = int(raw_input("Type in the number of column(between 0 and 4): "))
    if board < 0 or board > 4:
        print "Invalid number of column."
        type_col(board)
    return board

ship_row = type_row(board)
ship_col = type_col(board)

for turn in range(5):
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Column:"))

    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
        break
    else:
        if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
            print "Oops, that's not even in the ocean."
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
        else:
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"
       
 
    print "Turn" , turn + 1, "is over."
    print_board(board)
    if turn == 4:
        print "Game over, the battleship lies at (", ship_row, ",", ship_col, ")"


#2

It isn't all that uncommon for applications to require passwords which shouldn't show up as they are typed:

import getpass

getpass.getpass('Row number: ')

Alternatively you could print a bunch of newlines just to get it off the screen.
There's probably some way to over-write previous output too.


#3

The input method would have to return a string of backspace characters.


#4

No I mean actually setting the row and column of the cursor, don't know to what degree it's at the mercy of the OS/terminal.


#5

But wouldn't even a rudimentary terminal be able to backspace over the inputs? All it needs is a count of the key presses.


#6

Well, this works as intended in cinnamon's terminal emulator (based on gnome's terminal emulator):

C#

using System;

class CursorPos {
    static void Main() {
        Console.SetCursorPosition(5, 5);
        Console.WriteLine("writing starting at 5, 5 (from top-left corner)");
    }
}

Compile and run (requires mono):

mcs CursorPos.cs
./CursorPos.exe

How is that implemented? Don't really know, but I'd be very surprised if Python can't do the very same thing. Might be googleable or findable in mono's source code. There are escape sequences, don't know if that's what the above uses, but that would probably be fine for most uses.

The last time I did anything that moved the cursor around was with C# on windows, so I wanted to see if the same works in the free world as well, hence the C#.


#7

But I still don't know how to adjust my code in order to "hide" the output.....:joy:


#8

What is stopping you?


#9

When I used the "getpass" code, the console prints a sentence with red color: Warning: Password input may be echoed.


#10

Sounds like that terminal doesn't support hiding what's typed, so you'll need another terminal or another method of hiding.


#11

:sweat:What do you mean by another terminal?


#12

Or do you know another way to let the player type in the number without showing it on the console?:slight_smile:


#13

The program taking keystrokes and displaying text.

Another way? I suggested three ways in my first post.