New Posts  All Forums:Forum Nav:

C# Coding help

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:40pm
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
Thread Starter 
sorry about this being posted twice...accident on my part
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
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Operating Systems