5 Replies - 415 Views - Last Post: 07 March 2012 - 07:37 PM Rate Topic: -----

#1 Xna4life  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 21-February 12

scoring goes crazzzy

Posted 07 March 2012 - 01:57 PM

Hi Guys...im creating a 2D scrabble board game and at the minute have a problem with the scoring.
Below are my two classes 'clickable letter' and 'level' which are the only places my 'point value' int is used, and its this that totals the score
ATM when the player hits check score the score can go from 0 to 42 to 135 to 213, crazy numbers and there doesnt seem to too much of a pattern each time i try it.
This happens even with no letters on the board so clearly something is wrong. Please help...thanks in advance

look for the countScore method in my level class, for the sake of sorting the problem I have set all the point values of each letter to '1' inside the level constructor

namespace Scrabble 
{ 
    class ClickableLetter:ClickableGameplayObject 
    { 
        //Point value  
        public int PointValue; 
 
        Mouse mouse; 
 
        //Reference XNA framework 
        Game gameReference; 
 
        public int x; 
        public int y; 
 
        MouseState previousState, currentState; 
 
        public ContentManager Content 
        { 
            get { return content; } 
        } 
        ContentManager content; 
 
        Random random; 
        public Texture2D ClickedTexture 
        { 
            get; 
            set; 
        } 
 
        Texture2D saved; 
 
        public ClickableLetter(Game theGame, int points, int x, int y, string textureSource) 
            : base() 
        { 
            Alpha = 0.5f; 
            random = new Random((int)DateTime.Now.TimeOfDay.TotalMilliseconds); 
            content = theGame.Content; 
            Texture = Content.Load<Texture2D>(textureSource); 
            this.gameReference = theGame; 
            PointValue = points; 
            Position = new Vector2(x, y); 
        } 
 
        public override void OnHover() 
        { 
            base.OnHover(); 
 
            //Alpha = 1.0f; 
 
            // Rotation += 0.01f; 
        } 
 
        public override void OnLeave() 
        { 
            base.OnLeave(); 
 
            Alpha = 0.5f; 
        } 
 
        public override void OnLeftClick() 
        { 
            base.OnLeftClick(); 
 
            Position = new Microsoft.Xna.Framework.Vector2(ActiveMouse.Position.X, ActiveMouse.Position.Y); 
        } 
 
        public override void OnHeldLeftClick() 
        { 
            base.OnHeldLeftClick(); 
 
            Position = new Microsoft.Xna.Framework.Vector2(ActiveMouse.Position.X, ActiveMouse.Position.Y); 
        } 
 
        protected override void UpdateGameplayObject(Microsoft.Xna.Framework.GameTime gameTime) 
        { 
 
        } 
 
        public void Update(GameTime gameTime, SpriteBatch spriteBatch) 
        { 
        } 
 
        public override void OnLeftRelease() 
        { 
            centerLetterOnTile(); 
        } 
 
        //this is used to center the letter when placed on board 
        private void centerLetterOnTile() 
        { 
            Level.x = Level.currentState.X; 
            Level.y = Level.currentState.Y; 
 
            if (Level.x >= 300 && Level.x <= 1050) 
            { 
                //find relative pos of tile (3,3) on overall screen, that will give pos on board 
                Level.x -= 300; 
                Level.x /= 50; 
                Level.y -= 1; 
                Level.y /= 50; 
                // / 50 is to know how many times you have to move - relative to tiles 
 
                //variable will now be stored as (0,1)(1,1) etc... so *50 to get tile pos, and + 25 to center 
                Level.x = (Level.x * 50) + 25; 
                Level.y = (Level.y * 50) + 25; 
 
                //find exact pos on board - on screen 
                Level.x += 300; 
                Level.y += 1; 
 
                Vector2 temp = new Vector2(Level.x, Level.y); 
                Position = temp; 
 
                x = Level.x; 
                y = Level.y; 
            } 
            else 
            { 
                x = Level.currentState.X; 
                y = Level.currentState.Y; 
            } 
        } 
    } 
} 



