Jump to content

C# Game Help

- - - - -

  • Please log in to reply
No replies to this topic

#1
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
I need to refactor this code so there is a player class and then an instance for player one and player two can be made. I put all of the code into one class found below however I think this is not the best way to go about it. When i've tried to do it, the dice values arent being outputted nor is the position. Any ideas as to why? Or how I can go about changing it?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AdvancedSolution
{
    public partial class Form2 : Form
    {
        //Variables
        Random randomGenerator;
        dice gameDice;
        public int P1RollValue;
        public int P2RollValue;
        public int playerOnePosition;
        public int playerTwoPosition;
        int turn;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            lblPlayerOneName.Text = Form1.playerOneName;
            lblPlayerTwoName.Text = Form1.playerTwoName;

            randomGenerator = new Random();
            gameDice = new dice(randomGenerator);
            
        }

        public void playerOneRoll()
        {
            P1RollValue = gameDice.Roll();
            lblRollValue.Text = Convert.ToString(P1RollValue);
        }

        public void playerTwoRoll()
        {
            P2RollValue = gameDice.Roll();
            lblRollValue.Text = Convert.ToString(P2RollValue);
        }

        public int getP1Position()
        {
            return playerOnePosition += P1RollValue;
        }

        public void playerOnePositionOutput()
        {
            int p1Pos = getP1Position();
            lblPlayerOnePositionValue.Text = Convert.ToString(p1Pos);
        }

        public int getP2Position()
        {
            return playerTwoPosition += P2RollValue;
        }

        public void playerTwoPositionOutput()
        {
            int p2Pos = getP2Position();
            lblPlayerTwoPositionValue.Text = Convert.ToString(p2Pos);
        }


        private void btnRoll_Click(object sender, EventArgs e)
        {
            if (turn % 2 == 0) //if turn is even
            {
                playerOneRoll();
                playerOnePositionOutput();
                
                lblCurrentPlayer.Text = "Player Two, Click Roll to roll the Dice!";
                //player1 turn
                turn++;
            }
            else //otherwise its odd
            {
                playerTwoRoll();
                playerTwoPositionOutput();
                lblCurrentPlayer.Text = "Player One, Click Roll to roll the Dice!";
                //player2 turn
                turn++;
            }

        }
    }
}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users