summaryrefslogtreecommitdiffstats
path: root/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSimController.cpp
blob: cd5e750712f43564f8167ee50debcdad0ab00f57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * DiceSimController.cpp
 *
 *  Created on: 11.05.2011
 *      Author: sven
 */
#ifndef DICESIMCONTROLLER_H_
	#include "DiceSimController.h"
#endif

#ifndef _LIBGLADEMM_XML_H
	#include <libglademm/xml.h>
#endif

#ifndef DICESIM_H_
	#include "DiceSim.h"
#endif

#include <iostream>
#include <fstream>
#include "Util.h"

// because of static
const char* DiceSimController::UI_FILENAME = "ui/dicesim.glade";

DiceSimController::DiceSimController() {
	Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file(UI_FILENAME);
	builder->get_widget("mainwindow",mPtrMainWin);
	builder->get_widget("startbtn",mPtrStartBtn);
	builder->get_widget("dicecombo",mPtrDiceCombo);
	builder->get_widget("rollscombo",mPtrRollsCombo);
	builder->get_widget("textview1",mPtrTextView);

	mRefPtrTextBuffer = Gtk::TextBuffer::create();
	mPtrTextView->set_buffer(mRefPtrTextBuffer);

	mRollsTreeStore = Gtk::TreeStore::create(mRollsCols);
	mPtrRollsCombo->set_model(mRollsTreeStore);
	mDiceTreeStore = Gtk::TreeStore::create(mDiceCols);
	mPtrDiceCombo->set_model(mDiceTreeStore);

	Gtk::TreeModel::Row rowDice1 = *(mDiceTreeStore->append());
	rowDice1[mDiceCols.mData] = 1;
	Gtk::TreeModel::Row rowDice2 = *(mDiceTreeStore->append());
	rowDice2[mDiceCols.mData] = 2;
	Gtk::TreeModel::Row rowDice10 = *(mDiceTreeStore->append());
	rowDice10[mDiceCols.mData] = 10;

	Gtk::TreeModel::Row rowRolls500 = *(mRollsTreeStore->append());
	rowRolls500[mRollsCols.mData] = 500;
	Gtk::TreeModel::Row rowRolls1000 = *(mRollsTreeStore->append());
	rowRolls1000[mRollsCols.mData] = 1000;
	Gtk::TreeModel::Row rowRolls10000 = *(mRollsTreeStore->append());
	rowRolls10000[mRollsCols.mData] = 10000;

	mPtrDiceCombo->pack_start(mDiceCols.mData);
	mPtrRollsCombo->pack_start(mRollsCols.mData);
	mPtrDiceCombo->set_active(0);
	mPtrRollsCombo->set_active(0);

	mPtrMainWin->signal_hide().connect(sigc::ptr_fun(&Gtk::Main::quit));
	mPtrStartBtn->signal_clicked().connect(sigc::mem_fun(this,&DiceSimController::on_startbutton_clicked));
}

DiceSimController::~DiceSimController() {
	// TODO Auto-generated destructor stub
}

void DiceSimController::startApp(Gtk::Main& kit)
{
	kit.run(*mPtrMainWin);
}

void DiceSimController::on_startbutton_clicked()
{
	Gtk::TreeModel::iterator diceIter = mPtrDiceCombo->get_active();
	Gtk::TreeModel::Row diceRow = *diceIter;
	Gtk::TreeModel::iterator rollsIter = mPtrRollsCombo->get_active();
	Gtk::TreeModel::Row rollsRow = *rollsIter;
	unsigned numDice = diceRow[mDiceCols.mData];
	unsigned numRolls = rollsRow[mRollsCols.mData];
	std::cout << "dice: " << numDice << " rolls: " << numRolls << std::endl;
	DiceSim diceSim(numDice,numRolls);
	std::string text = "Eyes\tabs. freq.\n";
	const std::vector<unsigned>& results = diceSim.getResult();
	std::vector<unsigned>::const_iterator it = results.begin();
	unsigned n=numDice;
	for( ; it != results.end() ; it++)
	{
		if (n <= 6 * numDice) {
			text += Util::ToUString(n);
			text += "\t";
			text += Util::ToUString(*it);
			text += "\n";
			n++;
		}
	}
	mRefPtrTextBuffer->set_text(text);
	writeToCSV(results,numDice);
	startGnuPlot();
}

void DiceSimController::writeToCSV(const std::vector<unsigned>& results, unsigned numDice)
{
	std::ofstream csvFile;
	csvFile.open("dicesim.csv", std::ios::out);
	std::vector<unsigned>::const_iterator it;
	unsigned n=numDice;
	for(it = results.begin() ; it != results.end() ; it++) {
		if (n <= 6 * numDice) {
			if (it != results.begin()) {
				csvFile << std::endl;
			}
			csvFile << n << ";";
			csvFile << *it;
			n++;
		}
	}
	csvFile.close();
}
void DiceSimController::startGnuPlot()
{
	int i = system("gnuplot dicesim.plt -p");
	if (i != 0) {
		std::cerr << "Error starting gnuplot: " << i << std::endl;
	}
}