namespace Scrabble 
{ 
    class Level 
    { 
        [DllImport("user32.dll", CharSet = CharSet.Auto)] 
        public static extern uint MessageBox(IntPtr hWndle, String text, String caption, int buttons); 
        
        Game gameReference; 
 
        //score 
        int player1Score, player2Score; 
 
        //textures 
        private Texture2D backgroundImage; 
        private Texture2D p1on, p1off, p2on, p2off; 
 
        //array for tiles displayed. 
        public static Tile[,] tiles; 
 
        //Add Pause 
        private Pause pause; 
 
        //Bools for pausing, resuming gameplay and drawing moving blocks  
        public bool paused = false; 
        public bool notPaused = true; 
        public static bool player1Turn = true; 
        public static bool player2Turn; 
        public static bool drawP1 = true; 
        public static bool drawP2 = false; 
        public bool cursorInsideScorebtn = false; 
        public bool press1; 
        public bool press2; 
 
 
        // The layer which entities are drawn on top of. 
        private const int EntityLayer = 2; 
 
        // Level game state & Camera for scroll screen 
        private Random random = new Random(354668); 
        public float cameraPosition; 
        public float cameraPositionY; 
 
        //Sound Effect for exit reached & Bonus collected. 
        private SoundEffect exitReachedSound; 
        private SoundEffect CollectBonus; 
 
        //Return Score 
        public int Score 
        { 
            get { return score; } 
        } 
        int score; 
        #region 
        //start of file from other game 
        public static int x; 
        public static int y; 
        public bool ScoreButtonPressed; 
 
        SpriteFont hudFont; 
 
        //clickable letter on screen 
        public static ClickableLetter letterA;  
        public static ClickableLetter letterB; 
        public static ClickableLetter letterC; 
        public static ClickableLetter letterD; 
        public static ClickableLetter letterE; 
        public static ClickableLetter letterF; 
        public static ClickableLetter letterT; 
        public static ClickableLetter letterH; 
        public static ClickableLetter letterI; 
        public static ClickableLetter letterJ; 
        public static ClickableLetter letterK; 
        public static ClickableLetter letterL; 
        public static ClickableLetter letterM; 
        public static ClickableLetter letterN; 
 
        //static mouse 
        public static Mouse mouse; 
 
       //ClickablePlayer 
        List<ClickableLetter> clickableObjects; 
        public static MouseState mouseState; 
 
        /// <summary> 
        /// The X, Y screen coordinates of the mouse 
        /// </summary> 
        public Vector2 Position//used for hud 
        { 
            get; 
            protected set; 
        } 
 
 
        public Vector2 Positionofc2//used for hud 
        { 
            get; 
            protected set; 
        } 
 
 
        //pos of tile on board 
        public Rectangle checkScoreBTN 
        { 
            get; 
            set; 
        } 
 
        public bool DIOC; 
 
        //Textures for images 
        private Texture2D TileA1; 
        private Texture2D checkScoreB; 
        private Texture2D checkScoreW; 
        private Texture2D shelf; 
        #endregion 
 
        //Call Content         
        public ContentManager Content 
        { 
            get { return content; } 
        } 
        ContentManager content; 
        #region Loading 
        /// <summary> 
        /// Constructs a new level. 
        /// The service provider that will be used to construct a ContentManager. 
        /// </summary><param name="serviceProvider"><param name="path"> 
        /// The absolute path to the level file to be loaded. 
        /// </param> 
        public Level(Game theGame, IServiceProvider serviceProvider, Stream fileStream, int levelIndex) 
   
