Java多线程实现跑步比赛【比赛详解】

本文详细介绍了如何使用Java多线程技术模拟一场跑步比赛,比赛中运动员的运动状态受到地图上随机事件的影响,如速度增减、距离变化等。通过SpringBoot调用逻辑,实现比赛过程并记录结果,最后将比赛数据存储到数据库,并生成比赛解说文字。

文章链接

Java多线程实现跑步比赛【比赛详解】
Java多线程实现跑步比赛【基本设计】
Java多线程实现跑步比赛【RunMap——地图映射类】
Java多线程实现跑步比赛【RunMan——运动员映射类】
Java多线程实现跑步比赛【RunManFunc,RunStart,Utils——工具类】
Java多线程实现跑步比赛【SpringBoot——调用逻辑】

实现要求

使用多线程实现跑步比赛。利用地图文件,控制虚拟运动员进行跑步比赛,用时最短的人获得胜利。

  1. 地图文件(Yaml文件)
  2. 运动员(信息存于MySQL数据库)

比赛详解

  1. 【指定地图比赛API】
    ① 按照下边的输入输出定义完成API,注意备考内容
    ② 指定地图进行比赛,输出比赛结果
    ③ 使用多线程实现,每一个运动员为一个独立线程运行
    ④ 将比赛结果插入到数据库中
    ➄ 根据比赛输出解说文字
  2. 【读取数据】
    读取运动员的配置信息,生成运动员实例(MySQL数据库)
    读取地图的配置信息,生成地图实例(Yaml文件)
  3. 【数据库】
    在这里插入图片描述
    计算速度公式—速度 = 平均速度 - 负重*0.1
    在这里插入图片描述
  4. 【YAML文件】
