- Home /
formatting errors converting this code!
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.\nHow much do you wager?\n\nUP: Increase wager\nDOWN: Decrease wager\nA: Set wager\n\n";
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!\n\n";
return;//temp till I fix text gui
}
else
{
//cout << "\n";
break;
}
}
}
bool result = playGame()
{
if (result == true)
{
//cout << "Won " << bid << " credits!\n";
credits += bid;
bid = 0;
}
else
{
//cout << "Lost " << bid << " credits...\n";
credits -= bid;
bid = 0;
if (credits == 0)
{
//cout << "\nLeft the game with no credits\nleft...\n\nPlayed " << roundsPlayed << " games.\n\nPress START to play again.";
break;
}
while(1)
{
//cout << "\nA: Continue game\nB: Retire\n";
if (Input.GetKeyDown("y"))
{
break;
}
if (Input.GetKeyDown("n"))
{
done = true;
//printf("\e[1;1H\e[2J");
//cout << "\nLeft the game with " << credits << "\ncredits.\n\nPlayed " << roundsPlayed << " games.\n\nPress 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 << "\n";
if (total == 7 || total == 11) //Player rolls a 7 or 11 - They win the game
{
//cout << "You won! Rolled a " << total << ".\n";
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 << ".\n";
return false;
}
else //otherwise we set a point and attempt to win the game.
{
//cout << "\nRolled a " << total << ", you must now roll\nuntil you get " << total << " again, or lose\nif you roll 7!\n\n";
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 << "\n";
if (total1 == 7)
{
//cout << "You lose!\n";
return false;
}
else if (total1 == point)
{
return true;
}
}
}
}
}
Answer by Pharan · Oct 04, 2015 at 12:22 AM
I think the main problem here is that this code is for something super low level. The places where there's while (1)
actually defines the "game loop" at a low level, which doesn't make sense running here since Unity is much higher level than a console program (which the original code describes). In a Unity C# script, this logic would just freeze because it's technically an infinite loop trying to live inside Unity's subsystems.
The solution here is to either use Unity coroutines or just define game logic in Update.
But before all that, it's kinda obvious that you're not too familiar with how Unity works yet, so my first recommendation is to go watch Unity's Scripting tutorials. They're pretty bite-sized so it shouldn't take too much time. If you already know how to program in C++, you could probably skip the first few lessons and start at lesson 8, which starts to cover Unity's scripting setup. (what MonoBehaviours do and the messages it receives, etc...)
Remember, when you're programming in Unity, you're not starting from scratch. Instead, you need to work on top of everything that's already been built. The good thing is that you don't need to build all those complicated things yourself. The downside is that you need to take a bit of time to learn what's there and how to build on it; that's less time and effort, but you still have to do it.
You can probably glean bits of logic and algorithms and things with a knowledge of C++, but you should also read up on some basics of C# syntax and standard naming so you can begin to understand C# code samples architecturally.
When you're mostly done with all that:
(1) Temporarily, you can replace the couts with Debug.Log. That's what behaves most like cout.
GUI Text is something that needs to be called every Update (more specifically, in OnGUI) so there are additional steps to get it to work. You could probably use a member string field and print that on screen in OnGUI. Then just modify that string where the couts are.
(2) I think Start needs to be void Start or IEnumerator Start. I'm not sure it would work as int Start.
I finally trashed this code entirely, and built from scratch. Used the new UI and everything is working great so far.
This was an early port and was nowhere near fixed for Unity. The formatting broke, and it was just completely something I shouldn't have tried to directly port in the first place. This is an example of noobishness that is quite embarrassing.
I have learned quite a few things in this simple project, ad the funny thing is I have written the bulk of this so far completely on my own from scratch.
I am unfamiliar with C++ and was only able to read this code and get a general understanding thanks to C# being similar.
So far, it looks like the only thing I will need his code for, is to get the checks for the die, besides the general idea of what is going on.
Thanks for the attempt to help, and thanks for the suggestions. I need to look more into the unity API and how to do things on the unity side, rather than directly focusing on the code alone.
Answer by hexagonius · Oct 03, 2015 at 04:12 PM
move the bracket from 64 to the end, right now the class ends there. and either you use Debug.Log for each cout, or you create a variable for each one which you use for a GUI.Label in OnGUI
I saw the error at 64 earlier and have not had a chance to inform about it. The code is still somehow broken even fixing this.
I am considering rewriting the code from scratch using the existing as a guideline unless someone can help me fix this before I finish.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Importing C++/CLI DLL causes errors 0 Answers
Cannot convert float to int when only using floats (C#) 3 Answers
Distribute terrain in zones 3 Answers
IUnityEventQueue - how do I use it? 2 Answers