This is my first time posting here so I hope that I get the posting procedures correct. I am currently taking a C# programming class and our teacher has assigned us a project that entails us designing a C# Farkle Game. If you haven't played Farkle before (which I haven't prior to taking this class) all you really need to know is it's basically like Yahtzee. You roll six dice and try to get the most points out of the dice you hold, then roll again but minus the dice you've already held aside... if that makes sense. Well, I have to create a form that controls the Farkle board then create another class that can ONLY be for code whos' sole purpose is to calculate the score of the held dice. However, the kicker is that I can't use any code that represents picture boxes, labels, text boxes, etc. Just old school c++ style code (i.e. arrays, ints, doubles, etc.).
Using the code below, my problem is that after the user rolls the dice and holds whatever dice/he she wants, I need a way to pass the dice that was held to the other class for calculating the best score. I can figure that out, however I can't figure out how to pass which dice was held to the other class. Since I can't use images, pictureboxes, etc. in the other class per my professor, my plan is as follows (starting after the 'Roll Dice' button has been clicked and the user checks which dice he/she wants to hold THEN clicks the 'Roll Dice' Button again):
Use the for loop(s) and if/else statements to go through and find out which dice was (or wasn't held). If the dice wasn't held, then go ahead and randomize the dice. If the dice was held, then somehow store the value of the dice in temp_array. My thought process in doing this was since the imageList1 stores all the dice*.png images (i.e. index 0 = 1, index 1 = 2, etc.) then I could somehow store the index of the dice in the temp_array and pass the temp_array to the other class for review and ultimately scoring. However, I can't get the pogram to read the index and respective dice face correctly. If I go in and click 'Roll Dice' the first time to randomize the dice, hold my dice, then click the array again, the value of index that pops up in label1 (which is just there to test what is being passed to my other class) isn't correct. I need to find out a way to store the face of the dice being held in an array so I can use it later in my other class. I can't figure out what I am doing wrong!! I go through each iteration on a piece of paper and everything seems to add up correctly. Any help is greatly appreciated. The code for the form is below. Also, an image is attached as well of what the form looks like after I select which dice to hold and click 'Roll Dice'. Please keep in mind that my professor put something in my code that I didn't take out before this post that sets the label1 label to all zeros initially so you are going to have to disregard the first six zeros. Sorry. However, as you can see, 2 and 3 are not the dice that I held.
Thanks in advance for your help!!! There are several events below but the 'Roll Dice' click event is the one (I think) is causing the problem.
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HonorsPracticeFarkle
{
public partial class Form1 : Form
{
ImageList imageList1 = new ImageList();
private Random m_rand = new Random();
private int[] i_array = new int[6] { 0, 1, 2, 3, 4, 5 };
private int counter = 0;
private FrmRegistration theApp;
private PictureBox[] pic_array = new PictureBox[6];
private CheckBox[] chbx_array = new CheckBox[6];
private int[] diceHeld_array = new int[6];
public Form1(FrmRegistration frm)
{
InitializeComponent();
theApp = frm;
imageList1.ImageSize = new Size(50, 50);
imageList1.Images.Add(Image.FromFile(@"..\..\Dice Image Files\die1.png"));
imageList1.Images.Add(Image.FromFile(@"..\..\Dice Image Files\die2.png"));
imageList1.Images.Add(Image.FromFile(@"..\..\Dice Image Files\die3.png"));
imageList1.Images.Add(Image.FromFile(@"..\..\Dice Image Files\die4.png"));
imageList1.Images.Add(Image.FromFile(@"..\..\Dice Image Files\die5.png"));
imageList1.Images.Add(Image.FromFile(@"..\..\Dice Image Files\die6.png"));
pic_array[0] = picDice1;
pic_array[1] = picDice2;
pic_array[2] = picDice3;
pic_array[3] = picDice4;
pic_array[4] = picDice5;
pic_array[5] = picDice6;
chbx_array[0] = chbxDie1;
chbx_array[1] = chbxDie2;
chbx_array[2] = chbxDie3;
chbx_array[3] = chbxDie4;
chbx_array[4] = chbxDie5;
chbx_array[5] = chbxDie6;
for (int i = 0; i < pic_array.Length; i++)
{
pic_array[i].Image = imageList1.Images[i];
}
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnRollDice_Click(object sender, EventArgs e)
{
int[] temp_array = new int[6];
int index = 0;
if (counter >= 1 && (chbxDie1.Checked == false && chbxDie2.Checked == false && chbxDie3.Checked == false
&& chbxDie4.Checked == false && chbxDie5.Checked == false && chbxDie6.Checked == false))
{
MessageBox.Show("You must select a die or dice to proceed!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
for (int i = 0; i < pic_array.Length; i++)
{
if (chbx_array[i].Checked == false)
{
index = m_rand.Next(0, 5);
pic_array[i].Image = imageList1.Images[index];
pic_array[i].Image.Tag = index;
}
else
{
for (int j = 0; j < pic_array.Length; j++)
if (pic_array[i].Image.Tag == imageList1.Images[j].Tag)
index = j + 1;
temp_array[i] = index;
}
}
for (int i = 0; i < temp_array.Length; i++)
label1.Text += temp_array[i].ToString();
}
counter++;
lblMessageToCurrent.Text = "Pick which dice to hold";
}
private void btnReplay_Click(object sender, EventArgs e)
{
for (int i = 0; i < pic_array.Length; i++)
{
pic_array[i].Image = imageList1.Images[i];
}
lblGameScore1Output.Text = "0";
lblGameScore2Output.Text = "0";
lblRollScore1Output.Text = "0";
lblRollScore2Output.Text = "0";
lblTurnScore1Output.Text = "0";
lblTurnScore2Output.Text = "0";
btnRollDice.Enabled = false;
btnNextPlayer.Enabled = false;
btnReplay.Enabled = false;
btnGameOver.Enabled = false;
for (int i = 0; i < chbx_array.Length; i++)
chbx_array[i].Checked = false;
lblMessageToCurrent.Text = "Guess Numbers To Play";
}
private void btnGameOver_Click(object sender, EventArgs e)
{
lblMessageToCurrent.Text = "***GAME OVER***\nPlease Press 'Replay' or 'Close'!";
btnRollDice.Enabled = false;
btnNextPlayer.Enabled = false;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
theApp.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
lblMessageToCurrent.Text = theApp.getOutput();
}
}
}
Attached File(s)
-
Farkle Program Example.doc (216K)
Number of downloads: 59

New Topic/Question
Reply



MultiQuote








|