In: Computer Science
Convert this C++ to js/html and run in browser.
/////////////////////////////////////////////////
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <iostream>
using namespace std;
/*
*
*/
class Dice{
private:
static const int MAXDICE=6;
static const int MINDICE=1;
int faceVal;
public:
Dice(int);
void setFace(int);
int getFace();
string toString();
};
Dice::Dice(int faceVal)
{
if(faceVal<MINDICE&&faceVal>MAXDICE)
{
setFace(1);
}
else
{
this->faceVal=faceVal;
}
}
void Dice::setFace(int faceVal)
{
this->faceVal=faceVal;
}
int Dice::getFace()
{
return faceVal;
}
string Dice::toString()
{
stringstream ss;
ss<<faceVal;
string str="face value is ";
str+=ss.str();
return str;
}
int main(int argc, char** argv) {
srand((unsigned)time(NULL));
Dice dice((rand()%(6))+1);
cout<<dice.toString()<<endl;
return 0;
}
<script language="JavaScript">
var nl = getNewLine()
function getNewLine() {
var agent = navigator.userAgent
if (agent.indexOf("Win") >= 0)
return "\r\n"
else
if (agent.indexOf("Mac") >=
0)
return "\r"
return "\r"
}
pagecode = '#include <cstdlib>
#include <ctime>
#include <sstream>
#include <iostream>
using namespace std;
/*
*
*/
class Dice{
private:
static const int MAXDICE=6;
static const int MINDICE=1;
int faceVal;
public:
Dice(int);
void setFace(int);
int getFace();
string toString();
};
Dice::Dice(int faceVal)
{
if(faceVal<MINDICE&&faceVal>MAXDICE)
{
setFace(1);
}
else
{
this->faceVal=faceVal;
}
}
void Dice::setFace(int faceVal)
{
this->faceVal=faceVal;
}
int Dice::getFace()
{
return faceVal;
}
string Dice::toString()
{
stringstream ss;
ss<<faceVal;
string str="face value is ";
str+=ss.str();
return str;
}
int main(int argc, char** argv) {
srand((unsigned)time(NULL));
Dice dice((rand()%(6))+1);
cout<<dice.toString()<<endl;
return 0;
}'
document.write(pagecode);
</script>