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
{
public int PointValue;
Mouse mouse;
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();
}
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();
}
private void centerLetterOnTile()
{
Level.x = Level.currentState.X;
Level.y = Level.currentState.Y;
if (Level.x >= 300 && Level.x <= 1050)
{
Level.x -= 300;
Level.x /= 50;
Level.y -= 1;
Level.y /= 50;
Level.x = (Level.x * 50) + 25;
Level.y = (Level.y * 50) + 25;
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;
int player1Score, player2Score;
private Texture2D backgroundImage;
private Texture2D p1on, p1off, p2on, p2off;
public static Tile[,] tiles;
private Pause pause;
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;
private const int EntityLayer = 2;
private Random random = new Random(354668);
public float cameraPosition;
public float cameraPositionY;
private SoundEffect exitReachedSound;
private SoundEffect CollectBonus;
public int Score
{
get { return score; }
}
int score;
#region
public static int x;
public static int y;
public bool ScoreButtonPressed;
SpriteFont hudFont;
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;
public static Mouse mouse;
List<clickableletter> clickableObjects;
public static MouseState mouseState;
public Vector2 Position
{
get;
protected set;
}
public Vector2 Positionofc2
{
get;
protected set;
}
public Rectangle checkScoreBTN
{
get;
set;
}
public bool DIOC;
private Texture2D TileA1;
private Texture2D checkScoreB;
private Texture2D checkScoreW;
private Texture2D shelf;
#endregion
public ContentManager Content
{
get { return content; }
}
ContentManager content;
#region Loading
public Level(Game theGame, IServiceProvider serviceProvider, Stream fileStream, int levelIndex)
{
whosTurnIsIt();
content = new ContentManager(serviceProvider, "Content");
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();
}
private void LoadPause()
{
pause = new Pause(this);
}
public static MouseState previousState, currentState;
private void LoadContent()
{
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");
clickableObjects = new List<clickableletter>(14);
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)
{
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();
}
}
tiles = new Tile[width, lines.Count];
for (int y = 0; y < Height; ++y)
{
for (int x = 0; x < Width; ++x)
{
char tileType = lines[y][x];
tiles[x, y] = LoadTile(tileType, x, y);
}
}
}
private Tile LoadTile(char tileType, int x, int y)
{
switch (tileType)
{
case '.':
return new Tile(null, TileCollision.Passable,0);
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));
}
}
private Tile LoadTile(string name, TileCollision collision, int type)
{
return new Tile(Content.Load<texture2d>("Tiles/" + name), collision, type);
}
public void Dispose()
{
Content.Unload();
}
#endregion
#region Bounds and collision
public TileCollision GetCollision(int x, int y)
{
if (x < 0 || x >= Width)
return TileCollision.Impassable;
if (y < 0 || y >= Height)
return TileCollision.Passable;
return tiles[x, y].Collision;
}
public Rectangle GetBounds(int x, int y)
{
return new Rectangle(x * Tile.Width, y * Tile.Height, Tile.Width, Tile.Height);
}
public int Width
{
get { return tiles.GetLength(0); }
}
public int Height
{
get { return tiles.GetLength(1); }
}
#endregion
#region Update
public void Update(GameTime gameTime, SpriteBatch spriteBatch)
{
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.P))
{
notPaused = false;
paused = true;
}
if (keyboardState.IsKeyDown(Keys.R))
{
notPaused = true;
paused = false;
}
if (notPaused)
{
PerformMouseInteractions(gameTime);
PerformNormalUpdate(gameTime);
currentState = Microsoft.Xna.Framework.Input.Mouse.GetState();
mouse.Update();
checkScoreButton();
}
pause.Update(gameTime);
}
public void PerformNormalUpdate(GameTime gameTime)
{
foreach (ClickableGameplayObject cgo in clickableObjects)
{
if (cgo != mouse.ClickedObject)
{
cgo.Update(gameTime);
}
}
}
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;
}
}
}
public void checkScoreButton()
{
checkScoreBTN = new Rectangle(87, 605, 163, 59);
x = currentState.X;
y = currentState.Y;
mouseState = Microsoft.Xna.Framework.Input.Mouse.GetState();
if (checkScoreBTN.Contains(x, y))
{
cursorInsideScorebtn = true;
}
if (!checkScoreBTN.Contains(x, y))
{
cursorInsideScorebtn = false;
}
if (player1Turn & cursorInsideScorebtn)
{
if (mouseState.LeftButton == ButtonState.Pressed)
{
ScoreButtonPressed = true;
countScore();
press2 = false;
press1 = true;
}
else if (mouseState.LeftButton == ButtonState.Released)
{
ScoreButtonPressed = false;
if (press1)
{
player1Turn = false;
player2Turn = true;
}
}
}
else if (player2Turn & cursorInsideScorebtn)
{
if (mouseState.LeftButton == ButtonState.Pressed)
{
ScoreButtonPressed = true;
countScore();
press1 = false;
press2 = true;
}
if (mouseState.LeftButton == ButtonState.Released)
{
ScoreButtonPressed = false;
if (press2)
{
player1Turn = true;
player2Turn = false;
}
}
}
}
public void countScore()
{
int scoreThisTurn = 0;
if (player1Turn)
{
for (int i = 0; i < 7; ++i)
{
scoreThisTurn += 1;
}
player1Score += scoreThisTurn;
}
else
{
for (int i = 7; i < 14; ++i)
{
scoreThisTurn += addScore(clickableObjects[i]);
}
}
}
private int addScore(ClickableLetter letter)
{
if (letter.x >= 300 && letter.x <= 1050)
{
int tempX = letter.x;
int tempY = letter.y;
tempX -= 300;
tempX /= 50;
tempY -= 1;
tempY /= 50;
switch (tiles[tempX, tempY].Type)
{
case 3:
case 4:
default:
return letter.PointValue;
}
}
return 0;
}
public static void whosTurnIsIt()
{
if (player1Turn)
{
drawP1 = true;
drawP2 = false;
}
else if (player2Turn)
{
drawP1 = false;
drawP2 = true;
}
}
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);
}
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);
}
DrawTiles(spriteBatch);
letterA.Draw(gameTime, spriteBatch);
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)
{
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);
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);
}
}
public void ScrollCamera(Viewport viewport)
{
const float ViewMargin = 0.35f;
float marginWidth = viewport.Width * ViewMargin;
float marginLeft = cameraPosition + marginWidth;
float marginRight = cameraPosition + viewport.Width - marginWidth;
float marginHeight = viewport.Height * ViewMargin;
float maxCameraPositionX = Tile.Width * Width - viewport.Width;
float maxCameraPositionY = Tile.Height * Height - viewport.Height;
}
public void DrawTiles(SpriteBatch spriteBatch)
{
int left = (int)Math.Floor(cameraPosition / Tile.Width);
int right = left + spriteBatch.GraphicsDevice.Viewport.Width / Tile.Width;
right = Math.Min(right, Width - 1);
int top = (int)Math.Floor(cameraPositionY / Tile.Height);
int bottom = top + spriteBatch.GraphicsDevice.Viewport.Height / Tile.Height;
bottom = Math.Min(bottom, Height);
for (int y = top; y <= bottom; ++y)
{
for (int x = left; x <= right; ++x)
{
Texture2D texture = tiles[x, y].Texture;
if (texture != null)
{
Vector2 position = new Vector2(x, y) * Tile.Size;
spriteBatch.Draw(texture, position, Color.White);
}
}
}
}
#endregion
}
}