いくつかの配列とループを使用して簡素化された首謀ゲーム

エミール:

これは私がクラスを持っているプロジェクトです。このトピックは文字列のみ、ループと配列までカバーものになっているので、私は、まだ初心者です。あなたがゲームに慣れていない場合は、高度なバージョンがあり、このしかし、私は簡単なバージョンを任務としています。ここでは、このバージョンのためのルールです。

プログラムは、ゲームに使用するパターンを入力する最初のプレーヤー、コードメーカーを尋ねることによって開始します。パターンは、4文字の長さであり、各文字は(R Gが緑色である、赤)色を表します。
ゲームを簡単にするため、使用することができる唯一の2色、赤と緑があります。したがって、たとえば、ユーザーが赤赤緑赤を表現するためにRRGRを入力することができ、またはそれらは緑緑緑緑を表現するためにGGGGを入力することができます。簡単にするために、あなたはパターンだけRのおよび/またはGのから成るであろうと仮定することができ、間違った文字やsymbols.Howeverを入力しないであろうコードメーカーは、あなたがコードメーカーはそれが4文字の長さであること(正確に入力された4文字のことをチェックしなければなりません、5ないとしない3)。そうでない場合は、彼らが行うまでは、プロンプトに必要です。コードメーカーが上位または小文字を入力する可能性があり、あなたはそれを処理することができるはずです。

プログラムはまた、許可されている推測の最大数を要求します。今、ゲーム缶begin.Theコードブレーカーは今、ゲームに勝つしようとするパターンを推測します。彼らは推測の最大数によってそうすることができない場合、彼らは失います。コードブレーカーを支援するために、あなたのプログラムは、各推測の後にフィードバックを与えます。具体的には、正確に正しい位置にある各色のために、それは黒いペグを示すであろう。正しい位置にないが、パターン内の各色について、それが白いペグを示すであろう。この例のサンプルの相互作用を参照してください。詳細な要件とヒント:

ここでは、それがどのように動作するかをのサンプルは次のとおりです。

Code maker, please enter a pattern of 4 colors (the possible colors are R=red and G=green):

GGGR

What is the maximum number of guesses allowed for this game?

4

Okay, now it's the code breaker's turn. You have 4 chances to guess the pattern. Enter a guess:

RRRR

The code maker sets out 1 black pegs and 0 white pegs. 

Enter a guess:

GGRG

The code maker sets out 2 black pegs and 2 white pegs

Enter a guess:

GGGR

You got the pattern! The code breaker wins!

WHERE I AM SO FAR

まず、私は黒のペグを計算します。それを行うための最善の方法は、FORループ使って配列に文字列の入力を変換することです。二つの主アレイ、解決する必要がある元のパターン、ユーザによって行われたすべての試みのための試みのアレイが存在します。その後、我々は、配列の内容を比較し、黒のペグに彼らが試みに一致するたびに追加するには、FORループを使用します。

これは私が(黒のピンを計算する)に焦点を当てていたコードです。私の問題は、これまでのところ、私はすべての配列の内容に合わせてピンを得る傾けることです。基本的に、それは常に、それは必要に応じて、全体の未完成のコードは、この部分の下にある3本のピンまたは2でなければならない時間が存在するにもかかわらず、4本のピンをもたらします。

String pattern = keys.nextLine().toUpperCase(); // this is used to enter the original pattern
String attempt = keys.nextLine().toUpperCase(); // this is to enter an attempt

String pattern_array [] = new String [5];
String attempt_array [] = new String [5];

int i;          
int black_pegs = 0;
for (i=0; i<pattern.length(); i++) {
    pattern_array[i] = pattern.substring(i,(i+1)); 
}
for ( i=0; i<attempt.length();i++) {
    attempt_array[i] = attempt.substring(i,i+1);
}
for (int x=0; x<4; x++) {
    if (pattern_array[i]==attempt_array[i]) {
        black_pegs++;
    }
}

これは私が(見て、必要に応じて他のことを指摘してお気軽に)これまで持っているものの私のコードです

