if消除术之 Map + Function

if消除术之 Map + Function

需求描述

设计一个简易计算器,只支持 a+b, a-b, a*b, a/b

具体实现

利用Map,key是具体的加减乘除操作,value是一个Function

package com.wy.ifkiller;

import cn.hutool.core.map.MapUtil;

import java.util.Map;
import java.util.function.BiFunction;

import static com.wy.ifkiller.FunctionMap.Action.ADD;
import static com.wy.ifkiller.FunctionMap.Action.DIV;
import static com.wy.ifkiller.FunctionMap.Action.MUL;
import static com.wy.ifkiller.FunctionMap.Action.SUB;

/**
 * @author HelloWorld
 * @date 2023/10/7 17:21
 * @email helloworld.dng@gmail.com
 */
public class FunctionMap {
    private Map<Action, BiFunction<Integer, Integer, Integer>> getActionMap() {
        return MapUtil.<Action, BiFunction<Integer, Integer, Integer>>builder()
                .put(ADD, this::add)
                .put(SUB, this::sub)
                .put(MUL, this::mul)
                .put(DIV, this::div)
                .map();
    }

    public static void main(String[] args) {
        FunctionMap functionMap = new FunctionMap();
        Map<Action, BiFunction<Integer, Integer, Integer>> actionMap = functionMap.getActionMap();

        System.out.println(actionMap.get(ADD).apply(3, 2));
        System.out.println(actionMap.get(SUB).apply(3, 2));
    }

    private int add(int a, int b) {
        return a + b;
    }

    private int sub(int a, int b) {
        return a - b;
    }

    private int mul(int a, int b) {
        return a * b;
    }

    private int div(int a, int b) {
        return a / b;
    }

    enum Action {
        ADD,
        SUB,
        MUL,
        DIV;
    }
}
解释下面代码game.map = { startX: 40.5, //棋盘X坐标 startY: 60.5, //棋盘Y坐标 width: game.cellCount * game.cellWidth, height: game.cellCount * game.cellWidth, bubbles: [], init: function () { for (var i = 0; i < game.cellCount; i++) { var row = []; for (var j = 0; j < game.cellCount; j++) { row.push(new Bubble(j, i, null)); } this.bubbles.push(row); } }, clearLine: function (x1, y1, color, isClick) { if (this.isEmpty(x1, y1)) { if (isClick) game.ready.flyin(); return; }; //给定一个坐标,看是否有满足的line可以被消除 //4根线 一 | / \ //横线 var current = this.getBubble(x1, y1); if (!current.color) { console.log(current); } var arr1, arr2, arr3, arr4; arr1 = this.bubbles[y1]; arr2 = []; for (var y = 0; y < game.cellCount; y++) arr2.push(this.getBubble(x1, y)); arr3 = [current]; arr4 = [current]; for (var i = 1; i < game.cellCount ; i++) { if (x1 - i >= 0 && y1 - i >= 0) arr3.unshift(this.getBubble(x1 - i, y1 - i)); if (x1 + i < game.cellCount && y1 + i < game.cellCount) arr3.push(this.getBubble(x1 + i, y1 + i)); if (x1 - i >= 0 && y1 + i < game.cellCount) arr4.push(this.getBubble(x1 - i, y1 + i)); if (x1 + i < game.cellCount && y1 - i >= 0) arr4.unshift(this.getBubble(x1 + i, y1 - i)); } var line1 = getLine(arr1); var line2 = getLine(arr2); var line3 = getLine(arr3); var line4 = getLine(arr4); var line = line1.concat(line2).concat(line3).concat(line4); if (line.length < 5) { if (isClick) game.ready.flyin(); return; } else { var me = this; var i = 0; game.play("clearline", function () { if (i == line.length) { game.score.addScore(line.length); game.stop("clearline"); me.isMoving = false; //game.ready.flyin(); return; } me.isMoving = true; var p = line[i]; me.setBubble(p.x, p.y, null); i++; }, 100); }
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值