How to work out how many times a value appears in an array c# -
i have been set assignment, means need create '3 or more dice game'. i'm stuck on scoring system needed game, goes follows: " players in turn roll 5 dice , score three-of-a-kind or better. if player has two-of-a-kind, may re-throw remaining dice in attempt improve matching dice values. if no matching numbers rolled, player scores 0.
players score following number of points accordingly:
3-of-a-kind: 3 points 4-of-a-kind: 6 points 5-of-a-kind: 12 points
a set number of rounds played (say 50) , player highest total score @ end of game, winner. " need work out how add random dice's numbers see numbers matching.
namespace dicegame { class program { static void main(string[] args) { console.writeline("hello, computer , speaking you, welcome dice game, here rules:"); console.writeline("3-of-a-kind: 3 points "); console.writeline("4-of-a-kind: 6 points "); console.writeline("5-of-a-kind: 12 points"); console.writeline("first player reach 50 wins"); //integer array store 5 values dice int[] rolls = new int[5]; //calls class dice (calls integer value) (int numdice = 0; numdice < 5; numdice++) { rolls[numdice] = dice.roll(); //for each loop, position of array , place number want in position console.writeline(rolls[numdice]); } //makes new game object used call functions game game = new game(); game.startgame(); console.readline(); } //produces 1 random number public class dice { static random rng = new random(); public static int roll() { //create random number generator return rng.next(1, 7); } } public class game { public void startgame() { //prompts user input value , stores int playerno = numberofplayers(); while (playerno < 2) { console.writeline("please enter number between 2-4"); playerno = numberofplayers(); } //now have number of players, need loop through //creates number of players in array player[] listofplayers = new player[playerno]; //this looks @ current player code looking @ (int currentplayer = 0; currentplayer < playerno; currentplayer++) { listofplayers[currentplayer] = new player(); console.writeline("it player {0}'s turn", currentplayer + 1); listofplayers[currentplayer].rollplayerdice(); console.writeline(listofplayers[currentplayer].score); listofplayers[currentplayer].playersscore(); } } void inputplayers() { //create number of players code //create way input name of players //depending on number of players, repeat code below many times string player1 = console.readline(); } public int numberofplayers() { int playernum = 0; try { console.writeline("please enter number of players play 2-4"); playernum = int.parse(console.readline()); } catch (exception e) { console.writeline(e.message); } return playernum; //if playerno equal 2 = 2 players //if playerno equal 3 = 3 players //if playerno equal 4 = 4 players //if playerno less 2 , more 4 loop through if statement , ask input , number "between 2-4" } public class player { public int score = 0; int[] playerroll = new int[5]; public void rollplayerdice() { (int currentdice = 0; currentdice < playerroll.length; currentdice++) { playerroll[currentdice] = dice.roll(); console.writeline("dice {0} rolled {1}", currentdice, playerroll[currentdice]); } } public int playersscore() { int[] dicefaces = new int[6]; /*for (int diceface = 0; diceface < playerroll.length; diceface++) { int onecounter = 0; //number of 1's = //number of 2's = //number of 3's = //number of 4's = //number of 5's = //number of 6's = //create system work out score //create switch see player score equal (switch 3, 4, 5 , add points correlate) } */ foreach (int d in playerroll) { dicefaces[d]++; } int caseswitch = 0; switch (caseswitch) { case 3: //add on points players score score += 3; break; case 4: //add on points player score break; case 5: //add on points of players score break; } return 0; } } } } } ^above whole code , below code working in right on attempting scoring.
public int playersscore() { int[] dicefaces = new int[6]; /*for (int diceface = 0; diceface < playerroll.length; diceface++) { int onecounter = 0; //number of 1's = //number of 2's = //number of 3's = //number of 4's = //number of 5's = //number of 6's = //create system work out score //create switch see player score equal (switch 3, 4, 5 , add points correlate) } */ foreach (int d in playerroll) { dicefaces[d]++; } int caseswitch = 0; switch (caseswitch) { case 3: //add on points players score score += 3; break; case 4: //add on points player score break; case 5: //add on points of players score break; } return 0; } }
you can use groupby(), count():
int score = 0; var grouped = dice.groupby(x => x); //score 3 of kinds score += grouped.count(x => x.count() == 3) * 3; //score 4 of kinds score += grouped.count(x => x.count() == 4) * 6; //score 5 of kinds score += grouped.count(x => x.count() == 5) * 12; working fiddle
Comments
Post a Comment