        { 
            whosTurnIsIt(); 
 
            // Create a new content manager to load content used just by this level. 
            content = new ContentManager(serviceProvider, "Content"); 
 
            //Call Load tiles method 
            LoadTiles(fileStream); 
 
            backgroundImage = Content.Load<Texture2D>("Images/backgroundImage"); 
            p1on = Content.Load<Texture2D>("Player/p1on"); 
            p1off = Content.Load<Texture2D>("Player/p1off"); 
            p2on = Content.Load<Texture2D>("Player/p2on"); 
            p2off = Content.Load<Texture2D>("Player/p2off"); 
 
            LoadPause(); 
 
            content = theGame.Content; 
            this.gameReference = theGame; 
 
            
            letterA = new ClickableLetter(theGame, 1, 148, 475, "Letters/a"); 
            letterB = new ClickableLetter(theGame, 1, 1125, 475, "Letters/b"); 
            letterC = new ClickableLetter(theGame, 1, 233, 475, "Letters/c"); 
            letterD = new ClickableLetter(theGame, 1, 1240, 475, "Letters/d"); 
            letterE = new ClickableLetter(theGame, 1, 1065, 475, "Letters/e"); 
            letterF = new ClickableLetter(theGame, 1, 61, 475, "Letters/t"); 
            letterT = new ClickableLetter(theGame, 1, 61, 475, "Letters/t"); 
            letterH = new ClickableLetter(theGame, 1, 148, 475, "Letters/a"); 
            letterI = new ClickableLetter(theGame, 1, 1125, 475, "Letters/b"); 
            letterJ = new ClickableLetter(theGame, 1, 233, 475, "Letters/c"); 
            letterK = new ClickableLetter(theGame, 1, 1240, 475, "Letters/d"); 
            letterL = new ClickableLetter(theGame, 1, 1065, 475, "Letters/e"); 
            letterM = new ClickableLetter(theGame, 1, 61, 475, "Letters/t"); 
            letterN = new ClickableLetter(theGame, 1, 61, 475, "Letters/t"); 
    
            LoadContent(); 
         
 
 
        }//End of Level Constructor 
 
 
        /// <summary> 
        /// Will load the pause class 
        /// </summary> 
        private void LoadPause() 
        { 
            pause = new Pause(this); 
 
        }//End of LoadPause 
 
        //bool to test collisin + colour 
        public static MouseState previousState, currentState; 
 
 
        private void LoadContent() 
        { 
            //load mouse 
            mouse = new Mouse(content); 
 
            shelf = Content.Load<Texture2D>("Backgrounds/shelf"); 
            checkScoreB = Content.Load<Texture2D>("Old/CheckScoreB"); 
            checkScoreW = Content.Load<Texture2D>("Old/CheckScoreW"); 
            hudFont = content.Load<SpriteFont>("Old/Hud"); 
 
            //list for letters 
            clickableObjects = new List<ClickableLetter>(14); 
 
            //load letter as clickable object 
            clickableObjects.Add(letterA); 
            clickableObjects.Add(letterB); 
            clickableObjects.Add(letterC); 
            clickableObjects.Add(letterD); 
            clickableObjects.Add(letterE); 
            clickableObjects.Add(letterF); 
            clickableObjects.Add(letterT); 
            clickableObjects.Add(letterH); 
            clickableObjects.Add(letterI); 
            clickableObjects.Add(letterJ); 
            clickableObjects.Add(letterK); 
            clickableObjects.Add(letterL); 
            clickableObjects.Add(letterM); 
            clickableObjects.Add(letterN); 
        } 
 
 
        private void LoadTiles(Stream fileStream) 
        { 
            // Load the level and ensure all of the lines are the same length. 
            int width; 
            List<string> lines = new List<string>(); 
            using (StreamReader reader = new StreamReader(fileStream)) 
            { 
                string line = reader.ReadLine(); 
                width = line.Length; 
                while (line != null) 
                { 
                    lines.Add(line); 
                    if (line.Length != width) 
                        throw new Exception(String.Format("The length of line {0} is different from all preceeding lines.", lines.Count)); 
                    line = reader.ReadLine(); 
                } 
            } 
 
            // Allocate the tile grid. 
            //array used for tiles 
            tiles = new Tile[width, lines.Count]; 
 
            // Loop over every tile position, 
            for (int y = 0; y < Height; ++y) 
            { 
                for (int x = 0; x < Width; ++x) 
                { 
                    // To load each tile. 
                    char tileType = lines[y][x]; 
                    tiles[x, y] = LoadTile(tileType, x, y); 
                } 
            } 
 
        }//End of LoadTiles 
 
