码迷,mamicode.com
首页 > 其他好文 > 详细

实验二:关于梭哈游戏的代码的测试

时间:2015-04-29 23:17:31      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

实验二:关于梭哈游戏的代码的测试

一.题目简介

  1. Github基本源代码控制方法
  2. 利用Junit4进行程序模块的测试,回归测试
  3. 编码规范的考量
  4. C/Java等基本程序设计语言的运用。 (考虑到有些同学的基础参差不齐)
  5. 实现纸牌游戏梭哈

二、个人github地址

          https://github.com/qingfengsuixin/test/blob/master/ShowHand

 

三、所设计的模块测试用例、测试结果截图:

package com.langsin.text2;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;

//梭哈游戏
public class ShowHand {
    // 1.创建扑克牌,用7-k共七种四个花色
    //储存扑克的集合
    private List<String> pukelist = new ArrayList<String>();
    //玩家手牌集合
    private Map<String, TreeSet<String>> map=new HashMap<String, TreeSet<String>>();
    //玩家分数
    private Map<String, Double> sorceMap=new HashMap<String, Double>();
    private boolean flag=true;

    private void creatPuke() {
        String[] point = { "7", "8", "9", "10", "J", "Q", "K" };
        String[] type = { "黑桃", "红桃", "梅花", "方块" };
        for (int i = 0; i < type.length; i++) {
            for (int j = 0; j < point.length; j++) {
                pukelist.add(type[i] + point[j]);
            }
        }
        System.out.println(pukelist);
        System.out.println("-------------------");
    }
    //2.对扑克牌进行洗牌
    private void sortedPuke(){
           Random rand=new Random();
        for (int i = 0; i <pukelist.size()*5; i++) {
            int index1=rand.nextInt(this.pukelist.size());
            int index2=rand.nextInt(this.pukelist.size());
            String temp=this.pukelist.get(index1);
            this.pukelist.set(index1, this.pukelist.get(index2));
            this.pukelist.set(index2, temp);
        }
        System.out.println(this.pukelist);
    }
    //3.创建参加游戏的人
    private void creatPlayer(){
        System.out.println("请输入参与游戏玩家的名称。名字中间请用空格隔开");
        Scanner scan=new Scanner(System.in);
        String players=scan.nextLine();
        String data[] = players.split(" ");
        if (!(data.length>=2&&data.length<=5)) {
            System.out.println("参与游戏的玩家人数不符合要求,程序终止进行!");
            flag=false;
            return;
        }
        //玩家手牌排序器,作用排列玩家发牌后到手的牌的顺序方便最后的比较
        Comparator<String> comp=new Comparator<String>() {
            //此为比较器
            public int compare(String str1, String str2) {
                int point1=getPoint(str1.substring(2));
                int point2=getPoint(str2.substring(2));
                if(point1>point2){
                    return 1;
                }else if(point1<point2){
                    return -1;
                }else{
                    int type1=getType(str1.substring(0, 2));
                    int type2=getType(str2.substring(0, 2));
                    if (type1>type2) {
                        return 1;
                    } else {
                        return -1;
                    }
                }
                
            }
            
        };
        for (int i = 0; i < data.length; i++) {
            map.put(data[i], new TreeSet<String>(comp));
        }
    }
      //此为比较器的点数判断辅助方法和花色判断辅助方法
    private int getPoint(String point){
        if ("J".equals(point)) {
            return 11;
        }else if("Q".equals(point)){
            return 12;
        }else if("K".equals(point)){
            return 13;
        }else{
            return Integer.parseInt(point);
        }
    }
    private int getType(String type){
        if ("黑桃".equals(type)) {
            return 1;
        }else if("红桃".equals(type)){
            return 2;
        }else if("梅花".equals(type)){
            return 3;
        }else{
            return 4;
        }
    }
    //4.给玩家发牌
    private void showPuke(){
        for (int i = 0; i < 5; i++) {
            for (String key : map.keySet()) {
                String puke=this.pukelist.remove(0);
                map.get(key).add(puke);
            }
        }
        for (String key : map.keySet()) {
            System.out.println("玩家名称"+key+":"+map.get(key));
        }
    }
    //计算玩家分数
    private void getSorce(){
        
        String winner=null;
        double winnerSorce=0;
        for (String key : map.keySet()) {
            TreeSet<String> set=map.get(key);
            double sorce = this.sorce(set);
            this.sorceMap.put(key, sorce);
            System.out.println("玩家"+key+"的牌为:"+this.map.get(key)+" 分值为:"+sorce);
            for(String key1:sorceMap.keySet()){
                if(sorceMap.get(key1)>winnerSorce){
                    winnerSorce=sorceMap.get(key1);
                    winner=key1;
                }
            }
        }
        System.out.println("获胜者是: "+winner+"    分数是:"+winnerSorce);
    }
    private double sorce(TreeSet<String> set){
        double sorce=0;
        String[] pukes= set.toArray(new String[set.size()]);
        //判断是不是顺子
        for (int i = 0; i < pukes.length; i++) {
            int point1=getPoint(pukes[i].substring(2));
            int point2=getPoint(pukes[i+1].substring(2));
            if (point1-point2!=1) {
                flag=false;
                break;
            }
        }
        if (flag) {
            sorce=5.0+this.getPoint(pukes[4].substring(2))*0.01;
            return sorce;
        }
        //取出5张牌的数字
        int point1=this.getPoint(pukes[0].substring(2));
        int point2=this.getPoint(pukes[1].substring(2));
        int point3=this.getPoint(pukes[2].substring(2));
        int point4=this.getPoint(pukes[3].substring(2));
        int point5=this.getPoint(pukes[4].substring(2));
        if (point1==point4||point2==point5) {
            sorce=4.0+this.getPoint(pukes[3].substring(2))*0.01;
            return sorce;
        }
        if (point1==point3||point2==point4||point3==point5) {
            sorce=3.0+this.getPoint(pukes[2].substring(2))*0.01;
            return sorce;
        }
        if (point1==point2&&point3==point4) {
            sorce=2.0+this.getPoint(pukes[2].substring(2))*0.01+this.getPoint(pukes[0].substring(2))*0.0001+this.getPoint(pukes[4].substring(2))*0.000001;
            return sorce;
        }
        if (point2==point3&&point4==point5) {
            sorce=2.0+this.getPoint(pukes[4].substring(2))*0.01+this.getPoint(pukes[1].substring(2))*0.0001+this.getPoint(pukes[0].substring(2))*0.000001;
            return sorce;
        }
        if (point1==point2&&point4==point5) {
            sorce=2.0+this.getPoint(pukes[4].substring(2))*0.01+this.getPoint(pukes[0].substring(2))*0.0001+this.getPoint(pukes[2].substring(2))*0.000001;
            return sorce;
        }
        if (point1==point2) {
            sorce=1.0+this.getPoint(pukes[0].substring(2))*0.01+this.getPoint(pukes[4].substring(2))*0.0001+this.getPoint(pukes[3].substring(2))*0.000001+this.getPoint(pukes[2].substring(2))*0.00000001;
            return sorce;
        }
        if (point2==point3) {
            sorce=1.0+this.getPoint(pukes[1].substring(2))*0.01+this.getPoint(pukes[4].substring(2))*0.0001+this.getPoint(pukes[3].substring(2))*0.000001+this.getPoint(pukes[0].substring(2))*0.00000001;
            return sorce;
        }
        if (point3==point4) {
            sorce=1.0+this.getPoint(pukes[2].substring(2))*0.01+this.getPoint(pukes[4].substring(2))*0.0001+this.getPoint(pukes[1].substring(2))*0.000001+this.getPoint(pukes[0].substring(2))*0.00000001;
            return sorce;
        }
        if (point4==point5) {
            sorce=1.0+this.getPoint(pukes[3].substring(2))*0.01+this.getPoint(pukes[2].substring(2))*0.0001+this.getPoint(pukes[1].substring(2))*0.000001+this.getPoint(pukes[0].substring(2))*0.00000001;
            return sorce;
        }
        return sorce=this.getPoint(pukes[4].substring(2))*0.01+this.getPoint(pukes[3].substring(2))*0.0001+this.getPoint(pukes[2].substring(2))*0.000001+this.getPoint(pukes[1].substring(2))*0.000001+this.getPoint(pukes[0].substring(2))*0.00000001;
    }
    //主程序
    public void init() {
        this.creatPuke();
        this.sortedPuke();
        this.creatPlayer();
        if(flag){
            this.showPuke();
            this.getSorce();
        }
    }
    //main函数
    public static void main(String[] args) {
        new ShowHand().init();
    }

 

梭哈截图:
技术分享技术分享

 测试截图:

技术分享

四.问题及解决方案、心得体会

         通过本次实验我学会了如何使用Myeclipse进行程序测试,了解了自己所不了解的程序测试方法感觉十分有意义,此外showhand项目的成功也让我体会到了编程的乐趣,实在是不枉我这段时间的学习。

 

实验二:关于梭哈游戏的代码的测试

标签:

原文地址:http://www.cnblogs.com/qingfengsuixin/p/4467636.html

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