-3

I am getting an error with my code.. I am new to C# and I am trying to make a two player turn based game. Doing this using visual studio

The error is "Warning 2 The variable 'gs' is assigned but its value is never used"

The code is below;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;

namespace GridWorld
{
public class newAI : BasePlayer
{
    PlayerWorldState myWorldState;

    private double maxSpeed = 5f;
    //public int MaxNumTurn = 15;
    private int Steps;
    private object count;


    public newAI ()
        : base()
    {
        this.Name = "AI FOR GAMES THE A TEAM";
        // this.MaxNumTurn = 15;
        var gs = new GridSquare.ContentType(); // the variable gs is now a new GridSquare content type

    }


    void Pathfind()
    {

        PlayerWorldState startingPoint = BasePlayer(GridSquare.ContentType.Snail);
        int myX = 0;
        int myY = 0;

        if (myX == -1 || myY == -1)     // if myX & myY are equal to minus 1 then increment the steps
            Steps++;
        {
            return;

        }

        gs [myX, myY].Steps = 0;     // outside whileloop 

        while (true)
        {
            bool madeProgress = false;

            foreach (startingPoint mainPoint in gs)
            {
                int x = mainPoint.X;
                int y = mainPoint.Y;

                if (SquareOpen(x, y))
                {
                    int passHere = GridSquare[x, y].Steps;

                    foreach (startingPoint movePoint in ValidMoves(x, y))
                    {
                        int myX = movePoint.X;
                        int myY = movePoint.Y;
                        int newPass = passHere + 1;

                        if (GridSquare[myX, myY].Steps > newPass)
                        {
                            GridSquare[myX, myY].Steps = newPass;
                            madeProgress = true;
                        }
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
sg82013
  • 31
  • 4
  • 1
    Firstly it is not an error but a waring. And it is really clear from your code. you declaring and initializing the value of a variable gs which not being used further which means to compiler a waste of memory for no reason. – usamazf Mar 14 '16 at 11:06
  • dont worry, its just a warning not error.. and declare `gs` globally to use it. – Shaminder Singh Mar 14 '16 at 11:08
  • You're looking in the wrong place. This line should give an error: `gs [myX, myY].Steps = 0; ` (gs is not defined) – Dennis_E Mar 14 '16 at 11:08
  • @UsamaZafar I am getting this error also on this line of code gs [myX, myY].Steps = 0; // outside whileloop – sg82013 Mar 14 '16 at 11:08
  • @Dennis_E yes that is where I am getting the error and this question is related to visual studio and AI – sg82013 Mar 14 '16 at 11:09
  • The error message is self-explanatory. Consult the documentation if you need more help. – Cody Gray - on strike Mar 14 '16 at 11:09
  • @sg82013 The gs in the constructor is a different thing from the gs in the method. The first one only generates a warning. And no, this question is not related to visual studio or ai. Your program may be, but the question is not. – Dennis_E Mar 14 '16 at 11:11

2 Answers2

0

It is not really an error. As it says, its just a warning stating that the variable 'gs' is assigned somewhere but never used anywhere. This should not cause your application to stop working.

0

You're declaring and defining gs in your constructor newAI and attempting to use it in Pathfind. Pathfind can't see it, because it's local to your class constructor i.e. a local scoped variable. Move it into your class like your maxSpeed, Steps and count variables.

It is only a warning, but your code is going to have undefined behaviour at the very least.

Jonathon Ogden
  • 1,562
  • 13
  • 19
  • thank you for your help.. I have done this now I am getting the error with the 'var' which says: Error 1 The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?) – sg82013 Mar 14 '16 at 11:12
  • @sg82013 Yes, it will and that is normal. I recommend doing some research into the C# language. Here is the last hint I will give you that puts you in the right place to resolve this issue: [Implicitly Typed Local Variables](https://msdn.microsoft.com/en-us/library/bb384061.aspx). Read the **Remarks** section to see where `var` can and cannot be used and look over my answer again. Also, research what a _type_ and _namespace_ are too as it seems you aren't overly familiar with those concepts. – Jonathon Ogden Mar 14 '16 at 11:16