        private Tile LoadTile(char tileType, int x, int y) 
        { 
            switch (tileType) 
            { 
                //Blank space 
                case '.': 
                    return new Tile(null, TileCollision.Passable,0); 
 
 
                //Floating platform 
               // case '-': 
                 //   return LoadTile("Platform", TileCollision.Platform); 
 
                case '1': 
                    return LoadTile("1Centertile", TileCollision.Platform,1); 
                case '2': 
                    return LoadTile("2normal", TileCollision.Platform,2); 
                case '3': 
                    return LoadTile("3DoubleLetterScore", TileCollision.Platform,3); 
                case '4': 
                    return LoadTile("4TripleLetterScore", TileCollision.Platform,4); 
                case '5': 
                    return LoadTile("5DoubleWordScore", TileCollision.Platform,5); 
                case '6': 
                    return LoadTile("6TripleWordScore", TileCollision.Platform,6); 
 
                case '7': 
                    return LoadTile("2", TileCollision.Platform,7); 
               
                   default: 
                    return new Tile(null, TileCollision.Passable, 0 ); 
 
                    throw new NotSupportedException(String.Format("Unsupported tile type character '{0}' at position {1}, {2}.", tileType, x, y)); 
            }//End of Switch 
 
        }//End of Tile-LoadTile 
 
 
       
        private Tile LoadTile(string name, TileCollision collision, int type) 
        { 
            return new Tile(Content.Load<Texture2D>("Tiles/" + name), collision, type); 
 
        }//End of Tile -LoadTile 
 
        /// <summary> 
        /// Unloads the level content. 
        /// </summary> 
        public void Dispose() 
        { 
            Content.Unload(); 
 
        }//End of Dispose 
        #endregion 
        #region Bounds and collision 
 
        /// <summary> 
        /// Gets the collision mode of the tile at a particular location. 
        /// This method handles tiles outside of the levels boundries by making it 
        /// impossible to escape past the left or right edges, but allowing things 
        /// to jump beyond the top of the level and fall off the bottom. 
        /// </summary> 
        public TileCollision GetCollision(int x, int y) 
        { 
            // Prevent escaping past the level ends. 
            if (x < 0 || x >= Width) 
                return TileCollision.Impassable; 
            //Allow jumping past the level top and falling through the bottom. 
            if (y < 0 || y >= Height) 
                return TileCollision.Passable; 
            return tiles[x, y].Collision; 
 
        }//End of TileCollision 
 
 
        /// <summary> 
        /// Gets the bounding rectangle of a tile in world space. 
        /// </summary>         
        public Rectangle GetBounds(int x, int y) 
        { 
            return new Rectangle(x * Tile.Width, y * Tile.Height, Tile.Width, Tile.Height); 
 
        }//End of GetBounds 
 
 
        /// <summary> 
        /// Width of level measured in tiles. 
        /// </summary> 
        public int Width 
        { 
            get { return tiles.GetLength(0); } 
 
        }//End of Width 
 
 
        /// <summary> 
        /// Height of the level measured in tiles. 
        /// </summary> 
        public int Height 
        { 
            get { return tiles.GetLength(1); } 
 
        }//End of Height 
 
        //MY CODE 
        #endregion 
        #region Update 
 
