"Battleship" level in Python


#1

Translating Python to JS isn't fun... haha

So... you know the "Battleship" level in Python? I've been trying to translate it to JavaScript and is absolutely failing...

Yeah, I'm not good at programming. Mostly because this is my first time. :smiley:

You know the "From random import randint" part? I have no idea how to translate that to JS. Anyways, There's a lot more problems in here, so... please help :smiley:.

var randint = Math.random(Math.floor());

var board = [];
for(var x in range(5)){
    board.append(["O"] * 5);
}
function print_board(board){
    for (var row in board){
        console.log( " ".join(row));
}
}
console.log( "Let's play Battleship!");
print_board(board);

function random_row(board){
    return randint(0, len(board) - 1);
}
function random_col(board){
    return randing(0, len(board[0]) - 1);
}
var ship_row = random_row(board);
var ship_col = random_col(board);
console.log(ship_row);
console.log(ship_col);

//Everything from here on should go in your for loop!
//Be sure to indent four spaces!
var guess_row = prompt("Guess Row:");
var guess_col = prompt("Guess Col:");

if (guess_row == ship_row && guess_col == ship_col){
    console.log("Congratulations! You sunk my battleship!"); 
}
else{
    if (guess_row < 0 || guess_row > 4 || guess_col < 0 || guess_col > 4){
        console.log("Oops, that's not even in the ocean.");
    }
    else if(board[guess_row][guess_col] == "X"){
        console.log("You guessed that one already.");
    }
    else{
        console.log("You missed my battleship!");
        board[guess_row][guess_col] = "X";
        if(turn == 3){
            console.log("Game Over");
        }
        for(var turn in range(4)){
            console.log("Turn", turn + 1);
            print_board(board);
        }
    }
}

Okay, I can't seem to download the editing thing, so I'll have to copy paste it on notes. :expressionless:

Editing it would be helpful :smiley:. Haha I'm not good at this. :smiley:


#2

One tip, you can't really .append in JavaScript. Something you might do there is board[0] = ["0", "0", "0", "0", "0"];


#3

Yeah... I knew that. :smiley:
TOTALLY. Please tell me everything that goes wrong, because I can't really tell differences.... :smiley:


#4

@ragezapper take a look at the code below. its your original code corrected but you would need to add some more handlers for some cases like when the user enters nothing

//===========================Globals=============================
var board = [];
var turn = 1;
var ship_row ;
var ship_col;

//populate board
for(var x = 0; x<5; x++){
    board.push([]);
    for(var y = 0 ;y<5 ;y++){
        board[x].push("O");
    }
}

//=======================Functions=================================
// generate ramdom number within a range
function randint(min, max) {
    return Math.floor(Math.random() * (max - min) + min);
}
//print a message to console
function print(message){
    console.log(message);
}
//print board formatted
function print_board(board){
    for(var x= 0; x<board.length;x++){
        print(board[x].join(" "));
    }     
}
//generate a random row  in from array
function random_row(board){
    return randint(0, board.length - 1);

}
//generate a random column  in from array
function random_col(board){
    return randint(0, board.length - 1);
}

//====================debugging/trace statements===================
//print(board);
//print_board(board);
ship_row = random_row(board);
ship_col = random_col(board);

//print(ship_row);
//print(ship_col);


//==========================Application Code========================
print( "Let's play Battleship!");
for (;;){
    print("Turn "+ (turn ));
    print_board(board);
    var guess_row = prompt("Guess Row:");
    var guess_col = prompt("Guess Col:");
    if (guess_row == ship_row && guess_col == ship_col){
        print("Congratulations! You sunk my battleship!"); 
        break;
    }
    else{
        var is_valid =(guess_row >=0 && guess_row <= 4) && (guess_col >= 0 && guess_col <= 4);
        if (is_valid){
    	    if(board[guess_row][guess_col] == "X"){
        	    print("You guessed that one already.");
    	    }
            else{
                print("You missed my battleship!");
            }

        	board[guess_row][guess_col] = "X";
        }
        else{
            print("Oops, that's not even in the ocean.");
        }
        turn++;
        if(turn == 6){
            print("Game Over");
    	    break;
        }
    }
}

You can test it here too
Click here to labs


#6

There is no attribution data in the linked page. Is it yours, or
somebody else's? Please give that information in lab posted code. Thank
you.


#7

@mtf sorry i don't you. What must be added again


#8

You linked to a page in the lab but there is no attribution information in that page. I am suspicious of such pages since they are typically repro's of pages ranging in the pastures of the internet. If it's not yours, then say so. If it is yours, then attribute it to yourself. I will usually write 'study by Roy' or something that says this is my scratch sketch, and not someone else's in new clothes. If it isn't mine, then why should I mind crediting whomever it belongs to?


#9

Ohh I see. I will update the link


#10

And we're going to believe that you dreamt this up, all by yourself? Give us some credit and take a little less yourself. Please. Let's be honest here, and credit others upstream who feed us with ideas and mechanics.


#11

In other words.... 'Study by Eric, et al'


#12

Yeah i will go with that sounds simpler and better


#13

Time for a test drive.

function randint(min, max) {
    return Math.floor(Math.random() * (max - min) + min);
}

Using the lab console,

 > randint()
=> NaN
 > randint(0)
=> NaN
 > randint(0,0)
=> 0
 > randint(1)
=> NaN
 > randint(1,1)
=> 1
 > randint(1,0)
=> 0
 > randint(0, 1)
=> 0

Lots of predicatible outcomes generally spells weak algorithm as probability goes.


#14

Okay.... you guys are confusing me. :smiley:

FYI: I am using labs, that's how I got it.

Anyways,

What do you mean? I have no idea what you are talking about.:smiley:

You said there were lots of predictable outcomes, which spells weak algorithm in probability. Doesn't that mean that it's good? Sorry, I can't really understand.:confused:


#15

For something to be truly random, it cannot have any predictability, only probability, much like a photon is not predictible, but we can venture where it is more likely to be (the double slit experiment, as an example). What I was pointing out above is that false inputs could produce expected results. Our algorithm would need to be hardened against such eventualities.


#16

Ohhhh.... so what has to be made to help that?:neutral_face:

Is there like a function or something? Like, how do you make the algorithm stronger?


#17

A couple of things to consider...

  1. When there are inputs, the program should check and validate those inputs to be sure they are the correct type and in range, and that none are missing,
  2. If the program is fixed in some way, then fix the function to match, that way no inputs are needed.

#18

Hmmm.... so are you saying that there should be something like a function with a for loop? That checks each answer?:confused:


#19

Referring only to the randint function above, inputs meaning parameters. We want to be sure that the program can't send invalid calls to the function.


#20

I don't get the second point


#21

fixed => set, constant