亲宝软件园·资讯

展开

JavaSE猜拳游戏 JavaSE实现猜拳游戏

Sylarjyd 人气:0

import java.util.Random;
import java.util.Scanner;

public class CaiQuan {
  private String userName;//存储用户名
  private String cName;//存储对手
  private int cNameNum;//存储玩家选择的对手代号
  private String userInfo;//用户出拳信息
  private String cInfo;//电脑出拳信息
  private int uNum;//接收用户输入的数
  private int cNum;//接受电脑产生的数
  private Scanner sc;
  private static final String Army01 = "张飞";
  private static final String Army02 = "关羽";
  private static final String Army03 ="刘备";
  private static boolean flag ;//平局和电脑胜为true,玩家赢为fasle
  private int selectInfo;//玩家是否继续游戏,1表示继续,0表示结束
  private int score = 0;//分数

  public static void main(String[] args) {
    CaiQuan cq = new CaiQuan();
    cq.gameInfo();
    do{
      cq.selectArmy();
      cq.userInput();
      cq.cOutput();
      cq.compareCaiQuan();
      cq.gameOver();
    }while(flag);
  }

  public CaiQuan(){

  }
  //打印游戏信息
  public void gameInfo(){
    System.out.println("欢迎进行猜拳小游戏!");
    System.out.println("1.表示石头  2.表示剪刀  3.表示布");
    sc = new Scanner(System.in);
    if(userName==null){
      System.out.println("玩家请输入用户名:");
      System.out.println("=============================================");
      userName = sc.next();
    }
  }
  //选择对手
  public void selectArmy(){
    System.out.println("=============================================");
    System.out.println("对手选择列表:");
    System.out.println("1:"+Army01+"  2:"+Army02+"   3:"+Army03);
    System.out.println(userName+"请输入对手:");
    System.out.println("=============================================");
    test01();
    System.out.println(userName+",您选择的对手为"+cName);
    System.out.println("=============================================");
  }
  //判断对手
  public void test01(){
    cNameNum = sc.nextInt();
    if(cNameNum==1){
      cName = Army01;
    }else if(cNameNum==2){
      cName = Army02;
    }else if(cNameNum==3){
      cName = Army03;
    }else{
      System.out.println("对手选择错误,请重新选择!");
      test01();
    }  
  }
  //开始游戏
  public void userInput(){
    System.out.println(userName+"请出拳:");
    System.out.println("1.表示石头  2.表示剪刀  3.表示布");
    System.out.println("=============================================");
    test();
    System.out.println("=============================================");
    System.out.println(userName+"的出拳为:"+userInfo);
  }
  //判断输入的数是否错误
  public void test(){
    uNum = sc.nextInt();
    if(uNum==1){
      userInfo = "石头";
    }else if(uNum==2){
      userInfo = "剪刀";
    }else if(uNum==3){
      userInfo = "布";
    }else{
      System.out.println("输入错误,请请重新输入");
      test();
    }
  }
  //电脑出拳
  public void cOutput(){
    Random random = new Random();
    cNum = random.nextInt(3)+1;
    if(cNum==1){
      cInfo = "石头";
    }else if(cNum==2){
      cInfo = "剪刀";
    }else if(cNum==3){
      cInfo = "布";
    }
    System.out.println(cName+"出拳为:"+cInfo);
  }
  //比较大小
  public void compareCaiQuan(){
    String msg = null;
    if(uNum == 1){
      if(cNum == 1){
        msg="此时平局";
        flag = true;
      }else if(cNum == 2){
        score += 1; 
        msg="此时"+userName+"胜出";
        flag = false;
      }else {
        score -= 1; 
        msg="此时"+cName+"胜出";
        flag = true;
      }
      return;
    }else if(uNum == 2){
      if(cNum == 1){
        score -= 1;
        msg="此时"+cName+"胜出";
        flag = true;
      }else if(cNum == 2){
        msg="此时平局";
        flag = true;
      }else {
        score += 1;
        msg="此时"+userName+"胜出";
        flag = false;
      }
    }else {
      if(cNum == 1){
        score += 1;
        msg="此时"+userName+"胜出";
        flag = false;
      }else if(cNum == 2){
        score -= 1;
        msg="此时"+cName+"胜出";
        flag = true;
      }else {
        msg=" 此时平局";
        flag = true;
      }
    }
    System.out.println(msg);
  }
  //游戏结束信息
  public void gameOver(){
    System.out.println("=============================================");
    if(flag==true){
      System.out.println(userName+",您没有战胜电脑,不要灰心,请继续游戏!");
      System.out.println("=============================================");
      System.out.println("继续游戏请输入:1   结束游戏请输入:2");
      test02();
    }else if(flag==false){
      System.out.println(userName+",您太厉害了,竟然战胜了"+cName+",游戏结束!");
      System.out.println("=============================================");
      System.out.println("继续游戏请输入:1   结束游戏请输入:2");
      test02();
    }
  }
  //判断玩家是否继续游戏
  public void test02(){
    selectInfo = sc.nextInt();
    if(selectInfo==1){
      flag = true;
    }else if(selectInfo==2){
      System.out.println(userName+",您的游戏分数为:"+score);
      System.exit(0);
    }else{
      System.out.println(userName+",您输入错误,请重新输入!");
      test02();  
    }
  }
}

加载全部内容

相关教程
猜你喜欢
用户评论