        /// <summary> 
        /// Updates all objects in the world, performs collision between them, 
        /// and handles the time limit with scoring. 
        /// </summary> 
        public void Update(GameTime gameTime, SpriteBatch spriteBatch) 
        { 
           // onLettersPlaced(); 
 
 
            //Allow keyboard input - used for pausing game 
            KeyboardState keyboardState = Keyboard.GetState(); 
 
            //If 'P' is pressed - Pause game 
            if (keyboardState.IsKeyDown(Keys.P)) 
            { 
                notPaused = false; 
                paused = true; 
 
            }//End of if 
 
            //If 'R' is pressed - Resume game 
            if (keyboardState.IsKeyDown(Keys.R)) 
            { 
                notPaused = true; 
                paused = false; 
 
            }//End of if 
 
 
            //If game is not paused 
            if (notPaused) 
            { 
                //methods to allow letters to be dragged and dropped 
                PerformMouseInteractions(gameTime); 
                PerformNormalUpdate(gameTime); 
 
 
                currentState = Microsoft.Xna.Framework.Input.Mouse.GetState();//mouse 
                mouse.Update(); 
                 
                //check score 
                checkScoreButton();          
 
            } 
             pause.Update(gameTime); 
 
        } 
 
 
                //method from mouse class to allow letter to be dragged and dropped 
        public void PerformNormalUpdate(GameTime gameTime) 
        { 
            foreach (ClickableGameplayObject cgo in clickableObjects) 
            { 
                if (cgo != mouse.ClickedObject) 
                { 
                    cgo.Update(gameTime); 
                } 
            } 
        } 
 
        //method from mouse class to allow letter to be dragged and dropped 
        public void PerformMouseInteractions(GameTime gameTime) 
        { 
            foreach (ClickableGameplayObject cgo in clickableObjects) 
            { 
                if (mouse.ClickedObject == null) 
                {   
                    cgo.Update(gameTime, mouse); 
                    if (cgo.ActiveMouse != null) 
                    { 
                        return; 
                    } 
                } 
                else if (mouse.ClickedObject != null) 
                { 
                    mouse.ClickedObject.Update(gameTime, mouse); 
 
                    return; 
                } 
            } 
        } 
 
         
        //check score button 
                public void checkScoreButton() 
                { 
                    //rect for 'check score' button 
                        checkScoreBTN = new Rectangle(87, 605, 163, 59); 
 
                        x = currentState.X; 
                        y = currentState.Y; 
                        mouseState = Microsoft.Xna.Framework.Input.Mouse.GetState();//mouse 
 
                    //if 'check score' button contains cursor return true                        
                            if (checkScoreBTN.Contains(x, y)) 
                            { 
                                cursorInsideScorebtn = true; 
                            } 
 
                            //if 'check score' button does NOT contain cursor return false   
                            if (!checkScoreBTN.Contains(x, y)) 
                            { 
                                cursorInsideScorebtn = false; 
                            } 
 
                             
                    //if it is player 1's turn and mouse is inside 'check score button' 
                            if (player1Turn & cursorInsideScorebtn) 
                            { 
                                //if mouse is pressed- run scount score 
                                if (mouseState.LeftButton == ButtonState.Pressed) 
                                { 
                                    //show light when button is pressed 
                                    ScoreButtonPressed = true; 
                                    countScore(); 
                                    //lights for players turn 
                                    press2 = false; 
                                    press1 = true; 
                                } 
 
                                else if (mouseState.LeftButton == ButtonState.Released) 
                                    { 
                                    //dont show light when button is released 
                                        ScoreButtonPressed = false; 
 
                                        if (press1) 
                                        { 
                                            //lights to show user whos turn it is 
                                            player1Turn = false; 
                                            player2Turn = true; 
                                        } 
 
 
                                    } 
                                } 
 
 
                        //if it is player 2's turn and mouse is inside 'check score button' 
                            else if (player2Turn & cursorInsideScorebtn) 
                            { 
                                //if mouse is pressed- run scount score 
                                if (mouseState.LeftButton == ButtonState.Pressed) 
                                {//show light when button is pressed 
                                    ScoreButtonPressed = true; 
 
                                    countScore(); 
                                    //lights to show user whos turn it is 
                                    press1 = false; 
                                    press2 = true; 
                 
                                } 
 
                            
                                if (mouseState.LeftButton == ButtonState.Released) 
                                {//dont show light when button is released 
                                    ScoreButtonPressed = false; 
 
                                    if (press2) 
                                    {//lights to show user whos turn it is 
                                        player1Turn = true; 
                                        player2Turn = false; 
                                    } 
 
                                } 
                            } 
                              
                         
                     
                } 
 
