码迷,mamicode.com
首页 > 编程语言 > 详细

猜拳游戏(Java实现)

时间:2021-03-09 12:57:12      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:sys   com   oar   not   pre   set   inf   int   bat   

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

public class MoraGame {
    public static void main(String[] args) {
        int humanNum = 0;
        int n = 0;
        int i = 0;
        int j;
        int k;
        //保存对战信息
        int[][] Combat = new int[3][3];
        //保存输赢情况
        String[] infoArr = new String[3];
        Scanner scanner = new Scanner(System.in);
        Caiquan c1 = new Caiquan();

        while (n < 3) {
            n++;
            System.out.println("请输入你要出的拳(0-拳头 1-剪刀 2-布:)");
            if (scanner.hasNext()) {
                humanNum = scanner.nextInt();
            }
            c1.computerNum();
            c1.sethumanNum(humanNum);
            int res = c1.vsComputer();
            String notice = c1.winorlose(res);
            System.out.println("==========================================================");
            System.out.println("局数\t玩家的出拳\t电脑的出拳\t输赢情况");
            System.out.println(c1.totalcount + "\t" + c1.humanNum + "\t\t" + c1.comNum + "\t\t" + notice + "\n");
            infoArr[i] = notice;
            Combat[i][0] = c1.totalcount;
            Combat[i][1] = c1.humanNum;
            Combat[i][2] = c1.comNum;
            i++;
        }
        System.out.println("局数\t玩家的出拳\t电脑的出拳\t输赢情况");
        for (j = 0; j < Combat.length; j++) {
            for(k = 0; k < Combat[j].length; k++) {
                System.out.print(Combat[j][k] + "\t\t");
            }
            System.out.println(infoArr[j]);
        }
        System.out.println("你赢了" + c1.wincount + "次");   
    }
}

class Caiquan {
    int humanNum;
    int comNum;
    int wincount;
    int losecount;
    int tiecount;
    int totalcount;

    public void sethumanNum(int humanNum) {
        this.humanNum = humanNum;
    }

    public int computerNum() {
        Random r = new Random();
        this.comNum = r.nextInt(3);

        return this.comNum;
    }

    public int vsComputer() {
        //0-拳头 1-剪刀 2-布
        //返回值:0 赢 -1 输  2 平局 -2异常
        if (this.humanNum == 0 && this.comNum == 1) {
            return 0;
        }
        else if (this.humanNum == 0 && this.comNum == 2) {
            return -1;
        }
        else if (this.humanNum == 0 && this.comNum == 0) {
            return 2;
        }
        else if (this.humanNum == 1 && this.comNum == 0) {
            return -1;
        }
        else if (this.humanNum == 1 && this.comNum == 2) {
            return 0;
        }
        else if (this.humanNum == 1 && this.comNum == 1) {
            return 2;
        }
        else if (this.humanNum == 2 && this.comNum == 0) {
            return 0;
        }
        else if (this.humanNum == 2 && this.comNum == 1) {
            return -1;
        }
        else if (this.humanNum == 2 && this.comNum == 2) {
            return 2;
        }
        else return -2;
    }

    public String winorlose(int res) {
        //0 赢 -1 输  2 平局
        if (res == 0) {
            this.wincount++;
            this.totalcount++;
            return "你赢了";
        }
        else if (res == -1) {
            this.losecount++;
            this.totalcount++;
            return "你输了";
        }
        else if (res == 2) {
            this.tiecount++;
            this.totalcount++;
            return "平局了";
        }
        else
            this.totalcount++;
            return "异常!";
    }


}

运行示例:

输入你要出的拳(0-拳头 1-剪刀 2-布:)
1
==========================================================*
局数 玩家的出拳 电脑的出拳 输赢情况
1 1 0 你输了

请输入你要出的拳(0-拳头 1-剪刀 2-布:)
2
==========================================================*
局数 玩家的出拳 电脑的出拳 输赢情况
2 2 0 你赢了

请输入你要出的拳(0-拳头 1-剪刀 2-布:)
0
==========================================================*
局数 玩家的出拳 电脑的出拳 输赢情况
3 0 2 你输了

局数 玩家的出拳 电脑的出拳 输赢情况
1 1 0 你输了
2 2 0 你赢了
3 0 2 你输了
你赢了1次

猜拳游戏(Java实现)

标签:sys   com   oar   not   pre   set   inf   int   bat   

原文地址:https://www.cnblogs.com/irisx33/p/14497313.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!