05_打字游戏

自己版本:

package com.entity;

import java.util.Arrays;
import java.util.Random;

public class Computer {

    public String print(char[] base,int rank){
        char[] c;
        switch(rank){
        case 1:
            c=Arrays.copyOf(base, 10);
            return getPrint(c,2);
        case 2:
            c=Arrays.copyOf(base, 10);
            return getPrint(c,3);
        case 3:
            c=Arrays.copyOf(base, 36);
            return getPrint(c,4);
        case 4:
            c=Arrays.copyOf(base, 36);
            return getPrint(c,5);
        case 5:
            return getPrint(base,6);
        case 6:
            return getPrint(base,8);
        }
        return null;

    }

    public String getPrint(char[] score,int number){
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<number;i++){
            int x=new Random().nextInt(score.length);
            sb.append(score[x]);
        }
        System.out.println(sb);
        return sb.toString();
    }

}

package com.entity;

import java.util.Scanner;

public class Game {
    private static int rank=1;
    private User user;
    private Computer computer;
    public final long canUseTime[]={4000,4000,5000,5000,6000,70000};
    public final int needScore[]={80,100,100,100,200,100};
    public final char[] base={‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘,‘g‘,‘h‘,‘i‘,‘j‘,‘k‘,‘l‘,‘m‘,‘n‘,‘o‘,‘p‘,‘q‘,‘r‘,‘s‘,‘t‘,‘u‘,‘v‘,‘w‘,‘x‘,‘y‘,‘z‘,‘>‘,‘<‘,‘_‘,‘^‘,‘&‘,‘*‘,‘$‘,‘(‘,‘)‘,‘@‘,‘#‘,‘%‘};

    public Game(){
        user=new User();
        computer=new Computer();
    }
    public void setComputer(Computer computer) {
        this.computer = computer;
    }
    public Computer getComputer() {
        return computer;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public User getUser() {
        return user;
    }
    public static void setRank(int rank) {
        Game.rank = rank;
    }

    public static int getRank() {
        return rank;
    }

    public void startGame(){
        Scanner sc=new Scanner(System.in);
        System.out.println("-------------------------------");
        System.out.println("欢迎来到最新科技打字游戏!");
        System.out.println("输入s开始游戏!");
        boolean flag=sc.nextLine().equals("s") ? true :false;
        if(flag){
            while(rank<=6){
                doGame();
                if(rank>6){
                    System.out.println("恭喜您通关了!\n游戏结束!");
                    System.exit(0);
                }
            }
        }else{
            System.out.println("谢谢使用,再见!");
            System.exit(0);
        }
    }

    public void doGame(){
        String answer=computer.print(base, rank);
        String result=user.print();
        if(!answer.equals(result)){
            System.out.println("输入错误,游戏结束!");
            System.exit(1);
        }else if(user.getSpentTime()>canUseTime[rank-1]){
            System.out.println("输入超时,游戏结束!");
            System.exit(1);
        }else{
            user.setScore(user.getScore()+10);//输入正确一次加10积分
            if(user.getScore()>=needScore[rank-1]){
                rank=rank+1;
                user.clearScore();
                System.out.println("输入正确,您升级了,当前级别为:"+rank+",当前积分为:"+user.getScore());
            }else{
                System.out.println("输入正确,您的积分为:"+user.getScore()+"您的级别为:"+rank);
            }
        }
    }

}

package com.entity;

import java.util.Scanner;

public class User {
    private int score;
    private String name;
    private long spentTime;

    public void setScore(int score) {
        this.score = score;
    }

    public int getScore() {
        return score;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public User(){

    }
    public User(String name){
        this.name=name;
    }
    public void setSpentTime(long spentTime) {
        this.spentTime = spentTime;
    }

    public long getSpentTime() {
        return spentTime;
    }

    public String print(){//人的输入
        spentTime=0;
        Scanner sc=new Scanner(System.in);
        long startTime=System.currentTimeMillis();
        String userPrint= sc.nextLine();
        long endTime=System.currentTimeMillis();
        spentTime=endTime-startTime;
        return userPrint;
    }
    public void clearScore(){//积分清0
        this.score=0;
    }

}

package com.test;

import com.entity.Game;

public class TestGame {
    public static void main(String[] args) {
        Game game=new Game();
        game.startGame();
    }

}

ppt版本:

package com.entity;

import java.util.Scanner;

public class Player {
    private int levelNo=1;//玩家级别号
    private int currScore;//当前级别积分
    private long startTime;//当前级别开始时间
    private int elapsedTime;//当前级别已用时间

    public String play(){//玩家输入
        Scanner sc=new Scanner(System.in);
        long startTime=System.currentTimeMillis();
        String s= sc.nextLine();
        long endTime=System.currentTimeMillis();
        this.elapsedTime+=(int) ((endTime-startTime)/1000);
        return s;
    }
    public void clear(){
        this.currScore=0;
        this.elapsedTime=0;
    }

    public int getLevelNo() {
        return levelNo;
    }

    public void setLevelNo(int levelNo) {
        this.levelNo = levelNo;
    }

    public int getCurrScore() {
        return currScore;
    }

    public void setCurrScore(int currScore) {
        this.currScore = currScore;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public int getElapsedTime() {
        return elapsedTime;
    }

    public void setElapsedTime(int elapsedTime) {
        this.elapsedTime = elapsedTime;
    }
}

package com.entity;

public class LevelParam {

    public static Level[] levels=new Level[6];
    static {
        levels[0]=new Level(1,2,10,30,1);
        levels[1]=new Level(2,3,9,26,2);
        levels[2]=new Level(3,4,8,22,5);
        levels[3]=new Level(4,5,7,18,8);
        levels[4]=new Level(5,6,6,15,10);
        levels[5]=new Level(6,7,5,12,15);
    }

}
package com.entity;

public class Level {
    private int levelNo;//级别号
    private int StringLength;//一次输出字符串的长度
    private int strTimes;//输出字符串的次数
    private int timeLimit;//各级别闯关的时间限制
    private int perScore;//输入正确一次的得分
    public int getStrTimes() {
        return strTimes;
    }
    public Level(int levelNo, int stringLength, int strTimes,  int timeLimit,
            int perScore) {
        super();
        this.levelNo = levelNo;
        this.strTimes = strTimes;
        StringLength = stringLength;
        this.timeLimit = timeLimit;
        this.perScore = perScore;
    }
    public void setStrTimes(int strTimes) {
        this.strTimes = strTimes;
    }
    public int getStringLength() {
        return StringLength;
    }
    public void setStringLength(int stringLength) {
        StringLength = stringLength;
    }
    public long getTimeLimit() {
        return timeLimit;
    }
    public void setTimeLimit(int timeLimit) {
        this.timeLimit = timeLimit;
    }
    public int getPerScore() {
        return perScore;
    }
    public void setPerScore(int perScore) {
        this.perScore = perScore;
    }

    public void setLevelNo(int levelNo) {
        this.levelNo = levelNo;
    }
    public int getLevelNo() {
        return levelNo;
    }
}

package com.entity;

import java.util.Random;

public class Game {
    public final char[] base=new char[94];
    public String printStr(Level l){
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<l.getStringLength();i++){
            sb.append(base[new Random().nextInt(94)]);
        }
        System.out.println(sb);
        return sb.toString();
    }
    public Game(){
        init();
    }
    public void init(){/*初始化final类*/
        for(int j=0,i=33;i<=126;i++,j++){
            base[j]=(char)i;
        }
//        for(char ch:base){
//            System.out.println(ch);
//        }
    }

}

package com.test;

import java.util.Scanner;

import com.entity.Game;
import com.entity.Level;
import com.entity.LevelParam;
import com.entity.Player;

public class TestGame {
    public Level level=LevelParam.levels[0];
    public Player player=new Player();
    public Game game=new Game();
    public static void main(String[] args) {
        TestGame tg=new TestGame();
        Scanner sc=new Scanner(System.in);
        System.out.println("-------------------------------");
        System.out.println("欢迎来到最新科技打字游戏!");
        System.out.println("输入s开始游戏!");
        boolean flag=sc.nextLine().equals("s") ? true :false;
        if(flag){
            tg.doGame();
        }else{
            System.out.println("谢谢使用,再见!");
            System.exit(0);
        }
    }
    public void doGame(){
        while(true){
            for(int i=0;i<level.getStrTimes();i++){
                String computerDo=game.printStr(level);
                String playerDo=player.play();
                printResult(computerDo,playerDo);
                if(i==level.getStrTimes()-1){
                    player.setLevelNo(player.getLevelNo()+1);
                    player.clear();
                }
                    System.out.println("输入正确,您的积分"+player.getCurrScore()+"您的级别"+player.getLevelNo()+"已用时间"+player.getElapsedTime()+"秒");

            }
            if(level.getLevelNo()==6){
                System.out.println("恭喜你,通关了!");
                System.exit(0);
            }else{
                level=LevelParam.levels[level.getLevelNo()+1];
            }
        }
    }
    public void printResult(String out,String in){
        if(!out.equals(in)){
            System.out.println("输入错误,游戏结束!");
            System.exit(0);
        }else if(level.getTimeLimit()<player.getElapsedTime()){
            System.out.println("你输入太慢了,游戏结束!");
            System.exit(0);
        }else{
            player.setCurrScore(player.getCurrScore()+level.getPerScore());
        }
    }

}
时间: 2024-12-28 05:01:24

05_打字游戏的相关文章

打字游戏可能出现的问题

最近有同学告诉我,即使按照打字游戏的视频做也会出现乱码的问题,如下所示: 出现这个问题的原因是安装了搜狗输入法,并将其设置成了首选输入法,解决方案有二: 删除搜狗输入法. 将英文设置为首选输入法.如下图所示: 如果按照以上两种方法做了,都不行请与我QQ联系.

jQuery 写的简单打字游戏

var off_x; //横坐标 var count=0; //总分 var speed=5000; //速度,默认是5秒. var keyErro=0; //输入错误次数 var keyRight=0; //输入正确的次数 //组织字母 var charArray=new Array("A", "B", "C", "D", "E", "F", "G", "

java打字游戏

小记:老早之前写的程序,今天发现之前在 csdn上写的东西的图片不显示了,搞得人好郁闷,所以把之前零星的几篇文章搬个家 游戏运行截图: 字母实体类 package com.git.game; import java.awt.Color; public class Letter { private String num; private Color color; private int x; private int y; private int speed = 1; public Letter()

打字游戏简约版

首先将要产生的按键存到数组当中,随机产生角标,获得按键,创建标签,添加到屏幕当中,然后获得按键事件,与每一个值进行比较,如果有一样的,则删除,如果没有,则随机添加几个字母,还有经过指定时间,添加合适数量的字母,直到全部删除完,结束. <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>打字游戏</title>

简单的Java打字游戏

原文:简单的Java打字游戏 源代码下载地址:http://www.zuidaima.com/share/1586973185674240.htm 仅供参考,很简单. swing实现 版权声明:本文为博主原创文章,未经博主允许不得转载.

swing简单的打字游戏源码

代码如下 import java.awt.*; import java.awt.event.*; public class Main{ public static Frame f =new Frame(); public static void main(String[] agrs){ f.setSize(500, 400); f.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-f.getSize().width)/2

打字游戏

"柴米油盐酱醋茶",现在我们想要写一个小游戏是不是得先要了解一些基本知识,就比如说你想要煮饭,你得认识"米".你得知道生火煮饭这个流程吧! 核心部分:第一步我们需要一个键盘按下事件onkeydown,第二步我们需要知道我们按下的是哪个按钮:e.keyCode ||e.which(返回的是ASCII码),第三步判断屏幕上字母的ASCII码是不是和你键盘按下的ASCII码是不是一致. <!DOCTYPE html><html lang="en

键盘输入盲打训练, 打字练习,打字游戏 打字教程推荐

作为一个程序员键盘输入算是基本功,这一项肯定是要牢靠才行,以下我总结了几个不错的资源分享给大家: --祝玩的开心 刚开始可以在这里训练下指头和按键的对应关系 http://www.typingstudy.com/zh/ speedcoder,有指法按键的提示,针对性的编程语境练习 http://www.speedcoder.net/lessons/ 打飞机的打字游戏 http://zty.pe/ 国内一个大神的镜像,可以看看他的博客很厉害的全栈 http://ztype.liyarou.com/

打字游戏1

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;namespace 随笔{ class Program { /// <summary> /// 比较两个字符串相相同元素的个数数 /// </summary> /// <param name="a&