                public void countScore() 
                { 
                    int scoreThisTurn = 0; 
                    if (player1Turn) 
                    {//player 1's seven letters he can choose from 
                        for (int i = 0; i < 7; ++i) 
                        { 
                            //scoreThisTurn += addScore(clickableObjects[i]);                       
                            scoreThisTurn += 1;//just for testing 
                        } 
                        player1Score += scoreThisTurn; 
 
                    } 
                    else 
                    {//player 2's seven letter theycan choose from 
                        for (int i = 7; i < 14; ++i) 
                        { 
                            scoreThisTurn += addScore(clickableObjects[i]);//add parameter to addScore method 
                        } 
                    } 
                } 
 
        //this is used if letter is placed on bonus tiles 
               private int addScore(ClickableLetter letter) 
                { 
                    //inside board, otherwise return 0 points 
                    if (letter.x >= 300 && letter.x <= 1050) 
                    { 
                        //temp pos, then get (1,1) on board 
                        int tempX = letter.x; 
                        int tempY = letter.y; 
                        tempX -= 300; 
                        tempX /= 50; 
                        tempY -= 1; 
                        tempY /= 50; 
                         //actual tile on board/now check for bonus, Type is int in Tile, depending on bonus 
                        switch (tiles[tempX, tempY].Type) 
                        { 
                            case 3: 
                              //  return 2 * letter.PointValue;//case 3 =double 
                            case 4: 
                              //  return 3 * letter.PointValue;//case4 = triple 
                            default: 
                                return letter.PointValue;//no bonus so return just letter points 
                        } 
                         
                    } 
 
                    return 0; 
                } 
 
                public static void whosTurnIsIt() 
                { 
                    if (player1Turn) 
                    {//lights to draw, depending on whos go it is 
                        drawP1 = true; 
                        drawP2 = false; 
                    } 
 
                    else if (player2Turn) 
                    {//lights to show whose turn it is 
                        drawP1 = false; 
                        drawP2 = true; 
                    } 
 
                } 
 
 
        //draw all to screen 
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch) 
        { 
 
            spriteBatch.End(); 
 
            ScrollCamera(spriteBatch.GraphicsDevice.Viewport); 
 
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend); 
 
 
            spriteBatch.Draw(backgroundImage, new Vector2(0, 0), null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f); 
 
            spriteBatch.Draw(shelf, new Vector2(5, 496), null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f); 
            spriteBatch.Draw(shelf, new Vector2(1062, 496), null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f); 
 
            if (ClickableGameplayObject.OnLeaveTest) 
            { 
                spriteBatch.Draw(shelf, new Vector2(651, 300), null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f); 
            } 
            if (ClickableGameplayObject.OnLeaveTestRect) 
            { 
                spriteBatch.Draw(shelf, new Vector2(651, 320), null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f); 
            } 
 
            //rect to test from clickable object 
            spriteBatch.Draw(shelf, new Vector2(651, 372), null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f); 
            spriteBatch.Draw(shelf, new Vector2(651, 422), null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f); 
 
           if (drawP1) 
           { 
 
                spriteBatch.Draw(p1on, new Vector2(20, 100), null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f); 
                spriteBatch.Draw(p2off, new Vector2(1060, 100), null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f); 
            } 
 
