さいころゲームについての質問です、お願いします。
Posted: 2015年4月25日(土) 09:46
C++始めて二ヶ月の初心者です。
今、クラスを使って二つのさいころをコンピュータとプレイヤーが六回ずつ振って、その合計値が高い方が勝ちというダイスゲームを作っています。
三つほどうまく出来ず、どうしていいかわからないところがあります。
(1)ひとつ目は中間のところで、さいころをどちらかが振るたびに何かしらキーを押さないと続かないようにしたいのですが、今はfor loopの中に入れてしまって、一回しか実行できません。
(2)さいころの値が何が出たかと一緒に、したのvoidのさいころの絵を一緒につけたいのですがどうすれば出来るでしょうか。今はどうして良いのかわからないのでとりあえずしたの残してあるという形になってます
(3)最後に結果をどちらが勝ったのか、また引き分けなのかを表示したいのですが、いろいろ試してみた結果うまく行かず、void kekkaという状態になっています。
どなたか教えていただけると助かります、よろしくお願いします。
今、クラスを使って二つのさいころをコンピュータとプレイヤーが六回ずつ振って、その合計値が高い方が勝ちというダイスゲームを作っています。
三つほどうまく出来ず、どうしていいかわからないところがあります。
(1)ひとつ目は中間のところで、さいころをどちらかが振るたびに何かしらキーを押さないと続かないようにしたいのですが、今はfor loopの中に入れてしまって、一回しか実行できません。
(2)さいころの値が何が出たかと一緒に、したのvoidのさいころの絵を一緒につけたいのですがどうすれば出来るでしょうか。今はどうして良いのかわからないのでとりあえずしたの残してあるという形になってます
(3)最後に結果をどちらが勝ったのか、また引き分けなのかを表示したいのですが、いろいろ試してみた結果うまく行かず、void kekkaという状態になっています。
どなたか教えていただけると助かります、よろしくお願いします。
#include<iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
void one();
void two();
void three();
void four();
void five();
void six();
class Dice
{
public:
Dice ();
void Roll();
void Display();
void Display2();
void Kekka();
private:
int Dice1;
int Dice2;
int Goukei;
int TotalRolls;
int TotalScore;
int totalSocre2;
};
Dice::Dice():Dice1(0),Dice2(0),Goukei(0),TotalRolls(0),TotalScore(0),totalSocre2(0)
{}
void Dice::Roll()//dice roll
{
Dice1=0;
Dice2=0;
TotalRolls++;
Dice1 =(rand()%6)+1;
Dice2=(rand()%6)+1;
Goukei=Dice1+Dice2;
TotalScore+= Goukei;
totalSocre2+=Goukei;
}
void Dice::Display()//display for the player
{
cout<<"Dice1 Roll was "<<Dice1<<endl;
cout<<"Dice2 Roll was "<<Dice2<<endl;
cout<<"You have rolled "<<TotalRolls<<endl;
cout<<"Your total score is "<<TotalScore<<endl<<endl;
}
void Dice::Display2()//display for the computer
{
cout<<"Dice1 Roll Was "<<Dice1<<endl;
cout<<"Dice2 Roll was "<<Dice2<<endl;
cout<<"Computer have rolled "<<TotalRolls<<endl;
cout<<"Computer's total socre is "<<totalSocre2<<endl<<endl;
}
void Dice::Kekka()//compare the last result of the two players
{
if (TotalScore>totalSocre2)
{cout<<"You won this game"<<endl;}
else if (totalSocre2<TotalScore)
{cout<<"You lost this game"<<endl;}
else
cout<<"This game is tie"<<endl;
}
void hajime();
int main ()
{
srand(unsigned(time(NULL)));
hajime();
Dice Pdice, CPdice, ketu;
cout<<"PLayer "<<endl;
Pdice.Display();
cout<<"Computer "<<endl;
CPdice.Display();
for(int kaisu=0; kaisu<6; kaisu++)
{ int conti;
Pdice.Roll();
cout<<"PLayer ";
Pdice.Display();
CPdice.Roll();
cout<<"Computer ";
CPdice.Display2();
cout<<"please enter any key to continue"<<endl;
cin>>conti;
}
Pdice.Display();
CPdice.Display2();
ketu.Kekka();
return 0;
}
void hajime()//this is the first message
{
cout<<"Welcome to dice game"<<endl<<endl;
cout<<"You are playing against the computer."<<endl;
cout<<"Each player gets to roll 2 dice a total of 6 times."<<endl;
cout<<"The player with highest totaal of dice values wins!"<<endl;
}
void one()
{
cout << "________" << endl;
cout << "| |" << endl;
cout << "| O |" << endl;
cout << "| |" << endl;
cout << "|_______|" <<endl;
}
void two()
{
cout << "________" << endl;
cout << "| O |" << endl;
cout << "| |" << endl;
cout << "| O |" << endl;
cout << "|_______|"<< endl;
}
void three()
{
cout << "________" << endl;
cout << "| O |" << endl;
cout << "| O |" << endl;
cout << "| O |" << endl;
cout << "|_______|" << endl;
}
void four()
{
cout << "_______" << endl;
cout << "|O O|" << endl;
cout << "| |" << endl;
cout << "|O O|" << endl;
cout << "|______|" << endl;
}
void five()
{
cout << "_______" << endl;
cout << "|O O|" << endl;
cout << "| O |" << endl;
cout << "|O O|" << endl;
cout << "|______|" << endl;
}
void six()
{
cout << "_______" << endl;
cout << "|O O|" << endl;
cout << "|O O|" << endl;
cout << "|O O|" << endl;
cout << "|______|" << endl;
}