1 Replies - 1145 Views - Last Post: 29 November 2008 - 03:47 AM Rate Topic: -----

#1 Wikaman1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-September 08

need help with arrays in a simple game

Post icon  Posted 28 November 2008 - 08:56 AM

I'm making a simple game that ask's the user for the number of words in a rhyme and the number of people playing. They then input the names of the people playing. Each word of the rhyme is for one person(if there are more words than people than it goes back to the start of the list of people and continues counting). Whoever the last word lands on are out and the game loops until there is only 1 person left. Here is what I have so far but there is something wrong because it sometimes the computer can't calculate who the winner of a round is. I think it has something to do with the one it landing on being "empty" but I cant work out how make it to to the next person if it is empty.

#include <iostream>
#include <string>
#include <windows.h>
#include <dos.h>
using namespace std;

int num_players;
int rhyme_words;
int player_count=0;

 int c=0;
 string out;
string player_names[100];


int main()
{
	 player_names[c];
	cout << "how many words are in the rhyme? " << endl;
	cin >> rhyme_words;

cout << "how many players? " << endl;
cin >> num_players;

for (int i=0; i<num_players; i++)
{
player_count=player_count+1;
cout << "enter the name of player " << player_count  << endl;
cin >> player_names[i];
}
for(int i=0; i<num_players; i++)
{
	
cout << player_names[i] << endl;
}
cout << "round 1.." << endl;
Sleep(2000);
do
{
system("cls");
cout << "3.." << endl;
Sleep(1000);
cout << "2.." << endl;
Sleep(1000);
cout << "1.." << endl;
Sleep(1000);

//int round_loser_pos=c;
//string loser=player_names[round_loser_pos];
for (int i=0; i<rhyme_words; i++)
{
	c=c++;
		if (c >= num_players)
		{
c=0;
		}
cout << player_names[i] << endl;

}
c=c-1;
out = player_names[c];

cout << "the loser of this round is " << out;
player_names[c] = '\0';
			   
Sleep(2000);
system("cls");
cout << "the people still in the game are " << endl;

for (int i=0; i<num_players; i++)
{
cout << player_names[i] << endl;
}
Sleep(2000);
}while (num_players >= 1);

for (int i=0; i<num_players; i++)
{
cout << "the winner is " << player_names[i];
}

}



Is This A Good Question/Topic? 0
  • +

Replies To: need help with arrays in a simple game

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: need help with arrays in a simple game

Posted 29 November 2008 - 03:47 AM

I have modified your code to sorta function. The reason your program doesn't exit with a winner is because you never decrement num_players. Which is a good thing because num_players is used for other things. I used player_count which was otherwise unused. I changed the loser selection routine to use a random number between zero and num_players - 1. This range corresponds to the correct indicies into the player_names[] array.

Hopefully the following code will get you closer to what you are trying to do. One problem that my code has is that when a player is removed, the next choose a loser iteration might pick the same user number as before. Since the user has already been eliminated, the player_names[] array has an empty string. What should happen is that the player_names array should be compacted to make the remaining names contiguous in the array. num_players should be decremented when the player_names array is condensed.

#include <iostream>
#include <string>
#include <windows.h>
#include <dos.h>

#include <ctime>

using namespace std;

int main()
{
	int num_players;
	int rhyme_words;

	int player_count = 0;

	int c = 0;

	//string out;
	string player_names[100];

	srand(time(NULL)); // initialize random number gen

	//player_names[c];
	cout << "how many words are in the rhyme? ";
	cin >> rhyme_words;
	cout << endl;

	cout << "how many players? ";
	cin >> num_players;
	cout << endl;

	for (player_count = 0; player_count < num_players; player_count++)
	{
		cout << "enter the name of player #" << player_count + 1 << ": ";
		cin >> player_names[player_count];
		//cout << endl;
	}
	for (int i = 0; i < num_players; i++)
		cout << player_names[i] << endl;

	cout << "round 1.." << endl;
	Sleep(2000);
	do
	{
		system("cls");
		cout << "3.." << endl;
		Sleep(1000);
		cout << "2.." << endl;
		Sleep(1000);
		cout << "1.." << endl;
		Sleep(1000);

		//int round_loser_pos=c;
		//string loser=player_names[round_loser_pos];
		for (int i = 0; i < rhyme_words; i++)
		{
			//c=c++;

			//if (c >= num_players)
			//{
			//	c=0;
			//}

			// c will range from zero to num_players - 1, which are
			//the only legal indicies into the player_names[] array
			c = rand() % num_players;
		}
		//c=c-1;
		//c -= 1;
		
		//cout << player_names[c] << endl;

		//out = player_names[c];

		cout << "the loser of this round is " << player_names[c];

		//player_names[c] = '\0';

		player_names[c].clear();  // remove name from string array
		player_count--;        // decrement number of players left

		Sleep(2000);
		system("cls");
		cout << "the people still in the game are " << endl;

		for (int i = 0; i < num_players; i++)
			cout << player_names[i] << endl;

		Sleep(2000);

	}while (player_count > 0);

	cout << "the winner is ";

	for (int i = 0; i < num_players; i++)
		cout << player_names[i];

	cout << endl;

	system("pause");
	return 0;
}


This post has been edited by n8wxs: 29 November 2008 - 03:51 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1