Php apply function in array if next entry is integer (Concatenate a valid FEN string)

NVRM

Trying to build a valid FEN string.

Given this 8*8 array example, symbolizing a checker board, ("1" are empty squares):

$checkerboard = [["r","n","b","q","k","b","n","r"],["p","p","p","p","p","p","p","p"],["1","1","1","1","1","1","1","1"],["1","1","1","1","1","1","1","1"],["1","1","1","1","P","1","1","1"],["1","1","1","1","1","1","1","1"],["P","P","P","P","1","P","P","P"],["R","N","B","Q","K","B","N","R"]]

In situ, this is the position:

enter image description here

The valid result I am looking for is:

rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR

And by now my output is:

rnbqkbnr/pppppppp/11111111/11111111/1111P111/11111111/PPPP1PPP/RNBQKBNR

Obviously, any integer entry in the array should be sum to the next, only if the next entry is an integer, and if so the next entry should be discarded till the end.

By iterating the array, I am not sure how to apply something like array_walk() or array_map() here in a simple way. Maybe a simple string operation is enough?

  $FEN = "";
  for ($i = 0;$i < 8;$i++){
    for ($j = 0;$j < 8;$j++){
      if ($checkerboard[$i][$j] === "1"){
        if ($checkerboard[$i][$j + 1] === "1"){
           /* How to iterate till the end */
           $FEN .= (int)$checkerboard[$i][$j] + (int)$checkerboard[$i][$j+1];
        }
      } else {
        $FEN .= $checkerboard[$i][$j];
      }      
    }
    $FEN .= "/";
  }

Any insights?

Example online: https://3v4l.org/tuqqo

Nuwan Dharshana
$checkerboard = [["r","n","b","q","k","b","n","r"],["p","p","p","p","p","p","p","p"],["1","1","1","1","1","1","1","1"],["1","1","1","1","1","1","1","1"],["1","1","1","1","P","1","1","1"],["1","1","1","1","1","1","1","1"],["P","P","P","P","1","P","P","P"],["R","N","B","Q","K","B","N","R"]];

$parts =  array();
foreach ($checkerboard as $innerArray) {
    $num = null;
    $str = '';
    foreach($innerArray as $innerval){
        if(is_numeric($innerval)){
            $num += (int) $innerval;
        }
        else{
            if(!is_null($num)){
                $str .=$num;
                $num = null;
            }
            $str .=$innerval;
        }
    }
    if(!is_null($num)){
        $str .=$num;
    }
    array_push($parts,$str);
}
$result = implode('/',$parts);

above code will generate required output and store it on the $result.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

IntFunction <String>和Function <Integer,String>

PriorityQueue <Map.Entry <String,Integer >>不接受我的地图中的输入对象

将Entry <String,List <String >>转换为Entry <String,String>

Java将一组字符串转换为String Array Integer Array

concatenate a part of two array in one

在PHP中将String推入Array返回null

PHP in_array和String包含

function.apply(argument1, array) 不从数组中获取所有值

存储Integer和array [String]值的最佳数据结构是什么?

flask_login中不存在next_is_valid()?

如何对类型<Entry <Character,Integer >>的ArrayList进行排序?

用Java降序输出Map.Entry <Sentence,Integer>

Map<Integer,Integer> 类型中的 put(Integer, Integer) 方法不适用于参数 (String, Integer)

如何在html中使用php array + string?

Redis缓存Map <Integer,String>

Integer('string')调用的内部工作?

Ada将Integer与String连接

PostgreSQL array_agg(INTEGER [])

numpy-numpy.r _ ['string integer',array]的第三个字符串整数的说明

流<String>到Map <String,Integer>

PHP next()无法正常工作

In PHP how do I rename a string in one function and use the result in a second function

PHP SeekableIterator:捕获OutOfBoundsException或检查valid()方法?

在PHP中,如何将String变量中的Array(字典)转换为Array结构?

PHP 致命错误:Uncaught TypeError: implode(): Argument #2 ($array) must be of type ?array, string given in

如何从 Hashtable<Integer,Hashtable<String,Integer>> h 获取值

通过malloc-heap(Concatenate string)分配内存

将<String,String>映射到TreeMap <Integer,String>

Function.prototype.apply.bind用法?