Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm sorry for my bad English, but it isn't my language.

I've done an exercise in C++11 for University (Goos Game):

this is the code (3 files):

header.h
C++
#ifndef C
#define C

class Campo{
	public:
		Campo(){
			turno=0;
			g = new giocatore[3];
			g[0].pos=g[1].pos=g[2].pos=0;
			field = new casella[50];
			srand(time(nullptr));
			g[0].stato=g[1].stato=g[2].stato=false;
		};
		~Campo(){
				delete[] g;
				delete[] field;
		};
		void creaCampo();
		int turnoDado();
		int stampaStato();
		
		typedef struct{
			int pos;
			bool stato;
		} giocatore;
		
		typedef struct{
			int posto;
			char vrn;
		} casella;
	private:		
		giocatore* g;
		casella* field;
		int turno;
};
#endif


impl.cpp
C++
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include "header.h"

void Campo::creaCampo(){
		for(int i=0; i<50; ++i){
			switch(rand()%3+1){
				case 1:
					field[i].vrn='n';
					break;
				case 2:
					field[i].vrn='r';
					break;
				case 3:
					field[i].vrn='v';
					break;
				default:
					break;
			}
		}
		std::cout<<"Campo creato!"<<std::endl;
}

int Campo::turnoDado(){
	std::cout<<"è il turno del giocatore n° "<<turno+1<<std::endl;
	
	for(int i=0; i!=50; ++i){
		if(field[i].posto==g[turno].pos && field[i].vrn=='r' && g[turno].stato==false){
			g[turno].stato=true;
			++turno;

			if(turno>2 || turno<0){
				turno=0;
			}

		}
		return 0;
	}	
	
	{
		int i=0;
		do{
			{
				int uscita1=(rand()%6+1);
				int uscita2=(rand()%6+1);
				g[turno].pos+=(uscita1+uscita2);
				std::cout<<"Primo dado: "<<uscita1<<" Secondo dado: "<<uscita2;
			}
		
			std::cout<<" quindi, posizione: "<<g[turno].pos<<std::endl;
			i=g[turno].pos;
 		}while(field[i].vrn=='v'); 	//se nella posizione appena uscita è "Verde", ritira
	}
	++turno;	//avanza il turno

	if(turno>2 || turno<0){
		turno=0;
	}

	for(int i=0; i!=50; ++i){
		if(field[i].posto==g[turno].pos && field[i].vrn=='r' && g[turno].stato==false){
			g[turno].stato=true;
			++turno;

			if(turno>2 || turno<0){
				turno=0;
			}

		}
		return 0;
	}
	for(int i=0; i!=3; ++i){
		if(g[i].pos>=50){
			std::cout<<"Complimenti! Il giocatore n°: "<<g[i].pos<<std::endl;
		}
	}
}

int Campo::stampaStato(){
	for(int i=0; i!=3; ++i){
		std::cout<<g[i].pos<<std::endl;
	}
}


main.cpp
C++
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include "header.h"
using namespace std;
int main(){
	Campo c;
	
	c.creaCampo();
	
	while(c.turnoDado()==0){
		c.stampaStato();		
		cout<<endl;
	}
	
	c.stampaStato();
return 0;
}


Thanks in advance.

Update 1:
Well, i modified the code and start a loop. But before the program stopped at first step or there was increments from g[turno] and others not.
Posted
Updated 31-Oct-15 3:39am
v2
Comments
Garth J Lancaster 31-Oct-15 8:51am    
as per your other question, you havnt told us what the problem is ... we cant help you unless you're a little clearer in what the issue is .. do you get an error, are you stuck with 'how to do something' for example ?

apologies for the cross-post comment - I thought you'd posted the same question twice ..

we are pretty tolerant of language btw - I know at least one of our members is fluent in Italian, I 'get by', that being said, il linguaggio di CodeProject e l'inglese
OriginalGriff 31-Oct-15 8:51am    
And?
What is the question?
Bear in mind that we can;t see your screen, access your HDD, or read your mind - and we have no idea what this is supposed to do, much less what it does that you didn't expect, or doesn't do that you did! :laugh:

So tell us!
Use the "Improve question" widget to edit your question and provide better information.
Stefano Lodico 31-Oct-15 9:14am    
The problem is a loop. Is there something incorrect?
phil.o 31-Oct-15 9:20am    
Please describe the problem itself. A loop is not a problem in itself, a loop is a way to accomplish something programmatically.
What is the symptom? Incorrect data? Crashes? Exceptions? Error messages? What? :)
You may use an online translator if you are not fluent in english.
Stefano Lodico 31-Oct-15 13:07pm    
I updated the main post.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900