Pairing programming--The Golden dot game

Source: Internet
Author: User
Tags float max float number rounds

Introduction : After one months of software engineering This course of study, generally benefited a lot. Learn how to be a qualified programmer for a qualifying program. Not to say that a program can run is the program, but all aspects, such as interface, simplicity, efficiency, perfection are to be perfect. A good programmer is not only to meet the needs of customers, but also to find their own future problems, and ensure the scalability of the program. This semester we will have a personal project, a pair of projects, the team project step by step in-depth study, then this time we received a team programming task.

Task : The topic of this pair programming is as follows: The Golden Dot game is a digital mini-game whose game rules are:

n students (n usually greater than ten), each writing a rational number between 0~100 ( excluding 0 or three ), to the referee, the referee calculates the average of all the numbers, and then times the 0.618(so-called golden partition constant) , the G value is obtained. The number of the submitted nearest to G(absolute value) of the students get n points, the farthest from the G students get-2 points, the other students get 0 points. after a few days of playing, we found some interesting phenomena, such as the golden dots moving down gradually.

1, this job belongs to the pair programming project, must be completed by two people, and will be sent to the blog, respectively, the operation of the source code submitted to the codeing system;

2, if possible to use the C/s or b/n as much as possible, that is, using the server to receive and process all the players submitted by the number, and the results back to the players, players can be submitted by the client number;

3, if the use of single-machine approach, it is necessary to provide users with convenient input interface;

4, the game can run at least 10 rounds at a time, and can keep the results of the rounds.

personnel : This pair programming, my teammates are hulin-shaped, he is my class the only Anhui villagers. He is low-key in his work and works silently and diligently. We have been actively discussing and exchanging views on many of the problems we have encountered in this programming.

My coding Address: Https://coding.net/u/lvjianxiong

Hulin-Shaped coding address: Https://coding.net/u/hulinzhuang

Hulin-Like blog address: http://www.cnblogs.com/hulinzhuang/

Programming Process : We actively begin to analyze the topic, understand what the title is, play is a game, we are programmed to use a pen instead of a computer, go through the process of the game. I think this is very necessary. Then into the programming, the choice of programming language is C language (c + + is not easy to understand C language; Java is still in the self-study stage, not enough to program). n Each student input a rational number, with the scanf function is easy to achieve, sum and then take the average, then calculate the G-point value, which is the simplest calculation, and then compare each value with the G-point approach, but considering not simple subtraction, because you do not know which value is large, if there are absolute values of the symbol is good. Baidu knows, Fabs is to seek the absolute value of floating point number. This is a small harvest, it is not known before; As for the module, we can set a max and Min values, the initial value is assigned to the first value, followed by the value of one and the initial comparison, and finally get the maximum and minimum value of this set of data. The maximum value is assigned n, the minimum value is 2, and the other values do not change.

Program Source Code

#include "stdafx.h"
#include "Stdlib.h"
#include "Windows.h"
#include "math.h"
typedef struct GAMEPLAYER//define player structure
{
int name;
int score;
float number;
}player; Contains the player's name, score, number, score information

Each player involves a lot of information, we naturally choose the structure.

void Startgame (FILE *fp); Initialization of the game
void Gamerule (); Game rules
void Suan_score (PLAYER *p,float ave,int num,file *FP);//function to calculate game scores

Here we mainly use these functions.

int main ()
{
System ("Color 3B");
int choice;
FILE *FP;
while (1)
{
Fp=fopen ("Goldgame.txt", "w+");
printf ("\ n golden dot game \ n");
printf ("1. Game rules \ n ");
printf ("2. Start the game \ n ");
printf ("3. Exit the game \ n ");
scanf ("%d", &choice);
Switch (choice)
{
Case 1:gamerule (); Break
Case 2:startgame (FP); Break
Case 3:exit (0); Break
Default
{
printf ("Your input is wrong, please re-enter \ n");
Break
}
}
}
return 0;
}

The main function, we designed the initial interface: including start game, game rules, exit the game simple function. Use the switch function to read the player's actions.

Void Startgame (FILE *fp)
{
PLAYER *p;
int i,playernum,gamenum,j;
int flag=1;
float sum,ave;
Cha R Choice;
p= (player) malloc (10*sizeof);//dynamically assign struct array
printf ("Enter the number of players involved:"),
scanf ("%d", &playernum);
if (playernum>10)
{
p= (player *) realloc (p,playernum*sizeof (player));//space is insufficient to add space
}
printf (" Please enter the race round: ");
scanf ("%d", &gamenum);
printf ("\ n");
for (j=0;j<gamenum;j++)
{
printf ("%d rounds: \ n", j+1),
for (i=0,sum=0;i<playernum;i++)//sum is set to total and
{
if (flag==1)
{
p[i].name=i+1;
P[i].score=0;
P[i].win=0;
P[i].fail=0;
}//Initialize initial value to 0
printf ("Player%d:", p[i].name);
scanf ("%f", &p[i].number);
Sum+=p[i].number;
}
Ave=sum/playernum;//calculate mean
ave= (float) (ave*0.618);//Calculate the value of the golden point
printf ("The value of golden point G:%f\n", ave);
Suan_ Score (P,AVE,PLAYERNUM,FP);
Flag=0;
}

}

This is the beginning of the game function, dynamic allocation of memory space, if not enough, need to add space. The main player number and game turn settings, and then read the player's various values, calculate the G-point value, into the Suan_score function.

void Suan_score (PLAYER *p,float ave,int num,file *fp)
{
int i;
Char ch;
float max= (float) fabs (p[0].number-ave);
float min= (float) fabs (p[0].number-ave);

for (i=0;i<num;i++)//Statistics out the maximum value, minimum value
{
p[i].b= (float) fabs (p[i].number-ave);
if (max<p[i].b)
max=p[i].b;
if (min>p[i].b)
min=p[i].b;
}

for (i=0;i<num;i++)//players give results
{
if (P[i].b==max)
{
p[i].score-=2;
}
if (p[i].b==min)
{
P[i].score+=num;
}
}
printf ("Cumulative game score: \ n");
for (i=0;i<num;i++)//Player output score
printf ("Player%d:%d\n", P[i].name,p[i].score);
while (1)
{
printf ("Whether to continue Y or N:");
scanf ("%c", &ch);
if (ch== ' Y ' | | ch== ' Y ')
{
Break
}
else if (ch== ' n ' | | ch== ' N ')
{
Exit (0);
}

}
}

This is the function that gives each player the result, first the max and Min are set to the first player's value, and then the subsequent values are compared one by one to select the maximum and minimum values for this group. The last player to compare the maximum +n, get the minimum player-2, other players we do not do any action. Output each player's score and proceed to the next round of gameplay.

void Gamerule ()
{
printf ("N" Students (n usually greater than 10), each writing a rational number between 0~100 (excluding 0 or 100), the referee calculates the average of all the numbers, and then multiplies by 0.618 (the so-called Golden Partition constant) to get the G value. The number of the submitted nearest to G (absolute value) of the students get n points, the farthest from the G students get-2 points, the other students get 0 points. \ n ");
}

   This is the reading of the game rules function, is simple printf can be done.

Summary: As our squad of two people are postgraduate, even Saturday Sunday have courses, time is relatively hasty, this programming also calculate time to complete. Although not perfect, we also want to be perfect, but still limited to their own ability and the tension of time, not to achieve the perfect heart. But it's still rewarding, brainstorming, and the strength of the team can make the problem easier, though it's just two of people. Hope that the follow-up work, we can do their best to complete, there will be a return on it.

Pairing programming--The Golden dot game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.