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.
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 .
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.
Editing it would be helpful . Haha I’m not good at this.
@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 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?
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.
[quote=“mtf, post:13, topic:11348”]
Lots of predicatible outcomes generally spells weak algorithm as probability goes.
[/quote]What do you mean? I have no idea what you are talking about.
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.
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.
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,
If the program is fixed in some way, then fix the function to match, that way no inputs are needed.