New Posts  All Forums:Forum Nav:

Help with C# coding

post #1 of 2
Thread Starter 
Hi all,

I have an assignment for my computer science class, and while i've completed part of it, im having trouble with two parts in particular. Making/editing two classes. I haven't had much experience at all coding so this has been a challenge. Any help would be appreciated.


The directions for the two classes are:
Die Specification
The Die class has two attributes:
1. a value for the die; this is different for each instance
2. an instance of Random; this must be the same for all instances, otherwise every die would roll the same sequence of numbers each time

The Die class has at least 2 methods, plus properties for the value attribute (properties are not needed for the Random object).
1. Default constructor – initialize all attributes of the object.
2. Roll – generate a new random value between 1 and 6 (inclusive) and return the value.
Player Specification
The Player class has two attributes:
1. a value for the position; this is different for each instance and is a whole numeric value. The range for this attribute is 0 to 32, with the initial value of 0. The value should not ever go below 0 or above 32 (there are no “board spaces†less than 0 or beyond 32).
2. a value for the number of rolls this player has made in this game; this is different for each instance

The Player class has at least 3 methods, plus properties for the attributes.
1. Default constructor – initialize all attributes of the object.
2. Move – takes a whole numeric number as input (number of spaces to move) and updates the position.
3. Roll – increments the number of rolls for this player by 1.

Any help would be greatly appreciated, i really don't have a clue as to what im doing when it comes to coding.
I put the entire project in a zip file, and heres the code as well:




using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace BoardGame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

Texture2D pawn;
Texture2D board;

Player avatar;

KeyboardState old;

Vector2[] positions;

int[] x_vals = {340, 284, 249, 215, 181, 148, 113, 79, 45, 45, 45, 79, 113, 113, 148,
181, 215, 249, 283, 283, 317, 351, 351, 351, 317, 283, 249, 215, 181, 148, 113, 79, 33 };
int[] y_vals = {244, 249, 249, 249, 249, 249, 249, 249, 249, 216, 183, 183, 183, 150, 150,
150, 150, 150, 150, 116, 116, 116, 83, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 };

int[] space_types = {0, 1, 2, 3, 4, 5, 1, 2, 4, 3, 5, 4, 2, 3, 1, 3, 5, 2, 1, 5, 3, 4, 5, 2, 3, 2, 1, 5, 3, 1, 2, 3, 6 };

int[] adjustment = {0, +2, -1, +1, -2, 0, 0};

SoundEffect[] sounds;

SpriteFont text;
string message;

Die d;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = 300;
graphics.PreferredBackBufferWidth = 400;
positions = new Vector2[33];
for (int i = 0; i < x_vals.Length; i++)
positions[i] = new Vector2(x_vals[i], y_vals[i]);
avatar = new Player();
message = "";
sounds = new SoundEffect[5];
d = new Die();
}

/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here

base.Initialize();
}

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

// TODO: use this.Content to load your game content here

board = Content.Load<Texture2D>("board");
pawn = Content.Load<Texture2D>("pawn");

text = Content.Load<SpriteFont>("SpriteFont1");

sounds[0] = Content.Load<SoundEffect>("01");
sounds[1] = Content.Load<SoundEffect>("02");
sounds[2] = Content.Load<SoundEffect>("03");
sounds[3] = Content.Load<SoundEffect>("04");
sounds[4] = Content.Load<SoundEffect>("05");
}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here
KeyboardState cur = Keyboard.GetState();

if (cur.IsKeyDown(Keys.Space) && !old.IsKeyDown(Keys.Space) && (avatar.Position < 32))
{
// handle space down
int i = d.Roll();
avatar.Move(i);
message = "You rolled a " + i;
avatar.Move(adjustment[space_types[avatar.Position]]);
avatar.Roll();

if ((avatar.Position != 0) && (avatar.Position != 32))
{
sounds[space_types[avatar.Position]-1].Play();
}
}

old = cur;

base.Update(gameTime);
}

/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here

spriteBatch.Begin();
spriteBatch.Draw(board, new Vector2(0, 0), Color.White);
spriteBatch.Draw(pawn, new Vector2(x_vals[avatar.Position]+4, y_vals[avatar.Position]+4), Color.White);