name: running-in-city 
description: 市内马拉松
distance: 8000
events:
  - occurredAt: 400
    description: 在马路牙子上磕了一下!
    effectsOfEvent:
      conditions:
        - description: 我跑得太快了,
          item: 速度
          operator: ">="
          value: 16
      immunizationCondition:
        description: 还好我身手敏捷,一个前滚翻,完美卸力~
        item: 掷骰子
        operator: ==
        value: 3
      effects:
        - description: 腿都卡秃噜皮了。
          item: 速度
          operator: =
          value: 0
          options:
            timeOfDuration: 8
  - occurredAt: 900
    description: 原来是一辆洒水车从后方接近!
    effectsOfEvent:
      conditions:
        - description: 这时,听到了一阵儿歌的声音...
          item: 时间
          operator: ">"
          value: 3
        - description: 
          item: 名次
          operator: <
          value: 4
      immunizationCondition:
        description: 还好司机及时发现了我,关闭了水阀。
        item: 速度
        operator: "<="
        value: 18
      effects:
        - description: 为了不被淋到,我夺路狂奔...
          item: 速度
          operator: +
          value: 20
          options:
            timeOfDuration: 5
  - occurredAt: 3000
    description: 幸亏我这会儿速度比较慢,不然就错过了。
    effectsOfEvent:
      conditions:
        - description: 朝左边无意的一瞥,在路边的书店看到了一直想买的绝版漫画!
          item: 速度
          operator: <=
          value: 17
      immunizationCondition:
        description: 一摸兜,没有带钱,我饮恨而去!
        item: 掷骰子
        operator: ==
        value: 6
      effects:
        - description: 不过有一说一,这套漫画还真挺沉的,想得第一恐怕费劲了。
          item: 负重
          operator: +
          value: 5
          options:
            timeOfDuration: -1
  - occurredAt: 4000
    description: 比赛前水喝的有点多,
    effectsOfEvent:
      immunizationCondition:
        description: 但是没找到厕所,只能忍一忍,到终点再说吧
        item: 时间
        operator: ">"
        value: 14
      effects:
        - description: 我不得不绕路找个厕所T_T
          item: 距离
          operator: +
          value: 300
          options:
            timeOfDuration: -1
  - occurredAt: 6000
    description: 前面有个马葫芦盖儿,没盖严!
    effectsOfEvent:
      immunizationCondition:
        description: 还好我双腿倒腾得飞快,直接跃过了这里。
        item: 掷骰子
        operator: "<="
        value: 3
      effects:
        - description: 咣当一声,我踉踉跄跄地蹦出几步...
          item: 速度
          operator: "-"
          value: 5
          options:
            timeOfDuration: 15
  - occurredAt: 7000
    description: 路上没有行人,我赶紧上前。
    effectsOfEvent:
      conditions:
        - description: 看见路边的老奶奶摔倒了!
          item: 时间
          operator: ">="
          value: 16
      immunizationCondition:
        description: 我刚想扶她,老奶奶淡定地摆摆手,站起来拍拍灰,拿着扇子去跳广场舞了!
        item: 掷骰子
        operator: ==
        value: 6
      effects:
        - description: 我背着老奶奶到了附近的社区医院,才回去接着比赛。
          item: 距离
          operator: +
          value: 300
          options:
            timeOfDuration: -1
  1. 【YAML地图文件定义规则】
{																																															
    "$schema": "http://json-schema.org/draft-04/schema",																																															
    "$comment": "https://lgtm.com/help/lgtm/lgtm.yml-configuration-file",																																															
    "description": "配置地图信息",																																															
    "type": "object",																																															
    "properties": {																																															
        "name": {																																															
            "title": "地图名",																																															
            "description": "地图的名字.",																																															
            "type": "string"																																															
        },																																															
        "description": {																																															
            "title": "地图描述",																																															
            "description": "关于地图的描述.",																																															
            "type": "string"																																															
        },																																															
        "distance": {																																															
            "title": "地图长度距离",																																															
            "description": "地图长度距离,单位是米.",																																															
            "type": "number"																																															
        },																																															
        "events": {																																															
            "title": "事件集合",																																															
            "description": "事件集合.",																																															
            "type": "array",																																															
            "items": {																																															
                "title": "事件",																																															
                "properties": {																																															
                    "occurredAt": {																																															
                        "title": "位置",																																															
                        "description": "事件所在位置,单位是米(即事件发生在第几米的位置).",																																															
                        "type": [																																															
                            "number"																																															
                        ]																																															
                    },																																															
                    "description": {																																															
                        "title": "事件描述",																																															
                        "description": "描述事件内容.",																																															
                        "type": [																																															
                            "string"																																															
                        ]																																															
                    },																																															
                    "effectsOfEvent": {																																															
                        "title": "事件的触发条件和副作用等",																																															
                        "properties": {																																															
                            "conditions": {																																															
                                "title": "触发条件",																																															
                                "type": "array",																																															
                                "items": {																																															
                                    "title": "条件",																																															
                                    "properties": {																																															
                                        "item": {																																															
                                            "title": "检查的项目名。时间代表比赛进行的时间,单位是分钟。速度的单位是km/h。",																																															
                                            "enum": [																																															
                                                "速度",																																															
                                                "名次",																																															
                                                "时间",																																															
                                                "掷骰子"																																															
                                            ]																																															
                                        },																																															
                                        "operator": {																																															
                                            "title": "检查的符号",																																															
                                            "enum": [																																															
                                                ">",																																															
                                                ">=",																																															
                                                "==",																																															
                                                "<",																																															
                                                "<="																																															
                                            ]																																															
                                        },																																															
                                        "value": {																																															
                                            "title": "检查的值",																																															
                                            "type": "number"																																															
                                        }																																															
                                    },																																															
                                    "required": [																																															
                                        "item",																																															
                                        "operator",																																															
                                        "value"																																															
                                    ]																																															
                                }																																															
                            },																																															
                            "immunizationCondition": {																																															
                                "title": "免疫条件",																																															
                                "type": "object",																																															
                                "properties": {																																															
                                    "description": {																																															
                                        "title": "免疫条件描述",																																															
                                        "description": "免疫条件描述.",																																															
                                        "type": [																																															
                                            "string"																																															
                                        ]																																															
                                    },																																															
                                    "item": {																																															
                                        "title": "检查的项目名。时间代表比赛进行的时间,单位是分钟。速度的单位是km/h。",																																															
                                        "enum": [																																															
                                            "速度",																																															
                                            "名次",																																															
                                            "时间",																																															
                                            "掷骰子"																																															
                                        ]																																															
                                    },																																															
                                    "operator": {																																															
                                        "title": "检查的符号",																																															
                                        "enum": [																																															
                                            ">",																																															
                                            ">=",																																															
                                            "==",																																															
                                            "<",																																															
                                            "<="																																															
                                        ]																																															
                                    },																																															
                                    "value": {																																															
                                        "title": "检查的值",																																															
                                        "type": "number"																																															
                                    }																																															
                                },																																															
                                "required": [																																															
                                    "item",																																															
                                    "operator",																																															
                                    "value"																																															
                                ]																																															
                            },																																															
                            "effects": {																																															
                                "title": "效应",																																															
                                "type": "array",																																															
                                "items": {																																															
                                    "properties": {																																															
                                        "description": {																																															
                                            "title": "效应描述",																																															
                                            "description": "效应的描述内容.",																																															
                                            "type": [																																															
                                                "string"																																															
                                            ]																																															
                                        },																																															
                                        "item": {																																															
                                            "title": "设定项目名,速度单位是km/h, 负重的单位是kg, 距离单位是米。",																																															
                                            "enum": [																																															
                                                "速度",																																															
                                                "负重",																																															
                                                "距离"																																															
                                            ]																																															
                                        },																																															
                                        "operator": {																																															
                                            "title": "设定的符号",																																															
                                            "enum": [																																															
                                                "+",																																															
                                                "-",																																															
                                                "="																																															
                                            ]																																															
                                        },																																															
                                        "value": {																																															
                                            "title": "设定的值",																																															
                                            "type": "number"																																															
                                        },																																															
                                        "options": {																																															
                                            "title": "效应参数",																																															
                                            "type": "object",																																															
                                            "properties": {																																															
                                                "timeOfDuration": {																																															
                                                    "title": "持续时间,单位为秒, 指定-1为永久",																																															
                                                    "type": "number"																																															
                                                }																																															
                                            }																																															
                                        }																																															
                                    },																																															
                                    "required": [																																															
                                        "item",																																															
                                        "operator",																																															
                                        "value"																																															
                                    ]																																															
                                }																																															
                            }																																															
                        },																																															
                        "required": [																																															
                            "effects"																																															
                        ]																																															
                    }																																															
                },																																															
                "required": [																																															
                    "occurredAt",																																															
                    "effectsOfEvent"																																															
                ]																																															
            }																																															
        }																																															
    },																																															
    "required": [																																															
        "name",																																															
        "distance",																																															
        "events"																																															
    ]																																															
}	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值