|
 |
|
 |
07-20-2005, 01:22 AM
|
#1 (permalink)
|
|
Salchester
Join Date: Jul 2005
Location: In a house
Posts: 230
|
Help
Can anybody help, As I have the following problem. I'm a bit rusty when it comes to this.
I have been asked to create a program, to be used during a school games day. In total there are 36 students from all classes that belong to “Houses” which compete for the House Cup.
Each Student belongs to either the Green or Yellow house. The names of the students and number in each class are provided below t, together with their house.
THE GAMES THAT ARE PLAYED DURING THE DAY ARE:
· Snap
· Cribbage
· Spilinkins
· Junior Scrabble
GAMES DAY RULES
The Yellow and Green houses compete against each other in a series of matches. Each match is ‘best’ of three games of one type of game. Pupils cannot play other pupils from the same class. Pupils can only play another pupil once. Each pupil will take part in one match for each time of game.
__________________
Many Thanks, in advance!
Salchester.
The Future Is Here - Are You Ready?
|
|
|
07-20-2005, 05:18 AM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
Yes, and Jones is the winner ???
What is it supposed to do, keep score? come up with a reasonable gameplan in order to distribute the participans?
I've removed the link in your sig, since it isn't appreciated for first time posters.
|
|
|
07-20-2005, 07:15 AM
|
#3 (permalink)
|
|
Salchester
Join Date: Jul 2005
Location: In a house
Posts: 230
|
Chears redhead for removing the link, the program is supposed to sort the 36 pupils so that each student plays all 4 of the available games, and can not play other pupils from the same class. Each pupil will take part in one match for each type of game. Any Ideas greatly appriated on how i might go about doing this, as i haven't got a clue!
__________________
Many Thanks, in advance!
Salchester.
The Future Is Here - Are You Ready?
|
|
|
07-20-2005, 09:08 AM
|
#4 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
Let's sum this up into something I can understand.
Code:
Houses
- green
- 36 students
- X classes
- yellow
- 36 students
- Y classes
Theres 4 games where each house has to compete against eachother the 36 students from yellow house has to compete against the 36 students from green house, noone is allowed to compete in one game more than one time, noone is allowed to compete against another student in their class, and each game is decieded by a "best out of three" match.
If this is the lineout of your problem? Then you can actualy see the design in my small setup there.
As you can see theres two arrays containing 36 students classes, each student has their respective class assigned, the C++ classes would look something like this:
Code:
class game{
private:
std::string name;
bool play;
bool won;
/* not allowed to create a game without name */
game(void){};
public:
game(std::string n){name=n; played=false; won=false; };
void win_game(void){won=true; };
void play_game(void){play=true; }
bool win(void){return won; };
bool played(void){return play; };
}
class student{
private:
std::string name;
/* class is a keyword, so shortened */
std::string clss;
std::vector <game> games;
/* not allowed to create a student without name */
student(void){};
bool in_game(std::string g){
for(int i=0; i < games.size(); ++i)
if(games[i].get_name() == g)
return true;
return false;
};
public:
student(std::string n){name= n;};
void assign_class(std::string c){clss = c; };
void assign_game(std::string g){
if(!this->in_game(g))
games.push_back(g);
};
bool play_game(std::string g){
for(int i=0; i < games.size(); ++i)
if(games[i].get_name() == g)
{
games[i].play_game();
return true;
}
return false;
};
bool win_game(std::string g){
for(int i=0; i < games.size(); ++i)
if(games[i].get_name() == g)
{
games[i].win_game();
return true;
}
return false;
};
std::string get_name(void){return name; };
std::string get_class(void){return clss; };
void games_played(void){
for(int i=0; i < games.size(); ++i)
if(games[i].played())
std::cout << games[i].get_name() << std::endl;
};
void games_won(void){
for(int i=0; i < games.size(); ++i)
if(games[i].win())
std::cout << games[i].get_name() << std::endl;
};
}
class house{
private:
std::string name;
std::vector <student> students;
/* not allowed to create a house without name */
house(void){};
bool in_house(std::string s){
for(int i=0; i < students.size(); ++i)
if(students[i].get_name() == g)
return true;
return false;
};
public:
house(std::string n){name=n; };
void assing_student(student s){
if(!this->in_house(s.get_name()))
students.push_back(s);
};
std::string get_name(void){return name; };
}
I'm sure it can be further developed, or even designed in another manner.. This was just what I first thought of.
Last edited by redhead; 07-20-2005 at 09:35 AM.
|
|
|
07-20-2005, 12:32 PM
|
#5 (permalink)
|
|
Salchester
Join Date: Jul 2005
Location: In a house
Posts: 230
|
Some Correct/Some Wrong
Some Correct Some Wrong
In total there are 36 students taking part in the schools games day. Eighteen of which are in the “Yellow” house, and 18 in the “Green” house.
The eighteen students in the “Yellow” house compete against the eighteen students in the “Green” house. (Vice Versa)
None is allowed to compete in one game more than one time (as you said)
None is allowed to compete against another student in their class (as you said)
and each game is decided by a "best out of three" match. (as you said)
What would the code including the following alterations?
__________________
Many Thanks, in advance!
Salchester.
The Future Is Here - Are You Ready?
|
|
|
07-20-2005, 01:02 PM
|
#6 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
No need to alter the code, I made it infinite, it opens possibileties to create unlimited students in unlimited houses compeeting in unlimited games placed in unlimited number of classes..
Just use the provided function get_name() from all classes to compare anywhere it is needed if it is permitted or not.
|
|
|
07-20-2005, 01:15 PM
|
#7 (permalink)
|
|
Salchester
Join Date: Jul 2005
Location: In a house
Posts: 230
|
Loading Bar
How do I create a loading progress bar. As i would like to use this when gathering information from a file.
__________________
Many Thanks, in advance!
Salchester.
The Future Is Here - Are You Ready?
|
|
|
07-20-2005, 01:41 PM
|
#8 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
Will this give you an idear
Code:
#include <iostream>
#include <unistd.h>
int main()
{
std::string out_bar("");
for(int i=1; i <= 100; ++i)
{
if(!(i%10))
out_bar += "#";
std::cout << "\r" << out_bar;
std::cout.width(20 - out_bar.size());
std::cout << i << "%";
std::cout.flush();
sleep(1);
}
std::cout << std::endl;
return 0;
}
The key here is the "\r" it returns the cursor to start of line, befor the writing begins, effectively overwriting the previus shown buffer on that line..
If you're looking for something like drawing windows or segments of the shown window, look at what curses.h or ncurses.h provides, but that is system specific.
|
|
|
07-20-2005, 10:33 PM
|
#9 (permalink)
|
|
Salchester
Join Date: Jul 2005
Location: In a house
Posts: 230
|
Commenting
Can you comment all code as i havn't got a clude what some it it means/does?
Cheers, sorry to be a pain in the arse!
__________________
Many Thanks, in advance!
Salchester.
The Future Is Here - Are You Ready?
|
|
|
07-20-2005, 11:52 PM
|
#10 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
Commented code from previus post:
Code:
/*
* The theory here is, that every student has to be assigned
* to a house, and every student must be assigned to a class
* furthermore every student _can_ be assigned to a game, but
* dosn't have to be.
* What we want, is basicaly a class handlet program, much like
* if it orriginaly was written in Java or ADA, where we totaly
* rely on the Object Oriented provided default constructor.
*
* The design:
* To give a basic idear, the design is layed out in an array
* structure, eventho we wont be using arrays, but the more
* flexible vector type.
*
* *Class House:
* name: Name of the house
* students: Any number of students asssigned to house
* *Class Student:
* name: Name of student
* classes: Any number of classes student attend
* *Class clss:
* name: Name of class
* grade: What grade given in class
* games: Any number of games student participate in
* *Class game:
* name: Name of game
* play: Has the game been played, values:
* 0) The game hasn't been played
* 1) It has been played one time
* 2) it has been played two times,
* check win value
* a) if two here, then end game
* and student is winner
* b) if only one here, play again
* 3) it has been played three times,
* not allowed to play any more.
* win: Number of times the game has ended
* in a win for student
*
* As it is clear, this design gives way for a number of extensions
* the classes a student is enroled in, can reveal their grades aswell
*/
class game{
private:
std::string name;
int play;
int won;
/* not allowed to create a game without name */
game(void){};
public:
/* initialize by setting played and win to 0 */
game(std::string n){name=n; played=0; won=0; };
/* for every win, just count it one up */
void win_game(void){won++; };
/* for every play, just count it one up */
void play_game(void){play++; }
bool win(void){return won; };
bool played(void){return play; };
}
class clss{
private:
std::string name;
double grade;
clss(void){};
public:
clss(std::string n): name(n); {};
void grade(double g):grade(g); {};
std::string get_name(void){return name; };
std::string get_grade(void){return grade; };
}
class student{
private:
std::string name;
/* class is a keyword, so shortened */
std::vector <clss> classes;
std::vector <game> games;
/* not allowed to create a student without name */
student(void){};
/* when we assign a game to the student, we need to know,
* if they've allready been assigned that game
*/
bool in_game(std::string g){
for(int i=0; i < games.size(); ++i)
if(games[i].get_name() == g)
return true;
return false;
};
/* when assigning a class to the student, we need to knwo,
* if they've allready been assigned to that calss
*/
bool in_class(std::string c){
for(int i=0; i < classes.size(); ++i)
if(classes[i].get_name() == c)
return true;
return false;
};
public:
student(std::string n):name (n);{};
void assign_class(std::string c){
if(!this->in_class(c))
classes.push_back(c);
};
void assign_game(std::string g){
/* if they are in the game, dont add it */
if(!this->in_game(g))
games.push_back(g);
};
bool play_game(std::string g){
/* if they can play the game, play it */
for(int i=0; i < games.size(); ++i)
if(games[i].get_name() == g)
{
games[i].play_game();
return true;
}
return false;
};
bool win_game(std::string g){
/* if they can win the game, win it */
for(int i=0; i < games.size(); ++i)
if(games[i].get_name() == g)
{
games[i].win_game();
return true;
}
return false;
};
std::string get_name(void){return name; };
std::string get_class(void){return clss; };
void games_played(void){
/* Create a way of showing what games have been played */
for(int i=0; i < games.size(); ++i)
if(games[i].played())
std::cout << games[i].get_name() << std::endl;
};
void games_won(void){
/* Create a way of showing what games have been won */
for(int i=0; i < games.size(); ++i)
if(games[i].win() > 1)
std::cout << games[i].get_name() << std::endl;
};
}
class house{
private:
std::string name;
std::vector <student> students;
/* not allowed to create a house without name */
house(void){};
/* when we assign a student to the house, we need to know,
* if they've allready been assigned that house
*/
bool in_house(std::string s){
for(int i=0; i < students.size(); ++i)
if(students[i].get_name() == g)
return true;
return false;
};
public:
house(std::string n){name=n; };
void assing_student(student s){
if(!this->in_house(s.get_name()))
students.push_back(s);
};
std::string get_name(void){return name; };
/* show teh students currently in this house */
void show_students(void){
for(int i=0; i < students.size(); ++i)
std::cout << students[i].get_name() << std::endl;
};
}
As you can see, the classes here aren't done. But the thought is to achieve everything by implementing functions within the house class, that calls the functions provided by student class which again calls functions provided by clss class and game class in order to decide when a student can be assigned a house/game/class, the real treat comes when providing a house function, which will allow a student to be compared up against the inbound students in the current house, displaying what games that student needs to play, and which students from current house is allowed as the opponent to those games.
But then again, as I started out, it could also well be that my quick design here is flawed, and it all needs to be reinvented.
Code:
#include <iostream>
#include <unistd.h>
int main()
{
/* initialize the string used to have nothing in it */
std::string out_bar("");
/* make a simple counter to display 1 - 100% */
for(int i=1; i <= 100; ++i)
{
/* for every 10% extend the progress bar with a # */
if(!(i%10))
out_bar += "#";
/* display progress bar by resetting cursor to start of line first */
std::cout << "\r" << out_bar;
/* set the width needed to display the % in the same place every time
* in this case 20 chars from start of line.
*/
std::cout.width(20 - out_bar.size());
/* print out the percentage reached */
std::cout << i << "%";
/* make sure everything in teh outpt buffer is writen to screen */
std::cout.flush();
/* have a one second sleep so we wont flyby with out progress bar */
sleep(1);
}
/* make a new line to nicely place any opther output away from our
* progress bar.
*/
std::cout << std::endl;
return 0;
}
|
|
|
07-21-2005, 01:01 AM
|
#11 (permalink)
|
|
Salchester
Join Date: Jul 2005
Location: In a house
Posts: 230
|
Arrays (Program Alterations)
I have been instructed to build the program using arrays, what would the code look like if this was to be done. I am very much in the dark at the moment.
__________________
Many Thanks, in advance!
Salchester.
The Future Is Here - Are You Ready?
|
|
|
07-21-2005, 01:51 AM
|
#12 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
The easiest way to create this using arrays, would be to know exactly how many classes/houses/students/games and then staticaly initialize the needed amount of memory, else you'll end up with a program that could have unpredictable outcomes, since you most likely at some point will use uninitialized memory which will overwrite and disturb other parts of your running code.
That is avoided when using vectors, since they have their own memory handler, which makes sure the needed memory is available and that you're not off by one in your indexing of the arrays.
Once I'll come up with some code for this, you can expect it to be in C, not C++, since I am at my best with this type of memory handling in strict C.
I will look closer at this during the weekend, but I would also like to see some code fragments from you, so I know where you stand. Right now it seems as if I'm the only one comming up with idears to a solution for this.
|
|
|
07-21-2005, 01:57 AM
|
#13 (permalink)
|
|
Salchester
Join Date: Jul 2005
Location: In a house
Posts: 230
|
Youv'e Nearly Got What I'm trying to do!
You have nearly got the jist of what am need to do!
Let’s start from the top. I have been asked to make a program, to be used on a school games day. The program will be coded, to read the students name, house & class out of a file.
The student’s names have been supplied to me within their appropriate house and class.
An algorithm needs to be made, to organise the students so that they play each of the 4 available games. Whilst ensuring, that no one plays against anybody more than once on each of the games and that no body plays another student in the same class. (This has been done and has been explained below.)
ALGORYTHM
Students are organised into house colours, whilst keeping them in alphabetical order.
The yellow house was moved down three names, whilst repositioning three bottom names at the top of the list!
THE GAMES THAT ARE PLAYED DURING THE DAY ARE:- Snap
- Cribbage
- Spilinkins
- Junior Scrabble
An option should be available to enter the scores for each student, to finally reveal the winner of the competition. (Name, house & class)
How would this be coded using arrays?
What would the coding look like?
I think I have covered everything, ill let you know If I haven’t.
Many Thanks
__________________
Many Thanks, in advance!
Salchester.
The Future Is Here - Are You Ready?
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 01:23 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|