import java.util.Scanner;
public class CopyOfAssignment5 {
    public static void main(String[] args) {
        Scanner keys = new Scanner(System.in);
        System.out.println("Codemaker, please enter a pattern of 4 colors (the possible colors are R=red and G=green");
        String pattern = keys.nextLine().toUpperCase();
        System.out.println("What is the maximum number of guesses for this game?");
        int number_of_guesses = keys.nextInt();
        int turns = number_of_guesses + 1;
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("Okay, now its the codebreaker's turn. You have " + number_of_guesses
                + " chances to guess the pattern");
        System.out.println("Enter a Guess:");

        int white_pegs = 0, black_pegs = 0;
        String pattern_array[] = new String[5];
        String attempt_array[] = new String[5];
        String attempt = null;

        while (turns > 0) { // while turns are not zero
            attempt = keys.nextLine().toUpperCase(); // then keep displaying the scanner input for an attempt.
            turns--;
        }
        if (attempt.equals(pattern)) { // if you get the correct patter, then you win. Loops stops.
            System.out.println("You got the pattern! The codebreaker wins!");
        }
        if (turns == 0 && !(attempt.equals(pattern))) {
            System.out
                    .println("The codemaker sets out " + black_pegs + " black pegs and " + white_pegs + " white pegs.");
            System.out.println("Sorry, that was your last chance. You lost.");
        } else if (turns < turns) {
            System.out
                    .println("The codemaker sets out " + black_pegs + " black pegs and " + white_pegs + " white pegs.");
            System.out.println("Enter a Guess:");
        }

        int i;
        for (i = 0; i < pattern.length(); i++) {
            pattern_array[i] = pattern.substring(i, (i + 1));
        }
        for (i = 0; i < attempt.length(); i++) {
            attempt_array[i] = attempt.substring(i, i + 1);
        }
        for (int x = 0; x < 4; x++) {
            if (pattern_array[i] == attempt_array[i]) {
                black_pegs++;
            }
        }
    }
}
アルンゴウダ:

あなたのプログラムの間違いがたくさんあります。

あなたは、長いターンが終わっていないようとしてwhileループで実行を維持する必要があります。私は閉じブレース見る}ためにwhile loop置き忘れています。

あなたはパターンを見つけたときにループから分割する必要があります。

turns < turnsそしてturns == 0ループ内では意味がありません。なぜなら、その場合には、それがループに入る方法はありません。

新しいString配列を作成する必要はありません。あなたは、文字列の使用できるchatAt()方法を。(これはかかわらず、論理的なエラーではありません)

入力の読み込みにバグがあります。あなたが使用した後Scanner.nextInt()、あなたが使用する必要がありますscanner.nextLine()述べたように、ここで

作るint white_pegs = 0, black_pegs = 0;ループに対するローカル変数を。完全に動作するコードを以下に示します。

public class CopyOfAssignment5 {

public static void main(String[] args) {
    Scanner keys = new Scanner(System.in);

    System.out.println("Codemaker, please enter a pattern of 4 colors (the possible colors are R=red and G=green");
    String pattern = keys.nextLine().toUpperCase();
    System.out.println("What is the maximum number of guesses for this game?");

    int number_of_guesses = keys.nextInt();
    keys.nextLine();
    int turns = number_of_guesses + 1;
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println("Okay, now its the codebreaker's turn. You have " + number_of_guesses
            + " chances to guess the pattern");
    System.out.println("Enter a Guess:");

    String attempt = null;

    while (turns > 0) { // while turns are not zero
        attempt = keys.nextLine();
        attempt = attempt.toUpperCase(); // then keep displaying the scanner input for an attempt.
        turns--;

        if (attempt.equals(pattern)) { // if you get the correct patter, then you win. Loops stops.
            System.out.println("You got the pattern! The codebreaker wins!");
            break;// Exit from while
        }

        // count black and white pegs to set.

        int rCountInPattern = 0;
        int gCountInPattern = 0;

        int rCountInAttempt = 0;
        int gCountInAttempt = 0;
        int white_pegs = 0, black_pegs = 0;

        for (int i = 0; i < 4; i++) {
            if (pattern.charAt(i) == attempt.charAt(i)) {
                black_pegs++;
            } else {
                if (pattern.charAt(i) == 'R') {
                    rCountInPattern++;
                } else {
                    gCountInPattern++;
                }

                if (attempt.charAt(i) == 'R') {
                    rCountInAttempt++;
                } else {
                    gCountInAttempt++;
                }
            }
        }

        white_pegs = Math.min(rCountInPattern, rCountInAttempt);
        white_pegs = white_pegs + (Math.min(gCountInPattern, gCountInAttempt));

        System.out
                .println("The codemaker sets out " + black_pegs + " black pegs and " + white_pegs + " white pegs.");
        System.out.println("Enter a Guess:");

    }

    if (turns <= 0) {
        System.out.println("Sorry, that was your last chance. You lost.");
    }

}

}

改善が必要なものがたくさんありますが、私はあなたがそれをよりよく理解できるように、溶液からあまり違いはありません答えを掲載しています。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=345302&siteId=1