Let's have a programming challenge just for fun.
Objective
Create a game in any language that will do the following.
- Play a game of dice against someone else. (ex: Player 1 vs Player 2)
- Must play a minimum of 2 games.
- Calculate the ratio of the game (Win / Lose / Draw) again Player vs Player
- Display the ratio in a pie chart.
You can add any feature you want. Unlimited players and unlimited games are perfect example. Remember it's just for fun. Just to test your skills and see if you can do it.
Results 1 to 2 of 2
- 23 Dec. 2013 11:27pm #1
Programming Challenge - Dice Game.
- 23 Dec. 2013 11:44pm #2
Here is my source in C#.
PHP Code:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Threading;
namespace Dice_Game
{
public partial class frmMain : Form
{
string[,] player_status = new string[5,5];
//This is only temporary but for the sake of saving the program from errors lets assume
//that there are five players and five games being played until it set later.
public frmMain()
{
InitializeComponent();
}
private void btnPlay_Click(object sender, EventArgs e)
{
Console.Clear();
btnShow.Enabled = false;
//Lets clear that console for a new game and disable the show results button
//to avoid bugs and people breaking the game.
player_status = new string[(int)numPlayers.Value,(int) numGames.Value];
//Setting the array for how many games are being played and how many players.
int[] player_score = new int[(int)numPlayers.Value];
for (int player_up = 0; player_up < numPlayers.Value; player_up++)
{
//This first for loop rolls the dice three times for the player that is up.
//Example; Player[0] (Player 1) is rolling and he will roll (x) amount of
//times before the next player goes.
int tempPlayer = player_up + 1;
Console.WriteLine("Player[" + tempPlayer + "]\r\n");
for (int play_game = 0; play_game < numGames.Value; play_game++)
{
//Player[x] is rolling and his scores will be saved with in an array player_status[x, s].
//s = score.
player_status[player_up, play_game] = new Random().Next(1, 7).ToString();
//We have to set the random function to 7 because it subtracts the last number by 1
//Player[x] rolls and the score is saved temporarily into this array.
//.ToString() is converting the output into a string due to the array being a string.
int tempGame = play_game + 1;
Console.WriteLine("Game [" + tempGame + "] - " + player_status[player_up, play_game]);
//Output Player[x]'s game results.
Thread.Sleep(175);
//We have to let the program rest or it will duplicate scores (its a bug).
}
Console.WriteLine("-------------------------------");
}
numPlayRes.Maximum = numPlayers.Value;
numPlayRes2.Maximum = numPlayers.Value;
btnShow.Enabled = true;
//Enable the show results button and set the game results maxium to how many games were played
//to avoid errors from non-existing games.
}
private void btnShow_Click(object sender, EventArgs e)
{
btnShow.Enabled = false;
int[,] total_scores = new int[2, 3];
//total_scores[x, y]; x = player, y[0] = Win, y[1] = Loss, y[2] = Draw.
if (numPlayRes.Value != numPlayRes2.Value)
{
//Check to see if its the same player vs same player to avoid error.
for (int i = 0; i < player_status.GetLongLength(1); i++)
{
//Gets how many games were played.
int player1_score = int.Parse(player_status[(int)numPlayRes.Value - 1, i]);
int player2_score = int.Parse(player_status[(int)numPlayRes2.Value - 1, i]);
//Grab both player scores into an integer.
if (player1_score > player2_score)
{
total_scores[0, 0]++;
total_scores[1, 1]++;
//Player 1 wins, player 2 loses.
}
else if (player2_score > player1_score)
{
total_scores[1, 0]++;
total_scores[0, 1]++;
//Player 2 wins, Player 1 loses.
}
else if (player1_score == player2_score)
{
total_scores[0, 2]++;
total_scores[1, 2]++;
//Both draw.
}
}
//below shows the records for the selected players.
Console.WriteLine("Player [" + numPlayRes.Value + "] Record\r\n" +
"Wins: " + total_scores[0, 0] + "\r\n" +
"Loses: " + total_scores[0, 1] + "\r\n" +
"Draws: " + total_scores[0, 2] + "\r\n------------------------------");
Console.WriteLine("Player [" + numPlayRes2.Value + "] Record\r\n" +
"Wins: " + total_scores[1, 0] + "\r\n" +
"Loses: " + total_scores[1, 1] + "\r\n" +
"Draws: " + total_scores[1, 2] + "\r\n------------------------------");
int games_played = (int)player_status.GetLongLength(1);
int tied_games = total_scores[0, 2] + total_scores[1, 2];
//Lets create the chart now.
Dictionary<string, double> game_results = new Dictionary<string, double>()
{
{ "Player [" + numPlayRes.Value + "]", Math.Round(Percentage(double.Parse(total_scores[0, 0].ToString()), games_played)) },
{ "Player [" + numPlayRes2.Value + "]", Math.Round(Percentage(double.Parse(total_scores[1,0].ToString()), games_played)) },
{ "Tied Games", Math.Round(Percentage(tied_games, games_played) / 2) }
};
// ^ This creates all the points and percentages.
chartPie.Series[0].Points.Clear();
chartPie.Series[0].ChartType = SeriesChartType.Pie;
foreach (string tag_results in game_results.Keys)
{
chartPie.Series[0].Points.AddXY(tag_results, game_results[tag_results]);
chartPie.Series[0].IsValueShownAsLabel = true;
}
//^ Creates, sets, and displays the graph.
}
else
{
MessageBox.Show("Please select two different players!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
btnShow.Enabled = true;
}
private double Percentage(double value, double games_played)
{
//Calculates the percentage of games won/lost/or tied.
if (value > 0)
{
return (value / games_played * 100);
}
return 0;
}
private void frmMain_Load(object sender, EventArgs e)
{
}
}
}