if (avatar.Position != 32)
{
spriteBatch.DrawString(text, message, new Vector2(6, 101), Color.Black);
spriteBatch.DrawString(text, message, new Vector2(5, 100), Color.White);
}
else
{
spriteBatch.DrawString(text, "You won in " + avatar.RollCount + " rolls!", new Vector2(6, 101), Color.Black);
spriteBatch.DrawString(text, "You won in " + avatar.RollCount + " rolls!", new Vector2(5, 100), Color.White);
}
spriteBatch.End();

base.Draw(gameTime);
}
}
}
Edited by Wight Faux - 10/6/11 at 3:39pm
BloodRage i7
(13 items)
 
  
CPUMotherboardGraphicsRAM
core i7 920 D0 @ 3.02 Foxconn Bloodrage GTi EVGA GTX 560 ti DS SC G. Skill Ripjaws 8GB 1600MHz 
Hard DriveOptical DriveMonitorKeyboard
2 640GB WD Caviar Black Asus DRW-24B1ST 24x DVD RW 23" Acer H233H 1080p & 22" Asus ML228h 1080p LED Razor Lycosa Mirror Edition 
PowerCaseMouse
Antec TruePower Quattro 1000W Antec 1200 Razer Naga 
  hide details  
Reply
BloodRage i7
(13 items)
 
  
CPUMotherboardGraphicsRAM
core i7 920 D0 @ 3.02 Foxconn Bloodrage GTi EVGA GTX 560 ti DS SC G. Skill Ripjaws 8GB 1600MHz 
Hard DriveOptical DriveMonitorKeyboard
2 640GB WD Caviar Black Asus DRW-24B1ST 24x DVD RW 23" Acer H233H 1080p & 22" Asus ML228h 1080p LED Razor Lycosa Mirror Edition 
PowerCaseMouse
Antec TruePower Quattro 1000W Antec 1200 Razer Naga 
  hide details  
Reply
post #2 of 2
Since it has been a week and nobody has posted a reply, here's some advice on getting your question answered:

1) Don't ask people to do your homework for you (on some forums this will even get you banned or your thread locked). Ask specific questions and you will get specific answers. People aren't going to go to a great deal of effort to do things for you, but they will answer your questions and help you over small stumbling blocks.

2) Format your post well. Don't [b] entire paragraphs and make sure to use [code] when you're posting code. In actual fact the question is very simple and you're making it look a lot scarier than it should be by formatting the way you have. People will take one look at this thread and then leave without even reading it fully or give themselves a headache by trying to read your code.



Here's a brief pseudocode answer. It's really just your question rephrased, but your question pretty much tells you exactly what to do on every single line anyway, so there's no way you can get lost on it.

Code:
class Die {
  private static Random Rand;
  private int Value;

  public Die() {
    // Initialise members
  }

  public int Roll() {
    // Roll the die
  }

  public int Value { get { ... } set { ... } }
}
See how easy that was? Now go do the same for Player.

One thing I will say though is be careful with the word "value", since it's a keyword in the language.

In reality Value shouldn't have a mutator ("set" property), it should only be set from Roll, but your question asked for "properties" on it, so both are there. It makes no sense unless you're actually trying to cheat on dice rolls, but it answers the question.
Edited by ghell - 10/14/11 at 6:11pm
Serene Power
(13 items)
 
  
CPUMotherboardGraphicsRAM
Dual Xeon E5620 Supermicro X8DAL-i EVGA 570HD SC 8GiB Corsair DDR3 1333 ECC-R 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 120GB SSD (+16TB NAS) LG 10x Blu-Ray Windows 7 x64 / Debian x64 Dell 2408WFP, P2411H 
PowerCase
850W CoolerMaster SilentPro M CoolerMaster Cosmos Pure 
  hide details  
Reply
Serene Power
(13 items)
 
  
CPUMotherboardGraphicsRAM
Dual Xeon E5620 Supermicro X8DAL-i EVGA 570HD SC 8GiB Corsair DDR3 1333 ECC-R 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 120GB SSD (+16TB NAS) LG 10x Blu-Ray Windows 7 x64 / Debian x64 Dell 2408WFP, P2411H 
PowerCase
850W CoolerMaster SilentPro M CoolerMaster Cosmos Pure 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming