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

黄金点游戏

时间:2016-08-07 15:23:00      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

游戏实现(C#语言)

程序入口: 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace GoldPoint_Game
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmWelcome());
}
}
}

在program类中,首先运行程序欢迎窗体。

欢迎窗体:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GoldPoint_Game
{
public partial class frmWelcome : Form
{
public frmWelcome()
{
InitializeComponent();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void frmWelcome_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
GoldGame f1 = new GoldGame(textBox2.Text,textBox3.Text);
f1.ShowDialog();
this.Close();
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

欢迎窗体的代码实现,在该类中,实现了对欢迎窗体的初始化,包括游戏规则的提示,游戏轮次、玩家个数的输入,退出游戏、进入游戏提示等。

游戏窗体:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GoldPoint_Game
{
partial class GoldGame : Form
{
......
}
}

游戏窗体时该程序的主体部分,下面进行详细的讲解。

一个Goldgame类和两个player,achieve列表:


class Goldgame
{
public int number;
public float digit;
public int score;
};
List<Goldgame> player = new List<Goldgame> { };
List<Goldgame> achieve = new List<Goldgame> { };

两个player,achieve列表分别用于保存游戏玩家的编号、数字、分数和游戏成就的编号、数字、分数。

两个整型变量gameCount、peopleCount:


private int gameCount;
private int peopleCount;
public int GameCount
{
get
{
return gameCount;
}
set
{
gameCount = value;
}
}

public int PeopleCount
{
get
{
return peopleCount;
}
set
{
peopleCount = value;
}
}

int rgameCount;
int rpeopleCount;

public GoldGame(string str1,string str2)
{
InitializeComponent();
this.gameCount = Convert.ToInt32(str1);
this.peopleCount = Convert.ToInt32(str2);
}
public GoldGame()
{
InitializeComponent();
}

两个整型变量gameCount和peopleCount用于接收欢迎窗体传过来的游戏轮次和游戏玩家个数的值,通过函数重载的方法实现。

游戏窗体Load事件:


private void Form1_Load(object sender, EventArgs e)
{
int i;
rgameCount = 1;
rpeopleCount = 1;
for (i = 0; i <= peopleCount; i++)
{
Goldgame players = new Goldgame();
players.number = rpeopleCount-1;
players.digit = 0;
players.score = 0;
player.Add(players);
Goldgame achieves = new Goldgame();
achieves.number = rpeopleCount-1;
achieves.digit = 0;
achieves.score = 0;
achieve.Add(achieves);
rpeopleCount++;
}
rpeopleCount = 1;
label3.Text = "第" + rgameCount + "轮游戏:";
textBox1.Text = Convert.ToString(rpeopleCount);
textBox2.Text = "0";
}

该事件中,实现了对玩家信息列表和游戏成就列表的赋初值,以及对各个控件的信息设置。

游戏输赢比较函数Campare:


void Campare(float goldpoint)
{
int i;
float max, min, winpeople = 0, lospeople = 0;
max = min = System.Math.Abs(player[1].digit - goldpoint);
for (i = 1; i <= peopleCount; i++)
{
if (System.Math.Abs(player[i].digit - goldpoint) <= min) //找到最接近黄金点的数
{
winpeople = player[i].digit;
min = System.Math.Abs(player[i].digit - goldpoint);
}
if (System.Math.Abs(player[i].digit - goldpoint) >= max) //找到离黄金点最远的数
{
lospeople = player[i].digit;
max = System.Math.Abs(player[i].digit - goldpoint);
}
}
for (i=1; i <= peopleCount; i++)
{
achieve[i].digit = player[i].digit;
}
listBox1.Items.Add(‘\n‘);
if (winpeople == lospeople)
{
listBox1.Items.Add("玩家数字一样,故不判断输赢!");
}
else
{
for (i = 1; i <= peopleCount; i++)
{
if (player[i].digit == winpeople)
{
achieve[i].score += peopleCount;
listBox1.Items.Add("第" + i + "号玩家赢。");
}
}
for (i = 1; i <= peopleCount; i++)
{
if (player[i].digit == lospeople)
{
achieve[i].score -= 2;
listBox1.Items.Add("第" + i + "号玩家输。");
}
}
}
listBox1.Items.Add(‘\n‘);
for (i = 1; i <= peopleCount; i++)
{
listBox1.Items.Add("第" + i + "号玩家," +"数字为:"+achieve[i].digit+ ",分数为:" + achieve[i].score);
}
}

算法思想同C语言实现,此处不再赘述。

裁判函数Referee:


private void Referee()
{
int i;
float sum = 0, goldpoint = 0;
for (i = 1; i <= peopleCount; i++)
{
sum += player[i].digit;
}
label3.Text = "第"+rgameCount+"轮游戏结果如下:";
listBox1.Items.Clear();
listBox1.Items.Add("第"+rgameCount+"轮游戏平均数为:"+sum/peopleCount);
goldpoint = Convert.ToSingle(sum / peopleCount * 0.618);
listBox1.Items.Add("第" + rgameCount + "轮游戏黄金点为:" + sum / peopleCount*0.618);
Campare(goldpoint);
}

该函数实现对平均数、黄金点的计算和输出,同时调用了输赢比较函数,实现了把对玩家输赢的输出以及各个玩家成绩输出到listBox1中。

输入分数按钮的Click事件:


private void button1_Click(object sender, EventArgs e)
{
if (rpeopleCount < peopleCount)
{
listBox1.Items.Add(Convert.ToString("玩家编号:"+ rpeopleCount+","+"玩家数字:" + textBox2.Text));
player[rpeopleCount].number = rpeopleCount;
player[rpeopleCount].digit = Convert.ToSingle(textBox2.Text);
rpeopleCount++;
}
else if((rpeopleCount == peopleCount))
{
listBox1.Items.Add(Convert.ToString("玩家编号:" + rpeopleCount + "," + "玩家数字:" + textBox2.Text));
player[rpeopleCount].number = rpeopleCount;
player[rpeopleCount].digit = Convert.ToSingle(textBox2.Text);
textBox1.Text = Convert.ToString(rpeopleCount);
listBox1.Text = "第"+rgameCount+"游戏成绩如下:";
Referee();
MessageBox.Show("全部玩家已输入数字,请点击下一轮游戏!", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
textBox1.Text = Convert.ToString(rpeopleCount);
}

该事件中,实现对玩家信息的输入,同时显示到listBox1中,输入完毕,宣布本轮游戏的游戏成绩并提示进入下一轮。

下轮游戏按钮的Click事件:


private void button4_Click(object sender, EventArgs e)
{
if (rgameCount < gameCount)
{
rgameCount++;
label3.Text = "第" + rgameCount + "轮游戏:";
rpeopleCount = 1;
textBox1.Text = Convert.ToString(rpeopleCount);
listBox1.Items.Clear();
}
else
{
label3.Text = "第" + rgameCount + "轮游戏:";
textBox1.Text = Convert.ToString(rpeopleCount);
MessageBox.Show("游戏结束,请点击公布成绩!", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

该事件中,实现了对每轮游戏的玩家编号的初始化及各个控件的设置。若游戏结束,则会提示游戏结束并提示点击公布成绩。

按分数从高到底排序函数Sort:


private void Sort()
{
int i, j;
for (i = 1; i <= peopleCount; i++)
{
int tempsignal = i, tempnumber = achieve[i].number, tempscore = achieve[i].score;
tempnumber = achieve[i].number;
tempscore = achieve[i].score;
for (j = i + 1; j <= peopleCount; j++)
{
if (tempscore < achieve[j].score)
{
tempsignal = j;
tempnumber = achieve[j].number;
tempscore = achieve[j].score;
}
}
achieve[tempsignal].number = achieve[i].number;
achieve[tempsignal].score = achieve[i].score;
achieve[i].number = tempnumber;
achieve[i].score = tempscore;
}
}

该函数实现了对分数从高到底的排序,具体实现同C语言,此处不再赘述。

公布成绩按钮的Click事件:


private void button2_Click(object sender, EventArgs e)
{
Sort();
int i;
label3.Text = "游戏分数排名:";
listBox1.Items.Clear();
for (i = 1; i <= peopleCount; i++)
{
listBox1.Items.Add("玩家编号:" + achieve[i].number + "," + "玩家分数:" + achieve[i].score);
}
}

该事件实现了把各个玩家成就输出到listBox1中。并设置各个控件。

重新开始按钮的Click事件:


private void button3_Click(object sender, EventArgs e)
{
player.Clear();
achieve.Clear();
int i;
rpeopleCount = 1;
rgameCount = 1;
for (i = 0; i <= peopleCount; i++)
{
Goldgame players = new Goldgame();
players.number = rpeopleCount-1;
players.digit = 0;
players.score = 0;
player.Add(players);
Goldgame achieves = new Goldgame();
achieves.number = rpeopleCount-1;
achieves.digit = 0;
achieves.score = 0;
achieve.Add(achieves);
rpeopleCount++;
}
rpeopleCount = 1;
label3.Text = "第" + rgameCount + "轮游戏:";
textBox1.Text = Convert.ToString(rpeopleCount);
listBox1.Items.Clear();
}

该事件同游戏窗体的Load事件,实现了对玩家信息列表和游戏成就列表的清空,游戏轮次、游戏玩家的初始化以及各个控件的设置。

退出游戏按钮的Click事件:

private void button5_Click(object sender, EventArgs e)
{
this.Close();
}

游戏界面:

 技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

结对总结

编程习惯总结:本次结对编程过程中,我和结对伙伴都对代码规范的要求都很高,经过几次的一起结对编程,似乎都形成了一种默契,例如:if-else语句中假如只有一条语句,也要打上花括号“{”和“}”、变量命首字母小写、复杂变量名用几个单词组合的方式、尽量不把多条语句子放在一行、功能实现函数首字母大写、花括号“{”和“}”独占一行等等。我和结对伙伴非常追求算法的效率,尽量减少代码的重复率。界面也很讲究,界面都追求美观、实用、简洁,要有适当的文字提示等等。

黄金点游戏

标签:

原文地址:http://www.cnblogs.com/zhaohaidong/p/5746161.html

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