Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Haskell Help!! I have a Javascript code like this. Could someone help me transfe

ID: 3861906 • Letter: H

Question

Haskell Help!!
I have a Javascript code like this. Could someone help me transfer it to Haskell code? Thanks a lot.

module.exports = {
onePlayerOneMove: onePlayerOneMove
}

var helpers = require('./helpers');

function onePlayerOneMove(game) {

function whatever(move) {
var flag = 1;
var row = game.length;
var col = game[0].length;
move = parseInt(move);
console.log(game);
//convert all the space to integer
for (var i = 0; i < row; i++) {
for (var j = 0; j < col; j++) {
game[i][j] = parseInt(game[i][j]);
}
}
  
var gameCopy = new Array();
for(var i = 0; i < row; i++){
gameCopy[i] = new Array();
for(var j = 0 ; j < col; j++ ){
gameCopy[i][j] = -1;
}
}
console.log();
console.log(gameCopy);
  
//Identify all the cluster and label them by 1st, 2nd, 3rd, 4th...
for(var i=0; i<row; i++) {
for(var j=0; j<col; j++) {
if(gameCopy[i][j] == -1) {
fill(i, j, game[i][j], flag++);
}
}
}
console.log();
console.log(gameCopy);

function fill(i, j, val, flag) {

// flag this cell
gameCopy[i][j] = flag;
// check if bottom cell can be flaged
if(i+1 < row) {
if(gameCopy[i+1][j] == -1 && game[i+1][j] == val) {
gameCopy[i+1][j] = flag;
//console.log("take from " + (i+1) + " " + j);
fill(i+1, j, val, flag);
}
}
  
// check if top cell can be flaged
if(i-1 >= 0) {
if(gameCopy[i-1][j] == -1 && game[i-1][j] == val) {
gameCopy[i-1][j] = flag;
//console.log("take from" + (i-1) + " " + j);
fill(i-1, j, val, flag);
}
}
  
// check if right cell can be flaged
if(j+1 < col) {
if(gameCopy[i][j+1] == -1 && game[i][j+1] == val) {
gameCopy[i][j+1] = flag;
//console.log("take from " + i + " " + (j+1));
fill(i, j+1, val, flag);
}

}
  
// check if left cell can be flaged
if(j-1 >= 0) {
if(gameCopy[i][j-1] == -1 && game[i][j-1] == val) {
gameCopy[i][j-1] = flag;
//console.log("take from " + i + " " + (j-1));
fill(i, j-1, val, flag);
}
}
}

//make change in the cluster based on the move
for (var i = 0; i < row; i++) {
for (var j = 0; j < col; j++) {
if(gameCopy[i][j] == move){
game[i][j] = 1;

}
}
}
return game;
}
return whatever;
}

Explanation / Answer

module.exports = {
onePlayerOneMove: onePlayerOneMove
}

var helpers = require('./helpers');

function onePlayerOneMove(game) {

function whatever(move) {
var flag = 1;
var row = game.length;
var col = game[0].length;
move = parseInt(move);
console.log(game);
//convert all the space to integer
for (var i = 0; i < row; i++) {
for (var j = 0; j < col; j++) {
game[i][j] = parseInt(game[i][j]);
}
}
  
var gameCopy = new Array();
for(var i = 0; i < row; i++){
gameCopy[i] = new Array();
for(var j = 0 ; j < col; j++ ){
gameCopy[i][j] = -1;
}
}
console.log();
console.log(gameCopy);
  
//Identify all the cluster and label them by 1st, 2nd, 3rd, 4th...
for(var i=0; i<row; i++) {
for(var j=0; j<col; j++) {
if(gameCopy[i][j] == -1) {
fill(i, j, game[i][j], flag++);
}
}
}
console.log();
console.log(gameCopy);

function fill(i, j, val, flag) {

// flag this cell
gameCopy[i][j] = flag;
// check if bottom cell can be flaged
if(i+1 < row) {
if(gameCopy[i+1][j] == -1 && game[i+1][j] == val) {
gameCopy[i+1][j] = flag;
//console.log("take from " + (i+1) + " " + j);
fill(i+1, j, val, flag);
}
}
  
// check if top cell can be flaged
if(i-1 >= 0) {
if(gameCopy[i-1][j] == -1 && game[i-1][j] == val) {
gameCopy[i-1][j] = flag;
//console.log("take from" + (i-1) + " " + j);
fill(i-1, j, val, flag);
}
}
  
// check if right cell can be flaged
if(j+1 < col) {
if(gameCopy[i][j+1] == -1 && game[i][j+1] == val) {
gameCopy[i][j+1] = flag;
//console.log("take from " + i + " " + (j+1));
fill(i, j+1, val, flag);
}

}
  
// check if left cell can be flaged
if(j-1 >= 0) {
if(gameCopy[i][j-1] == -1 && game[i][j-1] == val) {
gameCopy[i][j-1] = flag;
//console.log("take from " + i + " " + (j-1));
fill(i, j-1, val, flag);
}
}
}

//make change in the cluster based on the move
for (var i = 0; i < row; i++) {
for (var j = 0; j < col; j++) {
if(gameCopy[i][j] == move){
game[i][j] = 1;

}
}
}
return game;
}
return whatever;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote