package com.xjrsoft.common.utils;

import com.wf.captcha.ArithmeticCaptcha;

/**
 * 图形验证码（算术运算）
 */
public class FixedArithmeticCaptcha extends ArithmeticCaptcha {
    public FixedArithmeticCaptcha(int width, int height) {
        super(width, height);
    }

    @Override
    protected char[] alphas() {
        // 生成随机数字和运算符
        int n1 = num(1, 10), n2 = num(1, 10);
        int opt = num(3);
        // 处理减法结果不能为负数
        if (opt == 1 && n2 > n1) {
            int tmp = n2;
            n2 = n1;
            n1 = tmp;
        }

        // 计算结果
        int res = new int[]{n1 + n2, n1 - n2, n1 * n2}[opt];
        // 转换为字符运算符
        char optChar = "+-x".charAt(opt);

        this.setArithmeticString(String.format("%s%c%s=?", n1, optChar, n2));
        this.chars = String.valueOf(res);

        return chars.toCharArray();
    }
}