            if (drawP2) 
            { 
 
                spriteBatch.Draw(p1off, new Vector2(20, 100), null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f); 
                spriteBatch.Draw(p2on, new Vector2(1060, 100), null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f); 
            } 
 
 
 
            //Method to Draw Tiles 
            DrawTiles(spriteBatch); 
 
            letterA.Draw(gameTime, spriteBatch); 
            //letterA.Draw( 
            letterB.Draw(gameTime, spriteBatch); 
            letterC.Draw(gameTime, spriteBatch); 
            letterD.Draw(gameTime, spriteBatch); 
            letterE.Draw(gameTime, spriteBatch); 
            letterT.Draw(gameTime, spriteBatch); 
 
 
            spriteBatch.Draw(checkScoreB, new Vector2(80, 600), null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f); 
 
            if (ScoreButtonPressed) 
            { 
               // if (drawDiffScoreBTN) 
               // { 
                    spriteBatch.Draw(checkScoreW, new Vector2(80, 600), null, Color.White, 0, Vector2.Zero, 1.0f, 
                                     SpriteEffects.None, 1.0f); 
               // } 
            } 
 
 
 
            Vector2 hudLocation2 = new Vector2(180, 190); 
 
 
 
            //score ahs to be a string 
            string printStringP1; 
            string printStringP2; 
 
            printStringP1 = "Player 1: " + player1Score.ToString(); 
            printStringP2 = "Player 2: " + player2Score.ToString(); 
 
            spriteBatch.DrawString(hudFont, printStringP1, new Vector2(10.0f, 10.0f), 
                               Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 0); 
 
            spriteBatch.DrawString(hudFont, printStringP2, new Vector2(10.0f, 40.0f), 
                                   Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 0); 
 
 
 
            spriteBatch.End(); 
 
 
            mouse.Draw(spriteBatch); 
 
 
 
            if (paused) 
            { 
                pause.Draw(gameTime, spriteBatch, this); 
 
            } 
 
 
        }//End of if 
 
        /// <summary> 
        /// Method for Scroll Screen - Camera 
        /// </summary> 
        /// <param name="viewport"></param> 
        public void ScrollCamera(Viewport viewport) 
        { 
            //Float for margin 
            const float ViewMargin = 0.35f; 
 
            // Calculate the edges of the screen. 
            float marginWidth = viewport.Width * ViewMargin; 
            float marginLeft = cameraPosition + marginWidth; 
            float marginRight = cameraPosition + viewport.Width - marginWidth; 
 
            // Calculate the edges of the screen. 
            float marginHeight = viewport.Height * ViewMargin; 
 
 
            // Update the camera position, but prevent scrolling off the ends of the level. 
            float maxCameraPositionX = Tile.Width * Width - viewport.Width; 
            float maxCameraPositionY = Tile.Height * Height - viewport.Height; 
 
 
        }//End of Scroll Camera 
 
 
        /// <summary> 
        /// Draws each tile in the level. 
        /// </summary> 
        public void DrawTiles(SpriteBatch spriteBatch) 
        { 
            // Calculate the visible range of tiles. 
            int left = (int)Math.Floor(cameraPosition / Tile.Width); 
            int right = left + spriteBatch.GraphicsDevice.Viewport.Width / Tile.Width; 
            right = Math.Min(right, Width - 1); 
 
            // Calculate the visible range of tiles. 
            int top = (int)Math.Floor(cameraPositionY / Tile.Height); 
            int bottom = top + spriteBatch.GraphicsDevice.Viewport.Height / Tile.Height; 
            bottom = Math.Min(bottom, Height); 
 
            // For each tile position 
            for (int y = top; y <= bottom; ++y) 
            { 
                for (int x = left; x <= right; ++x) 
                { 
                    // If there is a visible tile in that position 
                    Texture2D texture = tiles[x, y].Texture; 
                    if (texture != null) 
                    { 
                        // Draw it in screen space. 
                        Vector2 position = new Vector2(x, y) * Tile.Size; 
                        spriteBatch.Draw(texture, position, Color.White); 
 
                    }//End of if 
 
                }//End of for 
 
            }//End of for 
 
        }//End of DrawTiles 
        #endregion 
    }//End of Level class 
 
}//End 
 



