I am trying to convert this code from C++ to C# and Unity. The game is simple roll a pair of die, check against specific numbers. if you hit the good ones, you win your bet, and get 2x the betted credits. if you hit the crap numbers, you loose your bet money.
This code is in a huge rough draft, with most things being commented out that will need replacing, and even a return where text should be generated for now.
My issue isn’t these minor nit picky errors.
It is formatting in general. I had almost all the immediate bugs worked out, but when getting to my "int PlayGame() function, unity refuses to recognize this as a function and returns a CS1519 error.
I pulled this code into Notepad++ to check my formatting. after bleeding through for a while I noticed that the code block above this specific function REFUSES to close no matter how many times I attempt to close it.
Somehow, I completely screwed up the formatting, and cannot figure it out. Everything APPEARS to be fine, but now I have a buttload of errors saying things like “unexpected symbol “else”.”
Please help me fix this, as I am close to giving up hope.
Also, while not in the scope of this question, if you help me figure out how to replace the “cout” function for unity GUI Text, I will be eternally grateful. I cannot find a decent example about setting up text and changing it on the fly through a script, except for things like health text which I can already change.
using UnityEngine;
using System.Collections;
public class Game : MonoBehaviour
{
//bool PlayGame();
//int die();
//int roll(int a, int b);
int Start()
{
if (Input.GetKeyDown("return"))
{
Begin();
}
}
bool Begin()
{
int roundsPlayed = 0;
int credits = 50;
int bid = 0;
bool done = false;
while (!done)
{
roundsPlayed++;
while(1)
{
//cout << "You currently have " << credits << " credits.
How much do you wager?
UP: Increase wager
DOWN: Decrease wager
A: Set wager
";
while(1)
{
if (Input.GetKeyDown(“up”))
{
bid++;
if (bid > credits)
{
bid = 0;
}
}
}
else if (Input.GetKeyDown(“down”))
{
bid–;
if (bid < 0)
{
bid = credits;
}
}
}
}
if (bid == 0)
{
//cout << "You cannot bid 0 credits!
";
return;//temp till I fix text gui
}
else
{
//cout << "
";
break;
}
}
}
bool result = playGame()
{
if (result == true)
{
//cout << "Won " << bid << " credits!
";
credits += bid;
bid = 0;
}
else
{
//cout << "Lost " << bid << " credits…
";
credits -= bid;
bid = 0;
if (credits == 0)
{
//cout << "
Left the game with no credits
left…
Played " << roundsPlayed << " games.
Press START to play again.“;
break;
}
while(1)
{
//cout << "
A: Continue game
B: Retire
“;
if (Input.GetKeyDown(“y”))
{
break;
}
if (Input.GetKeyDown(“n”))
{
done = true;
//printf(”\e[1;1H\e[2J”);
//cout << "
Left the game with " << credits << "
credits.
Played " << roundsPlayed << " games.
Press START to play again.";
break;
}
}
}
int die()//generates a value for the die.
{
int value = Random.Range(1,7);
return value;
}
int roll(int a, int b)//returns the sum of two rolls.
{
return a + b;
}
int PlayGame()
{
int roll1 = die();
int roll2 = die();
int total = roll(roll1, roll2);
//cout << "Player rolled " << roll1 << " + " << roll2 << " = " << total << "
";
if (total == 7 || total == 11) //Player rolls a 7 or 11 - They win the game
{
//cout << "You won! Rolled a " << total << ".
";
return true;
}
else if (total == 2 || total == 3 || total == 12)//Player rolls one of the craps - they lose the game
{
//cout << "You lose! Rolled a " << total << ".
";
return false;
}
else //otherwise we set a point and attempt to win the game.
{
//cout << "
Rolled a " << total << ", you must now roll
until you get " << total << " again, or lose
if you roll 7!
";
int point = total;
bool win = false;
while (!win)
{
int rollA = die();
int rollB = die();
int total1 = roll(rollA, rollB);
//cout << "Player rolled " << rollA << " + " << rollB << " = " << total1 << "
";
if (total1 == 7)
{
//cout << "You lose!
";
return false;
}
else if (total1 == point)
{
return true;
}
}
}
}
}