九宫格验证码实现

本文介绍了一个九宫格汉字验证码生成的实现,包括生成验证码的类`VerifyCodeASNI`,该类能够生成带有汉字的四字符验证码,并且包含扭曲、噪声、颜色等干扰元素。同时提供了一个Servlet `RandCodeServlet`用于处理HTTP请求,生成验证码图片并将其保存到HTTP响应中。
摘要由CSDN通过智能技术生成
web.xml 配置对应路径

package com.shancai.controller.rand;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;

/**
 * 一个能生成验证码的类
 * 默认四个汉字
 * 使用该类生成验证码时
 * @author
 */
public class VerifyCodeASNI {

    private final Random random = new Random(System.nanoTime());
    private int width;
    private int height;
    private int charNumber = 4;
    private float yawpRate = 0.05f; //噪声率
    private OutputStream outputStream;
    private char[] code;
    private char[] code2;
    private List<Integer> codelist;
     
    public VerifyCodeASNI() {
        //empty
    }
   
    /**
     * 返回生成的验证码字符串
     * @return
     */
    public String getVerifyCode(){
        return  getStr(codelist);
    }
   
    public VerifyCodeASNI(int width, int height) {
        this.width = width;
        this.height = height;
    }

    private void drawPane(Graphics2D g2d){
        g2d.setColor(Color.BLACK);
        g2d.setStroke(new BasicStroke(2.0f));
        g2d.drawRect(0, 0, width, height);
    }
   
    private void drawChars(Graphics2D g2d){
        float scale = 1f;
        int size = (int) (scale * 30);
        StringBuffer sb = new StringBuffer(1);
        sb.setLength(1);
        g2d.setColor(Color.BLACK);
     //   code = new char[charNumber];
        for (int i = 0; i < charNumber; i++) {
            //计算第i个字的x偏移
            int offsetWidth = i * (size+1)+10;
            //随机大小
            int charSize = size;//this.getRandomIntBetween((int) (size * 0.5), size);
            //随机样式
            int fontStyle = this.getRandomBoolean() ? Font.ITALIC : Font.ITALIC;
            g2d.setFont(new Font("隶书", fontStyle, charSize));
            //随机颜色
            //g2d.setColor(this.getRandomColor());
            //随机上移一点
            int moveUp = random.nextInt((int) (0.1 * offsetWidth));
            //随机字符
            sb.setCharAt(0, code[i]);
            g2d.drawString(sb.toString(), offsetWidth-moveUp, 45);
        }
    }
    private void drawCharsbody(Graphics2D g2d,int head){
        float scale = 1f;
        int size = (int) (scale * 30);
        StringBuffer sb = new StringBuffer(1);
        sb.setLength(1);
        g2d.setColor(Color.BLACK);
         int offsetWidth;
         int moveup;
         int v = 30;
         int w = 15;
        if(head<3){
            offsetWidth = head * (size+15)+w;
            moveup =70 + v *1;
        }else if(head<6){
           offsetWidth = (head-3) * (size+15)+w;
           moveup =70 + v *2;
        }
        else{
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 C 语言代码实现九宫格拼图的例子: ``` #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 3 // 拼图大小 void init_board(int board[][SIZE]); // 初始化棋盘 void shuffle_board(int board[][SIZE]); // 洗牌 void print_board(int board[][SIZE]); // 打印棋盘 int is_win(int board[][SIZE]); // 判断是否获胜 void move(int board[][SIZE], int direction); // 移动棋子 int main() { int board[SIZE][SIZE]; int direction; char input; srand(time(NULL)); // 随机种子 init_board(board); // 初始化棋盘 shuffle_board(board); // 洗牌 while (!is_win(board)) { print_board(board); printf("请输入移动方向(上:w,下:s,左:a,右:d):"); scanf(" %c", &input); switch (input) { case 'w': direction = 1; break; case 's': direction = 2; break; case 'a': direction = 3; break; case 'd': direction = 4; break; default: printf("无效的输入!\n"); continue; } move(board, direction); } printf("恭喜你获胜!\n"); return 0; } void init_board(int board[][SIZE]) { int i, j; for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { board[i][j] = i * SIZE + j + 1; } } board[SIZE - 1][SIZE - 1] = 0; // 最后一个格子为空白 } void shuffle_board(int board[][SIZE]) { int i, j, k; for (k = 0; k < 1000; k++) { // 洗牌1000次 i = rand() % SIZE; j = rand() % SIZE; if (board[i][j] == 0) { // 空白格子不动 k--; continue; } switch (rand() % 4) { // 随机选择一个方向 case 0: // 上 if (i > 0) { board[i][j] ^= board[i - 1][j] ^= board[i][j] ^= board[i - 1][j]; } break; case 1: // 下 if (i < SIZE - 1) { board[i][j] ^= board[i + 1][j] ^= board[i][j] ^= board[i + 1][j]; } break; case 2: // 左 if (j > 0) { board[i][j] ^= board[i][j - 1] ^= board[i][j] ^= board[i][j - 1]; } break; case 3: // 右 if (j < SIZE - 1) { board[i][j] ^= board[i][j + 1] ^= board[i][j] ^= board[i][j + 1]; } break; } } } void print_board(int board[][SIZE]) { int i, j; printf("\n"); for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { if (board[i][j] == 0) { printf(" "); // 空白格子用三个空格表示 } else { printf("%2d ", board[i][j]); } } printf("\n"); } } int is_win(int board[][SIZE]) { int i, j; for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { if (board[i][j] != i * SIZE + j + 1) { return 0; // 棋子未排列完成 } } } return 1; // 棋子已排列完成 } void move(int board[][SIZE], int direction) { int i, j, x, y; for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { if (board[i][j] == 0) { // 找到空白格子的位置 x = i; y = j; break; } } } switch (direction) { case 1: // 上 if (x > 0) { board[x][y] ^= board[x - 1][y] ^= board[x][y] ^= board[x - 1][y]; } else { printf("无效的移动!\n"); } break; case 2: // 下 if (x < SIZE - 1) { board[x][y] ^= board[x + 1][y] ^= board[x][y] ^= board[x + 1][y]; } else { printf("无效的移动!\n"); } break; case 3: // 左 if (y > 0) { board[x][y] ^= board[x][y - 1] ^= board[x][y] ^= board[x][y - 1]; } else { printf("无效的移动!\n"); } break; case 4: // 右 if (y < SIZE - 1) { board[x][y] ^= board[x][y + 1] ^= board[x][y] ^= board[x][y + 1]; } else { printf("无效的移动!\n"); } break; default: printf("无效的移动!\n"); break; } } ``` 这个例子使用了一个 3x3 的棋盘,初始化时将数字 1-9 放入相应的位置,然后洗牌,最后通过移动棋子使其重新排列。在输入移动方向时,w、s、a、d 分别表示上、下、左、右。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值