Click here to Skip to main content
16,004,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
namespace FirstConsoleProject
{
	class MainClass
	{
		public static void Main(string[] args)
		{
			Boolean turn = true;	
			Random ran = new Random ();
			int stone = ran.Next(15,31);
			string result;
			Console.WriteLine (stone);
			while (stone > 1) {
				if (turn) {
					PlayerTurn (out stone);
				} 
				else {
					ComputerTurn(out stone);
				}
				SwitchTurn(turn);
			}
			if (stone==0)
				result = (turn) ? "Player win!" : "Compuer win";
			else result = (turn) ? "Computer win!" : "Player win";

			Console.ReadLine ();
		}

		public static void SwitchTurn(out Boolean nowTurn) 
		{
			nowTurn = !nowTurn;
		}

		public static void PlayerTurn(out int stoneLeft)
		{
			private int num;
			Console.WriteLine("Player Turn");
			do{
				Console.Write ("Input the number of stones you want to remove: ");
				num = Convert.ToInt32 (Console.ReadLine ());
				if (num > stoneLeft)
					Console.WriteLine("Invalid input. Please re-enter.");
				else
					stoneLeft -= num;
			}
			while (num > stoneLeft); 
			Console.WriteLine ("Stones left = " + stoneLeft + "\n");
		}

		public static void ComputerTurn(out int stoneLeft)
		{
			private	int num;
			Console.WriteLine ("ComputerTurn");
			Random ran = new Random ();
			do{
				Console.Write ("Input the number of stones you want to remove: ");
				num = ran.Next(1,4);
				if (num > stoneLeft)
					Console.WriteLine("Invalid input. Please re-enter.");
				else
					stoneLeft -= num;
			}
			while (num > stoneLeft); 
			Console.WriteLine ("Stones left = " + stoneLeft + "\n");

		}
	}
}


This is my code and I keep receiving these error below in both "PlayerTurn" and "ComputerTurn" Method. Can someone help me figure out what's wrong. I am very new to C#. Thank you!
"} expected"
"invalid token '>'"
"invalid token '='"
"type or namespace definition, or end of file expected"

What I have tried:

just can't find any excess or missing open/close blanket plz help...i know it's a little bit long though
Posted
Updated 25-Mar-16 6:35am

Um - it's an odd one, but it's not the brackets that cause the error - it's that you are declaring private variables inside a function body, and the syustem is trying to find a polite way to tell you that's wrong!
Change these:
C#
public static void PlayerTurn(out int stoneLeft)
{
    private int num;
    ...
}

public static void ComputerTurn(out int stoneLeft)
{
    private int num;
    ...
}

To these:
C#
public static void PlayerTurn(out int stoneLeft)
{
    int num;
    ...
}

public static void ComputerTurn(out int stoneLeft)
{
    int num;
    ...
}
And those problems should go away.
And I'd strongly recommend against using 1TB while you are starting out - it makes it very difficult to see where code blocks start or end. Initially get used to K&R:
C#
public void MyMethod()
{
    statments;
    if (condition)
    {
        statements;
    }
    else
    {
        statements;
    }
}
Or better (in my opinion) Whitesmiths:
C#
public void MyMethod()
    {
    statments;
    if (condition)
        {
        statements;
        }
    else
        {
        statements;
        }
    }

They make it a lot easier to see what is going wrong!
 
Share this answer
 
Comments
Member 12416498 25-Mar-16 12:38pm    
thx bro it works!
And thank you for your suggestion as well, it indeed seems better. I will try.
Appreciate it.
OriginalGriff 25-Mar-16 12:54pm    
You're welcome!
Richard MacCutchan 25-Mar-16 12:46pm    
I just found that, but it was a bu***r to see it.
There's a lot of stuff wrong with this. Let's go through it step by step..

1) Both in PlayerTurn(..) and ComputerTurn(..) remove the private modifier from
C#
private int num;

Access modifiers make no sense for local identifiers of a method, they're not visible outside the method anyway.

2) In SwitchTurn(..) you modify the variable nowTurn based on its current value. So you don't just want to pass it out of the method, you also want to pass the current value into the method. So you need a ref modifer instead of the out modifier:
C#
public static void SwitchTurn(ref Boolean nowTurn)

And the call needs to be changed to:
C#
SwitchTurn(ref turn);


3) Exactly the same goes for the stoneLeft parameter of PlayerTurn(..) and ComputerTurn(..):
C#
public static void PlayerTurn(ref int stoneLeft)
// and
public static void ComputerTurn(ref int stoneLeft)

And the same for the calls:
C#
PlayerTurn(ref stone);
// and
ComputerTurn(ref stone);


Now the program at least compiles. I have no idea if it will do what you want and, honestly, I don't want to analyze this. Because there's another problem - not a syntactic problem but a style problem: All those ref-modifiers make it really a headache to follow the logic of your program. You should avoid out and ref unless you're very certain that they make sense. Sometimes they are useful and make sense, in this case, definitely not.

I would suggest you use a non-static class for your game logic. Create an instance of it in Main(..) and call a method on it to start the game. Almost all your variables would be class-instance-variables then and you wouldn't need those ref-modifiers.
 
Share this answer
 
v2

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