さいころゲームについての質問です、お願いします。

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
スポーツ馬鹿

さいころゲームについての質問です、お願いします。

#1

投稿記事 by スポーツ馬鹿 » 10年前

C++始めて二ヶ月の初心者です。
今、クラスを使って二つのさいころをコンピュータとプレイヤーが六回ずつ振って、その合計値が高い方が勝ちというダイスゲームを作っています。
三つほどうまく出来ず、どうしていいかわからないところがあります。
(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;
}



スポーツ馬鹿

少し変更したのですが 毎回の選択が出来ません

#2

投稿記事 by スポーツ馬鹿 » 10年前

少し変更しましたが、やはり毎回続けるためのコンティニューをどうすれば良いのかわかりません。お願いします。

コード:

#include<iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
class Dice
{
public:
    Dice ();
    int Roll();
    int GetNum();
    void Display();
    void Display2();
    
private:
    int Dice1;
    int Dice2;
    int Goukei;
    int TotalRolls;
    int TotalScore;
    int FinalSocre;
};

Dice::Dice():Dice1(0),Dice2(0),Goukei(0),TotalRolls(0),TotalScore(0),FinalSocre(0)
{}

int Dice::Roll()//dice roll
{
    Dice1=0;
    Dice2=0;
    TotalRolls++;
    Dice1 =(rand()%6)+1;
    Dice2=(rand()%6)+1;
    Goukei=Dice1+Dice2;
    TotalScore+= Goukei;
    if (Dice1==1) //dice picture 1 to 6
    {
        cout << "________" << endl;
        cout << "|       |" << endl;
        cout << "|   O   |" << endl;
        cout << "|       |" << endl;
        cout << "|_______|" <<endl;
        
    }
    else if (Dice1==2)
    {
        cout << "________" << endl;
        cout << "|     O |" << endl;
        cout << "|       |" << endl;
        cout << "| O     |" << endl;
        cout << "|_______|"<< endl;
    }
    else if(Dice1==3)
    {
        cout << "________" << endl;
        cout << "|     O |" << endl;
        cout << "|   O   |" << endl;
        cout << "| O     |" << endl;
        cout << "|_______|" << endl;
        
    }
    else if(Dice1==4)
    {
        cout << "_______" << endl;
        cout << "|O    O|" << endl;
        cout << "|      |" << endl;
        cout << "|O    O|" << endl;
        cout << "|______|" << endl;
        
    }
    else if(Dice1==5)
    {
        cout << "_______" << endl;
        cout << "|O    O|" << endl;
        cout << "|  O   |" << endl;
        cout << "|O    O|" << endl;
        cout << "|______|" << endl;
    }
    else if(Dice1==6)
    {
        cout << "_______" << endl;
        cout << "|O    O|" << endl;
        cout << "|O    O|" << endl;
        cout << "|O    O|" << endl;
        cout << "|______|" << endl;
        
    }
    if (Dice2==1)//dice2 picture 1 to 6
    {
        cout << "________" << endl;
        cout << "|       |" << endl;
        cout << "|   O   |" << endl;
        cout << "|       |" << endl;
        cout << "|_______|" <<endl;
        
    }
    else if (Dice2==2)
    {
        cout << "________" << endl;
        cout << "|     O |" << endl;
        cout << "|       |" << endl;
        cout << "| O     |" << endl;
        cout << "|_______|"<< endl;
    }
    else if(Dice2==3)
    {
        cout << "________" << endl;
        cout << "|     O |" << endl;
        cout << "|   O   |" << endl;
        cout << "| O     |" << endl;
        cout << "|_______|" << endl;
    }
    else if(Dice2==4)
    {
        cout << "_______" << endl;
        cout << "|O    O|" << endl;
        cout << "|      |" << endl;
        cout << "|O    O|" << endl;
        cout << "|______|" << endl;
        
    }
    else if(Dice2==5)
    {
        cout << "_______" << endl;
        cout << "|O    O|" << endl;
        cout << "|  O   |" << endl;
        cout << "|O    O|" << endl;
        cout << "|______|" << endl;
    }
    else if(Dice2==6)
    {
        cout << "_______" << endl;
        cout << "|O    O|" << endl;
        cout << "|O    O|" << endl;
        cout << "|O    O|" << endl;
        cout << "|______|" << endl;
        
    }
    return TotalScore;
}

int Dice::GetNum()
{
    FinalSocre=TotalScore;

    return FinalSocre;
}
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 "<<TotalScore<<endl<<endl;
}

void hajime();



int main ()
{
    
    int trigal;
    srand(unsigned(time(NULL)));
    hajime();
    cout<<"Are you ready?"<<endl;
    cout<<"PLease enter any key"<<endl;
    cin>>trigal;
    Dice Pdice,CPdice;
    cout<<"PLayer "<<endl;
    Pdice.Display();
    cout<<"Computer "<<endl;
    CPdice.Display();
    for(int kaisu=0; kaisu<6; kaisu++)
    {
        Pdice.Roll();
        Pdice.Display();
        
        CPdice.Roll();
        CPdice.Display2();
    }
    
    if (Pdice.GetNum() > CPdice.GetNum())
    {cout<<"You won this game"<<endl;}
    else if (Pdice.GetNum() < CPdice.GetNum())
    {cout<<"You lost this game"<<endl;}
    else
        cout<<"This game is tie"<<endl;


    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;
}

アバター
みけCAT
記事: 6734
登録日時: 15年前
住所: 千葉県
連絡を取る:

Re: 少し変更したのですが 毎回の選択が出来ません

#3

投稿記事 by みけCAT » 10年前

スポーツ馬鹿 さんが書きました:少し変更しましたが、やはり毎回続けるためのコンティニューをどうすれば良いのかわかりません。お願いします。
今回のコードではグローバル変数や静的(static)変数を使っていないようなので、簡単に実装するなら、今のmain関数を例えばmeinにリネームして、最後に

コード:

int main ()
{
	int again = 0;
	do {
		mein();
		cout<<"Play again?"<<endl;
		cout<<"yes : non-zero"<<endl;
		cout<<"no  : zero"<<endl;
		cout<<">";
		cin>>again;
	} while (again != 0);
	return 0;
}
というコードを追加するといいかもしれません(テストしていません)。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

サッカー馬鹿

Re: さいころゲームについての質問です、お願いします。

#4

投稿記事 by サッカー馬鹿 » 10年前

やってみます!コメントありがとうございます。

閉鎖

“C言語何でも質問掲示板” へ戻る