#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
#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; } }
#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; }
g[turno]
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)