Is This A Good Question/Topic? 0
  • +

Replies To: scoring goes crazzzy

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: scoring goes crazzzy

Posted 07 March 2012 - 02:04 PM

If you have to provide nearly 1000 lines of code, then your first step is to learn how to debug. We're not going to do all your debugging for you when you haven't even narrowed it down to a single method or class.

rookie What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute. Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


See FAQ # x. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2


FAQ (Frequently Asked Questions - Updated Feb 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 995
  • View blog
  • Posts: 2,385
  • Joined: 04-October 09

Re: scoring goes crazzzy

Posted 07 March 2012 - 02:11 PM

You call checkScoreButton() on every game update. How long do you think it takes the system to run one update and call your method? What happens when you hold the mouse button down over the button?

I believe you are running into a timing issue here, where the system is running much faster than you think, thus it does multiple score updates in the time it takes you to click the button.
Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: scoring goes crazzzy

Posted 07 March 2012 - 02:23 PM

I have my doubts about the archetecture of your game.
To start with you have a single class of nearly 800 lines. That's not a good sign for a game like this.

Scores shouldn't be all that tough.
You have a player class. It has a .Score property.
You have two player instances. When they earn points, add them to that player-instance's .Score property.
When you want to know their score, retreive that value.

If you've made it any more complicated than that... Why?

The fact that you have this makes me think you don't even have a Player class in your game.
011 int player1Score, player2Score;

It sure looks like you have the main logic trying to do everything and micromanage the works. That's part of what's making it hard for you to manage your own game.

If you make a player class, then you can do things like

class Player() 
{
    string Playername;
    List<tiles> Rack;
    int Score;
     void DrawTiles()
    {
          // Add more tiles until rack is full
    }

     void ExchangeTiles()
    {
    }

     void PlayTiles()
    {
    }

     void FindBestWordToPlay()
     {
     }
}


Until you start objectifying your code you're always going to have a more difficult task managing it, adding features, finding problems and so on.

Then you can make just an "Engine" class that takes events from the players and return results like the score for that word. The engine would do nothing about GUI or handling your cameras etc.

Then you make a "ViewManager" class that just handles the cameras and so on, and doesn't give a rats's about the actual game play: That's the GameEngine's problem.
Was This Post Helpful? 0
  • +
  • -

#5 Xna4life  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 21-February 12

Re: scoring goes crazzzy

Posted 07 March 2012 - 04:14 PM

Hi Momerath...Thanks for taking the time to reply....the check score button was running fine before the scoring was added. If i'm not running it from the update how can you advise how I can get it to run only when button is clicked...?

anyway....

Iv ran the break point with countScore() and it only gets called everytime the user clicks the 'check score' button in the game, however it times 7 each time its clicked, so 7, 14, 21, 28, 35...(but without the breakpoint it jumps straight to 49. - 7 tiles each by 7points each is 49) and then continue to jump on the next turn....I cant see why it is calling it so many times.)this is only for player one, and this is without any letters actually being placed on the board.

I dont need the addscore() method yet as its only for bonus tiles so iv taken it out. Sorry about the large amount of code Momerath but I posted this before I left work and didn't have time to propelry edit it, was hoping for a few replies to look at when I got home....anyway thanks for the help so far.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: scoring goes crazzzy

Posted 07 March 2012 - 07:37 PM

Aren't there 7 tiles in a rack? Is is possible you've inadvertently added that value into the score somewhere?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1