码迷,mamicode.com
首页 > Windows程序 > 详细

作业六:猜猜看,检验朋友熟悉度——Winform程序

时间:2015-07-08 14:50:33      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:中学生   源代码   可能性   朋友   图片   

1.目的 
以最快的速度认识、了解身边的人; 促进人与人之间的互动交流; 
2.目标 
满足基本功能;对原型进行简单的验证; 
3.内容 
功能: 
随机显示一个名字与 3张相片,选择相片,提示结果,显示图片正确信息; 
技术分享 
随机显示 一张相片与若3个名字,选择名字,提示结果; 
技术分享 
4.重点——随机概率 
概率计算:记录对每个人的认识概率P,并根据认识概率,确定“猜猜看”游戏中学生出现的频率。 
认识概率P = 识别的准确次数/(识别的准确次数+识别的错误次数) 
原则1:认识概率 高的学生,在猜猜看游戏中出现的频率相对低一些。 
原则2:认识概率 低的学生,在猜猜看游戏中出现的频率相对高一些。 
原则3:认识概率 100%的学生,也有出现的可能性。 
原则4:认识概率 0%的学生,也不必每次都出现。 


源代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Linq;
namespace GuessGame03
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        
        //定义变量
        String folderName;      //文件夹的名称
        String strClassFilename;    //班级名单  
        
        
        ArrayList ListId = new ArrayList();     //保存学号
        ArrayList ListName = new ArrayList();   //保存姓名
        
        int totalStu = 0; //学生人数
        ArrayList ListGuessArray = new ArrayList();   //保存每次随机数字数组
        
        // week 15
        String strResultFilename;   //结果文件的文件名
        ArrayList ListCorrect = new ArrayList();        //准确次数
        ArrayList ListWrong = new ArrayList();  //错误次数
        ArrayList ListResult = new ArrayList();     //准确率
        
        
        
        
        public MainForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            
            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
            label1.Text = "";
            label2.Text = "";
            label3.Text = "";
            label4.Text = "";
            label5.Text = "";
            label6.Text = "";
        }
        
        void Button1Click(object sender, EventArgs e)
        {
            // Show the FolderBrowserDialog.
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if( result == DialogResult.OK )
            {
                folderName = folderBrowserDialog1.SelectedPath;
                label1.Text = folderName;
            }
        }
        
        void Button2Click(object sender, EventArgs e)
        {
            // 获取相应的学生名单
            openFileDialog1.InitialDirectory = folderName;
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
            openFileDialog1.FilterIndex = 2 ;
            openFileDialog1.RestoreDirectory = true ;
            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                label2.Text = openFileDialog1.FileName;
                strClassFilename = openFileDialog1.FileName;
                
                // 根据班级文件名,得到保存结果的文件名名称:result_班级文件名
                strResultFilename = "result_" + openFileDialog1.SafeFileName;
                
                // 获取学生的学号与姓名
                StreamReader srReadFile = new StreamReader(strClassFilename, System.Text.Encoding.Default);
                
                int numStu = 0;
                // 读取流直至文件末尾结束
                while (!srReadFile.EndOfStream)
                {
                    string strReadLine = srReadFile.ReadLine(); //读取每行数据
                    //按照空格进行分割
                    string[] sArray=strReadLine.Split(‘ ‘);
                    ListId.Add(sArray[1]);
                    ListName.Add(sArray[2]);
                    
                    numStu = numStu+1;
                }
                // 关闭读取流文件
                srReadFile.Close();
                totalStu = numStu;
            
            
            // 判断是否有文件猜猜看结果文件存在:(1)存在,则调用相应的认识概率数据;(2)不存在,则生成一个初始的概率文件。
            // 文件名:result_信管1121-1122.txt
            String strTmp = folderName +"\\" + strResultFilename;
            if( !File.Exists(@strTmp) )
            {
                using (StreamWriter sw = new StreamWriter(@strTmp, true, System.Text.Encoding.Default ))
                {
                    for(int i=0; i<totalStu; i++)
                    {
                        //201211671101 蔡济屹  1  1  0.5
                        sw.WriteLine(ListId[i]+"  "+ListName[i]+"  1  1  0.5");                     
                    }                   
                }
            }
            
            // 读取相应的数据
            string line = "";
            Dictionary<string, double> dic = new Dictionary<string, double>();
            
            using (StreamReader sr = new StreamReader(@strTmp, System.Text.Encoding.Default ))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    //按照空格进行分割
                    string[] sArray=line.Split(new char[] {‘ ‘}, StringSplitOptions.RemoveEmptyEntries);
                    ListCorrect.Add(Convert.ToInt32(sArray[2]));
                    ListWrong.Add(Convert.ToInt32(sArray[3]));
                    ListResult.Add(Convert.ToDouble(sArray[4]));    
                    // 构造字典,方便自动排序
                    dic.Add(sArray[0],Convert.ToDouble(sArray[4]));
                }
            }
            
            // LINQ排序:升序
            Dictionary<string, double> dic1 = new Dictionary<string, double>();
            dic1 = (from entry in dic orderby entry.Value ascending
                                     select entry).ToDictionary(pair => pair.Key, pair => pair.Value);
            // 按照认识概率排序后的结果,依次挑选相片进行显示
            // 步骤:根据学号找到相应的图片,取出其中的三张;及生成一个随机数字来展示图片
            // (补充代码)
            
            
            // 选择猜测名字 及 随机图片
             CreateRmArrayAndUpdateImg(3);
            
            }
        }
        
        void CreateRmArrayAndUpdateImg(int RmNum)
        {
            // 产生三个不重复的随机数字组成数组,并从其中产生一个待猜测的数字
            //int RmNum = 3;
            
            int[] rmArray = new int[RmNum+1];
            Hashtable hashtable = new Hashtable();
            Random rm = new Random();
            for (int i = 0; hashtable.Count < RmNum; i++)
            {
                int nValue = rm.Next(0,totalStu);
                if (!hashtable.ContainsValue(nValue))
                {
                    rmArray[i] = nValue;
                    hashtable.Add(nValue, nValue);
                }
            }
            rmArray[RmNum] = rm.Next(0,RmNum);
            
            //保存带猜测数组
            ArrayList arrTmp = new ArrayList();
            foreach(int tmp in rmArray)
                arrTmp.Add(tmp);
            ListGuessArray.Add(arrTmp);
            
            
            // 产生相关的相片
            int nGuess = rmArray[ rmArray[RmNum] ];
            label3.Text = ListId[nGuess].ToString() + ListName[nGuess].ToString();
            
            // 更新图片,需要判断图片是否存在;若不存在则使用一幅画来替代test.jpg
            String strTest = folderName + "/" + "test.jpg";
            
            String strTmp = folderName + "/" + ListId[ rmArray[0] ] + ListName[ rmArray[0] ] + ".jpg";
            pictureBox1.Image = new Bitmap(GetPhoteFilename(strTmp, strTest));
            
            strTmp = folderName + "/" + ListId[ rmArray[1] ] + ListName[ rmArray[1] ] + ".jpg";
            pictureBox2.Image = new Bitmap(GetPhoteFilename(strTmp, strTest));
            
            strTmp = folderName + "/" + ListId[ rmArray[2] ] + ListName[ rmArray[2] ] + ".jpg";
            pictureBox3.Image = new Bitmap(GetPhoteFilename(strTmp, strTest));
            
        }
        
        // 获取显示的图片
        String GetPhoteFilename(String strFilename, String strTest)
        {
            if( File.Exists(@strFilename) )
            {
                return strFilename;
            }
            else
            {
                return strTest;
            }
        }
        //判断游戏是否继续
        void isCoutinue()
        {
            //获取最后一次猜测
            ArrayList arr = (ArrayList)ListGuessArray[ListGuessArray.Count - 1];
            label4.Text = ListId[(int)arr[0]].ToString() + ListName[(int)arr[0]].ToString();
            label5.Text = ListId[(int)arr[1]].ToString() + ListName[(int)arr[1]].ToString();
            label6.Text = ListId[(int)arr[2]].ToString() + ListName[(int)arr[2]].ToString();
            // 正确的答案
            int correct = (int)arr[3];
            string caption = "猜猜看游戏结果";
            string message;
            if (correct == 0)
            {
                message = "哇,记忆力真棒!是否继续?";
            }
            else
            {
                message = "哈哈,猜错了!是否继续?";
            }
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result;
            // Displays the MessageBox.
            result = MessageBox.Show(message, caption, buttons);
            //是否继续
            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                label4.Text = "";
                label5.Text = "";
                label6.Text = "";
                // 更换相片
                CreateRmArrayAndUpdateImg(3);
            }
            else
            {
                // Closes the parent form.
                this.Close();
            }
        }
        void PictureBox1Click(object sender, EventArgs e)
        {
            isCoutinue();
        }
        
        void PictureBox2Click(object sender, EventArgs e)
        {
            isCoutinue();
        }
        
        void PictureBox3Click(object sender, EventArgs e)
        {
            isCoutinue();
        }
    }
}

成果展示:

技术分享




作业六:猜猜看,检验朋友熟悉度——Winform程序

标签:中学生   源代码   可能性   朋友   图片   

原文地址:http://lsywuya77.blog.51cto.com/9516729/1671983

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